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