]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
fix "beeing" typo
[ngircd-alex.git] / src / ngircd / irc.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2004 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 commands
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc.c,v 1.132 2008/01/15 22:28:14 fw Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "ngircd.h"
25 #include "resolve.h"
26 #include "conn-func.h"
27 #include "conf.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "defines.h"
31 #include "irc-write.h"
32 #include "log.h"
33 #include "match.h"
34 #include "messages.h"
35 #include "parse.h"
36 #include "tool.h"
37
38 #include "exp.h"
39 #include "irc.h"
40
41
42 static char *Option_String PARAMS((CONN_ID Idx));
43 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType,
44                                  bool SendErrors));
45 static bool Send_Message_Mask PARAMS((CLIENT *from, char *command,
46                                       char *targetMask, char *message,
47                                       bool SendErrors));
48
49
50 GLOBAL bool
51 IRC_ERROR( CLIENT *Client, REQUEST *Req )
52 {
53         assert( Client != NULL );
54         assert( Req != NULL );
55
56         if (Req->argc < 1)
57                 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
58                     Client_Mask(Client));
59         else
60                 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
61                     Client_Mask(Client), Req->argv[0]);
62
63         return CONNECTED;
64 } /* IRC_ERROR */
65
66
67 /**
68  * Kill client on request.
69  * This function implements the IRC command "KILL" wich is used to selectively
70  * disconnect clients. It can be used by IRC operators and servers, for example
71  * to "solve" nick collisions after netsplits.
72  * Please note that this function is also called internally, without a real
73  * KILL command being received over the network! Client is Client_ThisServer()
74  * in this case. */
75 GLOBAL bool
76 IRC_KILL( CLIENT *Client, REQUEST *Req )
77 {
78         CLIENT *prefix, *c;
79         char reason[COMMAND_LEN], *msg;
80         CONN_ID my_conn, conn;
81
82         assert( Client != NULL );
83         assert( Req != NULL );
84
85         if(( Client_Type( Client ) != CLIENT_SERVER ) &&
86            ( ! Client_OperByMe( Client )))
87         {
88                 /* The originator of the KILL is neither an IRC operator of
89                  * this server nor a server. */
90                 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
91                                            Client_ID( Client ));
92         }
93
94         if( Req->argc != 2 )
95         {
96                 /* This command requires exactly 2 parameters! */
97                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
98                                            Client_ID( Client ), Req->command );
99         }
100
101         if( Req->prefix ) prefix = Client_Search( Req->prefix );
102         else prefix = Client;
103         if( ! prefix )
104         {
105                 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
106                      Req->prefix );
107                 prefix = Client_ThisServer( );
108         }
109
110         if( Client != Client_ThisServer( ))
111         {
112                 /* This is a "real" KILL received from the network. */
113                 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
114                      Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
115         }
116
117         /* Build reason string */
118         if( Client_Type( Client ) == CLIENT_USER )
119         {
120                 /* Prefix the "reason" if the originator is a regular user,
121                  * so users can't spoof KILLs of servers. */
122                 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
123                           Client_ID( Client ), Req->argv[1] );
124         }
125         else
126                 strlcpy( reason, Req->argv[1], sizeof( reason ));
127
128         /* Inform other servers */
129         IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
130                                    Req->argv[0], reason );
131
132         /* Save ID of this connection */
133         my_conn = Client_Conn( Client );
134
135         /* Do we host such a client? */
136         c = Client_Search( Req->argv[0] );
137         if( c )
138         {
139                 if(( Client_Type( c ) != CLIENT_USER ) &&
140                    ( Client_Type( c ) != CLIENT_GOTNICK ))
141                 {
142                         /* Target of this KILL is not a regular user, this is
143                          * invalid! So we ignore this case if we received a
144                          * regular KILL from the network and try to kill the
145                          * client/connection anyway (but log an error!) if the
146                          * origin is the local server. */
147
148                         if( Client != Client_ThisServer( ))
149                         {
150                                 /* Invalid KILL received from remote */
151                                 if( Client_Type( c ) == CLIENT_SERVER )
152                                         msg = ERR_CANTKILLSERVER_MSG;
153                                 else
154                                         msg = ERR_NOPRIVILEGES_MSG;
155                                 return IRC_WriteStrClient( Client, msg,
156                                         Client_ID( Client ));
157                         }
158
159                         Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
160                              Client_Type( c ), Req->argv[0] );
161                 }
162
163                 /* Kill client NOW! */
164                 conn = Client_Conn( c );
165                 Client_Destroy( c, NULL, reason, false );
166                 if( conn > NONE )
167                         Conn_Close( conn, NULL, reason, true );
168         }
169         else
170                 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
171
172         /* Are we still connected or were we killed, too? */
173         if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
174                 return CONNECTED;
175         else
176                 return DISCONNECTED;
177 } /* IRC_KILL */
178
179
180 /**
181  * Handler for the IRC command NOTICE.
182  */
183 GLOBAL bool
184 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
185 {
186         return Send_Message(Client, Req, CLIENT_USER, false);
187 } /* IRC_NOTICE */
188
189
190 /**
191  * Handler for the IRC command PRIVMSG.
192  */
193 GLOBAL bool
194 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
195 {
196         return Send_Message(Client, Req, CLIENT_USER, true);
197 } /* IRC_PRIVMSG */
198
199
200 /**
201  * Handler for the IRC command SQUERY.
202  */
203 GLOBAL bool
204 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
205 {
206         return Send_Message(Client, Req, CLIENT_SERVICE, true);
207 } /* IRC_SQUERY */
208
209
210 GLOBAL bool
211 IRC_TRACE( CLIENT *Client, REQUEST *Req )
212 {
213         CLIENT *from, *target, *c;
214         CONN_ID idx, idx2;
215         char user[CLIENT_USER_LEN];
216
217         assert( Client != NULL );
218         assert( Req != NULL );
219
220         /* Bad number of arguments? */
221         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
222
223         /* Search sender */
224         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
225         else from = Client;
226         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
227
228         /* Search target */
229         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
230         else target = Client_ThisServer( );
231         
232         /* Forward command to other server? */
233         if( target != Client_ThisServer( ))
234         {
235                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
236
237                 /* Send RPL_TRACELINK back to initiator */
238                 idx = Client_Conn( Client ); assert( idx > NONE );
239                 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
240                 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE_NAME, PACKAGE_VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), Option_String( idx2 ), time( NULL ) - Conn_StartTime( idx2 ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
241
242                 /* Forward command */
243                 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
244                 return CONNECTED;
245         }
246
247         /* Infos about all connected servers */
248         c = Client_First( );
249         while( c )
250         {
251                 if( Client_Conn( c ) > NONE )
252                 {
253                         /* Local client */
254                         if( Client_Type( c ) == CLIENT_SERVER )
255                         {
256                                 /* Server link */
257                                 strlcpy( user, Client_User( c ), sizeof( user ));
258                                 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
259                                 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), user, Client_Hostname( c ), Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( c )))) return DISCONNECTED;
260                         }
261                         if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
262                         {
263                                 /* IRC Operator */
264                                 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
265                         }
266                 }
267                 c = Client_Next( c );
268         }
269
270         IRC_SetPenalty( Client, 3 );
271         return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
272 } /* IRC_TRACE */
273
274
275 GLOBAL bool
276 IRC_HELP( CLIENT *Client, REQUEST *Req )
277 {
278         COMMAND *cmd;
279
280         assert( Client != NULL );
281         assert( Req != NULL );
282
283         /* Bad number of arguments? */
284         if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
285
286         cmd = Parse_GetCommandStruct( );
287         while( cmd->name )
288         {
289                 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
290                 cmd++;
291         }
292         
293         IRC_SetPenalty( Client, 2 );
294         return CONNECTED;
295 } /* IRC_HELP */
296
297
298 static char *
299 Option_String( CONN_ID Idx )
300 {
301         static char option_txt[8];
302         UINT16 options;
303
304         options = Conn_Options(Idx);
305
306         strcpy(option_txt, "F");        /* No idea what this means, but the
307                                          * original ircd sends it ... */
308 #ifdef ZLIB
309         if(options & CONN_ZIP)          /* zlib compression supported. */
310                 strcat(option_txt, "z");
311 #endif
312
313         return option_txt;
314 } /* Option_String */
315
316
317 static bool
318 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
319 {
320         CLIENT *cl, *from;
321         CHANNEL *chan;
322         char *currentTarget = Req->argv[0];
323         char *lastCurrentTarget = NULL;
324
325         assert(Client != NULL);
326         assert(Req != NULL);
327
328         if (Req->argc == 0) {
329                 if (!SendErrors)
330                         return CONNECTED;
331                 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
332                                           Client_ID(Client), Req->command);
333         }
334         if (Req->argc == 1) {
335                 if (!SendErrors)
336                         return CONNECTED;
337                 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
338                                           Client_ID(Client));
339         }
340         if (Req->argc > 2) {
341                 if (!SendErrors)
342                         return CONNECTED;
343                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
344                                           Client_ID(Client), Req->command);
345         }
346
347         if (Client_Type(Client) == CLIENT_SERVER)
348                 from = Client_Search(Req->prefix);
349         else
350                 from = Client;
351         if (!from)
352                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
353                                           Client_ID(Client), Req->prefix);
354
355         /* handle msgtarget = msgto *("," msgto) */
356         currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
357         ngt_UpperStr(Req->command);
358
359         while (currentTarget) {
360                 /* Check for and handle valid <msgto> of form:
361                  * RFC 2812 2.3.1:
362                  *   msgto =  channel / ( user [ "%" host ] "@" servername )
363                  *   msgto =/ ( user "%" host ) / targetmask
364                  *   msgto =/ nickname / ( nickname "!" user "@" host )
365                  */
366                 if (strchr(currentTarget, '!') == NULL)
367                         /* nickname */
368                         cl = Client_Search(currentTarget);
369                 else
370                         cl = NULL;
371
372                 if (cl == NULL) {
373                         /* If currentTarget isn't a nickname check for:
374                          * user ["%" host] "@" servername
375                          * user "%" host
376                          * nickname "!" user "@" host
377                          */
378                         char target[COMMAND_LEN];
379                         char * nick = NULL;
380                         char * user = NULL;
381                         char * host = NULL;
382                         char * server = NULL;
383
384                         strlcpy(target, currentTarget, COMMAND_LEN);
385                         server = strchr(target, '@');
386                         if (server) {
387                                 *server = '\0';
388                                 server++;
389                         }
390                         host = strchr(target, '%');
391                         if (host) {
392                                 *host = '\0';
393                                 host++;
394                         }
395                         user = strchr(target, '!');
396                         if (user) {
397                                 /* msgto form: nick!user@host */
398                                 *user = '\0';
399                                 user++;
400                                 nick = target;
401                                 host = server; /* not "@server" but "@host" */
402                         } else {
403                                 user = target;
404                         }
405
406                         for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
407                                 if (Client_Type(cl) != CLIENT_USER &&
408                                     Client_Type(cl) != CLIENT_SERVICE)
409                                         continue;
410                                 if (nick != NULL && host != NULL) {
411                                         if (strcmp(nick, Client_ID(cl)) == 0 &&
412                                             strcmp(user, Client_User(cl)) == 0 &&
413                                             strcasecmp(host, Client_Hostname(cl)) == 0)
414                                                 break;
415                                         else
416                                                 continue;
417                                 }
418                                 if (strcasecmp(user, Client_User(cl)) != 0)
419                                         continue;
420                                 if (host != NULL && strcasecmp(host,
421                                                 Client_Hostname(cl)) != 0)
422                                         continue;
423                                 if (server != NULL && strcasecmp(server,
424                                                 Client_ID(Client_Introducer(cl))) != 0)
425                                         continue;
426                                 break;
427                         }
428                 }
429
430                 if (cl) {
431                         /* Target is a user, enforce type */
432 #ifndef STRICT_RFC
433                         if (Client_Type(cl) != ForceType &&
434                             !(ForceType == CLIENT_USER &&
435                               (Client_Type(cl) == CLIENT_USER ||
436                                Client_Type(cl) == CLIENT_SERVICE))) {
437 #else
438                         if (Client_Type(cl) != ForceType) {
439 #endif
440                                 if (!SendErrors)
441                                         return CONNECTED;
442                                 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
443                                                           Client_ID(from),
444                                                           currentTarget);
445                         }
446
447 #ifndef STRICT_RFC
448                         if (ForceType == CLIENT_SERVICE &&
449                             (Conn_Options(Client_Conn(Client_NextHop(cl)))
450                              & CONN_RFC1459)) {
451                                 /* SQUERY command but RFC 1459 link: convert
452                                  * request to PRIVMSG command */
453                                 Req->command = "PRIVMSG";
454                         }
455 #endif
456
457                         if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
458                             && strchr(Client_Modes(cl), 'a')) {
459                                 /* Target is away */
460                                 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
461                                                         Client_ID(from),
462                                                         Client_ID(cl),
463                                                         Client_Away(cl)))
464                                         return DISCONNECTED;
465                         }
466                         if (Client_Conn(from) > NONE) {
467                                 Conn_UpdateIdle(Client_Conn(from));
468                         }
469                         if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
470                                                       Req->command, Client_ID(cl),
471                                                       Req->argv[1]))
472                                 return DISCONNECTED;
473                 } else if (ForceType != CLIENT_SERVICE
474                            && (chan = Channel_Search(currentTarget))) {
475                         if (!Channel_Write(chan, from, Client, Req->command,
476                                            SendErrors, Req->argv[1]))
477                                         return DISCONNECTED;
478                 } else if (ForceType != CLIENT_SERVICE
479                         /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
480                            && strchr("$#", currentTarget[0])
481                            && strchr(currentTarget, '.')) {
482                         /* targetmask */
483                         if (!Send_Message_Mask(from, Req->command, currentTarget,
484                                                Req->argv[1], SendErrors))
485                                 return DISCONNECTED;
486                 } else {
487                         if (!SendErrors)
488                                 return CONNECTED;
489                         if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
490                                                 Client_ID(from), currentTarget))
491                                 return DISCONNECTED;
492                 }
493
494                 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
495         }
496
497         return CONNECTED;
498 } /* Send_Message */
499
500
501 static bool
502 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
503                   char * message, bool SendErrors)
504 {
505         CLIENT *cl;
506         bool client_match;
507         char *mask = targetMask + 1;
508         const char *check_wildcards;
509
510         cl = NULL;
511
512         if (strchr(Client_Modes(from), 'o') == NULL) {
513                 if (!SendErrors)
514                         return true;
515                 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
516                                           Client_ID(from));
517         }
518
519         /*
520          * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
521          * dot (".") and no wildcards ("*", "?") following the last one.
522          */
523         check_wildcards = strrchr(targetMask, '.');
524         assert(check_wildcards != NULL);
525         if (check_wildcards &&
526                 check_wildcards[strcspn(check_wildcards, "*?")])
527         {
528                 if (!SendErrors)
529                         return true;
530                 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
531         }
532
533         /* #: hostmask, see RFC 2812, sec. 3.3.1 */
534         if (targetMask[0] == '#') {
535                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
536                         if (Client_Type(cl) != CLIENT_USER)
537                                 continue;
538                         client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
539                         if (client_match)
540                                 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
541                                                 command, Client_ID(cl), message))
542                                         return false;
543                 }
544         } else {
545                 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
546                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
547                         if (Client_Type(cl) != CLIENT_USER)
548                                 continue;
549                         client_match = MatchCaseInsensitive(mask,
550                                         Client_ID(Client_Introducer(cl)));
551                         if (client_match)
552                                 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
553                                                 command, Client_ID(cl), message))
554                                         return false;
555                 }
556         }
557         return CONNECTED;
558 } /* Send_Message_Mask */
559
560
561 /* -eof- */