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