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