]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
Fix t_diff(): declaration of 'div' shadows a global declaration
[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' ) && ! Channel_IsMemberOf( chan, Client )) continue;
936
937                 /* Concatenate channel names */
938                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
939                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
940                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
941                 strlcat( str, Channel_Name( chan ), sizeof( str ));
942
943                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
944                 {
945                         /* Line becomes too long: send it! */
946                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
947                         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
948                 }
949         }
950         if( str[strlen( str ) - 1] != ':')
951         {
952                 /* There is data left to send: */
953                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
954         }
955
956         /* IRC-Operator? */
957         if( Client_HasMode( c, 'o' ))
958         {
959                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
960         }
961
962         /* Idle and signon time (local clients only!) */
963         if (Client_Conn(c) > NONE ) {
964                 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
965                         Client_ID(from), Client_ID(c),
966                         (unsigned long)Conn_GetIdle(Client_Conn(c)),
967                         (unsigned long)Conn_GetSignon(Client_Conn(c))))
968                                 return DISCONNECTED;
969         }
970
971         /* Away? */
972         if( Client_HasMode( c, 'a' ))
973         {
974                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
975         }
976
977         /* End of Whois */
978         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
979 } /* IRC_WHOIS */
980
981
982 static bool
983 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
984 {
985         char t_str[60];
986
987         (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
988                                         localtime(&entry->time));
989
990         if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
991                         entry->id, entry->user, entry->host, entry->info))
992                                 return DISCONNECTED;
993
994         return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
995                   entry->id, entry->server, t_str);
996 }
997
998 /**
999  * IRC "WHOWAS" function.
1000  * This function implements the IRC command "WHOWHAS". It handles local
1001  * requests and request that should be forwarded to other servers.
1002  */
1003 GLOBAL bool
1004 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1005 {
1006         CLIENT *target, *prefix;
1007         WHOWAS *whowas;
1008         char tok_buf[COMMAND_LEN];
1009         int max, last, count, i, nc;
1010         const char *nick;
1011
1012         assert( Client != NULL );
1013         assert( Req != NULL );
1014
1015         /* Wrong number of parameters? */
1016         if (Req->argc > 3)
1017                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1018                                         Client_ID(Client), Req->command);
1019         if (Req->argc < 1)
1020                 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1021
1022         /* Search target */
1023         if (Req->argc == 3)
1024                 target = Client_Search(Req->argv[2]);
1025         else
1026                 target = Client_ThisServer();
1027
1028         /* Get prefix */
1029         if (Client_Type(Client) == CLIENT_SERVER)
1030                 prefix = Client_Search(Req->prefix);
1031         else
1032                 prefix = Client;
1033
1034         if (!prefix)
1035                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1036                                                 Client_ID(Client), Req->prefix);
1037
1038         /* Forward to other server? */
1039         if (target != Client_ThisServer()) {
1040                 if (!target || (Client_Type(target) != CLIENT_SERVER))
1041                         return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1042                                         Client_ID(prefix), Req->argv[2]);
1043
1044                 /* Forward */
1045                 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1046                                           Req->argv[0], Req->argv[1],
1047                                           Req->argv[2] );
1048                 return CONNECTED;
1049         }
1050
1051         whowas = Client_GetWhowas( );
1052         last = Client_GetLastWhowasIndex( );
1053         if (last < 0)
1054                 last = 0;
1055
1056         max = DEFAULT_WHOWAS;
1057         if (Req->argc > 1) {
1058                 max = atoi(Req->argv[1]);
1059                 if (max < 1)
1060                         max = MAX_WHOWAS;
1061         }
1062
1063         /*
1064          * Break up the nick argument into a list of nicks, if applicable
1065          * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1066          */
1067         strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1068         nick = strtok(tok_buf, ",");
1069
1070         for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1071                 nc = 0;
1072                 do {
1073                         /* Used entry? */
1074                         if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1075                                 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1076                                         return DISCONNECTED;
1077                                 nc++;
1078                                 count++;
1079                         }
1080                         /* previous entry */
1081                         i--;
1082
1083                         /* "underflow", wrap around */
1084                         if (i < 0)
1085                                 i = MAX_WHOWAS - 1;
1086
1087                         if (nc && count >= max)
1088                                 break;
1089                 } while (i != last);
1090
1091                 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1092                                                 Client_ID(prefix), nick))
1093                         return DISCONNECTED;
1094         }
1095         return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1096 } /* IRC_WHOWAS */
1097
1098
1099 GLOBAL bool
1100 IRC_Send_LUSERS( CLIENT *Client )
1101 {
1102         unsigned long cnt;
1103 #ifndef STRICT_RFC
1104         unsigned long max;
1105 #endif
1106
1107         assert( Client != NULL );
1108
1109         /* Users, services and serevers in the network */
1110         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1111
1112         /* Number of IRC operators */
1113         cnt = Client_OperCount( );
1114         if( cnt > 0 )
1115         {
1116                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1117         }
1118
1119         /* Unknown connections */
1120         cnt = Client_UnknownCount( );
1121         if( cnt > 0 )
1122         {
1123                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1124         }
1125
1126         /* Number of created channels */
1127         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1128
1129         /* Number of local users, services and servers */
1130         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1131
1132 #ifndef STRICT_RFC
1133         /* Maximum number of local users */
1134         cnt = Client_MyUserCount();
1135         max = Client_MyMaxUserCount();
1136         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1137                         cnt, max, cnt, max))
1138                 return DISCONNECTED;
1139         /* Maximum number of users in the network */
1140         cnt = Client_UserCount();
1141         max = Client_MaxUserCount();
1142         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1143                         cnt, max, cnt, max))
1144                 return DISCONNECTED;
1145 #endif
1146         
1147         return CONNECTED;
1148 } /* IRC_Send_LUSERS */
1149
1150
1151 static bool
1152 Show_MOTD_Start(CLIENT *Client)
1153 {
1154         return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1155                 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1156 }
1157
1158 static bool
1159 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1160 {
1161         return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1162 }
1163
1164 static bool
1165 Show_MOTD_End(CLIENT *Client)
1166 {
1167         return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1168 }
1169
1170
1171 GLOBAL bool
1172 IRC_Show_MOTD( CLIENT *Client )
1173 {
1174         char line[127];
1175         FILE *fd;
1176
1177         assert( Client != NULL );
1178
1179         if (Conf_MotdPhrase[0]) {
1180                 if (!Show_MOTD_Start(Client))
1181                         return DISCONNECTED;
1182                 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
1183                         return DISCONNECTED;
1184
1185                 return Show_MOTD_End(Client);
1186         }
1187
1188         fd = fopen( Conf_MotdFile, "r" );
1189         if( ! fd ) {
1190                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1191                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1192         }
1193
1194         if (!Show_MOTD_Start( Client )) {
1195                 fclose(fd);
1196                 return false;
1197         }
1198
1199         while (fgets( line, (int)sizeof line, fd )) {
1200                 ngt_TrimLastChr( line, '\n');
1201
1202                 if( ! Show_MOTD_Sendline( Client, line)) {
1203                         fclose( fd );
1204                         return false;
1205                 }
1206         }
1207         fclose(fd);
1208         return Show_MOTD_End(Client);
1209 } /* IRC_Show_MOTD */
1210
1211
1212 GLOBAL bool
1213 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1214 {
1215         bool is_visible, is_member;
1216         char str[LINE_LEN + 1];
1217         CL2CHAN *cl2chan;
1218         CLIENT *cl;
1219
1220         assert( Client != NULL );
1221         assert( Chan != NULL );
1222
1223         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1224         else is_member = false;
1225
1226         /* Secret channel? */
1227         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1228
1229         /* Alle Mitglieder suchen */
1230         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1231         cl2chan = Channel_FirstMember( Chan );
1232         while( cl2chan )
1233         {
1234                 cl = Channel_GetClient( cl2chan );
1235
1236                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1237                 else is_visible = true;
1238
1239                 if( is_member || is_visible )
1240                 {
1241                         /* Nick anhaengen */
1242                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1243                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1244                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1245                         strlcat( str, Client_ID( cl ), sizeof( str ));
1246
1247                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1248                         {
1249                                 /* Zeile wird zu lang: senden! */
1250                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1251                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1252                         }
1253                 }
1254
1255                 /* naechstes Mitglied suchen */
1256                 cl2chan = Channel_NextMember( Chan, cl2chan );
1257         }
1258         if( str[strlen( str ) - 1] != ':')
1259         {
1260                 /* Es sind noch Daten da, die gesendet werden muessen */
1261                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1262         }
1263
1264         return CONNECTED;
1265 } /* IRC_Send_NAMES */
1266
1267
1268
1269 /**
1270  * Send the ISUPPORT numeric (005).
1271  * This numeric indicates the features that are supported by this server.
1272  * See <http://www.irc.org/tech_docs/005.html> for details.
1273  */
1274 GLOBAL bool
1275 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1276 {
1277         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1278                                 Conf_MaxJoins))
1279                 return DISCONNECTED;
1280         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1281                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1282                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1283                                   COMMAND_LEN - 113);
1284 } /* IRC_Send_ISUPPORT */
1285
1286
1287 /* -eof- */