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