Introduce option to configure the maximum nick name lenth in ngircd.conf
[ngircd-alex.git] / src / ngircd / irc-info.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2005 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 static char UNUSED id[] = "$Id: irc-info.c,v 1.40 2007/11/21 12:16:36 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <strings.h>
26
27 #include "ngircd.h"
28 #include "cvs-version.h"
29 #include "conn-func.h"
30 #include "conn-zip.h"
31 #include "client.h"
32 #include "channel.h"
33 #include "resolve.h"
34 #include "conf.h"
35 #include "defines.h"
36 #include "log.h"
37 #include "messages.h"
38 #include "tool.h"
39 #include "parse.h"
40 #include "irc-write.h"
41
42 #include "exp.h"
43 #include "irc-info.h"
44
45
46 GLOBAL bool
47 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
48 {
49         CLIENT *target, *prefix;
50
51         assert( Client != NULL );
52         assert( Req != NULL );
53
54         /* Falsche Anzahl Parameter? */
55         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
56
57         /* Ziel suchen */
58         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
59         else target = Client_ThisServer( );
60
61         /* Prefix ermitteln */
62         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
63         else prefix = Client;
64         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
65
66         /* An anderen Server weiterleiten? */
67         if( target != Client_ThisServer( ))
68         {
69                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
70
71                 /* forwarden */
72                 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
73                 return CONNECTED;
74         }
75
76         /* mit Versionsinfo antworten */
77         if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
78         if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
79         if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
80         if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
81
82         IRC_SetPenalty( Client, 1 );
83         return CONNECTED;
84 } /* IRC_ADMIN */
85
86
87 GLOBAL bool
88 IRC_ISON( CLIENT *Client, REQUEST *Req )
89 {
90         char rpl[COMMAND_LEN];
91         CLIENT *c;
92         char *ptr;
93         int i;
94
95         assert( Client != NULL );
96         assert( Req != NULL );
97
98         /* Falsche Anzahl Parameter? */
99         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
100
101         strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
102         for( i = 0; i < Req->argc; i++ )
103         {
104                 ptr = strtok( Req->argv[i], " " );
105                 while( ptr )
106                 {
107                         ngt_TrimStr( ptr );
108                         c = Client_Search( ptr );
109                         if( c && ( Client_Type( c ) == CLIENT_USER ))
110                         {
111                                 /* Dieser Nick ist "online" */
112                                 strlcat( rpl, ptr, sizeof( rpl ));
113                                 strlcat( rpl, " ", sizeof( rpl ));
114                         }
115                         ptr = strtok( NULL, " " );
116                 }
117         }
118         ngt_TrimLastChr(rpl, ' ');
119
120         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
121 } /* IRC_ISON */
122
123
124 GLOBAL bool
125 IRC_LINKS( CLIENT *Client, REQUEST *Req )
126 {
127         CLIENT *target, *from, *c;
128         char *mask;
129
130         assert( Client != NULL );
131         assert( Req != NULL );
132
133         /* Falsche Anzahl Parameter? */
134         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
135
136         /* Server-Mask ermitteln */
137         if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
138         else mask = "*";
139
140         /* Absender ermitteln */
141         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
142         else from = Client;
143         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
144
145         /* An anderen Server forwarden? */
146         if( Req->argc == 2 )
147         {
148                 target = Client_Search( Req->argv[0] );
149                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
150                 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
151         }
152
153         /* Wer ist der Absender? */
154         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
155         else target = Client;
156         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
157
158         c = Client_First( );
159         while( c )
160         {
161                 if( Client_Type( c ) == CLIENT_SERVER )
162                 {
163                         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;
164                 }
165                 c = Client_Next( c );
166         }
167         
168         IRC_SetPenalty( target, 1 );
169         return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
170 } /* IRC_LINKS */
171
172
173 GLOBAL bool
174 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
175 {
176         CLIENT *target, *from;
177
178         assert( Client != NULL );
179         assert( Req != NULL );
180
181         /* Falsche Anzahl Parameter? */
182         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
183
184         /* Absender ermitteln */
185         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
186         else from = Client;
187         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
188
189         /* An anderen Server forwarden? */
190         if( Req->argc == 2 )
191         {
192                 target = Client_Search( Req->argv[1] );
193                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
194                 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
195         }
196
197         /* Wer ist der Absender? */
198         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
199         else target = Client;
200         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
201
202         IRC_Send_LUSERS( target );
203
204         IRC_SetPenalty( target, 1 );
205         return CONNECTED;
206 } /* IRC_LUSERS */
207
208
209 GLOBAL bool
210 IRC_MOTD( CLIENT *Client, REQUEST *Req )
211 {
212         CLIENT *from, *target;
213
214         assert( Client != NULL );
215         assert( Req != NULL );
216
217         /* Falsche Anzahl Parameter? */
218         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
219
220         /* From aus Prefix ermitteln */
221         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
222         else from = Client;
223         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
224
225         if( Req->argc == 1 )
226         {
227                 /* an anderen Server forwarden */
228                 target = Client_Search( Req->argv[0] );
229                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
230
231                 if( target != Client_ThisServer( ))
232                 {
233                         /* Ok, anderer Server ist das Ziel: forwarden */
234                         return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
235                 }
236         }
237
238         IRC_SetPenalty( from, 3 );
239         return IRC_Show_MOTD( from );
240 } /* IRC_MOTD */
241
242
243 GLOBAL bool
244 IRC_NAMES( CLIENT *Client, REQUEST *Req )
245 {
246         char rpl[COMMAND_LEN], *ptr;
247         CLIENT *target, *from, *c;
248         CHANNEL *chan;
249
250         assert( Client != NULL );
251         assert( Req != NULL );
252
253         /* Falsche Anzahl Parameter? */
254         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
255
256         /* From aus Prefix ermitteln */
257         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
258         else from = Client;
259         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
260
261         if( Req->argc == 2 )
262         {
263                 /* an anderen Server forwarden */
264                 target = Client_Search( Req->argv[1] );
265                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
266
267                 if( target != Client_ThisServer( ))
268                 {
269                         /* Ok, anderer Server ist das Ziel: forwarden */
270                         return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
271                 }
272         }
273
274         if( Req->argc > 0 )
275         {
276                 /* bestimmte Channels durchgehen */
277                 ptr = strtok( Req->argv[0], "," );
278                 while( ptr )
279                 {
280                         chan = Channel_Search( ptr );
281                         if( chan )
282                         {
283                                 /* Namen ausgeben */
284                                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
285                         }
286                         if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
287
288                         /* naechsten Namen ermitteln */
289                         ptr = strtok( NULL, "," );
290                 }
291                 return CONNECTED;
292         }
293
294         /* alle Channels durchgehen */
295         chan = Channel_First( );
296         while( chan )
297         {
298                 /* Namen ausgeben */
299                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
300
301                 /* naechster Channel */
302                 chan = Channel_Next( chan );
303         }
304
305         /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
306         c = Client_First( );
307         snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
308         while( c )
309         {
310                 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
311                 {
312                         /* Okay, das ist ein User: anhaengen */
313                         if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
314                         strlcat( rpl, Client_ID( c ), sizeof( rpl ));
315
316                         if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
317                         {
318                                 /* Zeile wird zu lang: senden! */
319                                 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
320                                 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
321                         }
322                 }
323
324                 /* naechster Client */
325                 c = Client_Next( c );
326         }
327         if( rpl[strlen( rpl ) - 1] != ':')
328         {
329                 /* es wurden User gefunden */
330                 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
331         }
332
333         IRC_SetPenalty( from, 1 );
334         return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
335 } /* IRC_NAMES */
336
337
338 GLOBAL bool
339 IRC_STATS( CLIENT *Client, REQUEST *Req )
340 {
341         CLIENT *from, *target, *cl;
342         CONN_ID con;
343         char query;
344         COMMAND *cmd;
345
346         assert( Client != NULL );
347         assert( Req != NULL );
348
349         /* Falsche Anzahl Parameter? */
350         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
351
352         /* From aus Prefix ermitteln */
353         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
354         else from = Client;
355         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
356
357         if( Req->argc == 2 )
358         {
359                 /* an anderen Server forwarden */
360                 target = Client_Search( Req->argv[1] );
361                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
362
363                 if( target != Client_ThisServer( ))
364                 {
365                         /* Ok, anderer Server ist das Ziel: forwarden */
366                         return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
367                 }
368         }
369
370         if( Req->argc > 0 ) query = Req->argv[0][0] ? Req->argv[0][0] : '*';
371         else query = '*';
372
373         switch ( query )
374         {
375                 case 'l':       /* Links */
376                 case 'L':
377                         con = Conn_First( );
378                         while( con != NONE )
379                         {
380                                 cl = Conn_GetClient( con );
381                                 if( cl && (( Client_Type( cl ) == CLIENT_SERVER ) || ( cl == Client )))
382                                 {
383                                         /* Server link or our own connection */
384 #ifdef ZLIB
385                                         if( Conn_Options( con ) & CONN_ZIP )
386                                         {
387                                                 if( ! IRC_WriteStrClient( from, RPL_STATSLINKINFOZIP_MSG, Client_ID( from ), Client_Mask( cl ), Conn_SendQ( con ), Conn_SendMsg( con ), Zip_SendBytes( con ), Conn_SendBytes( con ), Conn_RecvMsg( con ), Zip_RecvBytes( con ), Conn_RecvBytes( con ), (long)( time( NULL ) - Conn_StartTime( con )))) return DISCONNECTED;
388                                         }
389                                         else
390 #endif
391                                         {
392                                                 if( ! IRC_WriteStrClient( from, RPL_STATSLINKINFO_MSG, Client_ID( from ), Client_Mask( cl ), Conn_SendQ( con ), Conn_SendMsg( con ), Conn_SendBytes( con ), Conn_RecvMsg( con ), Conn_RecvBytes( con ), (long)( time( NULL ) - Conn_StartTime( con )))) return DISCONNECTED;
393                                         }
394                                 }
395                                 con = Conn_Next( con );
396                         }
397                         break;
398                 case 'm':       /* IRC-Befehle */
399                 case 'M':
400                         cmd = Parse_GetCommandStruct( );
401                         while( cmd->name )
402                         {
403                                 if( cmd->lcount > 0 || cmd->rcount > 0 )
404                                 {
405                                         if( ! IRC_WriteStrClient( from, RPL_STATSCOMMANDS_MSG, Client_ID( from ), cmd->name, cmd->lcount, cmd->bytes, cmd->rcount )) return DISCONNECTED;
406                                 }
407                                 cmd++;
408                         }
409                         break;
410         }
411
412         IRC_SetPenalty( from, 2 );
413         return IRC_WriteStrClient( from, RPL_ENDOFSTATS_MSG, Client_ID( from ), query );
414 } /* IRC_STATS */
415
416
417 GLOBAL bool
418 IRC_TIME( CLIENT *Client, REQUEST *Req )
419 {
420         CLIENT *from, *target;
421         char t_str[64];
422         time_t t;
423
424         assert( Client != NULL );
425         assert( Req != NULL );
426
427         /* Falsche Anzahl Parameter? */
428         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
429
430         /* From aus Prefix ermitteln */
431         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
432         else from = Client;
433         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
434
435         if( Req->argc == 1 )
436         {
437                 /* an anderen Server forwarden */
438                 target = Client_Search( Req->argv[0] );
439                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
440
441                 if( target != Client_ThisServer( ))
442                 {
443                         /* Ok, anderer Server ist das Ziel: forwarden */
444                         return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
445                 }
446         }
447
448         t = time( NULL );
449         (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
450         return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
451 } /* IRC_TIME */
452
453
454 GLOBAL bool
455 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
456 {
457         char rpl[COMMAND_LEN];
458         CLIENT *c;
459         int max, i;
460
461         assert( Client != NULL );
462         assert( Req != NULL );
463
464         /* Falsche Anzahl Parameter? */
465         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
466
467         if( Req->argc > 5 ) max = 5;
468         else max = Req->argc;
469
470         strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
471         for( i = 0; i < max; i++ )
472         {
473                 c = Client_Search( Req->argv[i] );
474                 if( c && ( Client_Type( c ) == CLIENT_USER ))
475                 {
476                         /* Dieser Nick ist "online" */
477                         strlcat( rpl, Client_ID( c ), sizeof( rpl ));
478                         if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
479                         strlcat( rpl, "=", sizeof( rpl ));
480                         if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
481                         else strlcat( rpl, "+", sizeof( rpl ));
482                         strlcat( rpl, Client_User( c ), sizeof( rpl ));
483                         strlcat( rpl, "@", sizeof( rpl ));
484                         strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
485                         strlcat( rpl, " ", sizeof( rpl ));
486                 }
487         }
488         ngt_TrimLastChr( rpl, ' ');
489
490         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
491 } /* IRC_USERHOST */
492
493
494 GLOBAL bool
495 IRC_VERSION( CLIENT *Client, REQUEST *Req )
496 {
497         CLIENT *target, *prefix;
498 #ifdef CVSDATE
499         char ver[12], vertxt[30];
500 #endif
501
502         assert( Client != NULL );
503         assert( Req != NULL );
504
505         /* Falsche Anzahl Parameter? */
506         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
507
508         /* Ziel suchen */
509         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
510         else target = Client_ThisServer( );
511
512         /* Prefix ermitteln */
513         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
514         else prefix = Client;
515         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
516
517         /* An anderen Server weiterleiten? */
518         if( target != Client_ThisServer( ))
519         {
520                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
521
522                 /* forwarden */
523                 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
524                 return CONNECTED;
525         }
526
527         /* mit Versionsinfo antworten */
528         IRC_SetPenalty( Client, 1 );
529 #ifdef CVSDATE
530         strlcpy( ver, CVSDATE, sizeof( ver ));
531         strncpy( ver + 4, ver + 5, 2 );
532         strncpy( ver + 6, ver + 8, 3 );
533         snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
534         return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, vertxt, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
535 #else
536         return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
537 #endif
538 } /* IRC_VERSION */
539
540
541 GLOBAL bool
542 IRC_WHO( CLIENT *Client, REQUEST *Req )
543 {
544         bool ok, only_ops;
545         char flags[8];
546         const char *ptr;
547         CL2CHAN *cl2chan;
548         CHANNEL *chan, *cn;
549         CLIENT *c;
550
551         assert( Client != NULL );
552         assert( Req != NULL );
553
554         /* Falsche Anzahl Parameter? */
555         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
556
557         only_ops = false;
558         chan = NULL;
559
560         if( Req->argc == 2 )
561         {
562                 /* Nur OPs anzeigen? */
563                 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = true;
564 #ifdef STRICT_RFC
565                 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
566 #endif
567         }
568
569         if( Req->argc >= 1 )
570         {
571                 /* wurde ein Channel oder Nick-Mask angegeben? */
572                 chan = Channel_Search( Req->argv[0] );
573         }
574
575         if( chan )
576         {
577                 /* User eines Channels ausgeben */
578                 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
579         }
580
581         c = Client_First( );
582         while( c )
583         {
584                 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
585                 {
586                         ok = false;
587                         if( Req->argc == 0 ) ok = true;
588                         else
589                         {
590                                 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = true;
591                                 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = true;
592                         }
593
594                         if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
595                         {
596                                 /* Get flags */
597                                 strcpy( flags, "H" );
598                                 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
599
600                                 /* Search suitable channel */
601                                 cl2chan = Channel_FirstChannelOf( c );
602                                 while( cl2chan )
603                                 {
604                                         cn = Channel_GetChannel( cl2chan );
605                                         if( Channel_IsMemberOf( cn, Client ) ||
606                                             ! strchr( Channel_Modes( cn ), 's' ))
607                                         {
608                                                 ptr = Channel_Name( cn );
609                                                 break;
610                                         }
611                                         cl2chan = Channel_NextChannelOf( c, cl2chan );
612                                 }
613                                 if( ! cl2chan ) ptr = "*";
614
615                                 if( ! IRC_WriteStrClient( Client, RPL_WHOREPLY_MSG, Client_ID( Client ), ptr, Client_User( c ), Client_Hostname( c ), Client_ID( Client_Introducer( c )), Client_ID( c ), flags, Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
616                         }
617                 }
618
619                 /* naechster Client */
620                 c = Client_Next( c );
621         }
622
623         if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
624         else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
625         else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
626 } /* IRC_WHO */
627
628
629 GLOBAL bool
630 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
631 {
632         CLIENT *from, *target, *c;
633         char str[LINE_LEN + 1];
634         CL2CHAN *cl2chan;
635         CHANNEL *chan;
636
637         assert( Client != NULL );
638         assert( Req != NULL );
639
640         /* Bad number of parameters? */
641         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
642
643         /* Search client */
644         c = Client_Search( Req->argv[Req->argc - 1] );
645         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
646
647         /* Search sender of the WHOIS */
648         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
649         else from = Client;
650         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
651
652         /* Forward to other server? */
653         if( Req->argc > 1 )
654         {
655                 /* Search target server (can be specified as nick of that server!) */
656                 target = Client_Search( Req->argv[0] );
657                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
658         }
659         else target = Client_ThisServer( );
660
661         assert( target != NULL );
662
663         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] );
664
665         /* Nick, user and name */
666         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
667
668         /* Server */
669         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;
670
671         /* Channels */
672         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
673         cl2chan = Channel_FirstChannelOf( c );
674         while( cl2chan )
675         {
676                 chan = Channel_GetChannel( cl2chan );
677                 assert( chan != NULL );
678
679                 /* next */
680                 cl2chan = Channel_NextChannelOf( c, cl2chan );
681
682                 /* Secret channel? */
683                 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
684
685                 /* Concatenate channel names */
686                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
687                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
688                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
689                 strlcat( str, Channel_Name( chan ), sizeof( str ));
690
691                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
692                 {
693                         /* Line becomes too long: send it! */
694                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
695                         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
696                 }
697         }
698         if( str[strlen( str ) - 1] != ':')
699         {
700                 /* There is data left to send: */
701                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
702         }
703
704         /* IRC-Operator? */
705         if( Client_HasMode( c, 'o' ))
706         {
707                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
708         }
709
710         /* Idle and signon time (local clients only!) */
711         if (Client_Conn(c) > NONE ) {
712                 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
713                         Client_ID(from), Client_ID(c),
714                         (unsigned long)Conn_GetIdle(Client_Conn(c)),
715                         (unsigned long)Conn_GetSignon(Client_Conn(c))))
716                                 return DISCONNECTED;
717         }
718
719         /* Away? */
720         if( Client_HasMode( c, 'a' ))
721         {
722                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
723         }
724
725         /* End of Whois */
726         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
727 } /* IRC_WHOIS */
728
729
730 /**
731  * IRC "WHOWAS" function.
732  * This function implements the IRC command "WHOWHAS". It handles local
733  * requests and request that should be forwarded to other servers.
734  */
735 GLOBAL bool
736 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
737 {
738         CLIENT *target, *prefix;
739         WHOWAS *whowas;
740         int max, last, count, i;
741         char t_str[60];
742         
743         assert( Client != NULL );
744         assert( Req != NULL );
745
746         /* Wrong number of parameters? */
747         if(( Req->argc < 1 ) || ( Req->argc > 3 ))
748                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
749                                            Client_ID( Client ), Req->command );
750
751         /* Search taget */
752         if( Req->argc == 3 )
753                 target = Client_Search( Req->argv[2] );
754         else
755                 target = Client_ThisServer( );
756
757         /* Get prefix */
758         if( Client_Type( Client ) == CLIENT_SERVER )
759                 prefix = Client_Search( Req->prefix );
760         else
761                 prefix = Client;
762
763         if( ! prefix )
764                 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
765                                            Client_ID( Client ), Req->prefix );
766
767         /* Forward to other server? */
768         if( target != Client_ThisServer( ))
769         {
770                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
771                         return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
772                                                    Client_ID( prefix ),
773                                                    Req->argv[2] );
774
775                 /* Forward */
776                 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
777                                           Req->argv[0], Req->argv[1],
778                                           Req->argv[2] );
779                 return CONNECTED;
780         }
781         
782         whowas = Client_GetWhowas( );
783         last = Client_GetLastWhowasIndex( );
784         if( last < 0 ) last = 0;
785         
786         if( Req->argc > 1 )
787         {
788                 max = atoi( Req->argv[1] );
789                 if( max < 1 ) max = MAX_WHOWAS;
790         }
791         else
792                 max = DEFAULT_WHOWAS;
793         
794         i = last;
795         count = 0;
796         do
797         {
798                 /* Used entry? */
799                 if( whowas[i].time > 0 &&
800                     strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
801                 {
802                         (void)strftime( t_str, sizeof(t_str),
803                                         "%a %b %d %H:%M:%S %Y",
804                                         localtime( &whowas[i].time ));
805                 
806                         if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
807                                                   Client_ID( prefix ),
808                                                   whowas[i].id,
809                                                   whowas[i].user,
810                                                   whowas[i].host, 
811                                                   whowas[i].info ))
812                                 return DISCONNECTED;
813                 
814                         if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
815                                                   Client_ID( prefix ),
816                                                   whowas[i].id,
817                                                   whowas[i].server, t_str ))
818                                 return DISCONNECTED;
819                 
820                         count++;
821                         if( count >= max ) break;
822                 }
823                 
824                 /* previos entry */
825                 i--;
826
827                 /* "underflow", wrap around */
828                 if( i < 0 ) i = MAX_WHOWAS - 1;
829         } while( i != last );
830         
831         return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
832                                    Client_ID( prefix ), Req->argv[0] );
833 } /* IRC_WHOWAS */
834
835
836 GLOBAL bool
837 IRC_Send_LUSERS( CLIENT *Client )
838 {
839         unsigned long cnt;
840 #ifndef STRICT_RFC
841         unsigned long max;
842 #endif
843
844         assert( Client != NULL );
845
846         /* Users, services and serevers in the network */
847         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
848
849         /* Number of IRC operators */
850         cnt = Client_OperCount( );
851         if( cnt > 0 )
852         {
853                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
854         }
855
856         /* Unknown connections */
857         cnt = Client_UnknownCount( );
858         if( cnt > 0 )
859         {
860                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
861         }
862
863         /* Number of created channels */
864         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
865
866         /* Number of local users, services and servers */
867         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
868
869 #ifndef STRICT_RFC
870         /* Maximum number of local users */
871         cnt = Client_MyUserCount();
872         max = Client_MyMaxUserCount();
873         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
874                         cnt, max, cnt, max))
875                 return DISCONNECTED;
876         /* Maximum number of users in the network */
877         cnt = Client_UserCount();
878         max = Client_MaxUserCount();
879         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
880                         cnt, max, cnt, max))
881                 return DISCONNECTED;
882 #endif
883         
884         return CONNECTED;
885 } /* IRC_Send_LUSERS */
886
887
888 static bool
889 Show_MOTD_Start(CLIENT *Client)
890 {
891         return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
892                 Client_ID( Client ), Client_ID( Client_ThisServer( )));
893 }
894
895 static bool
896 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
897 {
898         return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
899 }
900
901 static bool
902 Show_MOTD_End(CLIENT *Client)
903 {
904         return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
905 }
906
907
908 GLOBAL bool
909 IRC_Show_MOTD( CLIENT *Client )
910 {
911         char line[127];
912         FILE *fd;
913
914         assert( Client != NULL );
915
916         if (Conf_MotdPhrase[0]) {
917                 if (!Show_MOTD_Start(Client))
918                         return DISCONNECTED;
919                 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
920                         return DISCONNECTED;
921
922                 return Show_MOTD_End(Client);
923         }
924
925         fd = fopen( Conf_MotdFile, "r" );
926         if( ! fd ) {
927                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
928                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
929         }
930
931         if (!Show_MOTD_Start( Client )) {
932                 fclose(fd);
933                 return false;
934         }
935
936         while (fgets( line, (int)sizeof line, fd )) {
937                 ngt_TrimLastChr( line, '\n');
938
939                 if( ! Show_MOTD_Sendline( Client, line)) {
940                         fclose( fd );
941                         return false;
942                 }
943         }
944         fclose(fd);
945         return Show_MOTD_End(Client);
946 } /* IRC_Show_MOTD */
947
948
949 GLOBAL bool
950 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
951 {
952         bool is_visible, is_member;
953         char str[LINE_LEN + 1];
954         CL2CHAN *cl2chan;
955         CLIENT *cl;
956
957         assert( Client != NULL );
958         assert( Chan != NULL );
959
960         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
961         else is_member = false;
962
963         /* Secret channel? */
964         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
965
966         /* Alle Mitglieder suchen */
967         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
968         cl2chan = Channel_FirstMember( Chan );
969         while( cl2chan )
970         {
971                 cl = Channel_GetClient( cl2chan );
972
973                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
974                 else is_visible = true;
975
976                 if( is_member || is_visible )
977                 {
978                         /* Nick anhaengen */
979                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
980                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
981                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
982                         strlcat( str, Client_ID( cl ), sizeof( str ));
983
984                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
985                         {
986                                 /* Zeile wird zu lang: senden! */
987                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
988                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
989                         }
990                 }
991
992                 /* naechstes Mitglied suchen */
993                 cl2chan = Channel_NextMember( Chan, cl2chan );
994         }
995         if( str[strlen( str ) - 1] != ':')
996         {
997                 /* Es sind noch Daten da, die gesendet werden muessen */
998                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
999         }
1000
1001         return CONNECTED;
1002 } /* IRC_Send_NAMES */
1003
1004
1005 GLOBAL bool
1006 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
1007 {
1008         bool is_visible, is_member;
1009         CL2CHAN *cl2chan;
1010         char flags[8];
1011         CLIENT *c;
1012
1013         assert( Client != NULL );
1014         assert( Chan != NULL );
1015
1016         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1017         else is_member = false;
1018
1019         /* Secret channel? */
1020         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1021
1022         /* Alle Mitglieder suchen */
1023         cl2chan = Channel_FirstMember( Chan );
1024         while( cl2chan )
1025         {
1026                 c = Channel_GetClient( cl2chan );
1027
1028                 if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
1029                 else is_visible = true;
1030
1031                 if( is_member || is_visible )
1032                 {
1033                         /* Flags zusammenbasteln */
1034                         strcpy( flags, "H" );
1035                         if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
1036                         if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
1037                         else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
1038
1039                         /* ausgeben */
1040                         if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
1041                         {
1042                                 if( ! IRC_WriteStrClient( Client, RPL_WHOREPLY_MSG, Client_ID( Client ), Channel_Name( Chan ), Client_User( c ), Client_Hostname( c ), Client_ID( Client_Introducer( c )), Client_ID( c ), flags, Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
1043                         }
1044                 }
1045
1046                 /* naechstes Mitglied suchen */
1047                 cl2chan = Channel_NextMember( Chan, cl2chan );
1048         }
1049         return CONNECTED;
1050 } /* IRC_Send_WHO */
1051
1052
1053 /**
1054  * Send the ISUPPORT numeric (005).
1055  * This numeric indicates the features that are supported by this server.
1056  * See <http://www.irc.org/tech_docs/005.html> for details.
1057  */
1058 GLOBAL bool
1059 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1060 {
1061         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1062                                 Conf_MaxJoins))
1063                 return DISCONNECTED;
1064         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1065                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1066                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1067                                   COMMAND_LEN - 113);
1068 } /* IRC_Send_ISUPPORT */
1069
1070
1071 /* -eof- */