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