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