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
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
571
  
572
  
573
  
574
  
575
  
576
  
577
  
578
  
579
  
580
  
581
  
582
  
583
  
#pike __REAL_VERSION__ 
 
import "."; 
 
Raw raw; 
//FIXME: Unknown what this default password is good for. Probably not needed. 
string pass=MIME.encode_base64(random_string(6)); 
 
mapping options; 
 
mapping channels=([]); 
 
//! @decl void create(string|object server, void|mapping(string:mixed) options) 
//! @param server 
//!   The IRC server to connect to. 
//!   If server is an object, it is assumed to be a newly established 
//!   connection to the IRC server to be used. Pass @[SSL.File] 
//!   connections here to connect to SSL secured IRC networks. 
//! @param options 
//!   An optional mapping with additional IRC client options. 
//!   @mapping 
//!     @member int "port" 
//!       Defaults to 6667. 
//!     @member string "user" 
//!       Defaults to @expr{"unknown"@} on systems without @[getpwuid] and 
//!       @[getuid] and to @expr{getpwuid(getuid())[0]@} on systems with. 
//!     @member string "nick" 
//!       Defaults to @expr{"Unknown"@} on systems without @[getpwuid] and 
//!       @[getuid] and to @expr{String.capitalize(getpwuid(getuid())[0])@} 
//!       on systems with. 
//!     @member string "pass" 
//!       Server password, if any. Public servers seldom require this. 
//!     @member string "realname" 
//!       Defaults to @expr{"Mr. Anonymous"@} on systems without @[getpwuid] 
//!       and @[getuid] and to @expr{getpwuid(getuid())[4]@} on systems with. 
//!     @member string "host" 
//!       Defaults to @expr{"localhost"@} on systems without @[uname] and 
//!       to @expr{uname()->nodename@} on systems with. 
//!     @member int "ping_interval" 
//!       Defaults to 120. 
//!     @member int "ping_timeout" 
//!       Defaults to 120. 
//!     @member function(void:void) "connection_lost" 
//!       This function is called when the connection to the IRC server is 
//!       lost or when a ping isn't answered with a pong within the time 
//!       set by the @tt{ping_timeout@} option. The default behaviour is 
//!       to complain on stderr and self destruct. 
//!     @member program "channel_program" 
//!       An instance of this is created for each channel connected to via 
//!       join_channel() - should be a subclass of Protocols.IRC.Channel. 
//!     @member function(mixed ...:void) "error_notify" 
//!       This function is called when a KILL or ERROR command is recieved 
//!       from the IRC server. 
//!     @member function(string,void|string:void) "system_notify" 
//! 
//!     @member function(string,void|string:void) "motd_notify" 
//! 
//!     @member function(string) "error_nickinuse" 
//! 
//!     @member function(string,string,string,string,string) "generic_notify" 
//!       The arguments are from, type, to, message and extra. 
//!     @member function(string,string) "quit_notify" 
//!       The arguments are who and why. 
//!     @member function(Person,string,string) "privmsg_notify" 
//!       The arguments are originator, message and to. 
//!     @member function(Person,string,string) "notice_notify" 
//!       The arguments are originator, message and to. 
//!     @member function(Person,string) "nick_notify" 
//!       The arguments are originator and to. 
//!   @endmapping 
void create(string|object _server,void|mapping(string:mixed) _options) 
{ 
   options= 
      ([ 
         "port":6667, 
#if constant(getpwuid) && constant(getuid) 
       "user":(getpwuid(getuid())||({"-"}))[0], 
#else 
       "user":"unknown", 
#endif 
#if constant(getpwuid) && constant(getuid) 
       "nick":String.capitalize((getpwuid(getuid())||({"-"}))[0]), 
#else 
       "nick":"Unknown"+random(10000), 
#endif 
#if constant(getpwuid) && constant(getuid) 
       "realname":(getpwuid(getuid())||({0,0,0,0,"Mr. Nobody"}))[4], 
#else 
       "realname":"Mr. Anonymous", 
#endif 
#if constant(uname) 
       "host":uname()->nodename||"localhost", 
#else 
       "host":"localhost", 
#endif 
      ])|(_options||([])); 
 
   options->server=_server; 
 
   me=person(options->nick,options->user+"@"+options->host); 
 
   raw=Raw(options->server,options->port,got_command,got_notify, 
           connection_lost); 
 
   cmd->create(raw); 
 
   cmd->pass(options->pass||pass); 
   cmd->nick(options->nick); 
   // If a connection object was passed as 'server' parameter to the 
   // constructor, we just pretend to the other server that we reached it 
   // by connecting to "localhost". 
   // There could be a special config mapping entry for it, but I doubt 
   // that this information really is used much anywhere anyway. 
   cmd->user(options->user,options->host, 
             stringp(options->server) ? options->server : "localhost", 
             options->realname); 
 
   call_out(da_ping,options->ping_interval || 60); 
} 
 
