]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
Don't #include client.h when conn.h/conn-func.h is already included
[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 GLOBAL bool
631 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
632 {
633         char rpl[COMMAND_LEN];
634         CLIENT *c;
635         int max, i;
636
637         assert( Client != NULL );
638         assert( Req != NULL );
639
640         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
641
642         if( Req->argc > 5 ) max = 5;
643         else max = Req->argc;
644
645         strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
646         for( i = 0; i < max; i++ )
647         {
648                 c = Client_Search( Req->argv[i] );
649                 if( c && ( Client_Type( c ) == CLIENT_USER ))
650                 {
651                         /* This Nick is "online" */
652                         strlcat( rpl, Client_ID( c ), sizeof( rpl ));
653                         if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
654                         strlcat( rpl, "=", sizeof( rpl ));
655                         if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
656                         else strlcat( rpl, "+", sizeof( rpl ));
657                         strlcat( rpl, Client_User( c ), sizeof( rpl ));
658                         strlcat( rpl, "@", sizeof( rpl ));
659                         strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
660                         strlcat( rpl, " ", sizeof( rpl ));
661                 }
662         }
663         ngt_TrimLastChr( rpl, ' ');
664
665         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
666 } /* IRC_USERHOST */
667
668
669 /**
670  * Handler for the IRC command "USERS".
671  * See RFC 2812 section 4.6. As suggested there the command is disabled.
672  */
673 GLOBAL bool
674 IRC_USERS(CLIENT * Client, REQUEST * Req)
675 {
676         return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
677                                   Client_ID(Client), Req->command);
678 } /* IRC_USERS */
679
680
681 GLOBAL bool
682 IRC_VERSION( CLIENT *Client, REQUEST *Req )
683 {
684         CLIENT *target, *prefix;
685
686         assert( Client != NULL );
687         assert( Req != NULL );
688
689         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
690
691         /* Ziel suchen */
692         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
693         else target = Client_ThisServer( );
694
695         /* Prefix ermitteln */
696         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
697         else prefix = Client;
698         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
699
700         /* An anderen Server weiterleiten? */
701         if( target != Client_ThisServer( ))
702         {
703                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
704
705                 /* forwarden */
706                 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
707                 return CONNECTED;
708         }
709
710         /* send version information */
711         IRC_SetPenalty(Client, 1);
712         return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
713                                   PACKAGE_NAME, PACKAGE_VERSION,
714                                   NGIRCd_DebugLevel, Conf_ServerName,
715                                   NGIRCd_VersionAddition);
716 } /* IRC_VERSION */
717
718
719 static bool
720 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
721 {
722         return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client), channelname,
723                         Client_User(c), Client_Hostname(c), Client_ID(Client_Introducer(c)), Client_ID(c),
724                         flags, Client_Hops(c), Client_Info(c));
725 }
726
727
728 static const char *
729 who_flags_status(const char *client_modes)
730 {
731         if (strchr(client_modes, 'a'))
732                 return "G"; /* away */
733         return "H";
734 }
735
736
737 static const char *
738 who_flags_qualifier(const char *chan_user_modes)
739 {
740         if (strchr(chan_user_modes, 'o'))
741                 return "@";
742         else if (strchr(chan_user_modes, 'v'))
743                 return "+";
744         return "";
745 }
746
747
748 static bool
749 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
750 {
751         bool is_visible, is_member, is_ircop;
752         CL2CHAN *cl2chan;
753         const char *client_modes;
754         const char *chan_user_modes;
755         char flags[8];
756         CLIENT *c;
757
758         assert( Client != NULL );
759         assert( Chan != NULL );
760
761         is_member = Channel_IsMemberOf(Chan, Client);
762
763         /* Secret channel? */
764         if (!is_member && strchr(Channel_Modes(Chan), 's'))
765                 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
766
767         cl2chan = Channel_FirstMember(Chan);
768         for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
769                 c = Channel_GetClient(cl2chan);
770
771                 client_modes = Client_Modes(c);
772                 is_ircop = strchr(client_modes, 'o') != NULL;
773                 if (OnlyOps && !is_ircop)
774                         continue;
775
776                 is_visible = strchr(client_modes, 'i') == NULL;
777                 if (is_member || is_visible) {
778                         strcpy(flags, who_flags_status(client_modes));
779                         if (is_ircop)
780                                 strlcat(flags, "*", sizeof(flags));
781
782                         chan_user_modes = Channel_UserModes(Chan, c);
783                         strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
784
785                         if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
786                                 return DISCONNECTED;
787                 }
788         }
789         return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
790 } /* IRC_Send_WHO */
791
792
793 GLOBAL bool
794 IRC_WHO( CLIENT *Client, REQUEST *Req )
795 {
796         bool only_ops, have_arg, client_match;
797         const char *channelname, *client_modes, *chan_user_modes;
798         char pattern[COMMAND_LEN];
799         char flags[4];
800         CL2CHAN *cl2chan;
801         CHANNEL *chan, *cn;
802         CLIENT *c;
803
804         assert( Client != NULL );
805         assert( Req != NULL );
806
807         if (Req->argc > 2)
808                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
809
810         only_ops = false;
811         have_arg = false;
812
813         if (Req->argc == 2) {
814                 if (strcmp(Req->argv[1], "o") == 0)
815                         only_ops = true;
816 #ifdef STRICT_RFC
817                 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
818 #endif
819         }
820
821         IRC_SetPenalty(Client, 1);
822         if (Req->argc >= 1) { /* Channel or Mask. */
823                 chan = Channel_Search(Req->argv[0]);
824                 if (chan)
825                         return IRC_Send_WHO(Client, chan, only_ops);
826                 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
827                         have_arg = true;
828                         strlcpy(pattern, Req->argv[0], sizeof(pattern));
829                         ngt_LowerStr(pattern);
830                         IRC_SetPenalty(Client, 3);
831                 }
832         }
833
834         for (c = Client_First(); c != NULL; c = Client_Next(c)) {
835                 if (Client_Type(c) != CLIENT_USER)
836                         continue;
837                  /*
838                   * RFC 2812, 3.6.1:
839                   * In the absence of the parameter, all visible (users who aren't
840                   * invisible (user mode +i) and who don't have a common channel
841                   * with the requesting client) are listed.
842                   *
843                   * The same result can be achieved by using a [sic] of "0"
844                   * or any wildcard which will end up matching every visible user.
845                   *
846                   * The [sic] passed to WHO is matched against users' host, server, real name and
847                   * nickname if the channel cannot be found.
848                   */
849                 client_modes = Client_Modes(c);
850                 if (strchr(client_modes, 'i'))
851                         continue;
852
853                 if (only_ops && !strchr(client_modes, 'o'))
854                         continue;
855
856                 if (have_arg) { /* match pattern against user host/server/name/nick */
857                         client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
858                         if (!client_match)
859                                 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
860                         if (!client_match)
861                                 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
862                         if (!client_match)
863                                 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
864
865                         if (!client_match) /* This isn't the client you're looking for */
866                                 continue;
867                 }
868
869                 strcpy(flags, who_flags_status(client_modes));
870
871                 if (strchr(client_modes, 'o')) /* this client is an operator */
872                         strlcat(flags, "*", sizeof(flags));
873
874                 /* Search suitable channel */
875                 cl2chan = Channel_FirstChannelOf(c);
876                 while (cl2chan) {
877                         cn = Channel_GetChannel(cl2chan);
878                         if (Channel_IsMemberOf(cn, Client) ||
879                                     !strchr(Channel_Modes(cn), 's'))
880                         {
881                                 channelname = Channel_Name(cn);
882                                 break;
883                         }
884                         cl2chan = Channel_NextChannelOf(c, cl2chan);
885                 }
886                 if (cl2chan) {
887                         chan = Channel_GetChannel(cl2chan);
888                         chan_user_modes = Channel_UserModes(chan, c);
889                         strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
890                 } else
891                         channelname = "*";
892
893                 if (!write_whoreply(Client, c, channelname, flags))
894                         return DISCONNECTED;
895         }
896
897         if (Req->argc > 0)
898                 channelname = Req->argv[0];
899         else
900                 channelname = "*";
901
902         return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
903 } /* IRC_WHO */
904
905
906 GLOBAL bool
907 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
908 {
909         CLIENT *from, *target, *c;
910         char str[LINE_LEN + 1];
911         CL2CHAN *cl2chan;
912         CHANNEL *chan;
913
914         assert( Client != NULL );
915         assert( Req != NULL );
916
917         /* Bad number of parameters? */
918         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
919
920         /* Search client */
921         c = Client_Search( Req->argv[Req->argc - 1] );
922         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
923
924         /* Search sender of the WHOIS */
925         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
926         else from = Client;
927         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
928
929         /* Forward to other server? */
930         if( Req->argc > 1 )
931         {
932                 /* Search target server (can be specified as nick of that server!) */
933                 target = Client_Search( Req->argv[0] );
934                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
935         }
936         else target = Client_ThisServer( );
937
938         assert( target != NULL );
939
940         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] );
941
942         /* Nick, user and name */
943         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
944
945         /* Server */
946         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;
947
948         /* Channels */
949         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
950         cl2chan = Channel_FirstChannelOf( c );
951         while( cl2chan )
952         {
953                 chan = Channel_GetChannel( cl2chan );
954                 assert( chan != NULL );
955
956                 /* next */
957                 cl2chan = Channel_NextChannelOf( c, cl2chan );
958
959                 /* Secret channel? */
960                 if (strchr(Channel_Modes(chan), 's')
961                     && !Channel_IsMemberOf(chan, Client))
962                         continue;
963
964                 /* Local channel and request is not from a user? */
965                 if (Client_Type(Client) == CLIENT_SERVER
966                     && Channel_IsLocal(chan))
967                         continue;
968
969                 /* Concatenate channel names */
970                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
971                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
972                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
973                 strlcat( str, Channel_Name( chan ), sizeof( str ));
974
975                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
976                 {
977                         /* Line becomes too long: send it! */
978                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
979                         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
980                 }
981         }
982         if( str[strlen( str ) - 1] != ':')
983         {
984                 /* There is data left to send: */
985                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
986         }
987
988         /* IRC-Operator? */
989         if( Client_HasMode( c, 'o' ))
990         {
991                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
992         }
993
994         /* Connected using SSL? */
995         if (Conn_UsesSSL(Client_Conn(c))) {
996                 if (!IRC_WriteStrClient
997                     (from, RPL_WHOISSSL_MSG, Client_ID(from), Client_ID(c)))
998                         return DISCONNECTED;
999         }
1000
1001         /* Idle and signon time (local clients only!) */
1002         if (Client_Conn(c) > NONE ) {
1003                 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1004                         Client_ID(from), Client_ID(c),
1005                         (unsigned long)Conn_GetIdle(Client_Conn(c)),
1006                         (unsigned long)Conn_GetSignon(Client_Conn(c))))
1007                                 return DISCONNECTED;
1008         }
1009
1010         /* Away? */
1011         if( Client_HasMode( c, 'a' ))
1012         {
1013                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
1014         }
1015
1016         /* End of Whois */
1017         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1018 } /* IRC_WHOIS */
1019
1020
1021 static bool
1022 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1023 {
1024         char t_str[60];
1025
1026         (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1027                                         localtime(&entry->time));
1028
1029         if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1030                         entry->id, entry->user, entry->host, entry->info))
1031                                 return DISCONNECTED;
1032
1033         return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1034                   entry->id, entry->server, t_str);
1035 }
1036
1037 /**
1038  * IRC "WHOWAS" function.
1039  * This function implements the IRC command "WHOWHAS". It handles local
1040  * requests and request that should be forwarded to other servers.
1041  */
1042 GLOBAL bool
1043 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1044 {
1045         CLIENT *target, *prefix;
1046         WHOWAS *whowas;
1047         char tok_buf[COMMAND_LEN];
1048         int max, last, count, i, nc;
1049         const char *nick;
1050
1051         assert( Client != NULL );
1052         assert( Req != NULL );
1053
1054         /* Wrong number of parameters? */
1055         if (Req->argc > 3)
1056                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1057                                         Client_ID(Client), Req->command);
1058         if (Req->argc < 1)
1059                 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1060
1061         /* Search target */
1062         if (Req->argc == 3)
1063                 target = Client_Search(Req->argv[2]);
1064         else
1065                 target = Client_ThisServer();
1066
1067         /* Get prefix */
1068         if (Client_Type(Client) == CLIENT_SERVER)
1069                 prefix = Client_Search(Req->prefix);
1070         else
1071                 prefix = Client;
1072
1073         if (!prefix)
1074                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1075                                                 Client_ID(Client), Req->prefix);
1076
1077         /* Forward to other server? */
1078         if (target != Client_ThisServer()) {
1079                 if (!target || (Client_Type(target) != CLIENT_SERVER))
1080                         return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1081                                         Client_ID(prefix), Req->argv[2]);
1082
1083                 /* Forward */
1084                 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1085                                           Req->argv[0], Req->argv[1],
1086                                           Req->argv[2] );
1087                 return CONNECTED;
1088         }
1089
1090         whowas = Client_GetWhowas( );
1091         last = Client_GetLastWhowasIndex( );
1092         if (last < 0)
1093                 last = 0;
1094
1095         max = DEFAULT_WHOWAS;
1096         if (Req->argc > 1) {
1097                 max = atoi(Req->argv[1]);
1098                 if (max < 1)
1099                         max = MAX_WHOWAS;
1100         }
1101
1102         /*
1103          * Break up the nick argument into a list of nicks, if applicable
1104          * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1105          */
1106         strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1107         nick = strtok(tok_buf, ",");
1108
1109         for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1110                 nc = 0;
1111                 do {
1112                         /* Used entry? */
1113                         if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1114                                 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1115                                         return DISCONNECTED;
1116                                 nc++;
1117                                 count++;
1118                         }
1119                         /* previous entry */
1120                         i--;
1121
1122                         /* "underflow", wrap around */
1123                         if (i < 0)
1124                                 i = MAX_WHOWAS - 1;
1125
1126                         if (nc && count >= max)
1127                                 break;
1128                 } while (i != last);
1129
1130                 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1131                                                 Client_ID(prefix), nick))
1132                         return DISCONNECTED;
1133         }
1134         return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1135 } /* IRC_WHOWAS */
1136
1137
1138 GLOBAL bool
1139 IRC_Send_LUSERS( CLIENT *Client )
1140 {
1141         unsigned long cnt;
1142 #ifndef STRICT_RFC
1143         unsigned long max;
1144 #endif
1145
1146         assert( Client != NULL );
1147
1148         /* Users, services and serevers in the network */
1149         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1150
1151         /* Number of IRC operators */
1152         cnt = Client_OperCount( );
1153         if( cnt > 0 )
1154         {
1155                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1156         }
1157
1158         /* Unknown connections */
1159         cnt = Client_UnknownCount( );
1160         if( cnt > 0 )
1161         {
1162                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1163         }
1164
1165         /* Number of created channels */
1166         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1167
1168         /* Number of local users, services and servers */
1169         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1170
1171 #ifndef STRICT_RFC
1172         /* Maximum number of local users */
1173         cnt = Client_MyUserCount();
1174         max = Client_MyMaxUserCount();
1175         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1176                         cnt, max, cnt, max))
1177                 return DISCONNECTED;
1178         /* Maximum number of users in the network */
1179         cnt = Client_UserCount();
1180         max = Client_MaxUserCount();
1181         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1182                         cnt, max, cnt, max))
1183                 return DISCONNECTED;
1184         /* Connection counters */
1185         if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1186                         Conn_CountMax(), Conn_CountAccepted()))
1187                 return DISCONNECTED;
1188 #endif
1189         
1190         return CONNECTED;
1191 } /* IRC_Send_LUSERS */
1192
1193
1194 static bool
1195 Show_MOTD_Start(CLIENT *Client)
1196 {
1197         return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1198                 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1199 }
1200
1201 static bool
1202 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1203 {
1204         return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1205 }
1206
1207 static bool
1208 Show_MOTD_End(CLIENT *Client)
1209 {
1210         return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1211 }
1212
1213 #ifdef SSL_SUPPORT
1214 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1215 {
1216         bool ret = true;
1217         char buf[COMMAND_LEN] = "Connected using Cipher ";
1218
1219         if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1220                 return true;
1221
1222         if (!Show_MOTD_Sendline(Client, buf))
1223                 ret = false;
1224
1225         return ret;
1226 }
1227 #else
1228 static inline bool Show_MOTD_SSLInfo(UNUSED CLIENT *c) { return true; }
1229 #endif
1230
1231 GLOBAL bool
1232 IRC_Show_MOTD( CLIENT *Client )
1233 {
1234         char line[127];
1235         FILE *fd;
1236
1237         assert( Client != NULL );
1238
1239         if (Conf_MotdPhrase[0]) {
1240                 if (!Show_MOTD_Start(Client))
1241                         return DISCONNECTED;
1242                 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
1243                         return DISCONNECTED;
1244                 goto out;
1245         }
1246
1247         fd = fopen( Conf_MotdFile, "r" );
1248         if( ! fd ) {
1249                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1250                 if (Conn_UsesSSL(Client_Conn(Client))) {
1251                         if (!Show_MOTD_Start(Client))
1252                                 return DISCONNECTED;
1253                         goto out;
1254                 }
1255                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1256         }
1257
1258         if (!Show_MOTD_Start( Client )) {
1259                 fclose(fd);
1260                 return false;
1261         }
1262
1263         while (fgets( line, (int)sizeof line, fd )) {
1264                 ngt_TrimLastChr( line, '\n');
1265
1266                 if( ! Show_MOTD_Sendline( Client, line)) {
1267                         fclose( fd );
1268                         return false;
1269                 }
1270         }
1271         fclose(fd);
1272 out:
1273         if (!Show_MOTD_SSLInfo(Client))
1274                 return DISCONNECTED;
1275         return Show_MOTD_End(Client);
1276 } /* IRC_Show_MOTD */
1277
1278
1279 GLOBAL bool
1280 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1281 {
1282         bool is_visible, is_member;
1283         char str[LINE_LEN + 1];
1284         CL2CHAN *cl2chan;
1285         CLIENT *cl;
1286
1287         assert( Client != NULL );
1288         assert( Chan != NULL );
1289
1290         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1291         else is_member = false;
1292
1293         /* Secret channel? */
1294         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1295
1296         /* Alle Mitglieder suchen */
1297         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1298         cl2chan = Channel_FirstMember( Chan );
1299         while( cl2chan )
1300         {
1301                 cl = Channel_GetClient( cl2chan );
1302
1303                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1304                 else is_visible = true;
1305
1306                 if( is_member || is_visible )
1307                 {
1308                         /* Nick anhaengen */
1309                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1310                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1311                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1312                         strlcat( str, Client_ID( cl ), sizeof( str ));
1313
1314                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1315                         {
1316                                 /* Zeile wird zu lang: senden! */
1317                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1318                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1319                         }
1320                 }
1321
1322                 /* naechstes Mitglied suchen */
1323                 cl2chan = Channel_NextMember( Chan, cl2chan );
1324         }
1325         if( str[strlen( str ) - 1] != ':')
1326         {
1327                 /* Es sind noch Daten da, die gesendet werden muessen */
1328                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1329         }
1330
1331         return CONNECTED;
1332 } /* IRC_Send_NAMES */
1333
1334
1335
1336 /**
1337  * Send the ISUPPORT numeric (005).
1338  * This numeric indicates the features that are supported by this server.
1339  * See <http://www.irc.org/tech_docs/005.html> for details.
1340  */
1341 GLOBAL bool
1342 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1343 {
1344         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1345                                 Conf_MaxJoins))
1346                 return DISCONNECTED;
1347         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1348                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1349                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1350                                   COMMAND_LEN - 113);
1351 } /* IRC_Send_ISUPPORT */
1352
1353
1354 /* -eof- */