]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
5a55e3fdeb92b12ef95a92833918acfb7497d112
[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.29 2005/05/16 12:25:15 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         strcpy( rpl, RPL_ISON_MSG );
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 = Client_GetFromConn( 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         strcpy( rpl, RPL_USERHOST_MSG );
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], *ptr;
546         CL2CHAN *cl2chan;
547         CHANNEL *chan, *cn;
548         CLIENT *c;
549
550         assert( Client != NULL );
551         assert( Req != NULL );
552
553         /* Falsche Anzahl Parameter? */
554         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
555
556         only_ops = false;
557         chan = NULL;
558
559         if( Req->argc == 2 )
560         {
561                 /* Nur OPs anzeigen? */
562                 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = true;
563 #ifdef STRICT_RFC
564                 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
565 #endif
566         }
567
568         if( Req->argc >= 1 )
569         {
570                 /* wurde ein Channel oder Nick-Mask angegeben? */
571                 chan = Channel_Search( Req->argv[0] );
572         }
573
574         if( chan )
575         {
576                 /* User eines Channels ausgeben */
577                 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
578         }
579
580         c = Client_First( );
581         while( c )
582         {
583                 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
584                 {
585                         ok = false;
586                         if( Req->argc == 0 ) ok = true;
587                         else
588                         {
589                                 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = true;
590                                 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = true;
591                         }
592
593                         if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
594                         {
595                                 /* Get flags */
596                                 strcpy( flags, "H" );
597                                 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
598
599                                 /* Search suitable channel */
600                                 cl2chan = Channel_FirstChannelOf( c );
601                                 while( cl2chan )
602                                 {
603                                         cn = Channel_GetChannel( cl2chan );
604                                         if( Channel_IsMemberOf( cn, Client ) ||
605                                             ! strchr( Channel_Modes( cn ), 's' ))
606                                         {
607                                                 ptr = Channel_Name( cn );
608                                                 break;
609                                         }
610                                         cl2chan = Channel_NextChannelOf( c, cl2chan );
611                                 }
612                                 if( ! cl2chan ) ptr = "*";
613
614                                 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;
615                         }
616                 }
617
618                 /* naechster Client */
619                 c = Client_Next( c );
620         }
621
622         if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
623         else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
624         else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
625 } /* IRC_WHO */
626
627
628 GLOBAL bool
629 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
630 {
631         CLIENT *from, *target, *c;
632         char str[LINE_LEN + 1];
633         CL2CHAN *cl2chan;
634         CHANNEL *chan;
635
636         assert( Client != NULL );
637         assert( Req != NULL );
638
639         /* Bad number of parameters? */
640         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
641
642         /* Search client */
643         c = Client_Search( Req->argv[Req->argc - 1] );
644         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
645
646         /* Search sender of the WHOIS */
647         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
648         else from = Client;
649         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
650
651         /* Forward to other server? */
652         if( Req->argc > 1 )
653         {
654                 /* Search target server (can be specified as nick of that server!) */
655                 target = Client_Search( Req->argv[0] );
656                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
657         }
658         else target = Client_ThisServer( );
659
660         assert( target != NULL );
661
662         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] );
663
664         /* Nick, user and name */
665         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
666
667         /* Server */
668         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;
669
670         /* Channels */
671         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
672         cl2chan = Channel_FirstChannelOf( c );
673         while( cl2chan )
674         {
675                 chan = Channel_GetChannel( cl2chan );
676                 assert( chan != NULL );
677
678                 /* next */
679                 cl2chan = Channel_NextChannelOf( c, cl2chan );
680
681                 /* Secret channel? */
682                 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
683
684                 /* Concatenate channel names */
685                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
686                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
687                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
688                 strlcat( str, Channel_Name( chan ), sizeof( str ));
689
690                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
691                 {
692                         /* Line becomes too long: send it! */
693                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
694                         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
695                 }
696         }
697         if( str[strlen( str ) - 1] != ':')
698         {
699                 /* There is data left to send: */
700                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
701         }
702
703         /* IRC-Operator? */
704         if( Client_HasMode( c, 'o' ))
705         {
706                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
707         }
708
709         /* Idle (only local clients) */
710         if( Client_Conn( c ) > NONE )
711         {
712                 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
713         }
714
715         /* Away? */
716         if( Client_HasMode( c, 'a' ))
717         {
718                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
719         }
720
721         /* End of Whois */
722         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
723 } /* IRC_WHOIS */
724
725
726 /**
727  * IRC "WHOWAS" function.
728  * This function implements the IRC command "WHOWHAS". It handles local
729  * requests and request that should be forwarded to other servers.
730  */
731 GLOBAL bool
732 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
733 {
734         CLIENT *target, *prefix;
735         WHOWAS *whowas;
736         int max, last, count, i;
737         char t_str[60];
738         
739         assert( Client != NULL );
740         assert( Req != NULL );
741
742         /* Wrong number of parameters? */
743         if(( Req->argc < 1 ) || ( Req->argc > 3 ))
744                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
745                                            Client_ID( Client ), Req->command );
746
747         /* Search taget */
748         if( Req->argc == 3 )
749                 target = Client_Search( Req->argv[2] );
750         else
751                 target = Client_ThisServer( );
752
753         /* Get prefix */
754         if( Client_Type( Client ) == CLIENT_SERVER )
755                 prefix = Client_Search( Req->prefix );
756         else
757                 prefix = Client;
758
759         if( ! prefix )
760                 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
761                                            Client_ID( Client ), Req->prefix );
762
763         /* Forward to other server? */
764         if( target != Client_ThisServer( ))
765         {
766                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
767                         return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
768                                                    Client_ID( prefix ),
769                                                    Req->argv[2] );
770
771                 /* Forward */
772                 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
773                                           Req->argv[0], Req->argv[1],
774                                           Req->argv[2] );
775                 return CONNECTED;
776         }
777         
778         whowas = Client_GetWhowas( );
779         last = Client_GetLastWhowasIndex( );
780         if( last < 0 ) last = 0;
781         
782         if( Req->argc > 1 )
783         {
784                 max = atoi( Req->argv[1] );
785                 if( max < 1 ) max = MAX_WHOWAS;
786         }
787         else
788                 max = DEFAULT_WHOWAS;
789         
790         i = last;
791         count = 0;
792         do
793         {
794                 /* Used entry? */
795                 if( whowas[i].time > 0 &&
796                     strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
797                 {
798                         (void)strftime( t_str, sizeof(t_str),
799                                         "%a %b %d %H:%M:%S %Y",
800                                         localtime( &whowas[i].time ));
801                 
802                         if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
803                                                   Client_ID( prefix ),
804                                                   whowas[i].id,
805                                                   whowas[i].user,
806                                                   whowas[i].host, 
807                                                   whowas[i].info ))
808                                 return DISCONNECTED;
809                 
810                         if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
811                                                   Client_ID( prefix ),
812                                                   whowas[i].id,
813                                                   whowas[i].server, t_str ))
814                                 return DISCONNECTED;
815                 
816                         count++;
817                         if( count >= max ) break;
818                 }
819                 
820                 /* previos entry */
821                 i--;
822
823                 /* "underflow", wrap around */
824                 if( i < 0 ) i = MAX_WHOWAS - 1;
825         } while( i != last );
826         
827         return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
828                                    Client_ID( prefix ), Req->argv[0] );
829 } /* IRC_WHOWAS */
830
831
832 GLOBAL bool
833 IRC_Send_LUSERS( CLIENT *Client )
834 {
835         long cnt;
836
837         assert( Client != NULL );
838
839         /* Users, services and serevers in the network */
840         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
841
842         /* Number of IRC operators */
843         cnt = Client_OperCount( );
844         if( cnt > 0 )
845         {
846                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
847         }
848
849         /* Unknown connections */
850         cnt = Client_UnknownCount( );
851         if( cnt > 0 )
852         {
853                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
854         }
855
856         /* Number of created channels */
857         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
858
859         /* Number of local users, services and servers */
860         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
861
862 #ifndef STRICT_RFC
863         /* Maximum number of local users */
864         if( ! IRC_WriteStrClient( Client, RPL_LOCALUSERS_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyMaxUserCount( ))) return DISCONNECTED;
865         /* Maximum number of users in the network */
866         if( ! IRC_WriteStrClient( Client, RPL_NETUSERS_MSG, Client_ID( Client ), Client_UserCount( ), Client_MaxUserCount( ))) return DISCONNECTED;
867 #endif
868         
869         return CONNECTED;
870 } /* IRC_Send_LUSERS */
871
872
873 GLOBAL bool
874 IRC_Show_MOTD( CLIENT *Client )
875 {
876         bool ok;
877         char line[127];
878         FILE *fd;
879
880         assert( Client != NULL );
881
882         if( Conf_MotdPhrase[0] )
883         {
884                 if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
885                 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), Conf_MotdPhrase )) return DISCONNECTED;
886                 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
887         }
888
889         fd = fopen( Conf_MotdFile, "r" );
890         if( ! fd )
891         {
892                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
893                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
894         }
895
896         if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
897         while( true )
898         {
899                 if( ! fgets( line, sizeof( line ), fd )) break;
900
901                 ngt_TrimLastChr( line, '\n');
902
903                 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
904                 {
905                         fclose( fd );
906                         return false;
907                 }
908         }
909         ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
910
911         fclose( fd );
912
913         return ok;
914 } /* IRC_Show_MOTD */
915
916
917 GLOBAL bool
918 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
919 {
920         bool is_visible, is_member;
921         char str[LINE_LEN + 1];
922         CL2CHAN *cl2chan;
923         CLIENT *cl;
924
925         assert( Client != NULL );
926         assert( Chan != NULL );
927
928         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
929         else is_member = false;
930
931         /* Secret channel? */
932         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
933
934         /* Alle Mitglieder suchen */
935         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
936         cl2chan = Channel_FirstMember( Chan );
937         while( cl2chan )
938         {
939                 cl = Channel_GetClient( cl2chan );
940
941                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
942                 else is_visible = true;
943
944                 if( is_member || is_visible )
945                 {
946                         /* Nick anhaengen */
947                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
948                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
949                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
950                         strlcat( str, Client_ID( cl ), sizeof( str ));
951
952                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
953                         {
954                                 /* Zeile wird zu lang: senden! */
955                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
956                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
957                         }
958                 }
959
960                 /* naechstes Mitglied suchen */
961                 cl2chan = Channel_NextMember( Chan, cl2chan );
962         }
963         if( str[strlen( str ) - 1] != ':')
964         {
965                 /* Es sind noch Daten da, die gesendet werden muessen */
966                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
967         }
968
969         return CONNECTED;
970 } /* IRC_Send_NAMES */
971
972
973 GLOBAL bool
974 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
975 {
976         bool is_visible, is_member;
977         CL2CHAN *cl2chan;
978         char flags[8];
979         CLIENT *c;
980
981         assert( Client != NULL );
982         assert( Chan != NULL );
983
984         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
985         else is_member = false;
986
987         /* Secret channel? */
988         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
989
990         /* Alle Mitglieder suchen */
991         cl2chan = Channel_FirstMember( Chan );
992         while( cl2chan )
993         {
994                 c = Channel_GetClient( cl2chan );
995
996                 if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
997                 else is_visible = true;
998
999                 if( is_member || is_visible )
1000                 {
1001                         /* Flags zusammenbasteln */
1002                         strcpy( flags, "H" );
1003                         if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
1004                         if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
1005                         else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
1006
1007                         /* ausgeben */
1008                         if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
1009                         {
1010                                 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;
1011                         }
1012                 }
1013
1014                 /* naechstes Mitglied suchen */
1015                 cl2chan = Channel_NextMember( Chan, cl2chan );
1016         }
1017         return CONNECTED;
1018 } /* IRC_Send_WHO */
1019
1020
1021 /* -eof- */