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