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