Bonjour,

Depuis peu, je m'entraine a la création d'un bot IRC en PHP avec les sockets...
Mon bot marche très bien mais, au bout de quelques minutes de non utilisation, il quitte le serveur (le script se termine).

Sur ma console PHP j'ai : Program exited normally.
Et sur IRC : Quit : Client exited

Quelqu'un saurait-il d'où vient ce problème ?

Voici mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
 
set_time_limit(0);
 
ini_set('display_errors', 'on');
 
srand(date('s'));
 
function stripAccents($string){
    return strtr($string,'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ',
'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
}
 
$iii = false;
 
class ircBot
{
    // Constructeur de ircBot
    function ircBot($server='xxxxxx.xxxxxxxxxx.xxx',$port=6667,$nick='xxx',$user='xxx',$chan='#xxx')
    {
        $this->server = $server;
        $this->port = $port;
        $this->nick = $nick;
        $this->user = $user;
        $this->chan = $chan;
 
        $this->alreadyPing = 0;
    }
 
    // Ouverture de la connexion
    function start()
    {
        set_time_limit(0);
        $this->socket = fsockopen($this->server,$this->port);
        fputs($this->socket,'NICK '.$this->nick."\n");
        $this->run();
    }
 
    function run()
    {
 
        set_time_limit(0);
        // Lecture du socket
        while($this->line = fgets($this->socket,1024))
        {
            // Suppression des \r \n
            $this->line = str_replace("\r",'',$this->line);
            $this->line = str_replace("\n",'',$this->line);
 
            // Création d'un objet ircMsg
            $this->msg = new ircMsg($this->line);
 
            echo $this->line."\n";
 
            if($this->msg->type == 'JOIN' && $this->msg->nick != $this->nick)
                fputs($this->socket,'NOTICE '.$this->msg->nick.' Zalut '.$this->msg->nick.' ;) !'."\n");
            elseif($this->msg->type == 'JOIN' && $this->msg->nick == $this->nick && $iii != true)
                fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Coucou les gens ;) !'."\n");$iii=true;
 
            if(preg_match('#^!love#i', $this->msg->param1))
            {
                if(!isset($this->msg->param2) || !isset($this->msg->param3) || empty($this->msg->param2) || empty($this->msg->param3) || $this->msg->param2 == NULL || $this->msg->param3 == NULL)
                {
                    fputs($this->socket,'NOTICE '.$this->msg->nick.' Désolé, il faut faire comme ça : !love <pseudo1> <pseudo2>'."\n");
                }
                else
                {
                    fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Le taux d\'affinités entre '.stripAccents($this->msg->param2).' et '.stripAccents($this->msg->param3).' est de '.rand(1,100).' % !'."\n");
                }
            }
 
            if(preg_match('#^!chatouille#i', $this->msg->param1))
            {
                if(!isset($this->msg->param2) || empty($this->msg->param2) || $this->msg->param2 == NULL)
                {
                    fputs($this->socket,'NOTICE '.$this->msg->nick.' Désolé, il faut faire comme ça : !chatouille <pseudo>'."\n");
                }
                else
                {
                    fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Bouuuh, '.stripAccents($this->msg->param2).' je vaais te chatouuilleer à la demande de '.$this->msg->nick.' !'."\n");
                }
            }
 
            if(preg_match('#^!commandes$#i', $this->msg->param1))
            {
                fputs($this->socket,'NOTICE '.$this->msg->nick.' !love <pseudo1> <pseudo2> : Détermine les affinités entre deux pseudos'."\n");
                fputs($this->socket,'NOTICE '.$this->msg->nick.' !chatouille <pseudo> : Chatouille <pseudo>'."\n");
                fputs($this->socket,'NOTICE '.$this->msg->nick.' !biscuit <pseudo> : Vous donne un biscuit, ou, si <pseudo> est spécifié, donne un biscuit a <pseudo>'."\n");
                fputs($this->socket,'NOTICE '.$this->msg->nick.' !coca <pseudo> : Vous donne un coca, ou, si <pseudo> est spécifié, donne un coca a <pseudo>'."\n");
                fputs($this->socket,'NOTICE '.$this->msg->nick.' !pizza <pseudo> : Vous donne une pizza, ou, si <pseudo> est spécifié, donne une pizza a <pseudo>'."\n");
                fputs($this->socket,'NOTICE '.$this->msg->nick.' !hamburger <pseudo> : Vous donne un hamburger, ou, si <pseudo> est spécifié, donne un hamburger a <pseudo>'."\n");
                fputs($this->socket,'NOTICE '.$this->msg->nick.' !poing <pseudo> : Donne un coup de poing a <pseudo>'."\n");
                fputs($this->socket,'NOTICE '.$this->msg->nick.' !list_news : Donne la liste des news du site'."\n");
                fputs($this->socket,'NOTICE '.$this->msg->nick.' !last_new : Donne la dernière new du site'."\n");
                fputs($this->socket,'NOTICE '.$this->msg->nick.' !new <id> : Donne la new n°<id>'."\n");
                fputs($this->socket,'NOTICE '.$this->msg->nick.' !commandes : Affiche ceci'."\n");
                if($this->msg->nick == 'xxxxxxxxx' || $this->msg->nick == 'xxxxxxxxxx' && $this->msg->address == 'xxxxx')
                {
                    fputs($this->socket,'NOTICE '.$this->msg->nick.' !disconnect : Déconnecte le bot'."\n");
                }
            }
 
            if(preg_match('#^!biscuit#i', $this->msg->param1))
            {
                if(!isset($this->msg->param2) || empty($this->msg->param2) || $this->msg->param2 == NULL)
                {
                    fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Tient, voilà un biscuit '.$this->msg->nick.' ^^.'."\n");
                }
                else
                {
                    fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Oww c\'est gentil '.$this->msg->nick.' ^^. Tient, voilà un biscuit '.stripAccents($this->msg->param2).', de la part de '.$this->msg->nick.' ;).'."\n");
                }
            }
 
            if(preg_match('#^!coca#i', $this->msg->param1))
            {
                if(!isset($this->msg->param2) || empty($this->msg->param2) || $this->msg->param2 == NULL)
                {
                    fputs($this->socket,'PRIVMSG '.$this->chan.' :Tient, voilà une canette de coca '.$this->msg->nick.' ^^.'."\n");
                }
                else
                {
                    fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Oww c\'est gentil '.$this->msg->nick.' ^^. Tient, voilà une canette de coca '.stripAccents($this->msg->param2).', de la part de '.$this->msg->nick.' ;).'."\n");
                }
            }
 
            if(preg_match('#^!pizza#i', $this->msg->param1))
            {
                if(!isset($this->msg->param2) || empty($this->msg->param2) || $this->msg->param2 == NULL)
                {
                    fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Tient, voilà une pizza '.$this->msg->nick.' ^^.'."\n");
                }
                else
                {
                    fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Oww c\'est gentil '.$this->msg->nick.' ^^. Tient, voilà une pizza '.stripAccents($this->msg->param2).', de la part de '.$this->msg->nick.' ;).'."\n");
                }
            }
 
            if(preg_match('#^!hamburger#i', $this->msg->param1))
            {
                if(!isset($this->msg->param2) || empty($this->msg->param2) || $this->msg->param2 == NULL)
                {
                    fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Tient, voilà un hamburger '.$this->msg->nick.' ^^.'."\n");
                }
                else
                {
                    fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Oww c\'est gentil '.$this->msg->nick.' ^^. Tient, voilà un hamburger '.stripAccents($this->msg->param2).', de la part de '.$this->msg->nick.' ;).'."\n");
                }
            }
 
            if(preg_match('#^!poing#i', $this->msg->param1))
            {
                if(!isset($this->msg->param2) || empty($this->msg->param2) || $this->msg->param2 == NULL)
                {
                    fputs($this->socket,'NOTICE '.$this->msg->nick.' Désolé, il faut faire comme ça : !poing <pseudo>'."\n");
                }
                else
                {
                    fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Oww c\'est méchant '.$this->msg->nick.' ! Tient, voilà un coup de poing '.stripAccents($this->msg->param2).', de la part de '.$this->msg->nick.'.'."\n");
                }
            }
 
            if(preg_match('#^!disconnect$#i', $this->msg->param1) && ($this->msg->nick == 'xxxxxxxxx' || $this->msg->nick == 'xxxxxxxxxx' && $this->msg->address == 'xxxxxx'))
            {
                see_you($this->socket,$this->msg->nick);
            }
 
            $monfichier = fopen('http://xxxxxx.fr.nf/news/dern_new.php', 'r');
 
            $ligne = /*stripAccents(*/fgets($monfichier)/*)*/;
            $vals = explode('---|||---', $ligne);
            fclose($monfichier);
 
            if(preg_match('#^!last_new#i', $this->msg->param1))
            {
                    fputs($this->socket,'NOTICE '.$this->msg->nick.' :Dernière new :'."\n");
                    fputs($this->socket,'NOTICE '.$this->msg->nick.' :Postée le ' . date('d/m/Y', $vals[2]) . ' à ' . date('H:i:s', $vals[2]) . ' par ' . $vals[1] ."\n");
                    fputs($this->socket,'NOTICE '.$this->msg->nick.' :Titre : ' . $vals[0] . "\n");
                    fputs($this->socket,'NOTICE '.$this->msg->nick.' :Lien : http://xxxxxx.xxxxx-xxxxx.org/newsite/news.php#' . $vals[3] . "\n");
                    if(isset($vals[4]) && !empty($vals[4]))                    
                        fputs($this->socket,'NOTICE '.$this->msg->nick.' :Résumé : ' . $vals[4] . "\n");
            }
 
            if(preg_match('#^!new#i', $this->msg->param1))
            {
                if(isset($this->msg->param2) && !empty($this->msg->param2) && $this->msg->param2 != NULL && (int)$this->msg->param2 != 0)
                {
                    $monfichier = fopen('http://xxxxxx.fr.nf/news/recup_new.php?id=' . $this->msg->param2, 'r');
 
                    $ligne = /*stripAccents(*/fgets($monfichier)/*)*/;
                    if($ligne != 'false')
                    {
                        $vals = explode('---|||---', $ligne);
 
                        fputs($this->socket,'NOTICE '.$this->msg->nick.' :New n°' . $this->msg->param2 . '  :'."\n");
                        fputs($this->socket,'NOTICE '.$this->msg->nick.' :Postée le ' . date('d/m/Y', $vals[2]) . ' à ' . date('H:i:s', $vals[2]) . ' par ' . $vals[1] ."\n");
                        fputs($this->socket,'NOTICE '.$this->msg->nick.' :Titre : ' . $vals[0] . "\n");
                        fputs($this->socket,'NOTICE '.$this->msg->nick.' :Lien : http://xxxxxx.xxxxx-xxxxx.org/newsite/news.php#' . $vals[3] . "\n");
                        if(isset($vals[4]) && !empty($vals[4]))                    
                            fputs($this->socket,'NOTICE '.$this->msg->nick.' :Résumé : ' . $vals[4] . "\n");
                    }
                    else
                        fputs($this->socket,'NOTICE '.$this->msg->nick.' :New n°' . $this->msg->param2 . ' inexistante.'."\n");
                    fclose($monfichier);
                }
                else
                    fputs($this->socket,'NOTICE '.$this->msg->nick.' :Veuillez entrer un numéro de new valide après !new.'."\n");
            }
 
            if(preg_match('#^!list_news#i', $this->msg->param1))
            {
                    $monfichier = fopen('http://xxxxxx.fr.nf/news/list_news.php', 'r');
 
                    $ligne = /*stripAccents(*/fgets($monfichier)/*)*/;
 
                    $vals = explode('---|||---', $ligne);
 
                    fputs($this->socket,'NOTICE '.$this->msg->nick.' :Liste des news' . $this->msg->param2 . '  :'."\n");
                    $i = 0;
                    foreach($vals as $val){
                        $i++;
                        if($i > 1 && !empty($val) && $val[0] != NULL)
                        {
                            $tmp = explode(':::',$val);
                             fputs($this->socket,'NOTICE '.$this->msg->nick.' :' . $tmp[0] . ' : New n°' . $tmp[1] . "\n");
                            unset($tmp);
                        }
                    }                    
 
            }
 
            // PING
            if($this->msg->cmd == "PING" || $this->msg->type == "PING" || preg_match('#ping#i', $this->line) || preg_match('#ping#i', $this->msg->cmd) || preg_match('#ping#i', $this->msg->type) ||  preg_match('#PING#i', $this->line))
            {
                $this->pingHandler();
            }
            else
            {
                // Autres commandes
                switch($this->msg->type)
                {
                    /*case 'PRIVMSG':
                        $this->privmsgHandler();
                        break;*/
                    case 'NICK':
                        $this->nickHandler();
                        break;
                    case 'KICK':
                        $this->kickHandler();
                        break;
                    case 'MODE':
                        $this->modeHandler();
                        break;
                    case 'TOPIC':
                        $this->modeHandler();
                        break;
                    case 'INVITE':
                        $this->inviteHandler();
                        break;
                    case '433':
                        $this->c433Handler();
                        break;
                    case '332':
                        $this->c332Handler();
                        break;
                    case '353':
                        $this->c353Handler();
                        break;
                    default:
                        $this->defaultHandler();
                        break;
                }
            }
            // Destruction de l'objet ircMsg
            $this->msg->destroy();
        }
    }
 
    // Traitement d'un PING
    function pingHandler()
    {
        // Envoi d'un PONG
        echo "PIIING\n";
        $pong = explode('PONG ', str_replace('PING', 'PONG', $this->line));
        $pong = explode(':', $pong[1]);
        fputs($this->socket,"PONG " . $pong[1] . "\n");
        fputs($this->socket,"PRIVMSG " . $this->msg->nick . " :PONG " . $pong[1] . "\n");
        echo "PO00NG :".$pong[1]."\n";
        echo "PO00NG :".$pong[0]."\n";
        unset($pong);
 
        // Fin de la connexion au serveur
        if(!$this->alreadyPing)
        {
            fputs($this->socket,'USER '.$this->user.' localhost '.$this->server.' '.$this->nick."\n");
            fputs($this->socket,'JOIN '.$this->chan."\n");
            //$this->alreadyPing = 1;
            if(!$i)
                $i++;
            fputs($this->socket,'MODE ' . $this->nick .' +B\n');
        }
    }
 
    // :nick!ident@address PRIVMSG target :param
    /*function privmsgHandler()
    {
        fputs($this->socket,'PRIVMSG '.$this->msg->nick.' '.$this->msg->param."\n");
    }*/
 
    // :nick!ident@address NICK :param1
    function nickHandler()
    {
        if($this->msg->nick == $this->nick)
        {
            $this->c433Handler();
        }
    }
 
    // :nick!ident@address MODE target target2
    function modeHandler()
    {
    }
 
    // :nick!ident@address TOPIC target :param
    function topicHandler()
    {
 
    }
 
    // :nick!ident@address INVITE target :param1
    function inviteHandler()
    {
 
    }
 
    // :nick!ident@address KICK target target2 :param
    function kickHandler()
    {
        if($this->msg->target2 == $this->nick)
        {
            fputs($this->socket,'JOIN '.$this->chan."\n");
            fputs($this->socket,'PRIVMSG '.$this->chan.' '.'Miiiaaaeuuh ! J\'aime pas les kicks T-T.'."\n");
        }
    }
 
    // :nick!ident@address JOIN :target
    function joinHandler()
    {
        if(!isset($this->msg->target))
        {
            $target = $this->msg->target;
            $this->$target = new ircChan($target);
        }
    }
 
    // Nick already use
    function c433Handler()
    {
        $this->nick = $this->nick.rand(1,9999);
        fputs($this->socket,'NICK '.$this->nick."\n");
    }
 
    // :info 332 target target2 :param
    function c332Handler()
    {
        $chan = $this->msg->target2;
        $this->$chan->topic = $this->msg->param;
    }
 
    // :server 353 nick = chan :user1 user2 user3...
    function c353Handler()
    {
        $tab0 = explode(' ',trim($this->line));
        $chan = $tab0[4];
        $this->$chan->users = array_slice($tab0,6);
        $this->$chan->users[] = substr($tab0[5],1);
    }
 
    function defaultHandler()
    {
 
    }
 
    function see_you($socket, $nick)
    {
        fputs($socket,"QUIT Déconnecté par ".$nick."\n");
        socket_close($socket);
    }
 
}
 