//! Closes the connection to the server. 
void close() 
{ 
   if (raw->con) raw->con->close(); 
   destruct(raw); 
   raw=0; 
} 
 
string expecting_pong; 
 
void da_ping() 
{ 
   call_out(da_ping,options->ping_interval || 120); 
   call_out(no_ping_reply,options->ping_timeout || 120); // timeout 
   cmd->ping(expecting_pong= 
             options->host+" "+Array.shuffle("pikeIRCclient"/1)*""); 
} 
 
void no_ping_reply() 
{ 
   remove_call_out(da_ping); 
   werror("no ping reply\n"); 
   connection_lost(); 
} 
 
void connection_lost() 
{ 
   if (options->connection_lost) 
      options->connection_lost(); 
   else 
   { 
      werror("destructing self\n"); 
      catch { destruct(raw->con); }; 
      destruct(raw); 
      destruct(this); 
   } 
} 
 
void got_command(string what,string ... args) 
{ 
   // werror("got command: %O, %O\n",what,args); 
   // most commands can be handled immediately 
   switch (what) 
   { 
      case "PING": 
         cmd->pong(args[0]); 
         return; 
      case "KILL": 
      case "ERROR": 
         if (options->error_notify) 
            options->error_notify(@args); 
         return; 
   } 
//     werror("got command: %O, %O\n",what,args); 
} 
 
