]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
Fix "implicit conversion shortens 64-bit value" warning
[ngircd-alex.git] / src / ngircd / irc-info.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2009 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * IRC info commands
12  */
13
14
15 #include "portab.h"
16
17 #include "imp.h"
18 #include <assert.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <strings.h>
24
25 #include "ngircd.h"
26 #include "conn-func.h"
27 #include "conn-zip.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "resolve.h"
31 #include "conf.h"
32 #include "defines.h"
33 #include "log.h"
34 #include "messages.h"
35 #include "match.h"
36 #include "tool.h"
37 #include "parse.h"
38 #include "irc-write.h"
39
40 #include "exp.h"
41 #include "irc-info.h"
42
43
44 GLOBAL bool
45 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
46 {
47         CLIENT *target, *prefix;
48
49         assert( Client != NULL );
50         assert( Req != NULL );
51
52         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
53
54         /* find target ... */
55         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
56         else target = Client_ThisServer( );
57
58         /* find Prefix */
59         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
60         else prefix = Client;
61         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
62
63         /* forwad message to another server? */
64         if( target != Client_ThisServer( ))
65         {
66                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
67
68                 /* forward */
69                 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
70                 return CONNECTED;
71         }
72
73         /* mit Versionsinfo antworten */
74         if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
75         if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
76         if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
77         if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
78
79         IRC_SetPenalty( Client, 1 );
80         return CONNECTED;
81 } /* IRC_ADMIN */
82
83
84 /**
85  * Handler for the IRC command "INFO".
86  * See RFC 2812 section 3.4.10.
87  */
88 GLOBAL bool
89 IRC_INFO(CLIENT * Client, REQUEST * Req)
90 {
91         CLIENT *target, *prefix;
92         char msg[510];
93
94         assert(Client != NULL);
95         assert(Req != NULL);
96
97         /* Wrong number of parameters? */
98         if (Req->argc > 1)
99                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
100                                           Client_ID(Client), Req->command);
101
102         /* Determine prefix */
103         if (Client_Type(Client) == CLIENT_SERVER)
104                 prefix = Client_Search(Req->prefix);
105         else
106                 prefix = Client;
107         if (!prefix)
108                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
109                                           Client_ID(Client), Req->prefix);
110
111         /* Look for a target */
112         if (Req->argc > 0)
113                 target = Client_Search(Req->argv[0]);
114         else
115                 target = Client_ThisServer();
116         
117         /* Make sure that the target is a server */
118         if (target && Client_Type(target) != CLIENT_SERVER)
119                 target = Client_Introducer(target);
120
121         if (!target)
122                 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
123                                           Client_ID(prefix), Req->argv[0]);
124
125         /* Pass on to another server? */
126         if (target != Client_ThisServer()) {
127                 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
128                                          Req->argv[0]);
129                 return CONNECTED;
130         }
131
132         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
133                                 NGIRCd_Version))
134                 return DISCONNECTED;
135
136 #if defined(__DATE__) && defined(__TIME__)
137         snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
138         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
139                 return DISCONNECTED;
140 #endif
141
142         strlcpy(msg, "On-line since ", sizeof(msg));
143         strlcat(msg, NGIRCd_StartStr, sizeof(msg));
144         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
145                 return DISCONNECTED;
146
147         if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
148                 return DISCONNECTED;
149
150         IRC_SetPenalty(Client, 2);
151         return CONNECTED;
152 } /* IRC_INFO */
153
154
155 GLOBAL bool
156 IRC_ISON( CLIENT *Client, REQUEST *Req )
157 {
158         char rpl[COMMAND_LEN];
159         CLIENT *c;
160         char *ptr;
161         int i;
162
163         assert( Client != NULL );
164         assert( Req != NULL );
165
166         /* Falsche Anzahl Parameter? */
167         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
168
169         strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
170         for( i = 0; i < Req->argc; i++ )
171         {
172                 ptr = strtok( Req->argv[i], " " );
173                 while( ptr )
174                 {
175                         ngt_TrimStr( ptr );
176                         c = Client_Search( ptr );
177                         if( c && ( Client_Type( c ) == CLIENT_USER ))
178                         {
179                                 /* Dieser Nick ist "online" */
180                                 strlcat( rpl, ptr, sizeof( rpl ));
181                                 strlcat( rpl, " ", sizeof( rpl ));
182                         }
183                         ptr = strtok( NULL, " " );
184                 }
185         }
186         ngt_TrimLastChr(rpl, ' ');
187
188         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
189 } /* IRC_ISON */
190
191
192 GLOBAL bool
193 IRC_LINKS( CLIENT *Client, REQUEST *Req )
194 {
195         CLIENT *target, *from, *c;
196         char *mask;
197
198         assert( Client != NULL );
199         assert( Req != NULL );
200
201         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
202
203         /* Server-Mask ermitteln */
204         if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
205         else mask = "*";
206
207         /* Absender ermitteln */
208         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
209         else from = Client;
210         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
211
212         /* An anderen Server forwarden? */
213         if( Req->argc == 2 )
214         {
215                 target = Client_Search( Req->argv[0] );
216                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
217                 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
218         }
219
220         /* Wer ist der Absender? */
221         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
222         else target = Client;
223         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
224
225         c = Client_First( );
226         while( c )
227         {
228                 if( Client_Type( c ) == CLIENT_SERVER )
229                 {
230                         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;
231                 }
232                 c = Client_Next( c );
233         }
234         
235         IRC_SetPenalty( target, 1 );
236         return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
237 } /* IRC_LINKS */
238
239
240 GLOBAL bool
241 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
242 {
243         CLIENT *target, *from;
244
245         assert( Client != NULL );
246         assert( Req != NULL );
247
248         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
249
250         /* Absender ermitteln */
251         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
252         else from = Client;
253         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
254
255         /* An anderen Server forwarden? */
256         if( Req->argc == 2 )
257         {
258                 target = Client_Search( Req->argv[1] );
259                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
260                 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
261         }
262
263         /* Wer ist der Absender? */
264         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
265         else target = Client;
266         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
267
268         IRC_Send_LUSERS( target );
269
270         IRC_SetPenalty( target, 1 );
271         return CONNECTED;
272 } /* IRC_LUSERS */
273
274
275 /**
276  * Handler for the IRC command "SERVLIST".
277  * List registered services, see RFC 2811, section 3.5.1: the syntax is
278  * "SERVLIST [<mask> [<type>]]".
279  */
280 GLOBAL bool
281 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
282 {
283         CLIENT *c;
284
285         assert(Client != NULL);
286         assert(Req != NULL);
287
288         if (Req->argc > 2)
289                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
290                                           Client_ID(Client), Req->command);
291
292         if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
293                 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
294                         if (Client_Type(c) != CLIENT_SERVICE)
295                                 continue;
296                         if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
297                                                                   Client_ID(c)))
298                                 continue;
299                         if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
300                                         Client_ID(Client), Client_Mask(c),
301                                         Client_Mask(Client_Introducer(c)), "*",
302                                         0, Client_Hops(c), Client_Info(c)))
303                                 return DISCONNECTED;
304                 }
305         }
306
307         return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
308                                   Req->argc > 0 ? Req->argv[0] : "*",
309                                   Req->argc > 1 ? Req->argv[1] : "0");
310 } /* IRC_SERVLIST */
311
312
313 GLOBAL bool
314 IRC_MOTD( CLIENT *Client, REQUEST *Req )
315 {
316         CLIENT *from, *target;
317
318         assert( Client != NULL );
319         assert( Req != NULL );
320
321         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
322
323         /* From aus Prefix ermitteln */
324         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
325         else from = Client;
326         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
327
328         if( Req->argc == 1 )
329         {
330                 /* forward? */
331                 target = Client_Search( Req->argv[0] );
332                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
333
334                 if( target != Client_ThisServer( ))
335                 {
336                         /* Ok, anderer Server ist das Ziel: forwarden */
337                         return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
338                 }
339         }
340
341         IRC_SetPenalty( from, 3 );
342         return IRC_Show_MOTD( from );
343 } /* IRC_MOTD */
344
345
346 GLOBAL bool
347 IRC_NAMES( CLIENT *Client, REQUEST *Req )
348 {
349         char rpl[COMMAND_LEN], *ptr;
350         CLIENT *target, *from, *c;
351         CHANNEL *chan;
352
353         assert( Client != NULL );
354         assert( Req != NULL );
355
356         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
357
358         /* use prefix to determine "From" */
359         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
360         else from = Client;
361         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
362
363         if( Req->argc == 2 )
364         {
365                 /* forward to another server? */
366                 target = Client_Search( Req->argv[1] );
367                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
368
369                 if( target != Client_ThisServer( )) {
370                         /* target is another server, forward */
371                         return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
372                 }
373         }
374
375         if( Req->argc > 0 )
376         {
377                 /* bestimmte Channels durchgehen */
378                 ptr = strtok( Req->argv[0], "," );
379                 while( ptr )
380                 {
381                         chan = Channel_Search( ptr );
382                         if( chan )
383                         {
384                                 /* print name */
385                                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
386                         }
387                         if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
388
389                         /* get next channel name */
390                         ptr = strtok( NULL, "," );
391                 }
392                 return CONNECTED;
393         }
394
395         chan = Channel_First( );
396         while( chan )
397         {
398                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
399
400                 chan = Channel_Next( chan );
401         }
402
403         /* Now print all clients which are not in any channel */
404         c = Client_First( );
405         snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
406         while( c )
407         {
408                 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
409                 {
410                         /* its a user, concatenate ... */
411                         if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
412                         strlcat( rpl, Client_ID( c ), sizeof( rpl ));
413
414                         if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
415                         {
416                                 /* Line is gwoing too long, send now */
417                                 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
418                                 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
419                         }
420                 }
421
422                 c = Client_Next( c );
423         }
424         if( rpl[strlen( rpl ) - 1] != ':')
425         {
426                 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
427         }
428
429         IRC_SetPenalty( from, 1 );
430         return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
431 } /* IRC_NAMES */
432
433
434 static unsigned int
435 t_diff(time_t *t, const time_t d)
436 {
437         time_t diff, remain;
438
439         diff = *t / d;
440         remain = diff * d;
441         *t -= remain;
442
443         return (unsigned int)diff;
444 }
445
446
447 static unsigned int
448 uptime_days(time_t *now)
449 {
450         return t_diff(now, 60 * 60 * 24);
451 }
452
453
454 static unsigned int
455 uptime_hrs(time_t *now)
456 {
457         return t_diff(now, 60 * 60);
458 }
459
460
461 static unsigned int
462 uptime_mins(time_t *now)
463 {
464          return t_diff(now, 60);
465 }
466
467
468 GLOBAL bool
469 IRC_STATS( CLIENT *Client, REQUEST *Req )
470 {
471         CLIENT *from, *target, *cl;
472         CONN_ID con;
473         char query;
474         COMMAND *cmd;
475         time_t time_now;
476         unsigned int days, hrs, mins;
477
478         assert( Client != NULL );
479         assert( Req != NULL );
480
481         if (Req->argc > 2)
482                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
483
484         /* use prefix to determine "From" */
485         if (Client_Type(Client) == CLIENT_SERVER)
486                 from = Client_Search(Req->prefix);
487         else
488                 from = Client;
489
490         if (! from)
491                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix);
492
493         if (Req->argc == 2) {
494                 /* forward to another server? */
495                 target = Client_Search( Req->argv[1] );
496                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
497                         return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
498
499                 if( target != Client_ThisServer()) {
500                         /* forward to another server */
501                         return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
502                 }
503         }
504
505         if (Req->argc > 0)
506                 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
507         else
508                 query = '*';
509
510         switch (query) {
511                 case 'l':       /* Links */
512                 case 'L':
513                         time_now = time(NULL);
514                         for (con = Conn_First(); con != NONE ;con = Conn_Next(con)) {
515                                 cl = Conn_GetClient(con);
516                                 if (!cl)
517                                         continue;
518                                 if ((Client_Type(cl) == CLIENT_SERVER) || (cl == Client)) {
519                                         /* Server link or our own connection */
520 #ifdef ZLIB
521                                         if (Conn_Options(con) & CONN_ZIP) {
522                                                 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFOZIP_MSG,
523                                                         Client_ID(from), Client_Mask(cl), Conn_SendQ(con),
524                                                         Conn_SendMsg(con), Zip_SendBytes(con), Conn_SendBytes(con),
525                                                         Conn_RecvMsg(con), Zip_RecvBytes(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
526                                                                 return DISCONNECTED;
527                                                 continue;
528                                         }
529 #endif
530                                         if (!IRC_WriteStrClient(from, RPL_STATSLINKINFO_MSG, Client_ID(from),
531                                                 Client_Mask(cl), Conn_SendQ(con), Conn_SendMsg(con), Conn_SendBytes(con),
532                                                 Conn_RecvMsg(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
533                                                         return DISCONNECTED;
534                                 }
535                         }
536                         break;
537                 case 'm':       /* IRC-Commands */
538                 case 'M':
539                         cmd = Parse_GetCommandStruct( );
540                         for (; cmd->name ; cmd++) {
541                                 if (cmd->lcount == 0 && cmd->rcount == 0)
542                                         continue;
543                                 if (!IRC_WriteStrClient(from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
544                                                 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
545                                                         return DISCONNECTED;
546                         }
547                         break;
548                 case 'u':       /* server uptime */
549                 case 'U':
550                         time_now = time(NULL) - NGIRCd_Start;
551                         days = uptime_days(&time_now);
552                         hrs = uptime_hrs(&time_now);
553                         mins = uptime_mins(&time_now);
554                         if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
555                                         days, hrs, mins, (unsigned int) time_now))
556                                                 return DISCONNECTED;
557                         break;
558         }
559
560         IRC_SetPenalty(from, 2);
561         return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG, Client_ID(from), query);
562 } /* IRC_STATS */
563
564
565 /**
566  * Handler for the IRC command "SUMMON".
567  * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
568  * therefore answers with ERR_SUMMONDISABLED.
569  */
570 GLOBAL bool
571 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
572 {
573         return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
574                                   Client_ID(Client), Req->command);
575 } /* IRC_SUMMON */
576
577
578 GLOBAL bool
579 IRC_TIME( CLIENT *Client, REQUEST *Req )
580 {
581         CLIENT *from, *target;
582         char t_str[64];
583         time_t t;
584
585         assert( Client != NULL );
586         assert( Req != NULL );
587
588         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
589
590         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
591         else from = Client;
592         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
593
594         if( Req->argc == 1 )
595         {
596                 target = Client_Search( Req->argv[0] );
597                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
598
599                 if( target != Client_ThisServer( ))
600                 {
601                         return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
602                 }
603         }
604
605         t = time( NULL );
606         (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
607         return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
608 } /* IRC_TIME */
609
610
611 GLOBAL bool
612 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
613 {
614         char rpl[COMMAND_LEN];
615         CLIENT *c;
616         int max, i;
617
618         assert( Client != NULL );
619         assert( Req != NULL );
620
621         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
622
623         if( Req->argc > 5 ) max = 5;
624         else max = Req->argc;
625
626         strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
627         for( i = 0; i < max; i++ )
628         {
629                 c = Client_Search( Req->argv[i] );
630                 if( c && ( Client_Type( c ) == CLIENT_USER ))
631                 {
632                         /* This Nick is "online" */
633                         strlcat( rpl, Client_ID( c ), sizeof( rpl ));
634                         if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
635                         strlcat( rpl, "=", sizeof( rpl ));
636                         if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
637                         else strlcat( rpl, "+", sizeof( rpl ));
638                         strlcat( rpl, Client_User( c ), sizeof( rpl ));
639                         strlcat( rpl, "@", sizeof( rpl ));
640                         strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
641                         strlcat( rpl, " ", sizeof( rpl ));
642                 }
643         }
644         ngt_TrimLastChr( rpl, ' ');
645
646         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
647 } /* IRC_USERHOST */
648
649
650 /**
651  * Handler for the IRC command "USERS".
652  * See RFC 2812 section 4.6. As suggested there the command is disabled.
653  */
654 GLOBAL bool
655 IRC_USERS(CLIENT * Client, REQUEST * Req)
656 {
657         return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
658                                   Client_ID(Client), Req->command);
659 } /* IRC_USERS */
660
661
662 GLOBAL bool
663 IRC_VERSION( CLIENT *Client, REQUEST *Req )
664 {
665         CLIENT *target, *prefix;
666
667         assert( Client != NULL );
668         assert( Req != NULL );
669
670         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
671
672         /* Ziel suchen */
673         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
674         else target = Client_ThisServer( );
675
676         /* Prefix ermitteln */
677         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
678         else prefix = Client;
679         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
680
681         /* An anderen Server weiterleiten? */
682         if( target != Client_ThisServer( ))
683         {
684                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
685
686                 /* forwarden */
687                 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
688                 return CONNECTED;
689         }
690
691         /* send version information */
692         IRC_SetPenalty(Client, 1);
693         return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
694                                   PACKAGE_NAME, PACKAGE_VERSION,
695                                   NGIRCd_DebugLevel, Conf_ServerName,
696                                   NGIRCd_VersionAddition);
697 } /* IRC_VERSION */
698
699
700 static bool
701 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
702 {
703         return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client), channelname,
704                         Client_User(c), Client_Hostname(c), Client_ID(Client_Introducer(c)), Client_ID(c),
705                         flags, Client_Hops(c), Client_Info(c));
706 }
707
708
709 static const char *
710 who_flags_status(const char *client_modes)
711 {
712         if (strchr(client_modes, 'a'))
713                 return "G"; /* away */
714         return "H";
715 }
716
717
718 static const char *
719 who_flags_qualifier(const char *chan_user_modes)
720 {
721         if (strchr(chan_user_modes, 'o'))
722                 return "@";
723         else if (strchr(chan_user_modes, 'v'))
724                 return "+";
725         return "";
726 }
727
728
729 static bool
730 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
731 {
732         bool is_visible, is_member, is_ircop;
733         CL2CHAN *cl2chan;
734         const char *client_modes;
735         const char *chan_user_modes;
736         char flags[8];
737         CLIENT *c;
738
739         assert( Client != NULL );
740         assert( Chan != NULL );
741
742         is_member = Channel_IsMemberOf(Chan, Client);
743
744         /* Secret channel? */
745         if (!is_member && strchr(Channel_Modes(Chan), 's'))
746                 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
747
748         cl2chan = Channel_FirstMember(Chan);
749         for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
750                 c = Channel_GetClient(cl2chan);
751
752                 client_modes = Client_Modes(c);
753                 is_ircop = strchr(client_modes, 'o') != NULL;
754                 if (OnlyOps && !is_ircop)
755                         continue;
756
757                 is_visible = strchr(client_modes, 'i') == NULL;
758                 if (is_member || is_visible) {
759                         strcpy(flags, who_flags_status(client_modes));
760                         if (is_ircop)
761                                 strlcat(flags, "*", sizeof(flags));
762
763                         chan_user_modes = Channel_UserModes(Chan, c);
764                         strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
765
766                         if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
767                                 return DISCONNECTED;
768                 }
769         }
770         return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
771 } /* IRC_Send_WHO */
772
773
774 GLOBAL bool
775 IRC_WHO( CLIENT *Client, REQUEST *Req )
776 {
777         bool only_ops, have_arg, client_match;
778         const char *channelname, *client_modes, *chan_user_modes;
779         char pattern[COMMAND_LEN];
780         char flags[4];
781         CL2CHAN *cl2chan;
782         CHANNEL *chan, *cn;
783         CLIENT *c;
784
785         assert( Client != NULL );
786         assert( Req != NULL );
787
788         if (Req->argc > 2)
789                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
790
791         only_ops = false;
792         have_arg = false;
793
794         if (Req->argc == 2) {
795                 if (strcmp(Req->argv[1], "o") == 0)
796                         only_ops = true;
797 #ifdef STRICT_RFC
798                 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
799 #endif
800         }
801
802         IRC_SetPenalty(Client, 1);
803         if (Req->argc >= 1) { /* Channel or Mask. */
804                 chan = Channel_Search(Req->argv[0]);
805                 if (chan)
806                         return IRC_Send_WHO(Client, chan, only_ops);
807                 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
808                         have_arg = true;
809                         strlcpy(pattern, Req->argv[0], sizeof(pattern));
810                         ngt_LowerStr(pattern);
811                         IRC_SetPenalty(Client, 3);
812                 }
813         }
814
815         for (c = Client_First(); c != NULL; c = Client_Next(c)) {
816                 if (Client_Type(c) != CLIENT_USER)
817                         continue;
818                  /*
819                   * RFC 2812, 3.6.1:
820                   * In the absence of the parameter, all visible (users who aren't
821                   * invisible (user mode +i) and who don't have a common channel
822                   * with the requesting client) are listed.
823                   *
824                   * The same result can be achieved by using a [sic] of "0"
825                   * or any wildcard which will end up matching every visible user.
826                   *
827                   * The [sic] passed to WHO is matched against users' host, server, real name and
828                   * nickname if the channel cannot be found.
829                   */
830                 client_modes = Client_Modes(c);
831                 if (strchr(client_modes, 'i'))
832                         continue;
833
834                 if (only_ops && !strchr(client_modes, 'o'))
835                         continue;
836
837                 if (have_arg) { /* match pattern against user host/server/name/nick */
838                         client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
839                         if (!client_match)
840                                 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
841                         if (!client_match)
842                                 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
843                         if (!client_match)
844                                 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
845
846                         if (!client_match) /* This isn't the client you're looking for */
847                                 continue;
848                 }
849
850                 strcpy(flags, who_flags_status(client_modes));
851
852                 if (strchr(client_modes, 'o')) /* this client is an operator */
853                         strlcat(flags, "*", sizeof(flags));
854
855                 /* Search suitable channel */
856                 cl2chan = Channel_FirstChannelOf(c);
857                 while (cl2chan) {
858                         cn = Channel_GetChannel(cl2chan);
859                         if (Channel_IsMemberOf(cn, Client) ||
860                                     !strchr(Channel_Modes(cn), 's'))
861                         {
862                                 channelname = Channel_Name(cn);
863                                 break;
864                         }
865                         cl2chan = Channel_NextChannelOf(c, cl2chan);
866                 }
867                 if (cl2chan) {
868                         chan = Channel_GetChannel(cl2chan);
869                         chan_user_modes = Channel_UserModes(chan, c);
870                         strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
871                 } else
872                         channelname = "*";
873
874                 if (!write_whoreply(Client, c, channelname, flags))
875                         return DISCONNECTED;
876         }
877
878         if (Req->argc > 0)
879                 channelname = Req->argv[0];
880         else
881                 channelname = "*";
882
883         return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
884 } /* IRC_WHO */
885
886
887 GLOBAL bool
888 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
889 {
890         CLIENT *from, *target, *c;
891         char str[LINE_LEN + 1];
892         CL2CHAN *cl2chan;
893         CHANNEL *chan;
894
895         assert( Client != NULL );
896         assert( Req != NULL );
897
898         /* Bad number of parameters? */
899         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
900
901         /* Search client */
902         c = Client_Search( Req->argv[Req->argc - 1] );
903         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
904
905         /* Search sender of the WHOIS */
906         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
907         else from = Client;
908         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
909
910         /* Forward to other server? */
911         if( Req->argc > 1 )
912         {
913                 /* Search target server (can be specified as nick of that server!) */
914                 target = Client_Search( Req->argv[0] );
915                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
916         }
917         else target = Client_ThisServer( );
918
919         assert( target != NULL );
920
921         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] );
922
923         /* Nick, user and name */
924         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
925
926         /* Server */
927         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;
928
929         /* Channels */
930         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
931         cl2chan = Channel_FirstChannelOf( c );
932         while( cl2chan )
933         {
934                 chan = Channel_GetChannel( cl2chan );
935                 assert( chan != NULL );
936
937                 /* next */
938                 cl2chan = Channel_NextChannelOf( c, cl2chan );
939
940                 /* Secret channel? */
941                 if (strchr(Channel_Modes(chan), 's')
942                     && !Channel_IsMemberOf(chan, Client))
943                         continue;
944
945                 /* Local channel and request is not from a user? */
946                 if (Client_Type(Client) == CLIENT_SERVER
947                     && Channel_IsLocal(chan))
948                         continue;
949
950                 /* Concatenate channel names */
951                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
952                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
953                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
954                 strlcat( str, Channel_Name( chan ), sizeof( str ));
955
956                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
957                 {
958                         /* Line becomes too long: send it! */
959                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
960                         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
961                 }
962         }
963         if( str[strlen( str ) - 1] != ':')
964         {
965                 /* There is data left to send: */
966                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
967         }
968
969         /* IRC-Operator? */
970         if( Client_HasMode( c, 'o' ))
971         {
972                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
973         }
974
975         /* Idle and signon time (local clients only!) */
976         if (Client_Conn(c) > NONE ) {
977                 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
978                         Client_ID(from), Client_ID(c),
979                         (unsigned long)Conn_GetIdle(Client_Conn(c)),
980                         (unsigned long)Conn_GetSignon(Client_Conn(c))))
981                                 return DISCONNECTED;
982         }
983
984         /* Away? */
985         if( Client_HasMode( c, 'a' ))
986         {
987                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
988         }
989
990         /* End of Whois */
991         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
992 } /* IRC_WHOIS */
993
994
995 static bool
996 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
997 {
998         char t_str[60];
999
1000         (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1001                                         localtime(&entry->time));
1002
1003         if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1004                         entry->id, entry->user, entry->host, entry->info))
1005                                 return DISCONNECTED;
1006
1007         return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1008                   entry->id, entry->server, t_str);
1009 }
1010
1011 /**
1012  * IRC "WHOWAS" function.
1013  * This function implements the IRC command "WHOWHAS". It handles local
1014  * requests and request that should be forwarded to other servers.
1015  */
1016 GLOBAL bool
1017 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1018 {
1019         CLIENT *target, *prefix;
1020         WHOWAS *whowas;
1021         char tok_buf[COMMAND_LEN];
1022         int max, last, count, i, nc;
1023         const char *nick;
1024
1025         assert( Client != NULL );
1026         assert( Req != NULL );
1027
1028         /* Wrong number of parameters? */
1029         if (Req->argc > 3)
1030                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1031                                         Client_ID(Client), Req->command);
1032         if (Req->argc < 1)
1033                 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1034
1035         /* Search target */
1036         if (Req->argc == 3)
1037                 target = Client_Search(Req->argv[2]);
1038         else
1039                 target = Client_ThisServer();
1040
1041         /* Get prefix */
1042         if (Client_Type(Client) == CLIENT_SERVER)
1043                 prefix = Client_Search(Req->prefix);
1044         else
1045                 prefix = Client;
1046
1047         if (!prefix)
1048                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1049                                                 Client_ID(Client), Req->prefix);
1050
1051         /* Forward to other server? */
1052         if (target != Client_ThisServer()) {
1053                 if (!target || (Client_Type(target) != CLIENT_SERVER))
1054                         return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1055                                         Client_ID(prefix), Req->argv[2]);
1056
1057                 /* Forward */
1058                 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1059                                           Req->argv[0], Req->argv[1],
1060                                           Req->argv[2] );
1061                 return CONNECTED;
1062         }
1063
1064         whowas = Client_GetWhowas( );
1065         last = Client_GetLastWhowasIndex( );
1066         if (last < 0)
1067                 last = 0;
1068
1069         max = DEFAULT_WHOWAS;
1070         if (Req->argc > 1) {
1071                 max = atoi(Req->argv[1]);
1072                 if (max < 1)
1073                         max = MAX_WHOWAS;
1074         }
1075
1076         /*
1077          * Break up the nick argument into a list of nicks, if applicable
1078          * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1079          */
1080         strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1081         nick = strtok(tok_buf, ",");
1082
1083         for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1084                 nc = 0;
1085                 do {
1086                         /* Used entry? */
1087                         if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1088                                 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1089                                         return DISCONNECTED;
1090                                 nc++;
1091                                 count++;
1092                         }
1093                         /* previous entry */
1094                         i--;
1095
1096                         /* "underflow", wrap around */
1097                         if (i < 0)
1098                                 i = MAX_WHOWAS - 1;
1099
1100                         if (nc && count >= max)
1101                                 break;
1102                 } while (i != last);
1103
1104                 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1105                                                 Client_ID(prefix), nick))
1106                         return DISCONNECTED;
1107         }
1108         return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1109 } /* IRC_WHOWAS */
1110
1111
1112 GLOBAL bool
1113 IRC_Send_LUSERS( CLIENT *Client )
1114 {
1115         unsigned long cnt;
1116 #ifndef STRICT_RFC
1117         unsigned long max;
1118 #endif
1119
1120         assert( Client != NULL );
1121
1122         /* Users, services and serevers in the network */
1123         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1124
1125         /* Number of IRC operators */
1126         cnt = Client_OperCount( );
1127         if( cnt > 0 )
1128         {
1129                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1130         }
1131
1132         /* Unknown connections */
1133         cnt = Client_UnknownCount( );
1134         if( cnt > 0 )
1135         {
1136                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1137         }
1138
1139         /* Number of created channels */
1140         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1141
1142         /* Number of local users, services and servers */
1143         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1144
1145 #ifndef STRICT_RFC
1146         /* Maximum number of local users */
1147         cnt = Client_MyUserCount();
1148         max = Client_MyMaxUserCount();
1149         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1150                         cnt, max, cnt, max))
1151                 return DISCONNECTED;
1152         /* Maximum number of users in the network */
1153         cnt = Client_UserCount();
1154         max = Client_MaxUserCount();
1155         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1156                         cnt, max, cnt, max))
1157                 return DISCONNECTED;
1158 #endif
1159         
1160         return CONNECTED;
1161 } /* IRC_Send_LUSERS */
1162
1163
1164 static bool
1165 Show_MOTD_Start(CLIENT *Client)
1166 {
1167         return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1168                 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1169 }
1170
1171 static bool
1172 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1173 {
1174         return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1175 }
1176
1177 static bool
1178 Show_MOTD_End(CLIENT *Client)
1179 {
1180         return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1181 }
1182
1183 #ifdef SSL_SUPPORT
1184 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1185 {
1186         bool ret = true;
1187         char buf[COMMAND_LEN] = "Connected using Cipher ";
1188
1189         if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1190                 return true;
1191
1192         if (!Show_MOTD_Sendline(Client, buf))
1193                 ret = false;
1194
1195         return ret;
1196 }
1197 #else
1198 static inline bool Show_MOTD_SSLInfo(UNUSED CLIENT *c) { return true; }
1199 #endif
1200
1201 GLOBAL bool
1202 IRC_Show_MOTD( CLIENT *Client )
1203 {
1204         char line[127];
1205         FILE *fd;
1206
1207         assert( Client != NULL );
1208
1209         if (Conf_MotdPhrase[0]) {
1210                 if (!Show_MOTD_Start(Client))
1211                         return DISCONNECTED;
1212                 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
1213                         return DISCONNECTED;
1214                 goto out;
1215         }
1216
1217         fd = fopen( Conf_MotdFile, "r" );
1218         if( ! fd ) {
1219                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1220                 if (Conn_UsesSSL(Client_Conn(Client))) {
1221                         if (!Show_MOTD_Start(Client))
1222                                 return DISCONNECTED;
1223                         goto out;
1224                 }
1225                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1226         }
1227
1228         if (!Show_MOTD_Start( Client )) {
1229                 fclose(fd);
1230                 return false;
1231         }
1232
1233         while (fgets( line, (int)sizeof line, fd )) {
1234                 ngt_TrimLastChr( line, '\n');
1235
1236                 if( ! Show_MOTD_Sendline( Client, line)) {
1237                         fclose( fd );
1238                         return false;
1239                 }
1240         }
1241         fclose(fd);
1242 out:
1243         if (!Show_MOTD_SSLInfo(Client))
1244                 return DISCONNECTED;
1245         return Show_MOTD_End(Client);
1246 } /* IRC_Show_MOTD */
1247
1248
1249 GLOBAL bool
1250 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1251 {
1252         bool is_visible, is_member;
1253         char str[LINE_LEN + 1];
1254         CL2CHAN *cl2chan;
1255         CLIENT *cl;
1256
1257         assert( Client != NULL );
1258         assert( Chan != NULL );
1259
1260         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1261         else is_member = false;
1262
1263         /* Secret channel? */
1264         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1265
1266         /* Alle Mitglieder suchen */
1267         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1268         cl2chan = Channel_FirstMember( Chan );
1269         while( cl2chan )
1270         {
1271                 cl = Channel_GetClient( cl2chan );
1272
1273                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1274                 else is_visible = true;
1275
1276                 if( is_member || is_visible )
1277                 {
1278                         /* Nick anhaengen */
1279                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1280                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1281                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1282                         strlcat( str, Client_ID( cl ), sizeof( str ));
1283
1284                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1285                         {
1286                                 /* Zeile wird zu lang: senden! */
1287                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1288                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1289                         }
1290                 }
1291
1292                 /* naechstes Mitglied suchen */
1293                 cl2chan = Channel_NextMember( Chan, cl2chan );
1294         }
1295         if( str[strlen( str ) - 1] != ':')
1296         {
1297                 /* Es sind noch Daten da, die gesendet werden muessen */
1298                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1299         }
1300
1301         return CONNECTED;
1302 } /* IRC_Send_NAMES */
1303
1304
1305
1306 /**
1307  * Send the ISUPPORT numeric (005).
1308  * This numeric indicates the features that are supported by this server.
1309  * See <http://www.irc.org/tech_docs/005.html> for details.
1310  */
1311 GLOBAL bool
1312 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1313 {
1314         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1315                                 Conf_MaxJoins))
1316                 return DISCONNECTED;
1317         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1318                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1319                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1320                                   COMMAND_LEN - 113);
1321 } /* IRC_Send_ISUPPORT */
1322
1323
1324 /* -eof- */