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