]> arthur.barton.de Git - ngircd.git/blob - src/ngircd/irc-info.c
send "G" flag in WHOIS replies instead of "H" if client is marked as being away.
[ngircd.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.2.1 2008/02/26 12:06:57 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                                 if (strchr(Client_Modes( c ), 'a'))
652                                         strcpy(flags, "G"); /* away */
653                                 else
654                                         strcpy(flags, "H");
655
656                                 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
657
658                                 /* Search suitable channel */
659                                 cl2chan = Channel_FirstChannelOf( c );
660                                 while( cl2chan )
661                                 {
662                                         cn = Channel_GetChannel( cl2chan );
663                                         if( Channel_IsMemberOf( cn, Client ) ||
664                                             ! strchr( Channel_Modes( cn ), 's' ))
665                                         {
666                                                 ptr = Channel_Name( cn );
667                                                 break;
668                                         }
669                                         cl2chan = Channel_NextChannelOf( c, cl2chan );
670                                 }
671                                 if( ! cl2chan ) ptr = "*";
672
673                                 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;
674                         }
675                 }
676
677                 /* naechster Client */
678                 c = Client_Next( c );
679         }
680
681         if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
682         else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
683         else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
684 } /* IRC_WHO */
685
686
687 GLOBAL bool
688 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
689 {
690         CLIENT *from, *target, *c;
691         char str[LINE_LEN + 1];
692         CL2CHAN *cl2chan;
693         CHANNEL *chan;
694
695         assert( Client != NULL );
696         assert( Req != NULL );
697
698         /* Bad number of parameters? */
699         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
700
701         /* Search client */
702         c = Client_Search( Req->argv[Req->argc - 1] );
703         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
704
705         /* Search sender of the WHOIS */
706         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
707         else from = Client;
708         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
709
710         /* Forward to other server? */
711         if( Req->argc > 1 )
712         {
713                 /* Search target server (can be specified as nick of that server!) */
714                 target = Client_Search( Req->argv[0] );
715                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
716         }
717         else target = Client_ThisServer( );
718
719         assert( target != NULL );
720
721         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] );
722
723         /* Nick, user and name */
724         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
725
726         /* Server */
727         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;
728
729         /* Channels */
730         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
731         cl2chan = Channel_FirstChannelOf( c );
732         while( cl2chan )
733         {
734                 chan = Channel_GetChannel( cl2chan );
735                 assert( chan != NULL );
736
737                 /* next */
738                 cl2chan = Channel_NextChannelOf( c, cl2chan );
739
740                 /* Secret channel? */
741                 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
742
743                 /* Concatenate channel names */
744                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
745                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
746                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
747                 strlcat( str, Channel_Name( chan ), sizeof( str ));
748
749                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
750                 {
751                         /* Line becomes too long: send it! */
752                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
753                         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
754                 }
755         }
756         if( str[strlen( str ) - 1] != ':')
757         {
758                 /* There is data left to send: */
759                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
760         }
761
762         /* IRC-Operator? */
763         if( Client_HasMode( c, 'o' ))
764         {
765                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
766         }
767
768         /* Idle and signon time (local clients only!) */
769         if (Client_Conn(c) > NONE ) {
770                 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
771                         Client_ID(from), Client_ID(c),
772                         (unsigned long)Conn_GetIdle(Client_Conn(c)),
773                         (unsigned long)Conn_GetSignon(Client_Conn(c))))
774                                 return DISCONNECTED;
775         }
776
777         /* Away? */
778         if( Client_HasMode( c, 'a' ))
779         {
780                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
781         }
782
783         /* End of Whois */
784         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
785 } /* IRC_WHOIS */
786
787
788 /**
789  * IRC "WHOWAS" function.
790  * This function implements the IRC command "WHOWHAS". It handles local
791  * requests and request that should be forwarded to other servers.
792  */
793 GLOBAL bool
794 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
795 {
796         CLIENT *target, *prefix;
797         WHOWAS *whowas;
798         int max, last, count, i;
799         char t_str[60];
800         
801         assert( Client != NULL );
802         assert( Req != NULL );
803
804         /* Wrong number of parameters? */
805         if(( Req->argc < 1 ) || ( Req->argc > 3 ))
806                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
807                                            Client_ID( Client ), Req->command );
808
809         /* Search taget */
810         if( Req->argc == 3 )
811                 target = Client_Search( Req->argv[2] );
812         else
813                 target = Client_ThisServer( );
814
815         /* Get prefix */
816         if( Client_Type( Client ) == CLIENT_SERVER )
817                 prefix = Client_Search( Req->prefix );
818         else
819                 prefix = Client;
820
821         if( ! prefix )
822                 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
823                                            Client_ID( Client ), Req->prefix );
824
825         /* Forward to other server? */
826         if( target != Client_ThisServer( ))
827         {
828                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
829                         return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
830                                                    Client_ID( prefix ),
831                                                    Req->argv[2] );
832
833                 /* Forward */
834                 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
835                                           Req->argv[0], Req->argv[1],
836                                           Req->argv[2] );
837                 return CONNECTED;
838         }
839         
840         whowas = Client_GetWhowas( );
841         last = Client_GetLastWhowasIndex( );
842         if( last < 0 ) last = 0;
843         
844         if( Req->argc > 1 )
845         {
846                 max = atoi( Req->argv[1] );
847                 if( max < 1 ) max = MAX_WHOWAS;
848         }
849         else
850                 max = DEFAULT_WHOWAS;
851         
852         i = last;
853         count = 0;
854         do
855         {
856                 /* Used entry? */
857                 if( whowas[i].time > 0 &&
858                     strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
859                 {
860                         (void)strftime( t_str, sizeof(t_str),
861                                         "%a %b %d %H:%M:%S %Y",
862                                         localtime( &whowas[i].time ));
863                 
864                         if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
865                                                   Client_ID( prefix ),
866                                                   whowas[i].id,
867                                                   whowas[i].user,
868                                                   whowas[i].host, 
869                                                   whowas[i].info ))
870                                 return DISCONNECTED;
871                 
872                         if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
873                                                   Client_ID( prefix ),
874                                                   whowas[i].id,
875                                                   whowas[i].server, t_str ))
876                                 return DISCONNECTED;
877                 
878                         count++;
879                         if( count >= max ) break;
880                 }
881                 
882                 /* previos entry */
883                 i--;
884
885                 /* "underflow", wrap around */
886                 if( i < 0 ) i = MAX_WHOWAS - 1;
887         } while( i != last );
888         
889         return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
890                                    Client_ID( prefix ), Req->argv[0] );
891 } /* IRC_WHOWAS */
892
893
894 GLOBAL bool
895 IRC_Send_LUSERS( CLIENT *Client )
896 {
897         unsigned long cnt;
898 #ifndef STRICT_RFC
899         unsigned long max;
900 #endif
901
902         assert( Client != NULL );
903
904         /* Users, services and serevers in the network */
905         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
906
907         /* Number of IRC operators */
908         cnt = Client_OperCount( );
909         if( cnt > 0 )
910         {
911                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
912         }
913
914         /* Unknown connections */
915         cnt = Client_UnknownCount( );
916         if( cnt > 0 )
917         {
918                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
919         }
920
921         /* Number of created channels */
922         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
923
924         /* Number of local users, services and servers */
925         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
926
927 #ifndef STRICT_RFC
928         /* Maximum number of local users */
929         cnt = Client_MyUserCount();
930         max = Client_MyMaxUserCount();
931         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
932                         cnt, max, cnt, max))
933                 return DISCONNECTED;
934         /* Maximum number of users in the network */
935         cnt = Client_UserCount();
936         max = Client_MaxUserCount();
937         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
938                         cnt, max, cnt, max))
939                 return DISCONNECTED;
940 #endif
941         
942         return CONNECTED;
943 } /* IRC_Send_LUSERS */
944
945
946 static bool
947 Show_MOTD_Start(CLIENT *Client)
948 {
949         return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
950                 Client_ID( Client ), Client_ID( Client_ThisServer( )));
951 }
952
953 static bool
954 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
955 {
956         return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
957 }
958
959 static bool
960 Show_MOTD_End(CLIENT *Client)
961 {
962         return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
963 }
964
965
966 GLOBAL bool
967 IRC_Show_MOTD( CLIENT *Client )
968 {
969         char line[127];
970         FILE *fd;
971
972         assert( Client != NULL );
973
974         if (Conf_MotdPhrase[0]) {
975                 if (!Show_MOTD_Start(Client))
976                         return DISCONNECTED;
977                 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
978                         return DISCONNECTED;
979
980                 return Show_MOTD_End(Client);
981         }
982
983         fd = fopen( Conf_MotdFile, "r" );
984         if( ! fd ) {
985                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
986                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
987         }
988
989         if (!Show_MOTD_Start( Client )) {
990                 fclose(fd);
991                 return false;
992         }
993
994         while (fgets( line, (int)sizeof line, fd )) {
995                 ngt_TrimLastChr( line, '\n');
996
997                 if( ! Show_MOTD_Sendline( Client, line)) {
998                         fclose( fd );
999                         return false;
1000                 }
1001         }
1002         fclose(fd);
1003         return Show_MOTD_End(Client);
1004 } /* IRC_Show_MOTD */
1005
1006
1007 GLOBAL bool
1008 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1009 {
1010         bool is_visible, is_member;
1011         char str[LINE_LEN + 1];
1012         CL2CHAN *cl2chan;
1013         CLIENT *cl;
1014
1015         assert( Client != NULL );
1016         assert( Chan != NULL );
1017
1018         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1019         else is_member = false;
1020
1021         /* Secret channel? */
1022         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1023
1024         /* Alle Mitglieder suchen */
1025         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1026         cl2chan = Channel_FirstMember( Chan );
1027         while( cl2chan )
1028         {
1029                 cl = Channel_GetClient( cl2chan );
1030
1031                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1032                 else is_visible = true;
1033
1034                 if( is_member || is_visible )
1035                 {
1036                         /* Nick anhaengen */
1037                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1038                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1039                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1040                         strlcat( str, Client_ID( cl ), sizeof( str ));
1041
1042                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1043                         {
1044                                 /* Zeile wird zu lang: senden! */
1045                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1046                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1047                         }
1048                 }
1049
1050                 /* naechstes Mitglied suchen */
1051                 cl2chan = Channel_NextMember( Chan, cl2chan );
1052         }
1053         if( str[strlen( str ) - 1] != ':')
1054         {
1055                 /* Es sind noch Daten da, die gesendet werden muessen */
1056                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1057         }
1058
1059         return CONNECTED;
1060 } /* IRC_Send_NAMES */
1061
1062
1063 GLOBAL bool
1064 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
1065 {
1066         bool is_visible, is_member;
1067         CL2CHAN *cl2chan;
1068         char flags[8];
1069         CLIENT *c;
1070
1071         assert( Client != NULL );
1072         assert( Chan != NULL );
1073
1074         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1075         else is_member = false;
1076
1077         /* Secret channel? */
1078         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1079
1080         /* Alle Mitglieder suchen */
1081         cl2chan = Channel_FirstMember( Chan );
1082         while( cl2chan )
1083         {
1084                 c = Channel_GetClient( cl2chan );
1085
1086                 if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
1087                 else is_visible = true;
1088
1089                 if( is_member || is_visible ) {
1090                         if (strchr(Client_Modes( c ), 'a'))
1091                                 strcpy(flags, "G"); /* away */
1092                         else
1093                                 strcpy(flags, "H");
1094
1095                         if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
1096                         if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
1097                         else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
1098
1099                         /* ausgeben */
1100                         if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
1101                         {
1102                                 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;
1103                         }
1104                 }
1105
1106                 /* naechstes Mitglied suchen */
1107                 cl2chan = Channel_NextMember( Chan, cl2chan );
1108         }
1109         return CONNECTED;
1110 } /* IRC_Send_WHO */
1111
1112
1113 /**
1114  * Send the ISUPPORT numeric (005).
1115  * This numeric indicates the features that are supported by this server.
1116  * See <http://www.irc.org/tech_docs/005.html> for details.
1117  */
1118 GLOBAL bool
1119 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1120 {
1121         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1122                                 Conf_MaxJoins))
1123                 return DISCONNECTED;
1124         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1125                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1126                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1127                                   COMMAND_LEN - 113);
1128 } /* IRC_Send_ISUPPORT */
1129
1130
1131 /* -eof- */