]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
Add support for up to 3 targets in WHOIS queries.
[ngircd-alex.git] / src / ngircd / irc-info.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * IRC info commands
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <strings.h>
26
27 #include "ngircd.h"
28 #include "conn-func.h"
29 #include "conn-zip.h"
30 #include "channel.h"
31 #include "conf.h"
32 #include "defines.h"
33 #include "log.h"
34 #include "messages.h"
35 #include "match.h"
36 #include "tool.h"
37 #include "parse.h"
38 #include "irc-write.h"
39
40 #include "exp.h"
41 #include "irc-info.h"
42
43
44 GLOBAL bool
45 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
46 {
47         CLIENT *target, *prefix;
48
49         assert( Client != NULL );
50         assert( Req != NULL );
51
52         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
53
54         /* find target ... */
55         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
56         else target = Client_ThisServer( );
57
58         /* find Prefix */
59         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
60         else prefix = Client;
61         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
62
63         /* forwad message to another server? */
64         if( target != Client_ThisServer( ))
65         {
66                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
67
68                 /* forward */
69                 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
70                 return CONNECTED;
71         }
72
73         /* mit Versionsinfo antworten */
74         if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
75         if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
76         if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
77         if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
78
79         IRC_SetPenalty( Client, 1 );
80         return CONNECTED;
81 } /* IRC_ADMIN */
82
83
84 /**
85  * Handler for the IRC command "INFO".
86  * See RFC 2812 section 3.4.10.
87  */
88 GLOBAL bool
89 IRC_INFO(CLIENT * Client, REQUEST * Req)
90 {
91         CLIENT *target, *prefix;
92         char msg[510];
93
94         assert(Client != NULL);
95         assert(Req != NULL);
96
97         /* Wrong number of parameters? */
98         if (Req->argc > 1)
99                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
100                                           Client_ID(Client), Req->command);
101
102         /* Determine prefix */
103         if (Client_Type(Client) == CLIENT_SERVER)
104                 prefix = Client_Search(Req->prefix);
105         else
106                 prefix = Client;
107         if (!prefix)
108                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
109                                           Client_ID(Client), Req->prefix);
110
111         /* Look for a target */
112         if (Req->argc > 0)
113                 target = Client_Search(Req->argv[0]);
114         else
115                 target = Client_ThisServer();
116         
117         /* Make sure that the target is a server */
118         if (target && Client_Type(target) != CLIENT_SERVER)
119                 target = Client_Introducer(target);
120
121         if (!target)
122                 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
123                                           Client_ID(prefix), Req->argv[0]);
124
125         /* Pass on to another server? */
126         if (target != Client_ThisServer()) {
127                 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
128                                          Req->argv[0]);
129                 return CONNECTED;
130         }
131
132         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
133                                 NGIRCd_Version))
134                 return DISCONNECTED;
135
136 #if defined(__DATE__) && defined(__TIME__)
137         snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
138         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
139                 return DISCONNECTED;
140 #endif
141
142         strlcpy(msg, "On-line since ", sizeof(msg));
143         strlcat(msg, NGIRCd_StartStr, sizeof(msg));
144         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
145                 return DISCONNECTED;
146
147         if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
148                 return DISCONNECTED;
149
150         IRC_SetPenalty(Client, 2);
151         return CONNECTED;
152 } /* IRC_INFO */
153
154
155 GLOBAL bool
156 IRC_ISON( CLIENT *Client, REQUEST *Req )
157 {
158         char rpl[COMMAND_LEN];
159         CLIENT *c;
160         char *ptr;
161         int i;
162
163         assert( Client != NULL );
164         assert( Req != NULL );
165
166         /* Falsche Anzahl Parameter? */
167         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
168
169         strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
170         for( i = 0; i < Req->argc; i++ )
171         {
172                 ptr = strtok( Req->argv[i], " " );
173                 while( ptr )
174                 {
175                         ngt_TrimStr( ptr );
176                         c = Client_Search( ptr );
177                         if( c && ( Client_Type( c ) == CLIENT_USER ))
178                         {
179                                 /* Dieser Nick ist "online" */
180                                 strlcat( rpl, ptr, sizeof( rpl ));
181                                 strlcat( rpl, " ", sizeof( rpl ));
182                         }
183                         ptr = strtok( NULL, " " );
184                 }
185         }
186         ngt_TrimLastChr(rpl, ' ');
187
188         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
189 } /* IRC_ISON */
190
191
192 GLOBAL bool
193 IRC_LINKS( CLIENT *Client, REQUEST *Req )
194 {
195         CLIENT *target, *from, *c;
196         char *mask;
197
198         assert( Client != NULL );
199         assert( Req != NULL );
200
201         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
202
203         /* Server-Mask ermitteln */
204         if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
205         else mask = "*";
206
207         /* Absender ermitteln */
208         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
209         else from = Client;
210         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
211
212         /* An anderen Server forwarden? */
213         if( Req->argc == 2 )
214         {
215                 target = Client_Search( Req->argv[0] );
216                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
217                 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
218         }
219
220         /* Wer ist der Absender? */
221         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
222         else target = Client;
223         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
224
225         c = Client_First( );
226         while( c )
227         {
228                 if( Client_Type( c ) == CLIENT_SERVER )
229                 {
230                         if( ! IRC_WriteStrClient( target, RPL_LINKS_MSG, Client_ID( target ), Client_ID( c ), Client_ID( Client_TopServer( c ) ? Client_TopServer( c ) : Client_ThisServer( )), Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
231                 }
232                 c = Client_Next( c );
233         }
234         
235         IRC_SetPenalty( target, 1 );
236         return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
237 } /* IRC_LINKS */
238
239
240 GLOBAL bool
241 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
242 {
243         CLIENT *target, *from;
244
245         assert( Client != NULL );
246         assert( Req != NULL );
247
248         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
249
250         /* Absender ermitteln */
251         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
252         else from = Client;
253         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
254
255         /* An anderen Server forwarden? */
256         if( Req->argc == 2 )
257         {
258                 target = Client_Search( Req->argv[1] );
259                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
260                 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
261         }
262
263         /* Wer ist der Absender? */
264         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
265         else target = Client;
266         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
267
268         IRC_Send_LUSERS( target );
269
270         IRC_SetPenalty( target, 1 );
271         return CONNECTED;
272 } /* IRC_LUSERS */
273
274
275 /**
276  * Handler for the IRC command "SERVLIST".
277  * List registered services, see RFC 2811, section 3.5.1: the syntax is
278  * "SERVLIST [<mask> [<type>]]".
279  */
280 GLOBAL bool
281 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
282 {
283         CLIENT *c;
284
285         assert(Client != NULL);
286         assert(Req != NULL);
287
288         if (Req->argc > 2)
289                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
290                                           Client_ID(Client), Req->command);
291
292         if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
293                 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
294                         if (Client_Type(c) != CLIENT_SERVICE)
295                                 continue;
296                         if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
297                                                                   Client_ID(c)))
298                                 continue;
299                         if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
300                                         Client_ID(Client), Client_Mask(c),
301                                         Client_Mask(Client_Introducer(c)), "*",
302                                         0, Client_Hops(c), Client_Info(c)))
303                                 return DISCONNECTED;
304                 }
305         }
306
307         return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
308                                   Req->argc > 0 ? Req->argv[0] : "*",
309                                   Req->argc > 1 ? Req->argv[1] : "0");
310 } /* IRC_SERVLIST */
311
312
313 GLOBAL bool
314 IRC_MOTD( CLIENT *Client, REQUEST *Req )
315 {
316         CLIENT *from, *target;
317
318         assert( Client != NULL );
319         assert( Req != NULL );
320
321         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
322
323         /* From aus Prefix ermitteln */
324         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
325         else from = Client;
326         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
327
328         if( Req->argc == 1 )
329         {
330                 /* forward? */
331                 target = Client_Search( Req->argv[0] );
332                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
333
334                 if( target != Client_ThisServer( ))
335                 {
336                         /* Ok, anderer Server ist das Ziel: forwarden */
337                         return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
338                 }
339         }
340
341         IRC_SetPenalty( from, 3 );
342         return IRC_Show_MOTD( from );
343 } /* IRC_MOTD */
344
345
346 GLOBAL bool
347 IRC_NAMES( CLIENT *Client, REQUEST *Req )
348 {
349         char rpl[COMMAND_LEN], *ptr;
350         CLIENT *target, *from, *c;
351         CHANNEL *chan;
352
353         assert( Client != NULL );
354         assert( Req != NULL );
355
356         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
357
358         /* use prefix to determine "From" */
359         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
360         else from = Client;
361         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
362
363         if( Req->argc == 2 )
364         {
365                 /* forward to another server? */
366                 target = Client_Search( Req->argv[1] );
367                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
368
369                 if( target != Client_ThisServer( )) {
370                         /* target is another server, forward */
371                         return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
372                 }
373         }
374
375         if( Req->argc > 0 )
376         {
377                 /* bestimmte Channels durchgehen */
378                 ptr = strtok( Req->argv[0], "," );
379                 while( ptr )
380                 {
381                         chan = Channel_Search( ptr );
382                         if( chan )
383                         {
384                                 /* print name */
385                                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
386                         }
387                         if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
388
389                         /* get next channel name */
390                         ptr = strtok( NULL, "," );
391                 }
392                 return CONNECTED;
393         }
394
395         chan = Channel_First( );
396         while( chan )
397         {
398                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
399
400                 chan = Channel_Next( chan );
401         }
402
403         /* Now print all clients which are not in any channel */
404         c = Client_First( );
405         snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
406         while( c )
407         {
408                 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
409                 {
410                         /* its a user, concatenate ... */
411                         if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
412                         strlcat( rpl, Client_ID( c ), sizeof( rpl ));
413
414                         if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
415                         {
416                                 /* Line is gwoing too long, send now */
417                                 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
418                                 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
419                         }
420                 }
421
422                 c = Client_Next( c );
423         }
424         if( rpl[strlen( rpl ) - 1] != ':')
425         {
426                 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
427         }
428
429         IRC_SetPenalty( from, 1 );
430         return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
431 } /* IRC_NAMES */
432
433
434 static unsigned int
435 t_diff(time_t *t, const time_t d)
436 {
437         time_t diff, remain;
438
439         diff = *t / d;
440         remain = diff * d;
441         *t -= remain;
442
443         return (unsigned int)diff;
444 }
445
446
447 static unsigned int
448 uptime_days(time_t *now)
449 {
450         return t_diff(now, 60 * 60 * 24);
451 }
452
453
454 static unsigned int
455 uptime_hrs(time_t *now)
456 {
457         return t_diff(now, 60 * 60);
458 }
459
460
461 static unsigned int
462 uptime_mins(time_t *now)
463 {
464          return t_diff(now, 60);
465 }
466
467
468 /**
469  * Handler for the IRC command "STATS".
470  * See RFC 2812 section 3.4.4.
471  */
472 GLOBAL bool
473 IRC_STATS( CLIENT *Client, REQUEST *Req )
474 {
475         CLIENT *from, *target, *cl;
476         CONN_ID con;
477         char query;
478         COMMAND *cmd;
479         time_t time_now;
480         unsigned int days, hrs, mins;
481
482         assert(Client != NULL);
483         assert(Req != NULL);
484
485         if (Req->argc > 2)
486                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
487                                           Client_ID(Client), Req->command);
488
489         /* use prefix to determine "From" */
490         if (Client_Type(Client) == CLIENT_SERVER)
491                 from = Client_Search(Req->prefix);
492         else
493                 from = Client;
494
495         if (!from)
496                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
497                                           Client_ID(Client), Req->prefix);
498
499         if (Req->argc == 2) {
500                 /* forward to another server? */
501                 target = Client_Search(Req->argv[1]);
502                 if ((!target) || (Client_Type(target) != CLIENT_SERVER))
503                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
504                                                  Client_ID(from), Req->argv[1]);
505
506                 if (target != Client_ThisServer()) {
507                         /* forward to another server */
508                         return IRC_WriteStrClientPrefix(target, from,
509                                      "STATS %s %s", Req->argv[0], Req->argv[1]);
510                 }
511         }
512
513         if (Req->argc > 0)
514                 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
515         else
516                 query = '*';
517
518         switch (query) {
519         case 'l':       /* Link status (servers and own link) */
520         case 'L':
521                 time_now = time(NULL);
522                 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
523                         cl = Conn_GetClient(con);
524                         if (!cl)
525                                 continue;
526                         if ((Client_Type(cl) == CLIENT_SERVER)
527                             || (cl == Client)) {
528                                 /* Server link or our own connection */
529 #ifdef ZLIB
530                                 if (Conn_Options(con) & CONN_ZIP) {
531                                         if (!IRC_WriteStrClient
532                                             (from, RPL_STATSLINKINFOZIP_MSG,
533                                              Client_ID(from), Client_Mask(cl),
534                                              Conn_SendQ(con), Conn_SendMsg(con),
535                                              Zip_SendBytes(con),
536                                              Conn_SendBytes(con),
537                                              Conn_RecvMsg(con),
538                                              Zip_RecvBytes(con),
539                                              Conn_RecvBytes(con),
540                                              (long)(time_now - Conn_StartTime(con))))
541                                                 return DISCONNECTED;
542                                         continue;
543                                 }
544 #endif
545                                 if (!IRC_WriteStrClient
546                                     (from, RPL_STATSLINKINFO_MSG,
547                                      Client_ID(from), Client_Mask(cl),
548                                      Conn_SendQ(con), Conn_SendMsg(con),
549                                      Conn_SendBytes(con), Conn_RecvMsg(con),
550                                      Conn_RecvBytes(con),
551                                      (long)(time_now - Conn_StartTime(con))))
552                                         return DISCONNECTED;
553                         }
554                 }
555                 break;
556         case 'm':       /* IRC command status (usage count) */
557         case 'M':
558                 cmd = Parse_GetCommandStruct();
559                 for (; cmd->name; cmd++) {
560                         if (cmd->lcount == 0 && cmd->rcount == 0)
561                                 continue;
562                         if (!IRC_WriteStrClient
563                             (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
564                              cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
565                                 return DISCONNECTED;
566                 }
567                 break;
568         case 'u':       /* Server uptime */
569         case 'U':
570                 time_now = time(NULL) - NGIRCd_Start;
571                 days = uptime_days(&time_now);
572                 hrs = uptime_hrs(&time_now);
573                 mins = uptime_mins(&time_now);
574                 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
575                                        days, hrs, mins, (unsigned int)time_now))
576                         return DISCONNECTED;
577                 break;
578         }
579
580         IRC_SetPenalty(from, 2);
581         return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
582                                   Client_ID(from), query);
583 } /* IRC_STATS */
584
585
586 /**
587  * Handler for the IRC command "SUMMON".
588  * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
589  * therefore answers with ERR_SUMMONDISABLED.
590  */
591 GLOBAL bool
592 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
593 {
594         return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
595                                   Client_ID(Client), Req->command);
596 } /* IRC_SUMMON */
597
598
599 GLOBAL bool
600 IRC_TIME( CLIENT *Client, REQUEST *Req )
601 {
602         CLIENT *from, *target;
603         char t_str[64];
604         time_t t;
605
606         assert( Client != NULL );
607         assert( Req != NULL );
608
609         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
610
611         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
612         else from = Client;
613         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
614
615         if( Req->argc == 1 )
616         {
617                 target = Client_Search( Req->argv[0] );
618                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
619
620                 if( target != Client_ThisServer( ))
621                 {
622                         return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
623                 }
624         }
625
626         t = time( NULL );
627         (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
628         return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
629 } /* IRC_TIME */
630
631
632 /**
633  * Handler for the IRC command "USERHOST".
634  * See RFC 2812 section 4.8.
635  */
636 GLOBAL bool
637 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
638 {
639         char rpl[COMMAND_LEN];
640         CLIENT *c;
641         int max, i;
642
643         assert(Client != NULL);
644         assert(Req != NULL);
645
646         if ((Req->argc < 1))
647                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
648                                           Client_ID(Client), Req->command);
649
650         if (Req->argc > 5)
651                 max = 5;
652         else
653                 max = Req->argc;
654
655         strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
656         for (i = 0; i < max; i++) {
657                 c = Client_Search(Req->argv[i]);
658                 if (c && (Client_Type(c) == CLIENT_USER)) {
659                         /* This Nick is "online" */
660                         strlcat(rpl, Client_ID(c), sizeof(rpl));
661                         if (Client_HasMode(c, 'o'))
662                                 strlcat(rpl, "*", sizeof(rpl));
663                         strlcat(rpl, "=", sizeof(rpl));
664                         if (Client_HasMode(c, 'a'))
665                                 strlcat(rpl, "-", sizeof(rpl));
666                         else
667                                 strlcat(rpl, "+", sizeof(rpl));
668                         strlcat(rpl, Client_User(c), sizeof(rpl));
669                         strlcat(rpl, "@", sizeof(rpl));
670                         strlcat(rpl, Client_HostnameCloaked(c), sizeof(rpl));
671                         strlcat(rpl, " ", sizeof(rpl));
672                 }
673         }
674         ngt_TrimLastChr(rpl, ' ');
675
676         return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
677 } /* IRC_USERHOST */
678
679
680 /**
681  * Handler for the IRC command "USERS".
682  * See RFC 2812 section 4.6. As suggested there the command is disabled.
683  */
684 GLOBAL bool
685 IRC_USERS(CLIENT * Client, REQUEST * Req)
686 {
687         return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
688                                   Client_ID(Client), Req->command);
689 } /* IRC_USERS */
690
691
692 GLOBAL bool
693 IRC_VERSION( CLIENT *Client, REQUEST *Req )
694 {
695         CLIENT *target, *prefix;
696
697         assert( Client != NULL );
698         assert( Req != NULL );
699
700         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
701
702         /* Ziel suchen */
703         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
704         else target = Client_ThisServer( );
705
706         /* Prefix ermitteln */
707         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
708         else prefix = Client;
709         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
710
711         /* An anderen Server weiterleiten? */
712         if( target != Client_ThisServer( ))
713         {
714                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
715
716                 /* forwarden */
717                 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
718                 return CONNECTED;
719         }
720
721         /* send version information */
722         IRC_SetPenalty(Client, 1);
723         return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
724                                   PACKAGE_NAME, PACKAGE_VERSION,
725                                   NGIRCd_DebugLevel, Conf_ServerName,
726                                   NGIRCd_VersionAddition);
727 } /* IRC_VERSION */
728
729
730 static bool
731 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
732 {
733         return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
734                                   channelname, Client_User(c),
735                                   Client_HostnameCloaked(c),
736                                   Client_ID(Client_Introducer(c)), Client_ID(c),
737                                   flags, Client_Hops(c), Client_Info(c));
738 }
739
740
741 static const char *
742 who_flags_status(const char *client_modes)
743 {
744         if (strchr(client_modes, 'a'))
745                 return "G"; /* away */
746         return "H";
747 }
748
749
750 static const char *
751 who_flags_qualifier(const char *chan_user_modes)
752 {
753         if (strchr(chan_user_modes, 'o'))
754                 return "@";
755         else if (strchr(chan_user_modes, 'v'))
756                 return "+";
757         return "";
758 }
759
760
761 static bool
762 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
763 {
764         bool is_visible, is_member, is_ircop;
765         CL2CHAN *cl2chan;
766         const char *client_modes;
767         const char *chan_user_modes;
768         char flags[8];
769         CLIENT *c;
770
771         assert( Client != NULL );
772         assert( Chan != NULL );
773
774         is_member = Channel_IsMemberOf(Chan, Client);
775
776         /* Secret channel? */
777         if (!is_member && strchr(Channel_Modes(Chan), 's'))
778                 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
779
780         cl2chan = Channel_FirstMember(Chan);
781         for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
782                 c = Channel_GetClient(cl2chan);
783
784                 client_modes = Client_Modes(c);
785                 is_ircop = strchr(client_modes, 'o') != NULL;
786                 if (OnlyOps && !is_ircop)
787                         continue;
788
789                 is_visible = strchr(client_modes, 'i') == NULL;
790                 if (is_member || is_visible) {
791                         strcpy(flags, who_flags_status(client_modes));
792                         if (is_ircop)
793                                 strlcat(flags, "*", sizeof(flags));
794
795                         chan_user_modes = Channel_UserModes(Chan, c);
796                         strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
797
798                         if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
799                                 return DISCONNECTED;
800                 }
801         }
802         return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
803 } /* IRC_Send_WHO */
804
805
806 GLOBAL bool
807 IRC_WHO( CLIENT *Client, REQUEST *Req )
808 {
809         bool only_ops, have_arg, client_match;
810         const char *channelname, *client_modes, *chan_user_modes;
811         char pattern[COMMAND_LEN];
812         char flags[4];
813         CL2CHAN *cl2chan;
814         CHANNEL *chan, *cn;
815         CLIENT *c;
816
817         assert( Client != NULL );
818         assert( Req != NULL );
819
820         if (Req->argc > 2)
821                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
822
823         only_ops = false;
824         have_arg = false;
825
826         if (Req->argc == 2) {
827                 if (strcmp(Req->argv[1], "o") == 0)
828                         only_ops = true;
829 #ifdef STRICT_RFC
830                 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
831 #endif
832         }
833
834         IRC_SetPenalty(Client, 1);
835         if (Req->argc >= 1) { /* Channel or Mask. */
836                 chan = Channel_Search(Req->argv[0]);
837                 if (chan)
838                         return IRC_Send_WHO(Client, chan, only_ops);
839                 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
840                         have_arg = true;
841                         strlcpy(pattern, Req->argv[0], sizeof(pattern));
842                         ngt_LowerStr(pattern);
843                         IRC_SetPenalty(Client, 3);
844                 }
845         }
846
847         for (c = Client_First(); c != NULL; c = Client_Next(c)) {
848                 if (Client_Type(c) != CLIENT_USER)
849                         continue;
850                  /*
851                   * RFC 2812, 3.6.1:
852                   * In the absence of the parameter, all visible (users who aren't
853                   * invisible (user mode +i) and who don't have a common channel
854                   * with the requesting client) are listed.
855                   *
856                   * The same result can be achieved by using a [sic] of "0"
857                   * or any wildcard which will end up matching every visible user.
858                   *
859                   * The [sic] passed to WHO is matched against users' host, server, real name and
860                   * nickname if the channel cannot be found.
861                   */
862                 client_modes = Client_Modes(c);
863                 if (strchr(client_modes, 'i'))
864                         continue;
865
866                 if (only_ops && !strchr(client_modes, 'o'))
867                         continue;
868
869                 if (have_arg) { /* match pattern against user host/server/name/nick */
870                         client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
871                         if (!client_match)
872                                 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
873                         if (!client_match)
874                                 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
875                         if (!client_match)
876                                 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
877
878                         if (!client_match) /* This isn't the client you're looking for */
879                                 continue;
880                 }
881
882                 strcpy(flags, who_flags_status(client_modes));
883
884                 if (strchr(client_modes, 'o')) /* this client is an operator */
885                         strlcat(flags, "*", sizeof(flags));
886
887                 /* Search suitable channel */
888                 cl2chan = Channel_FirstChannelOf(c);
889                 while (cl2chan) {
890                         cn = Channel_GetChannel(cl2chan);
891                         if (Channel_IsMemberOf(cn, Client) ||
892                                     !strchr(Channel_Modes(cn), 's'))
893                         {
894                                 channelname = Channel_Name(cn);
895                                 break;
896                         }
897                         cl2chan = Channel_NextChannelOf(c, cl2chan);
898                 }
899                 if (cl2chan) {
900                         chan = Channel_GetChannel(cl2chan);
901                         chan_user_modes = Channel_UserModes(chan, c);
902                         strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
903                 } else
904                         channelname = "*";
905
906                 if (!write_whoreply(Client, c, channelname, flags))
907                         return DISCONNECTED;
908         }
909
910         if (Req->argc > 0)
911                 channelname = Req->argv[0];
912         else
913                 channelname = "*";
914
915         return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
916 } /* IRC_WHO */
917
918
919 /**
920  * Generate WHOIS reply of one actual client.
921  *
922  * @param Client        The client from which this command has been received.
923  * @param from          The client requesting the information ("originator").
924  * @param c             The client of which information should be returned.
925  * @returns             CONNECTED or DISCONNECTED.
926  */
927 static bool
928 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
929 {
930         char str[LINE_LEN + 1];
931         CL2CHAN *cl2chan;
932         CHANNEL *chan;
933
934         /* Nick, user, hostname and client info */
935         if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
936                                 Client_ID(c), Client_User(c),
937                                 Client_HostnameCloaked(c), Client_Info(c)))
938                 return DISCONNECTED;
939
940         /* Server */
941         if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
942                                 Client_ID(c), Client_ID(Client_Introducer(c)),
943                                 Client_Info(Client_Introducer(c))))
944                 return DISCONNECTED;
945
946         /* Channels */
947         snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
948                  Client_ID(from), Client_ID(c));
949         cl2chan = Channel_FirstChannelOf(c);
950         while (cl2chan) {
951                 chan = Channel_GetChannel(cl2chan);
952                 assert(chan != NULL);
953
954                 /* next */
955                 cl2chan = Channel_NextChannelOf(c, cl2chan);
956
957                 /* Secret channel? */
958                 if (strchr(Channel_Modes(chan), 's')
959                     && !Channel_IsMemberOf(chan, Client))
960                         continue;
961
962                 /* Local channel and request is not from a user? */
963                 if (Client_Type(Client) == CLIENT_SERVER
964                     && Channel_IsLocal(chan))
965                         continue;
966
967                 /* Concatenate channel names */
968                 if (str[strlen(str) - 1] != ':')
969                         strlcat(str, " ", sizeof(str));
970
971                 strlcat(str, who_flags_qualifier(Channel_UserModes(chan, c)),
972                                                  sizeof(str));
973                 strlcat(str, Channel_Name(chan), sizeof(str));
974
975                 if (strlen(str) > (LINE_LEN - CHANNEL_NAME_LEN - 4)) {
976                         /* Line becomes too long: send it! */
977                         if (!IRC_WriteStrClient(Client, "%s", str))
978                                 return DISCONNECTED;
979                         snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
980                                  Client_ID(from), Client_ID(c));
981                 }
982         }
983         if(str[strlen(str) - 1] != ':') {
984                 /* There is data left to send: */
985                 if (!IRC_WriteStrClient(Client, "%s", str))
986                         return DISCONNECTED;
987         }
988
989         /* IRC-Operator? */
990         if (Client_HasMode(c, 'o') &&
991                 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
992                                     Client_ID(from), Client_ID(c)))
993                         return DISCONNECTED;
994
995         /* Connected using SSL? */
996         if (Conn_UsesSSL(Client_Conn(c)) &&
997                 !IRC_WriteStrClient(from, RPL_WHOISSSL_MSG,
998                                     Client_ID(from), Client_ID(c)))
999                         return DISCONNECTED;
1000
1001         /* Idle and signon time (local clients only!) */
1002         if (Client_Conn(c) > NONE &&
1003                 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1004                                     Client_ID(from), Client_ID(c),
1005                                     (unsigned long)Conn_GetIdle(Client_Conn(c)),
1006                                     (unsigned long)Conn_GetSignon(Client_Conn(c))))
1007                         return DISCONNECTED;
1008
1009         /* Away? */
1010         if (Client_HasMode(c, 'a') &&
1011                 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
1012                                     Client_ID(from), Client_ID(c),
1013                                     Client_Away(c)))
1014                         return DISCONNECTED;
1015
1016         return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1017                                   Client_ID(from), Client_ID(c));
1018 } /* IRC_WHOIS_SendReply */
1019
1020
1021 /**
1022  * Handler for the IRC "WHOIS" command.
1023  *
1024  * See RFC 2812, 3.6.2 "Whois query".
1025  *
1026  * @param Client        The client from which this command has been received.
1027  * @param Req           Request structure with prefix and all parameters.
1028  * @return              CONNECTED or DISCONNECTED.
1029  */
1030 GLOBAL bool
1031 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1032 {
1033         CLIENT *from, *target, *c;
1034         unsigned int match_count = 0, found = 0;
1035         bool has_wildcards, is_remote;
1036         bool got_wildcard = false;
1037         const char *query;
1038
1039         assert( Client != NULL );
1040         assert( Req != NULL );
1041
1042         /* Bad number of parameters? */
1043         if (Req->argc < 1 || Req->argc > 2)
1044                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1045                                           Client_ID(Client), Req->command);
1046
1047         /* Search sender of the WHOIS */
1048         if (Client_Type(Client) == CLIENT_SERVER) {
1049                 from = Client_Search(Req->prefix);
1050         } else {
1051                 IRC_SetPenalty(Client, 1);
1052                 from = Client;
1053         }
1054         if (!from)
1055                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1056                                           Client_ID(Client), Req->prefix);
1057
1058         /* Get target server for this command */
1059         if (Req->argc > 1) {
1060                 /* Search the target server, which can be specified as a
1061                  * nick name on that server as well: */
1062                 target = Client_Search(Req->argv[0]);
1063                 if (!target)
1064                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
1065                                                 Client_ID(from), Req->argv[0]);
1066         } else
1067                 target = Client_ThisServer();
1068         assert(target != NULL);
1069
1070         /* Forward to other server? */
1071         if (Client_NextHop(target) != Client_ThisServer() &&
1072             Client_Type(Client_NextHop(target)) == CLIENT_SERVER)
1073                 return IRC_WriteStrClientPrefix(target, from,
1074                                                 "WHOIS %s :%s",
1075                                                 Req->argv[0], Req->argv[1]);
1076
1077         is_remote = Client_Conn(from) < 0;
1078         for (query = strtok(Req->argv[Req->argc - 1], ",");
1079                         query && found < 3;
1080                         query = strtok(NULL, ","), found++)
1081         {
1082                 has_wildcards = query[strcspn(query, "*?")] != 0;
1083                 /*
1084                  * follows ircd 2.10 implementation:
1085                  *  - handle up to 3 targets
1086                  *  - no wildcards for remote clients
1087                  *  - only one wildcard target per local client
1088                  *
1089                  *  also, at most ten matches are returned.
1090                  */
1091                 if (!has_wildcards || is_remote) {
1092                         c = Client_Search(query);
1093                         if (c) {
1094                                 if (!IRC_WHOIS_SendReply(Client, from, c))
1095                                         return DISCONNECTED;
1096                         } else {
1097                                 if (!IRC_WriteStrClient(Client,
1098                                                         ERR_NOSUCHNICK_MSG,
1099                                                         Client_ID(Client),
1100                                                         query))
1101                                         return DISCONNECTED;
1102                         }
1103                         continue;
1104                 }
1105                 if (got_wildcard) {
1106                         /* we already handled one wildcard query */
1107                         if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1108                              Client_ID(Client), query))
1109                                 return DISCONNECTED;
1110                         continue;
1111                 }
1112                 got_wildcard = true;
1113                 IRC_SetPenalty(Client, 3);
1114
1115                 for (c = Client_First(); c && match_count < 10; c = Client_Next(c)) {
1116                         if (Client_Type(c) != CLIENT_USER)
1117                                 continue;
1118                         if (!MatchCaseInsensitive(query, Client_ID(c)))
1119                                 continue;
1120                         if (!IRC_WHOIS_SendReply(Client, from, c))
1121                                 return DISCONNECTED;
1122                         match_count++;
1123                 }
1124
1125                 if (match_count == 0)
1126                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1127                                 Client_ID(Client), Req->argv[Req->argc - 1]);
1128         }
1129         return CONNECTED;
1130 } /* IRC_WHOIS */
1131
1132
1133 static bool
1134 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1135 {
1136         char t_str[60];
1137
1138         (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1139                                         localtime(&entry->time));
1140
1141         if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1142                         entry->id, entry->user, entry->host, entry->info))
1143                                 return DISCONNECTED;
1144
1145         return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1146                   entry->id, entry->server, t_str);
1147 }
1148
1149 /**
1150  * IRC "WHOWAS" function.
1151  * This function implements the IRC command "WHOWHAS". It handles local
1152  * requests and request that should be forwarded to other servers.
1153  */
1154 GLOBAL bool
1155 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1156 {
1157         CLIENT *target, *prefix;
1158         WHOWAS *whowas;
1159         char tok_buf[COMMAND_LEN];
1160         int max, last, count, i, nc;
1161         const char *nick;
1162
1163         assert( Client != NULL );
1164         assert( Req != NULL );
1165
1166         /* Wrong number of parameters? */
1167         if (Req->argc > 3)
1168                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1169                                         Client_ID(Client), Req->command);
1170         if (Req->argc < 1)
1171                 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1172
1173         /* Search target */
1174         if (Req->argc == 3)
1175                 target = Client_Search(Req->argv[2]);
1176         else
1177                 target = Client_ThisServer();
1178
1179         /* Get prefix */
1180         if (Client_Type(Client) == CLIENT_SERVER)
1181                 prefix = Client_Search(Req->prefix);
1182         else
1183                 prefix = Client;
1184
1185         if (!prefix)
1186                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1187                                                 Client_ID(Client), Req->prefix);
1188
1189         /* Forward to other server? */
1190         if (target != Client_ThisServer()) {
1191                 if (!target || (Client_Type(target) != CLIENT_SERVER))
1192                         return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1193                                         Client_ID(prefix), Req->argv[2]);
1194
1195                 /* Forward */
1196                 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1197                                           Req->argv[0], Req->argv[1],
1198                                           Req->argv[2] );
1199                 return CONNECTED;
1200         }
1201
1202         whowas = Client_GetWhowas( );
1203         last = Client_GetLastWhowasIndex( );
1204         if (last < 0)
1205                 last = 0;
1206
1207         max = DEFAULT_WHOWAS;
1208         if (Req->argc > 1) {
1209                 max = atoi(Req->argv[1]);
1210                 if (max < 1)
1211                         max = MAX_WHOWAS;
1212         }
1213
1214         /*
1215          * Break up the nick argument into a list of nicks, if applicable
1216          * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1217          */
1218         strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1219         nick = strtok(tok_buf, ",");
1220
1221         for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1222                 nc = 0;
1223                 do {
1224                         /* Used entry? */
1225                         if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1226                                 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1227                                         return DISCONNECTED;
1228                                 nc++;
1229                                 count++;
1230                         }
1231                         /* previous entry */
1232                         i--;
1233
1234                         /* "underflow", wrap around */
1235                         if (i < 0)
1236                                 i = MAX_WHOWAS - 1;
1237
1238                         if (nc && count >= max)
1239                                 break;
1240                 } while (i != last);
1241
1242                 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1243                                                 Client_ID(prefix), nick))
1244                         return DISCONNECTED;
1245         }
1246         return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1247 } /* IRC_WHOWAS */
1248
1249
1250 GLOBAL bool
1251 IRC_Send_LUSERS( CLIENT *Client )
1252 {
1253         unsigned long cnt;
1254 #ifndef STRICT_RFC
1255         unsigned long max;
1256 #endif
1257
1258         assert( Client != NULL );
1259
1260         /* Users, services and serevers in the network */
1261         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1262
1263         /* Number of IRC operators */
1264         cnt = Client_OperCount( );
1265         if( cnt > 0 )
1266         {
1267                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1268         }
1269
1270         /* Unknown connections */
1271         cnt = Client_UnknownCount( );
1272         if( cnt > 0 )
1273         {
1274                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1275         }
1276
1277         /* Number of created channels */
1278         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1279
1280         /* Number of local users, services and servers */
1281         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1282
1283 #ifndef STRICT_RFC
1284         /* Maximum number of local users */
1285         cnt = Client_MyUserCount();
1286         max = Client_MyMaxUserCount();
1287         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1288                         cnt, max, cnt, max))
1289                 return DISCONNECTED;
1290         /* Maximum number of users in the network */
1291         cnt = Client_UserCount();
1292         max = Client_MaxUserCount();
1293         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1294                         cnt, max, cnt, max))
1295                 return DISCONNECTED;
1296         /* Connection counters */
1297         if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1298                         Conn_CountMax(), Conn_CountAccepted()))
1299                 return DISCONNECTED;
1300 #endif
1301         
1302         return CONNECTED;
1303 } /* IRC_Send_LUSERS */
1304
1305
1306 static bool
1307 Show_MOTD_Start(CLIENT *Client)
1308 {
1309         return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1310                 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1311 }
1312
1313 static bool
1314 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1315 {
1316         return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1317 }
1318
1319 static bool
1320 Show_MOTD_End(CLIENT *Client)
1321 {
1322         return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1323 }
1324
1325 #ifdef SSL_SUPPORT
1326 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1327 {
1328         bool ret = true;
1329         char buf[COMMAND_LEN] = "Connected using Cipher ";
1330
1331         if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1332                 return true;
1333
1334         if (!Show_MOTD_Sendline(Client, buf))
1335                 ret = false;
1336
1337         return ret;
1338 }
1339 #else
1340 static inline bool
1341 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1342 { return true; }
1343 #endif
1344
1345 GLOBAL bool
1346 IRC_Show_MOTD( CLIENT *Client )
1347 {
1348         const char *line;
1349         size_t len_tot, len_str;
1350
1351         assert( Client != NULL );
1352
1353         len_tot = array_bytes(&Conf_Motd);
1354         if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1355                 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1356
1357         if (!Show_MOTD_Start(Client))
1358                 return DISCONNECTED;
1359
1360         line = array_start(&Conf_Motd);
1361         while (len_tot > 0) {
1362                 len_str = strlen(line) + 1;
1363
1364                 assert(len_tot >= len_str);
1365                 len_tot -= len_str;
1366
1367                 if (!Show_MOTD_Sendline(Client, line))
1368                         return DISCONNECTED;
1369                 line += len_str;
1370         }
1371
1372         if (!Show_MOTD_SSLInfo(Client))
1373                 return DISCONNECTED;
1374         return Show_MOTD_End(Client);
1375 } /* IRC_Show_MOTD */
1376
1377
1378 GLOBAL bool
1379 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1380 {
1381         bool is_visible, is_member;
1382         char str[LINE_LEN + 1];
1383         CL2CHAN *cl2chan;
1384         CLIENT *cl;
1385
1386         assert( Client != NULL );
1387         assert( Chan != NULL );
1388
1389         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1390         else is_member = false;
1391
1392         /* Secret channel? */
1393         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1394
1395         /* Alle Mitglieder suchen */
1396         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1397         cl2chan = Channel_FirstMember( Chan );
1398         while( cl2chan )
1399         {
1400                 cl = Channel_GetClient( cl2chan );
1401
1402                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1403                 else is_visible = true;
1404
1405                 if( is_member || is_visible )
1406                 {
1407                         /* Nick anhaengen */
1408                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1409                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1410                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1411                         strlcat( str, Client_ID( cl ), sizeof( str ));
1412
1413                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1414                         {
1415                                 /* Zeile wird zu lang: senden! */
1416                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1417                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1418                         }
1419                 }
1420
1421                 /* naechstes Mitglied suchen */
1422                 cl2chan = Channel_NextMember( Chan, cl2chan );
1423         }
1424         if( str[strlen( str ) - 1] != ':')
1425         {
1426                 /* Es sind noch Daten da, die gesendet werden muessen */
1427                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1428         }
1429
1430         return CONNECTED;
1431 } /* IRC_Send_NAMES */
1432
1433
1434 /**
1435  * Send the ISUPPORT numeric (005).
1436  * This numeric indicates the features that are supported by this server.
1437  * See <http://www.irc.org/tech_docs/005.html> for details.
1438  */
1439 GLOBAL bool
1440 IRC_Send_ISUPPORT(CLIENT * Client)
1441 {
1442         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1443                                 Conf_MaxJoins))
1444                 return DISCONNECTED;
1445         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1446                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1447                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1448                                   COMMAND_LEN - 113);
1449 } /* IRC_Send_ISUPPORT */
1450
1451
1452 /* -eof- */