void got_notify(string from,string type, 
                void|string to,void|string message, 
                string ...extra) 
{ 
   object c; 
   if (options->system_notify && glob("2??",type)) 
   { 
      options->system_notify(type,message); 
      return; 
   } 
   if (options->motd_notify && (<"372","375","376">)[type]) 
   { 
      options->motd_notify((["372":"cont", 
                             "375":"start", 
                             "376":"end"])[type],message); 
      return; 
   } 
 
   Person originator=person(@(from/"!")); 
 
   switch (type) 
   { 
      case "433": // nick in use 
         if (options->error_nickinuse) 
            options->error_nickinuse(message); 
         else if (options->generic_notify) break; 
         cmd->nick("unknown"+random(10000)); 
         return; 
 
      case "473": // failed to join 
         if ((c=channels[lower_case(message||"")])) 
         { 
            c->not_failed_to_join(@extra); 
            return; 
         } 
         break; 
 
      case "353": // names list 
         if (sizeof(extra) && (c=channels[lower_case(extra[0]||"")]) && 
             c->not_names) 
         { 
            c->not_names(map(extra[1]/" "-({""}), 
                             lambda(string name) 
                             { 
                                string a,b,c; 
                                sscanf(name,"%[+@%]%s%[+@%]", 
                                       a,b,c); 
                                Person p=person(b); 
                                p->channels[lower_case(extra[0])]=1; 
                                return ({p,a+c}); 
                             })); 
            return; 
         } 
         break; 
      case "366": // "end of names list" 
         break; 
 
      case "352": // who list 
         if (sizeof(extra)>2 && 
             message && (c=channels[lower_case(message||"")])) 
         { 
            Person p=person(extra[3],extra[0]+"@"+extra[1]); 
            p->server=extra[2]; 
            p->realname=extra[6..]*" "; 
            p->channels[lower_case(message)]=1; 
            if (c->not_who) c->not_who(p,extra[4]); 
            return; 
         } 
         break; 
      case "315": // "end of who list" 
         break; 
 
      case "482": // "you're not channel operator" 
         if ((c=channels[lower_case(message||"")])) 
         { 
            if (c->not_not_oper) c->not_not_oper(); 
            return; 
         } 
         break; 
 
      case "474": // "cannot join channel" 
        werror("%O\n",extra); 
         if ((c=channels[lower_case(message||"")])) 
         { 
            if (c->not_join_fail) c->not_join_fail(extra*" "); 
            return; 
         } 
         break; 
 
      case "401": // no such nick 
//     werror("%O\n",({from,type,to,message,extra})); 
//     werror("(got 401 %O %O)\n",message,extra*" "); 
         break; 
 
      case "367": // mode b line 
         if ((c=channels[lower_case(message||"")])) 
         { 
            if (c->not_mode_b) c->not_mode_b(extra*" "); 
            return; 
         } 
         break; 
 
 
         /* --- */ 
 
      case "PONG": 
         if (message==expecting_pong) 
         { 
            remove_call_out(no_ping_reply); 
            return; 
         } 
         break; 
 
      case "MODE": 
         if ((c=channels[lower_case(to||"")])) 
         { 
            // who, mode, by 
            c->not_mode(extra[0]?person(extra[0]):originator, 
                        message+( ({""})+extra)*" ",originator); 
            return; 
         } 
         break; 
 
      case "JOIN": 
//     werror("me=%O\n",me); 
         if ((c=channels[lower_case(to||"")])) 
         { 
            c->not_join(originator); 
            return; 
         } 
         break; 
 
      case "PART": 
         originator->channels[lower_case(to)]=0; 
         if ((c=channels[lower_case(to||"")])) 
         { 
            // who, why, by 
            c->not_part(originator,message,originator); 
            return; 
         } 
         break; 
 
      case "KICK": 
         person(message)->channels[lower_case(extra[0])]=0; 
         if ((c=channels[lower_case(to||"")])) 
         { 
            // who, why, by 
            c->not_part(person(message),extra[0],originator); 
            return; 
         } 
         break; 
 
      case "QUIT": 
         forget_person(originator); 
 
         if (options->quit_notify) 
         { 
            // who, why 
            options->quit_notify(originator,to); 
            return; 
         } 
 
         foreach (values(channels),c) 
            if (c) 
               if (c->not_quit || c->not_part) 
                  (c->not_quit||c->not_part)(originator,message,originator); 
 
         break; 
 
      case "PRIVMSG": 
         if ((c=channels[lower_case(to||"")])) 
         { 
            c->not_message(originator,message); 
            return; 
         } 
         if (!options->privmsg_notify) break; 
         options->privmsg_notify(originator,message,to); 
         return; 
 
      case "NOTICE": 
         if ((c=channels[lower_case(to||"")])) 
         { 
            c->not_message(originator,message); 
            return; 
         } 
         if (!options->notice_notify) break; 
         options->notice_notify(originator,message,to); 
         return; 
 
      case "NICK": 
         werror("%s is known as %s (aka %s)\n", 
                originator->nick, 
                to, 
                ((array)originator->aka)*","); 
         mixed err=0; 
         originator->aka[originator->nick]=1; 
         originator->aka[to]=1; 
         if (options->nick_notify) 
            err=catch { options->nick_notify(originator,to); }; 
         m_delete(nick2person,originator->nick); 
         originator->nick=to; 
         nick2person[to]=originator; 
         if (err) throw(err); 
         return; 
 
      case "INVITE": 
         if ((c=channels[lower_case(message||"")])) 
         { 
            c->not_invite(originator,@extra); 
            return; 
         } 
         if (!options->notice_invite) break; 
         options->notice_invite(originator,message,@extra); 
         return; 
 
      case "CAP": 
         if (message=="ACK") 
         { 
             if (!options->capability_notify) break; 
             options->capability_notify(from, to, extra); 
             return; 
         } 
         break; 
 
#ifdef IRC_DEBUG 
      default: 
         werror("got unknown message: %O, %O, %O, %O\n",from,type,to,message); 
#endif 
   } 
//    werror("got notify: %O, %O, %O, %O\n",from,type,to,message); 
   if (options->generic_notify) 
      options->generic_notify(from,type,to,message, 
                              (extra&&sizeof(extra))?extra[0]:0); 
} 
 
