]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
Cleaned up PRIVMSG and NOTICE patches.
[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 IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
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 GLOBAL bool
770 IRC_WHO( CLIENT *Client, REQUEST *Req )
771 {
772         bool only_ops, have_arg, client_match;
773         const char *channelname, *client_modes, *chan_user_modes;
774         char pattern[COMMAND_LEN];
775         char flags[4];
776         CL2CHAN *cl2chan;
777         CHANNEL *chan, *cn;
778         CLIENT *c;
779
780         assert( Client != NULL );
781         assert( Req != NULL );
782
783         if (Req->argc > 2)
784                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
785
786         only_ops = false;
787         have_arg = false;
788
789         if (Req->argc == 2) {
790                 if (strcmp(Req->argv[1], "o") == 0)
791                         only_ops = true;
792 #ifdef STRICT_RFC
793                 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
794 #endif
795         }
796
797         IRC_SetPenalty(Client, 1);
798         if (Req->argc >= 1) { /* Channel or Mask. */
799                 chan = Channel_Search(Req->argv[0]);
800                 if (chan)
801                         return IRC_Send_WHO(Client, chan, only_ops);
802                 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
803                         have_arg = true;
804                         strlcpy(pattern, Req->argv[0], sizeof(pattern));
805                         ngt_LowerStr(pattern);
806                         IRC_SetPenalty(Client, 3);
807                 }
808         }
809
810         for (c = Client_First(); c != NULL; c = Client_Next(c)) {
811                 if (Client_Type(c) != CLIENT_USER)
812                         continue;
813                  /*
814                   * RFC 2812, 3.6.1:
815                   * In the absence of the parameter, all visible (users who aren't
816                   * invisible (user mode +i) and who don't have a common channel
817                   * with the requesting client) are listed.
818                   *
819                   * The same result can be achieved by using a [sic] of "0"
820                   * or any wildcard which will end up matching every visible user.
821                   *
822                   * The [sic] passed to WHO is matched against users' host, server, real name and
823                   * nickname if the channel cannot be found.
824                   */
825                 client_modes = Client_Modes(c);
826                 if (strchr(client_modes, 'i'))
827                         continue;
828
829                 if (only_ops && !strchr(client_modes, 'o'))
830                         continue;
831
832                 if (have_arg) { /* match pattern against user host/server/name/nick */
833                         client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
834                         if (!client_match)
835                                 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
836                         if (!client_match)
837                                 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
838                         if (!client_match)
839                                 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
840
841                         if (!client_match) /* This isn't the client you're looking for */
842                                 continue;
843                 }
844
845                 strcpy(flags, who_flags_status(client_modes));
846
847                 if (strchr(client_modes, 'o')) /* this client is an operator */
848                         strlcat(flags, "*", sizeof(flags));
849
850                 /* Search suitable channel */
851                 cl2chan = Channel_FirstChannelOf(c);
852                 while (cl2chan) {
853                         cn = Channel_GetChannel(cl2chan);
854                         if (Channel_IsMemberOf(cn, Client) ||
855                                     !strchr(Channel_Modes(cn), 's'))
856                         {
857                                 channelname = Channel_Name(cn);
858                                 break;
859                         }
860                         cl2chan = Channel_NextChannelOf(c, cl2chan);
861                 }
862                 if (cl2chan) {
863                         chan = Channel_GetChannel(cl2chan);
864                         chan_user_modes = Channel_UserModes(chan, c);
865                         strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
866                 } else
867                         channelname = "*";
868
869                 if (!write_whoreply(Client, c, channelname, flags))
870                         return DISCONNECTED;
871         }
872
873         if (Req->argc > 0)
874                 channelname = Req->argv[0];
875         else
876                 channelname = "*";
877
878         return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
879 } /* IRC_WHO */
880
881
882 GLOBAL bool
883 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
884 {
885         CLIENT *from, *target, *c;
886         char str[LINE_LEN + 1];
887         CL2CHAN *cl2chan;
888         CHANNEL *chan;
889
890         assert( Client != NULL );
891         assert( Req != NULL );
892
893         /* Bad number of parameters? */
894         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
895
896         /* Search client */
897         c = Client_Search( Req->argv[Req->argc - 1] );
898         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
899
900         /* Search sender of the WHOIS */
901         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
902         else from = Client;
903         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
904
905         /* Forward to other server? */
906         if( Req->argc > 1 )
907         {
908                 /* Search target server (can be specified as nick of that server!) */
909                 target = Client_Search( Req->argv[0] );
910                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
911         }
912         else target = Client_ThisServer( );
913
914         assert( target != NULL );
915
916         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] );
917
918         /* Nick, user and name */
919         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
920
921         /* Server */
922         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;
923
924         /* Channels */
925         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
926         cl2chan = Channel_FirstChannelOf( c );
927         while( cl2chan )
928         {
929                 chan = Channel_GetChannel( cl2chan );
930                 assert( chan != NULL );
931
932                 /* next */
933                 cl2chan = Channel_NextChannelOf( c, cl2chan );
934
935                 /* Secret channel? */
936                 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
937
938                 /* Concatenate channel names */
939                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
940                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
941                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
942                 strlcat( str, Channel_Name( chan ), sizeof( str ));
943
944                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
945                 {
946                         /* Line becomes too long: send it! */
947                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
948                         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
949                 }
950         }
951         if( str[strlen( str ) - 1] != ':')
952         {
953                 /* There is data left to send: */
954                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
955         }
956
957         /* IRC-Operator? */
958         if( Client_HasMode( c, 'o' ))
959         {
960                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
961         }
962
963         /* Idle and signon time (local clients only!) */
964         if (Client_Conn(c) > NONE ) {
965                 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
966                         Client_ID(from), Client_ID(c),
967                         (unsigned long)Conn_GetIdle(Client_Conn(c)),
968                         (unsigned long)Conn_GetSignon(Client_Conn(c))))
969                                 return DISCONNECTED;
970         }
971
972         /* Away? */
973         if( Client_HasMode( c, 'a' ))
974         {
975                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
976         }
977
978         /* End of Whois */
979         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
980 } /* IRC_WHOIS */
981
982
983 static bool
984 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
985 {
986         char t_str[60];
987
988         (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
989                                         localtime(&entry->time));
990
991         if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
992                         entry->id, entry->user, entry->host, entry->info))
993                                 return DISCONNECTED;
994
995         return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
996                   entry->id, entry->server, t_str);
997 }
998
999 /**
1000  * IRC "WHOWAS" function.
1001  * This function implements the IRC command "WHOWHAS". It handles local
1002  * requests and request that should be forwarded to other servers.
1003  */
1004 GLOBAL bool
1005 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1006 {
1007         CLIENT *target, *prefix;
1008         WHOWAS *whowas;
1009         char tok_buf[COMMAND_LEN];
1010         int max, last, count, i, nc;
1011         const char *nick;
1012
1013         assert( Client != NULL );
1014         assert( Req != NULL );
1015
1016         /* Wrong number of parameters? */
1017         if (Req->argc > 3)
1018                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1019                                         Client_ID(Client), Req->command);
1020         if (Req->argc < 1)
1021                 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1022
1023         /* Search target */
1024         if (Req->argc == 3)
1025                 target = Client_Search(Req->argv[2]);
1026         else
1027                 target = Client_ThisServer();
1028
1029         /* Get prefix */
1030         if (Client_Type(Client) == CLIENT_SERVER)
1031                 prefix = Client_Search(Req->prefix);
1032         else
1033                 prefix = Client;
1034
1035         if (!prefix)
1036                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1037                                                 Client_ID(Client), Req->prefix);
1038
1039         /* Forward to other server? */
1040         if (target != Client_ThisServer()) {
1041                 if (!target || (Client_Type(target) != CLIENT_SERVER))
1042                         return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1043                                         Client_ID(prefix), Req->argv[2]);
1044
1045                 /* Forward */
1046                 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1047                                           Req->argv[0], Req->argv[1],
1048                                           Req->argv[2] );
1049                 return CONNECTED;
1050         }
1051
1052         whowas = Client_GetWhowas( );
1053         last = Client_GetLastWhowasIndex( );
1054         if (last < 0)
1055                 last = 0;
1056
1057         max = DEFAULT_WHOWAS;
1058         if (Req->argc > 1) {
1059                 max = atoi(Req->argv[1]);
1060                 if (max < 1)
1061                         max = MAX_WHOWAS;
1062         }
1063
1064         /*
1065          * Break up the nick argument into a list of nicks, if applicable
1066          * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1067          */
1068         strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1069         nick = strtok(tok_buf, ",");
1070
1071         for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1072                 nc = 0;
1073                 do {
1074                         /* Used entry? */
1075                         if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1076                                 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1077                                         return DISCONNECTED;
1078                                 nc++;
1079                                 count++;
1080                         }
1081                         /* previous entry */
1082                         i--;
1083
1084                         /* "underflow", wrap around */
1085                         if (i < 0)
1086                                 i = MAX_WHOWAS - 1;
1087
1088                         if (nc && count >= max)
1089                                 break;
1090                 } while (i != last);
1091
1092                 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1093                                                 Client_ID(prefix), nick))
1094                         return DISCONNECTED;
1095         }
1096         return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1097 } /* IRC_WHOWAS */
1098
1099
1100 GLOBAL bool
1101 IRC_Send_LUSERS( CLIENT *Client )
1102 {
1103         unsigned long cnt;
1104 #ifndef STRICT_RFC
1105         unsigned long max;
1106 #endif
1107
1108         assert( Client != NULL );
1109
1110         /* Users, services and serevers in the network */
1111         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1112
1113         /* Number of IRC operators */
1114         cnt = Client_OperCount( );
1115         if( cnt > 0 )
1116         {
1117                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1118         }
1119
1120         /* Unknown connections */
1121         cnt = Client_UnknownCount( );
1122         if( cnt > 0 )
1123         {
1124                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1125         }
1126
1127         /* Number of created channels */
1128         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1129
1130         /* Number of local users, services and servers */
1131         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1132
1133 #ifndef STRICT_RFC
1134         /* Maximum number of local users */
1135         cnt = Client_MyUserCount();
1136         max = Client_MyMaxUserCount();
1137         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1138                         cnt, max, cnt, max))
1139                 return DISCONNECTED;
1140         /* Maximum number of users in the network */
1141         cnt = Client_UserCount();
1142         max = Client_MaxUserCount();
1143         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1144                         cnt, max, cnt, max))
1145                 return DISCONNECTED;
1146 #endif
1147         
1148         return CONNECTED;
1149 } /* IRC_Send_LUSERS */
1150
1151
1152 static bool
1153 Show_MOTD_Start(CLIENT *Client)
1154 {
1155         return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1156                 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1157 }
1158
1159 static bool
1160 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1161 {
1162         return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1163 }
1164
1165 static bool
1166 Show_MOTD_End(CLIENT *Client)
1167 {
1168         return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1169 }
1170
1171
1172 GLOBAL bool
1173 IRC_Show_MOTD( CLIENT *Client )
1174 {
1175         char line[127];
1176         FILE *fd;
1177
1178         assert( Client != NULL );
1179
1180         if (Conf_MotdPhrase[0]) {
1181                 if (!Show_MOTD_Start(Client))
1182                         return DISCONNECTED;
1183                 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
1184                         return DISCONNECTED;
1185
1186                 return Show_MOTD_End(Client);
1187         }
1188
1189         fd = fopen( Conf_MotdFile, "r" );
1190         if( ! fd ) {
1191                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1192                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1193         }
1194
1195         if (!Show_MOTD_Start( Client )) {
1196                 fclose(fd);
1197                 return false;
1198         }
1199
1200         while (fgets( line, (int)sizeof line, fd )) {
1201                 ngt_TrimLastChr( line, '\n');
1202
1203                 if( ! Show_MOTD_Sendline( Client, line)) {
1204                         fclose( fd );
1205                         return false;
1206                 }
1207         }
1208         fclose(fd);
1209         return Show_MOTD_End(Client);
1210 } /* IRC_Show_MOTD */
1211
1212
1213 GLOBAL bool
1214 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1215 {
1216         bool is_visible, is_member;
1217         char str[LINE_LEN + 1];
1218         CL2CHAN *cl2chan;
1219         CLIENT *cl;
1220
1221         assert( Client != NULL );
1222         assert( Chan != NULL );
1223
1224         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1225         else is_member = false;
1226
1227         /* Secret channel? */
1228         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1229
1230         /* Alle Mitglieder suchen */
1231         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1232         cl2chan = Channel_FirstMember( Chan );
1233         while( cl2chan )
1234         {
1235                 cl = Channel_GetClient( cl2chan );
1236
1237                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1238                 else is_visible = true;
1239
1240                 if( is_member || is_visible )
1241                 {
1242                         /* Nick anhaengen */
1243                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1244                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1245                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1246                         strlcat( str, Client_ID( cl ), sizeof( str ));
1247
1248                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1249                         {
1250                                 /* Zeile wird zu lang: senden! */
1251                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1252                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1253                         }
1254                 }
1255
1256                 /* naechstes Mitglied suchen */
1257                 cl2chan = Channel_NextMember( Chan, cl2chan );
1258         }
1259         if( str[strlen( str ) - 1] != ':')
1260         {
1261                 /* Es sind noch Daten da, die gesendet werden muessen */
1262                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1263         }
1264
1265         return CONNECTED;
1266 } /* IRC_Send_NAMES */
1267
1268
1269
1270 /**
1271  * Send the ISUPPORT numeric (005).
1272  * This numeric indicates the features that are supported by this server.
1273  * See <http://www.irc.org/tech_docs/005.html> for details.
1274  */
1275 GLOBAL bool
1276 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1277 {
1278         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1279                                 Conf_MaxJoins))
1280                 return DISCONNECTED;
1281         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1282                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1283                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1284                                   COMMAND_LEN - 113);
1285 } /* IRC_Send_ISUPPORT */
1286
1287
1288 /* -eof- */