]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
24db75dea109b59a6242b4432310ce501a872e51
[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.41 2007/12/11 11:29:44 fw 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 static unsigned int
339 t_diff(time_t *t, const time_t div)
340 {
341         time_t diff, remain;
342
343         diff = *t / div;
344
345         remain = diff * div;
346         *t -= remain;
347
348         return diff;
349 }
350
351
352 static unsigned int
353 uptime_days(time_t *now)
354 {
355         return t_diff(now, 60 * 60 * 24);
356 }
357
358
359 static unsigned int
360 uptime_hrs(time_t *now)
361 {
362         return t_diff(now, 60 * 60);
363 }
364
365
366 static unsigned int
367 uptime_mins(time_t *now)
368 {
369          return t_diff(now, 60);
370 }
371
372
373 GLOBAL bool
374 IRC_STATS( CLIENT *Client, REQUEST *Req )
375 {
376         CLIENT *from, *target, *cl;
377         CONN_ID con;
378         char query;
379         COMMAND *cmd;
380         time_t time_now;
381         unsigned int days, hrs, mins;
382
383         assert( Client != NULL );
384         assert( Req != NULL );
385
386         /* Falsche Anzahl Parameter? */
387         if (Req->argc > 2)
388                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
389
390         /* From aus Prefix ermitteln */
391         if (Client_Type(Client) == CLIENT_SERVER)
392                 from = Client_Search(Req->prefix);
393         else
394                 from = Client;
395
396         if (! from)
397                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix);
398
399         if (Req->argc == 2) {
400                 /* an anderen Server forwarden */
401                 target = Client_Search( Req->argv[1] );
402                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
403                         return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
404
405                 if( target != Client_ThisServer()) {
406                         /* Ok, anderer Server ist das Ziel: forwarden */
407                         return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
408                 }
409         }
410
411         if (Req->argc > 0)
412                 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
413         else
414                 query = '*';
415
416         switch (query) {
417                 case 'l':       /* Links */
418                 case 'L':
419                         time_now = time(NULL);
420                         for (con = Conn_First(); con != NONE ;con = Conn_Next(con)) {
421                                 cl = Conn_GetClient(con);
422                                 if (!cl)
423                                         continue;
424                                 if ((Client_Type(cl) == CLIENT_SERVER) || (cl == Client)) {
425                                         /* Server link or our own connection */
426 #ifdef ZLIB
427                                         if (Conn_Options(con) & CONN_ZIP) {
428                                                 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFOZIP_MSG,
429                                                         Client_ID(from), Client_Mask(cl), Conn_SendQ(con),
430                                                         Conn_SendMsg(con), Zip_SendBytes(con), Conn_SendBytes(con),
431                                                         Conn_RecvMsg(con), Zip_RecvBytes(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
432                                                                 return DISCONNECTED;
433                                                 continue;
434                                         }
435 #endif
436                                         if (!IRC_WriteStrClient(from, RPL_STATSLINKINFO_MSG, Client_ID(from),
437                                                 Client_Mask(cl), Conn_SendQ(con), Conn_SendMsg(con), Conn_SendBytes(con),
438                                                 Conn_RecvMsg(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
439                                                         return DISCONNECTED;
440                                 }
441                         }
442                         break;
443                 case 'm':       /* IRC-Commands */
444                 case 'M':
445                         cmd = Parse_GetCommandStruct( );
446                         for (; cmd->name ; cmd++) {
447                                 if (cmd->lcount == 0 && cmd->rcount == 0)
448                                         continue;
449                                 if (!IRC_WriteStrClient(from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
450                                                 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
451                                                         return DISCONNECTED;
452                         }
453                         break;
454                 case 'u':       /* server uptime */
455                 case 'U':
456                         time_now = time(NULL) - NGIRCd_Start;
457                         days = uptime_days(&time_now);
458                         hrs = uptime_hrs(&time_now);
459                         mins = uptime_mins(&time_now);
460                         if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
461                                         days, hrs, mins, (unsigned int) time_now))
462                                                 return DISCONNECTED;
463                         break;
464         }
465
466         IRC_SetPenalty(from, 2);
467         return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG, Client_ID(from), query);
468 } /* IRC_STATS */
469
470
471 GLOBAL bool
472 IRC_TIME( CLIENT *Client, REQUEST *Req )
473 {
474         CLIENT *from, *target;
475         char t_str[64];
476         time_t t;
477
478         assert( Client != NULL );
479         assert( Req != NULL );
480
481         /* Falsche Anzahl Parameter? */
482         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
483
484         /* From aus Prefix ermitteln */
485         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
486         else from = Client;
487         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
488
489         if( Req->argc == 1 )
490         {
491                 /* an anderen Server forwarden */
492                 target = Client_Search( Req->argv[0] );
493                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
494
495                 if( target != Client_ThisServer( ))
496                 {
497                         /* Ok, anderer Server ist das Ziel: forwarden */
498                         return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
499                 }
500         }
501
502         t = time( NULL );
503         (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
504         return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
505 } /* IRC_TIME */
506
507
508 GLOBAL bool
509 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
510 {
511         char rpl[COMMAND_LEN];
512         CLIENT *c;
513         int max, i;
514
515         assert( Client != NULL );
516         assert( Req != NULL );
517
518         /* Falsche Anzahl Parameter? */
519         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
520
521         if( Req->argc > 5 ) max = 5;
522         else max = Req->argc;
523
524         strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
525         for( i = 0; i < max; i++ )
526         {
527                 c = Client_Search( Req->argv[i] );
528                 if( c && ( Client_Type( c ) == CLIENT_USER ))
529                 {
530                         /* Dieser Nick ist "online" */
531                         strlcat( rpl, Client_ID( c ), sizeof( rpl ));
532                         if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
533                         strlcat( rpl, "=", sizeof( rpl ));
534                         if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
535                         else strlcat( rpl, "+", sizeof( rpl ));
536                         strlcat( rpl, Client_User( c ), sizeof( rpl ));
537                         strlcat( rpl, "@", sizeof( rpl ));
538                         strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
539                         strlcat( rpl, " ", sizeof( rpl ));
540                 }
541         }
542         ngt_TrimLastChr( rpl, ' ');
543
544         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
545 } /* IRC_USERHOST */
546
547
548 GLOBAL bool
549 IRC_VERSION( CLIENT *Client, REQUEST *Req )
550 {
551         CLIENT *target, *prefix;
552 #ifdef CVSDATE
553         char ver[12], vertxt[30];
554 #endif
555
556         assert( Client != NULL );
557         assert( Req != NULL );
558
559         /* Falsche Anzahl Parameter? */
560         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
561
562         /* Ziel suchen */
563         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
564         else target = Client_ThisServer( );
565
566         /* Prefix ermitteln */
567         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
568         else prefix = Client;
569         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
570
571         /* An anderen Server weiterleiten? */
572         if( target != Client_ThisServer( ))
573         {
574                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
575
576                 /* forwarden */
577                 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
578                 return CONNECTED;
579         }
580
581         /* mit Versionsinfo antworten */
582         IRC_SetPenalty( Client, 1 );
583 #ifdef CVSDATE
584         strlcpy( ver, CVSDATE, sizeof( ver ));
585         strncpy( ver + 4, ver + 5, 2 );
586         strncpy( ver + 6, ver + 8, 3 );
587         snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
588         return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, vertxt, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
589 #else
590         return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
591 #endif
592 } /* IRC_VERSION */
593
594
595 GLOBAL bool
596 IRC_WHO( CLIENT *Client, REQUEST *Req )
597 {
598         bool ok, only_ops;
599         char flags[8];
600         const char *ptr;
601         CL2CHAN *cl2chan;
602         CHANNEL *chan, *cn;
603         CLIENT *c;
604
605         assert( Client != NULL );
606         assert( Req != NULL );
607
608         /* Falsche Anzahl Parameter? */
609         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
610
611         only_ops = false;
612         chan = NULL;
613
614         if( Req->argc == 2 )
615         {
616                 /* Nur OPs anzeigen? */
617                 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = true;
618 #ifdef STRICT_RFC
619                 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
620 #endif
621         }
622
623         if( Req->argc >= 1 )
624         {
625                 /* wurde ein Channel oder Nick-Mask angegeben? */
626                 chan = Channel_Search( Req->argv[0] );
627         }
628
629         if( chan )
630         {
631                 /* User eines Channels ausgeben */
632                 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
633         }
634
635         c = Client_First( );
636         while( c )
637         {
638                 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
639                 {
640                         ok = false;
641                         if( Req->argc == 0 ) ok = true;
642                         else
643                         {
644                                 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = true;
645                                 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = true;
646                         }
647
648                         if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
649                         {
650                                 /* Get flags */
651                                 strcpy( flags, "H" );
652                                 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
653
654                                 /* Search suitable channel */
655                                 cl2chan = Channel_FirstChannelOf( c );
656                                 while( cl2chan )
657                                 {
658                                         cn = Channel_GetChannel( cl2chan );
659                                         if( Channel_IsMemberOf( cn, Client ) ||
660                                             ! strchr( Channel_Modes( cn ), 's' ))
661                                         {
662                                                 ptr = Channel_Name( cn );
663                                                 break;
664                                         }
665                                         cl2chan = Channel_NextChannelOf( c, cl2chan );
666                                 }
667                                 if( ! cl2chan ) ptr = "*";
668
669                                 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;
670                         }
671                 }
672
673                 /* naechster Client */
674                 c = Client_Next( c );
675         }
676
677         if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
678         else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
679         else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
680 } /* IRC_WHO */
681
682
683 GLOBAL bool
684 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
685 {
686         CLIENT *from, *target, *c;
687         char str[LINE_LEN + 1];
688         CL2CHAN *cl2chan;
689         CHANNEL *chan;
690
691         assert( Client != NULL );
692         assert( Req != NULL );
693
694         /* Bad number of parameters? */
695         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
696
697         /* Search client */
698         c = Client_Search( Req->argv[Req->argc - 1] );
699         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
700
701         /* Search sender of the WHOIS */
702         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
703         else from = Client;
704         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
705
706         /* Forward to other server? */
707         if( Req->argc > 1 )
708         {
709                 /* Search target server (can be specified as nick of that server!) */
710                 target = Client_Search( Req->argv[0] );
711                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
712         }
713         else target = Client_ThisServer( );
714
715         assert( target != NULL );
716
717         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] );
718
719         /* Nick, user and name */
720         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
721
722         /* Server */
723         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;
724
725         /* Channels */
726         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
727         cl2chan = Channel_FirstChannelOf( c );
728         while( cl2chan )
729         {
730                 chan = Channel_GetChannel( cl2chan );
731                 assert( chan != NULL );
732
733                 /* next */
734                 cl2chan = Channel_NextChannelOf( c, cl2chan );
735
736                 /* Secret channel? */
737                 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
738
739                 /* Concatenate channel names */
740                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
741                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
742                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
743                 strlcat( str, Channel_Name( chan ), sizeof( str ));
744
745                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
746                 {
747                         /* Line becomes too long: send it! */
748                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
749                         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
750                 }
751         }
752         if( str[strlen( str ) - 1] != ':')
753         {
754                 /* There is data left to send: */
755                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
756         }
757
758         /* IRC-Operator? */
759         if( Client_HasMode( c, 'o' ))
760         {
761                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
762         }
763
764         /* Idle and signon time (local clients only!) */
765         if (Client_Conn(c) > NONE ) {
766                 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
767                         Client_ID(from), Client_ID(c),
768                         (unsigned long)Conn_GetIdle(Client_Conn(c)),
769                         (unsigned long)Conn_GetSignon(Client_Conn(c))))
770                                 return DISCONNECTED;
771         }
772
773         /* Away? */
774         if( Client_HasMode( c, 'a' ))
775         {
776                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
777         }
778
779         /* End of Whois */
780         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
781 } /* IRC_WHOIS */
782
783
784 /**
785  * IRC "WHOWAS" function.
786  * This function implements the IRC command "WHOWHAS". It handles local
787  * requests and request that should be forwarded to other servers.
788  */
789 GLOBAL bool
790 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
791 {
792         CLIENT *target, *prefix;
793         WHOWAS *whowas;
794         int max, last, count, i;
795         char t_str[60];
796         
797         assert( Client != NULL );
798         assert( Req != NULL );
799
800         /* Wrong number of parameters? */
801         if(( Req->argc < 1 ) || ( Req->argc > 3 ))
802                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
803                                            Client_ID( Client ), Req->command );
804
805         /* Search taget */
806         if( Req->argc == 3 )
807                 target = Client_Search( Req->argv[2] );
808         else
809                 target = Client_ThisServer( );
810
811         /* Get prefix */
812         if( Client_Type( Client ) == CLIENT_SERVER )
813                 prefix = Client_Search( Req->prefix );
814         else
815                 prefix = Client;
816
817         if( ! prefix )
818                 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
819                                            Client_ID( Client ), Req->prefix );
820
821         /* Forward to other server? */
822         if( target != Client_ThisServer( ))
823         {
824                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
825                         return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
826                                                    Client_ID( prefix ),
827                                                    Req->argv[2] );
828
829                 /* Forward */
830                 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
831                                           Req->argv[0], Req->argv[1],
832                                           Req->argv[2] );
833                 return CONNECTED;
834         }
835         
836         whowas = Client_GetWhowas( );
837         last = Client_GetLastWhowasIndex( );
838         if( last < 0 ) last = 0;
839         
840         if( Req->argc > 1 )
841         {
842                 max = atoi( Req->argv[1] );
843                 if( max < 1 ) max = MAX_WHOWAS;
844         }
845         else
846                 max = DEFAULT_WHOWAS;
847         
848         i = last;
849         count = 0;
850         do
851         {
852                 /* Used entry? */
853                 if( whowas[i].time > 0 &&
854                     strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
855                 {
856                         (void)strftime( t_str, sizeof(t_str),
857                                         "%a %b %d %H:%M:%S %Y",
858                                         localtime( &whowas[i].time ));
859                 
860                         if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
861                                                   Client_ID( prefix ),
862                                                   whowas[i].id,
863                                                   whowas[i].user,
864                                                   whowas[i].host, 
865                                                   whowas[i].info ))
866                                 return DISCONNECTED;
867                 
868                         if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
869                                                   Client_ID( prefix ),
870                                                   whowas[i].id,
871                                                   whowas[i].server, t_str ))
872                                 return DISCONNECTED;
873                 
874                         count++;
875                         if( count >= max ) break;
876                 }
877                 
878                 /* previos entry */
879                 i--;
880
881                 /* "underflow", wrap around */
882                 if( i < 0 ) i = MAX_WHOWAS - 1;
883         } while( i != last );
884         
885         return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
886                                    Client_ID( prefix ), Req->argv[0] );
887 } /* IRC_WHOWAS */
888
889
890 GLOBAL bool
891 IRC_Send_LUSERS( CLIENT *Client )
892 {
893         unsigned long cnt;
894 #ifndef STRICT_RFC
895         unsigned long max;
896 #endif
897
898         assert( Client != NULL );
899
900         /* Users, services and serevers in the network */
901         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
902
903         /* Number of IRC operators */
904         cnt = Client_OperCount( );
905         if( cnt > 0 )
906         {
907                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
908         }
909
910         /* Unknown connections */
911         cnt = Client_UnknownCount( );
912         if( cnt > 0 )
913         {
914                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
915         }
916
917         /* Number of created channels */
918         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
919
920         /* Number of local users, services and servers */
921         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
922
923 #ifndef STRICT_RFC
924         /* Maximum number of local users */
925         cnt = Client_MyUserCount();
926         max = Client_MyMaxUserCount();
927         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
928                         cnt, max, cnt, max))
929                 return DISCONNECTED;
930         /* Maximum number of users in the network */
931         cnt = Client_UserCount();
932         max = Client_MaxUserCount();
933         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
934                         cnt, max, cnt, max))
935                 return DISCONNECTED;
936 #endif
937         
938         return CONNECTED;
939 } /* IRC_Send_LUSERS */
940
941
942 static bool
943 Show_MOTD_Start(CLIENT *Client)
944 {
945         return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
946                 Client_ID( Client ), Client_ID( Client_ThisServer( )));
947 }
948
949 static bool
950 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
951 {
952         return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
953 }
954
955 static bool
956 Show_MOTD_End(CLIENT *Client)
957 {
958         return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
959 }
960
961
962 GLOBAL bool
963 IRC_Show_MOTD( CLIENT *Client )
964 {
965         char line[127];
966         FILE *fd;
967
968         assert( Client != NULL );
969
970         if (Conf_MotdPhrase[0]) {
971                 if (!Show_MOTD_Start(Client))
972                         return DISCONNECTED;
973                 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
974                         return DISCONNECTED;
975
976                 return Show_MOTD_End(Client);
977         }
978
979         fd = fopen( Conf_MotdFile, "r" );
980         if( ! fd ) {
981                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
982                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
983         }
984
985         if (!Show_MOTD_Start( Client )) {
986                 fclose(fd);
987                 return false;
988         }
989
990         while (fgets( line, (int)sizeof line, fd )) {
991                 ngt_TrimLastChr( line, '\n');
992
993                 if( ! Show_MOTD_Sendline( Client, line)) {
994                         fclose( fd );
995                         return false;
996                 }
997         }
998         fclose(fd);
999         return Show_MOTD_End(Client);
1000 } /* IRC_Show_MOTD */
1001
1002
1003 GLOBAL bool
1004 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1005 {
1006         bool is_visible, is_member;
1007         char str[LINE_LEN + 1];
1008         CL2CHAN *cl2chan;
1009         CLIENT *cl;
1010
1011         assert( Client != NULL );
1012         assert( Chan != NULL );
1013
1014         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1015         else is_member = false;
1016
1017         /* Secret channel? */
1018         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1019
1020         /* Alle Mitglieder suchen */
1021         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1022         cl2chan = Channel_FirstMember( Chan );
1023         while( cl2chan )
1024         {
1025                 cl = Channel_GetClient( cl2chan );
1026
1027                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1028                 else is_visible = true;
1029
1030                 if( is_member || is_visible )
1031                 {
1032                         /* Nick anhaengen */
1033                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1034                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1035                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1036                         strlcat( str, Client_ID( cl ), sizeof( str ));
1037
1038                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1039                         {
1040                                 /* Zeile wird zu lang: senden! */
1041                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1042                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1043                         }
1044                 }
1045
1046                 /* naechstes Mitglied suchen */
1047                 cl2chan = Channel_NextMember( Chan, cl2chan );
1048         }
1049         if( str[strlen( str ) - 1] != ':')
1050         {
1051                 /* Es sind noch Daten da, die gesendet werden muessen */
1052                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1053         }
1054
1055         return CONNECTED;
1056 } /* IRC_Send_NAMES */
1057
1058
1059 GLOBAL bool
1060 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
1061 {
1062         bool is_visible, is_member;
1063         CL2CHAN *cl2chan;
1064         char flags[8];
1065         CLIENT *c;
1066
1067         assert( Client != NULL );
1068         assert( Chan != NULL );
1069
1070         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1071         else is_member = false;
1072
1073         /* Secret channel? */
1074         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1075
1076         /* Alle Mitglieder suchen */
1077         cl2chan = Channel_FirstMember( Chan );
1078         while( cl2chan )
1079         {
1080                 c = Channel_GetClient( cl2chan );
1081
1082                 if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
1083                 else is_visible = true;
1084
1085                 if( is_member || is_visible )
1086                 {
1087                         /* Flags zusammenbasteln */
1088                         strcpy( flags, "H" );
1089                         if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
1090                         if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
1091                         else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
1092
1093                         /* ausgeben */
1094                         if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
1095                         {
1096                                 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;
1097                         }
1098                 }
1099
1100                 /* naechstes Mitglied suchen */
1101                 cl2chan = Channel_NextMember( Chan, cl2chan );
1102         }
1103         return CONNECTED;
1104 } /* IRC_Send_WHO */
1105
1106
1107 /**
1108  * Send the ISUPPORT numeric (005).
1109  * This numeric indicates the features that are supported by this server.
1110  * See <http://www.irc.org/tech_docs/005.html> for details.
1111  */
1112 GLOBAL bool
1113 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1114 {
1115         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1116                                 Conf_MaxJoins))
1117                 return DISCONNECTED;
1118         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1119                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1120                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1121                                   COMMAND_LEN - 113);
1122 } /* IRC_Send_ISUPPORT */
1123
1124
1125 /* -eof- */