object cmd=class 
{ 
   object raw; 
   public void create(object _raw) { raw=_raw; } 
 
   class SyncRequest 
   { 
      program prog; 
      object ret; 
 
      void create(program p,object _ret) 
      { 
         prog=p; 
         ret=_ret; 
      } 
 
      mixed `()(mixed ...args) 
      { 
         mixed m; 
         object req=prog(); 
         m=req->sync(raw,@args); 
         if (!m) return ret; 
         else return m; 
      } 
   } 
 
   class AsyncRequest 
   { 
      program prog; 
 
      void create(program p) 
      { 
         prog=p; 
      } 
 
      mixed `()(mixed ...args) 
      { 
         object req=prog(); 
         req->async(this,@args); 
         return req; 
      } 
   } 
 
   class AsyncCBRequest 
   { 
      program prog; 
 
      void create(program p) 
      { 
         prog=p; 
      } 
 
      mixed `()(function callback,mixed ...args) 
      { 
         object req=prog(); 
         req->callback=callback; 
         req->async(this,@args); 
         return req; 
      } 
   } 
 
   mixed `->(string request) 
   { 
      mixed|program p; 
 
 
      if ( (p=Requests[request]) ) 
         return SyncRequest(p,this); 
      else if ( request[..5]=="async_" && 
                (p=Requests[request[6..]]) ) 
         return AsyncRequest(p); 
      else if ( request[..8]=="async_cb_" && 
                (p=Requests[request[9..]]) ) 
         return AsyncCBRequest(p); 
      else 
      { 
         p=::`[](request); 
         if (!p) 
         { 
            werror("FOO! FOO! FOO! %O %O\n",request,p); 
            Error.internal("unknown command %O",request); 
            return 0; 
         } 
         return p; 
      } 
   } 
} (raw); 
 
// ----- commands 
 
void send_message(string|array to,string msg) 
{ 
   cmd->privmsg( (arrayp(to)?to*",":to), msg); 
} 
 
void join_channel(string chan) 
{ 
   cmd->join(chan); 
   if (options->channel_program) 
   { 
      object ch = options->channel_program(); 
      ch->name = lower_case(chan); 
      channels[lower_case(chan)] = ch; 
   } 
} 
 
void part_channel(string chan) 
{ 
   cmd->part(chan); 
   m_delete(channels, lower_case(chan)); 
} 
 
// ----- persons 
 
class Person 
{ 
   inherit .Person; 
 
   void say(string message) 
   { 
      cmd->privmsg(nick,message); 
   } 
 
   void notice(string message) 
   { 
      cmd->notice(nick,message); 
   } 
 
   void action(string what) 
   { 
      say("\1ACTION "+what+"\1"); 
   } 
 
   string _sprintf(int t) 
   { 
     return t=='O' && sprintf("%O(%s!%s@%s%s)", this_program, 
                              nick,user||"?",ip||"?", 
                              (realname!="?")?"("+realname+")":""); 
   } 
} 
 
mapping nick2person=([]); 
Person me; 
 
Person person(string who,void|string ip) 
{ 
   Person p; 
 
   if ( ! (p=nick2person[lower_case(who)]) ) 
   { 
      p=Person(); 
      p->nick=who; 
      nick2person[lower_case(who)]=p; 
//        werror("new person: %O\n",p); 
   } 
   else if (lower_case(p->nick)!=lower_case(who)) 
   { 
      werror("nick mismatch: %O was %O\n",who,p->nick); 
      p->nick=who; 
   } 
   if (ip && !p->ip) 
      sscanf(ip,"%*[~]%s@%s",p->user,p->ip); 
 
   return p; 
} 
 
void forget_person(object p) 
{ 
   m_delete(nick2person,p->nick); 
}