class ircChan
{
    function ircChan($name)
    {
        $this->name = $name;
    }
}
 
class ircMsg
{
    function ircMsg($line)
    {
        // cmd:nick!ident@address TYPE target target2:param1 param2 param3
        $tab0 = explode(':',$line,3);
        $this->cmd = trim($tab0[0]);
        $tab1 = explode('!',$tab0[1]);
        $this->nick = trim($tab1[0]);
        $tab2 = explode(' ',$tab0[1]);
        $this->info = trim($tab2[0]);
        $this->type = trim($tab2[1]);
        $this->target = trim($tab2[2]);
        $this->target2 = trim($tab2[3]);
        $tab3 = explode('@',$tab2[0]);
        $this->ident = trim($tab3[0]);
        $this->address = trim($tab3[1]);
        $tab4 = explode(' ',$tab0[2],3);
        $this->param = trim($tab0[2]);
        $this->param1 = trim($tab4[0]);
        $this->param2 = trim($tab4[1]);
        $this->param3 = trim($tab4[2]);
        $this->param4 = trim($tab4[3]);
        $this->param5 = trim($tab4[4]);
    }
 
    function destroy()
    {
        unset($this->line,$tab0,$this->target2,$this->cmd,$tab1,$this->param,$this->nick,$tab2,$this->info,$this->type,$this->target,$tab3,$this->ident,$this->address,$tab4,$this->param1,$this->param2,$this->param3);
    }
}
 
$bot = new ircBot();
$bot->start();
 
?>
Merci d'avance de votre aide.
aurel2108

PS : Sauriez-vous quel est l'équivalent de la commande /me avec les sockets ?
Et y'aurait-il un moyen pour passer le MOTD du serveur ?