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