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