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