]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
Don't forward KILL commands for unknown clients
[ngircd-alex.git] / src / ngircd / irc.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
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
12 #include "portab.h"
13
14 /**
15  * @file
16  * IRC commands
17  */
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 "conn-encoding.h"
29 #include "defines.h"
30 #include "irc-macros.h"
31 #include "irc-write.h"
32 #include "log.h"
33 #include "match.h"
34 #include "messages.h"
35 #include "parse.h"
36 #include "op.h"
37 #include "tool.h"
38
39 #include "exp.h"
40 #include "irc.h"
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 static bool Help PARAMS((CLIENT *Client, const char *Topic));
49
50 /**
51  * Check if a list limit is reached and inform client accordingly.
52  *
53  * @param From The client.
54  * @param Count Reply item count.
55  * @param Limit Reply limit.
56  * @param Name Name of the list.
57  * @return true if list limit has been reached; false otherwise.
58  */
59 GLOBAL bool
60 IRC_CheckListTooBig(CLIENT *From, const int Count, const int Limit,
61                     const char *Name)
62 {
63         assert(From != NULL);
64         assert(Count >= 0);
65         assert(Limit > 0);
66         assert(Name != NULL);
67
68         if (Count < Limit)
69                 return false;
70
71         (void)IRC_WriteStrClient(From,
72                                  "NOTICE %s :%s list limit (%d) reached!",
73                                  Client_ID(From), Name, Limit);
74         IRC_SetPenalty(From, 2);
75         return true;
76 }
77
78 /**
79  * Handler for the IRC "ERROR" command.
80  *
81  * @param Client The client from which this command has been received.
82  * @param Req Request structure with prefix and all parameters.
83  * @return CONNECTED or DISCONNECTED.
84 */
85 GLOBAL bool
86 IRC_ERROR(CLIENT *Client, REQUEST *Req)
87 {
88         assert( Client != NULL );
89         assert( Req != NULL );
90
91         if (Client_Type(Client) != CLIENT_GOTPASS
92             && Client_Type(Client) != CLIENT_GOTPASS_2813
93             && Client_Type(Client) != CLIENT_UNKNOWNSERVER
94             && Client_Type(Client) != CLIENT_SERVER
95             && Client_Type(Client) != CLIENT_SERVICE) {
96                 LogDebug("Ignored ERROR command from \"%s\" ...",
97                          Client_Mask(Client));
98                 IRC_SetPenalty(Client, 2);
99                 return CONNECTED;
100         }
101
102         if (Req->argc < 1)
103                 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
104                     Client_Mask(Client));
105         else
106                 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
107                     Client_Mask(Client), Req->argv[0]);
108
109         return CONNECTED;
110 } /* IRC_ERROR */
111
112 /**
113  * Handler for the IRC "KILL" command.
114  *
115  * This function implements the IRC command "KILL" which is used to selectively
116  * disconnect clients. It can be used by IRC operators and servers, for example
117  * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
118  *
119  * Please note that this function is also called internally, without a real
120  * KILL command being received over the network! Client is Client_ThisServer()
121  * in this case, and the prefix in Req is NULL.
122  *
123  * @param Client The client from which this command has been received or
124  * Client_ThisServer() when generated interanlly.
125  * @param Req Request structure with prefix and all parameters.
126  * @return CONNECTED or DISCONNECTED.
127  */
128 GLOBAL bool
129 IRC_KILL(CLIENT *Client, REQUEST *Req)
130 {
131         CLIENT *prefix;
132         char reason[COMMAND_LEN];
133
134         assert (Client != NULL);
135         assert (Req != NULL);
136
137         if (Client_Type(Client) != CLIENT_SERVER && !Op_Check(Client, Req))
138                 return Op_NoPrivileges(Client, Req);
139
140         /* Get prefix (origin); use the client if no prefix is given. */
141         if (Req->prefix)
142                 prefix = Client_Search(Req->prefix);
143         else
144                 prefix = Client;
145
146         /* Log a warning message and use this server as origin when the
147          * prefix (origin) is invalid. And this is the reason why we don't
148          * use the _IRC_GET_SENDER_OR_RETURN_ macro above! */
149         if (!prefix) {
150                 Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
151                     Req->prefix );
152                 prefix = Client_ThisServer();
153         }
154
155         if (Client != Client_ThisServer())
156                 Log(LOG_NOTICE|LOG_snotice,
157                     "Got KILL command from \"%s\" for \"%s\": \"%s\".",
158                     Client_Mask(prefix), Req->argv[0], Req->argv[1]);
159
160         /* Build reason string: Prefix the "reason" if the originator is a
161          * regular user, so users can't spoof KILLs of servers. */
162         if (Client_Type(Client) == CLIENT_USER)
163                 snprintf(reason, sizeof(reason), "KILLed by %s: %s",
164                          Client_ID(Client), Req->argv[1]);
165         else
166                 strlcpy(reason, Req->argv[1], sizeof(reason));
167
168         return IRC_KillClient(Client, prefix, Req->argv[0], reason);
169 }
170
171 /**
172  * Handler for the IRC "NOTICE" command.
173  *
174  * @param Client The client from which this command has been received.
175  * @param Req Request structure with prefix and all parameters.
176  * @return CONNECTED or DISCONNECTED.
177 */
178 GLOBAL bool
179 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
180 {
181         return Send_Message(Client, Req, CLIENT_USER, false);
182 } /* IRC_NOTICE */
183
184 /**
185  * Handler for the IRC "PRIVMSG" command.
186  *
187  * @param Client The client from which this command has been received.
188  * @param Req Request structure with prefix and all parameters.
189  * @return CONNECTED or DISCONNECTED.
190  */
191 GLOBAL bool
192 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
193 {
194         return Send_Message(Client, Req, CLIENT_USER, true);
195 } /* IRC_PRIVMSG */
196
197 /**
198  * Handler for the IRC "SQUERY" command.
199  *
200  * @param Client The client from which this command has been received.
201  * @param Req Request structure with prefix and all parameters.
202  * @return CONNECTED or DISCONNECTED.
203  */
204 GLOBAL bool
205 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
206 {
207         return Send_Message(Client, Req, CLIENT_SERVICE, true);
208 } /* IRC_SQUERY */
209
210 /*
211  * Handler for the IRC "TRACE" command.
212  *
213  * @param Client The client from which this command has been received.
214  * @param Req Request structure with prefix and all parameters.
215  * @return CONNECTED or DISCONNECTED.
216  */
217  GLOBAL bool
218 IRC_TRACE(CLIENT *Client, REQUEST *Req)
219 {
220         CLIENT *from, *target, *c;
221         CONN_ID idx, idx2;
222         char user[CLIENT_USER_LEN];
223
224         assert(Client != NULL);
225         assert(Req != NULL);
226
227         IRC_SetPenalty(Client, 3);
228
229         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
230         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
231
232         /* Forward command to other server? */
233         if (target != Client_ThisServer()) {
234                 /* Send RPL_TRACELINK back to initiator */
235                 idx = Client_Conn(Client);
236                 assert(idx > NONE);
237                 idx2 = Client_Conn(Client_NextHop(target));
238                 assert(idx2 > NONE);
239
240                 if (!IRC_WriteStrClient(from, RPL_TRACELINK_MSG,
241                                         Client_ID(from), PACKAGE_NAME,
242                                         PACKAGE_VERSION, Client_ID(target),
243                                         Client_ID(Client_NextHop(target)),
244                                         Option_String(idx2),
245                                         time(NULL) - Conn_StartTime(idx2),
246                                         Conn_SendQ(idx), Conn_SendQ(idx2)))
247                         return DISCONNECTED;
248
249                 /* Forward command */
250                 IRC_WriteStrClientPrefix(target, from, "TRACE %s", Req->argv[0]);
251                 return CONNECTED;
252         }
253
254         /* Infos about all connected servers */
255         c = Client_First();
256         while (c) {
257                 if (Client_Conn(c) > NONE) {
258                         /* Local client */
259                         if (Client_Type(c) == CLIENT_SERVER) {
260                                 /* Server link */
261                                 strlcpy(user, Client_User(c), sizeof(user));
262                                 if (user[0] == '~')
263                                         strlcpy(user, "unknown", sizeof(user));
264                                 if (!IRC_WriteStrClient(from,
265                                                 RPL_TRACESERVER_MSG,
266                                                 Client_ID(from), Client_ID(c),
267                                                 user, Client_Hostname(c),
268                                                 Client_Mask(Client_ThisServer()),
269                                                 Option_String(Client_Conn(c))))
270                                         return DISCONNECTED;
271                         }
272                         if (Client_Type(c) == CLIENT_USER
273                             && Client_HasMode(c, 'o')) {
274                                 /* IRC Operator */
275                                 if (!IRC_WriteStrClient(from,
276                                                 RPL_TRACEOPERATOR_MSG,
277                                                 Client_ID(from), Client_ID(c)))
278                                         return DISCONNECTED;
279                         }
280                 }
281                 c = Client_Next( c );
282         }
283
284         return IRC_WriteStrClient(from, RPL_TRACEEND_MSG, Client_ID(from),
285                                   Conf_ServerName, PACKAGE_NAME,
286                                   PACKAGE_VERSION, NGIRCd_DebugLevel);
287 } /* IRC_TRACE */
288
289 /**
290  * Handler for the IRC "HELP" command.
291  *
292  * @param Client The client from which this command has been received.
293  * @param Req Request structure with prefix and all parameters.
294  * @return CONNECTED or DISCONNECTED.
295  */
296 GLOBAL bool
297 IRC_HELP(CLIENT *Client, REQUEST *Req)
298 {
299         COMMAND *cmd;
300
301         assert(Client != NULL);
302         assert(Req != NULL);
303
304         IRC_SetPenalty(Client, 2);
305
306         if ((Req->argc == 0 && array_bytes(&Conf_Helptext) > 0)
307             || (Req->argc >= 1 && strcasecmp(Req->argv[0], "Commands") != 0)) {
308                 /* Help text available and requested */
309                 if (Req->argc >= 1)
310                         return Help(Client, Req->argv[0]);
311
312                 if (!Help(Client, "Intro"))
313                         return DISCONNECTED;
314                 return CONNECTED;
315         }
316
317         cmd = Parse_GetCommandStruct();
318         while(cmd->name) {
319                 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
320                                         Client_ID(Client), cmd->name))
321                         return DISCONNECTED;
322                 cmd++;
323         }
324         return CONNECTED;
325 } /* IRC_HELP */
326
327 /**
328  * Kill an client identified by its nick name.
329  *
330  * Please note that after killig a client, its CLIENT cond CONNECTION
331  * structures are invalid. So the caller must make sure on its own not to
332  * access data of probably killed clients after calling this function!
333  *
334  * @param Client The client from which the command leading to the KILL has
335  *              been received, or NULL. The KILL will no be forwarded in this
336  *              direction. Only relevant when From is set, too.
337  * @param From The client from which the command originated, or NULL for
338                 the local server.
339  * @param Nick The nick name to kill.
340  * @param Reason Text to send as reason to the client and other servers.
341  */
342 GLOBAL bool
343 IRC_KillClient(CLIENT *Client, CLIENT *From, const char *Nick, const char *Reason)
344 {
345         const char *msg;
346         CONN_ID my_conn, conn;
347         CLIENT *c;
348
349         /* Do we know such a client in the network? */
350         c = Client_Search(Nick);
351         if (!c) {
352                 LogDebug("Client with nick \"%s\" is unknown, not forwaring.", Nick);
353                 return CONNECTED;
354         }
355
356         /* Inform other servers */
357         IRC_WriteStrServersPrefix(From ? Client : NULL,
358                                   From ? From : Client_ThisServer(),
359                                   "KILL %s :%s", Nick, Reason);
360
361         if (Client_Type(c) != CLIENT_USER && Client_Type(c) != CLIENT_GOTNICK) {
362                 /* Target of this KILL is not a regular user, this is
363                  * invalid! So we ignore this case if we received a
364                  * regular KILL from the network and try to kill the
365                  * client/connection anyway (but log an error!) if the
366                  * origin is the local server. */
367
368                 if (Client != Client_ThisServer()) {
369                         /* Invalid KILL received from remote */
370                         if (Client_Type(c) == CLIENT_SERVER)
371                                 msg = ERR_CANTKILLSERVER_MSG;
372                         else
373                                 msg = ERR_NOPRIVILEGES_MSG;
374                         return IRC_WriteErrClient(Client, msg, Client_ID(Client));
375                 }
376
377                 Log(LOG_ERR,
378                     "Got KILL for invalid client type: %d, \"%s\"!",
379                     Client_Type(c), Nick);
380         }
381
382         /* Save ID of this connection */
383         my_conn = Client_Conn(Client);
384
385         /* Kill the client NOW:
386          *  - Close the local connection (if there is one),
387          *  - Destroy the CLIENT structure for remote clients.
388          * Note: Conn_Close() removes the CLIENT structure as well. */
389         conn = Client_Conn(c);
390         if(conn > NONE)
391                 Conn_Close(conn, NULL, Reason, true);
392         else
393                 Client_Destroy(c, NULL, Reason, false);
394
395         /* Are we still connected or were we killed, too? */
396         if (my_conn > NONE && Conn_GetClient(my_conn))
397                 return CONNECTED;
398         else
399                 return DISCONNECTED;
400 }
401
402 /**
403  * Send help for a given topic to the client.
404  *
405  * @param Client The client requesting help.
406  * @param Topoc The help topic requested.
407  * @return CONNECTED or DISCONNECTED.
408  */
409 static bool
410 Help(CLIENT *Client, const char *Topic)
411 {
412         char *line;
413         size_t helptext_len, len_str, idx_start, lines = 0;
414         bool in_article = false;
415
416         assert(Client != NULL);
417         assert(Topic != NULL);
418
419         helptext_len = array_bytes(&Conf_Helptext);
420         line = array_start(&Conf_Helptext);
421         while (helptext_len > 0) {
422                 len_str = strlen(line) + 1;
423                 assert(helptext_len >= len_str);
424                 helptext_len -= len_str;
425
426                 if (in_article) {
427                         /* The first character in each article text line must
428                          * be a TAB (ASCII 9) character which will be stripped
429                          * in the output. If it is not a TAB, the end of the
430                          * article has been reached. */
431                         if (line[0] != '\t') {
432                                 if (lines > 0)
433                                         return CONNECTED;
434                                 else
435                                         break;
436                         }
437
438                         /* A single '.' character indicates an empty line */
439                         if (line[1] == '.' && line[2] == '\0')
440                                 idx_start = 2;
441                         else
442                                 idx_start = 1;
443
444                         if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
445                                                 Client_ID(Client),
446                                                 &line[idx_start]))
447                                 return DISCONNECTED;
448                         lines++;
449
450                 } else {
451                         if (line[0] == '-' && line[1] == ' '
452                             && strcasecmp(&line[2], Topic) == 0)
453                                 in_article = true;
454                 }
455
456                 line += len_str;
457         }
458
459         /* Help topic not found (or empty)! */
460         if (!IRC_WriteStrClient(Client, "NOTICE %s :No help for \"%s\" found!",
461                                 Client_ID(Client), Topic))
462                 return DISCONNECTED;
463
464         return CONNECTED;
465 }
466
467 /**
468  * Get pointer to a static string representing the connection "options".
469  *
470  * @param Idx Connection index.
471  * @return Pointer to static (global) string buffer.
472  */
473 static char *
474 #ifdef ZLIB
475 Option_String(CONN_ID Idx)
476 #else
477 Option_String(UNUSED CONN_ID Idx)
478 #endif
479 {
480         static char option_txt[8];
481 #ifdef ZLIB
482         UINT16 options;
483 #endif
484
485         assert(Idx != NONE);
486
487         options = Conn_Options(Idx);
488         strcpy(option_txt, "F");        /* No idea what this means, but the
489                                          * original ircd sends it ... */
490 #ifdef SSL_SUPPORT
491         if(options & CONN_SSL)          /* SSL encrypted link */
492                 strlcat(option_txt, "s", sizeof(option_txt));
493 #endif
494 #ifdef ZLIB
495         if(options & CONN_ZIP)          /* zlib compression enabled */
496                 strlcat(option_txt, "z", sizeof(option_txt));
497 #endif
498         LogDebug(" *** %d: %d = %s", Idx, options, option_txt);
499
500         return option_txt;
501 } /* Option_String */
502
503 static bool
504 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
505 {
506         CLIENT *cl, *from;
507         CL2CHAN *cl2chan;
508         CHANNEL *chan;
509         char *currentTarget = Req->argv[0];
510         char *lastCurrentTarget = NULL;
511         char *message = NULL;
512
513         assert(Client != NULL);
514         assert(Req != NULL);
515
516         if (Req->argc == 0) {
517                 if (!SendErrors)
518                         return CONNECTED;
519                 return IRC_WriteErrClient(Client, ERR_NORECIPIENT_MSG,
520                                           Client_ID(Client), Req->command);
521         }
522         if (Req->argc == 1) {
523                 if (!SendErrors)
524                         return CONNECTED;
525                 return IRC_WriteErrClient(Client, ERR_NOTEXTTOSEND_MSG,
526                                           Client_ID(Client));
527         }
528         if (Req->argc > 2) {
529                 if (!SendErrors)
530                         return CONNECTED;
531                 IRC_SetPenalty(Client, 2);
532                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
533                                           Client_ID(Client), Req->command);
534         }
535
536         if (Client_Type(Client) == CLIENT_SERVER)
537                 from = Client_Search(Req->prefix);
538         else
539                 from = Client;
540         if (!from)
541                 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
542                                           Client_ID(Client), Req->prefix);
543
544 #ifdef ICONV
545         if (Client_Conn(Client) > NONE)
546                 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
547         else
548 #endif
549                 message = Req->argv[1];
550
551         /* handle msgtarget = msgto *("," msgto) */
552         currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
553         ngt_UpperStr(Req->command);
554
555         while (currentTarget) {
556                 /* Check for and handle valid <msgto> of form:
557                  * RFC 2812 2.3.1:
558                  *   msgto =  channel / ( user [ "%" host ] "@" servername )
559                  *   msgto =/ ( user "%" host ) / targetmask
560                  *   msgto =/ nickname / ( nickname "!" user "@" host )
561                  */
562                 if (strchr(currentTarget, '!') == NULL)
563                         /* nickname */
564                         cl = Client_Search(currentTarget);
565                 else
566                         cl = NULL;
567
568                 if (cl == NULL) {
569                         /* If currentTarget isn't a nickname check for:
570                          * user ["%" host] "@" servername
571                          * user "%" host
572                          * nickname "!" user "@" host
573                          */
574                         char target[COMMAND_LEN];
575                         char * nick = NULL;
576                         char * user = NULL;
577                         char * host = NULL;
578                         char * server = NULL;
579
580                         strlcpy(target, currentTarget, COMMAND_LEN);
581                         server = strchr(target, '@');
582                         if (server) {
583                                 *server = '\0';
584                                 server++;
585                         }
586                         host = strchr(target, '%');
587                         if (host) {
588                                 *host = '\0';
589                                 host++;
590                         }
591                         user = strchr(target, '!');
592                         if (user) {
593                                 /* msgto form: nick!user@host */
594                                 *user = '\0';
595                                 user++;
596                                 nick = target;
597                                 host = server; /* not "@server" but "@host" */
598                         } else {
599                                 user = target;
600                         }
601
602                         for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
603                                 if (Client_Type(cl) != CLIENT_USER &&
604                                     Client_Type(cl) != CLIENT_SERVICE)
605                                         continue;
606                                 if (nick != NULL && host != NULL) {
607                                         if (strcasecmp(nick, Client_ID(cl)) == 0 &&
608                                             strcasecmp(user, Client_User(cl)) == 0 &&
609                                             strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
610                                                 break;
611                                         else
612                                                 continue;
613                                 }
614                                 if (strcasecmp(user, Client_User(cl)) != 0)
615                                         continue;
616                                 if (host != NULL && strcasecmp(host,
617                                                 Client_HostnameDisplayed(cl)) != 0)
618                                         continue;
619                                 if (server != NULL && strcasecmp(server,
620                                                 Client_ID(Client_Introducer(cl))) != 0)
621                                         continue;
622                                 break;
623                         }
624                 }
625
626                 if (cl) {
627                         /* Target is a user, enforce type */
628 #ifndef STRICT_RFC
629                         if (Client_Type(cl) != ForceType &&
630                             !(ForceType == CLIENT_USER &&
631                               (Client_Type(cl) == CLIENT_USER ||
632                                Client_Type(cl) == CLIENT_SERVICE))) {
633 #else
634                         if (Client_Type(cl) != ForceType) {
635 #endif
636                                 if (SendErrors && !IRC_WriteErrClient(
637                                     from, ERR_NOSUCHNICK_MSG,Client_ID(from),
638                                     currentTarget))
639                                         return DISCONNECTED;
640                                 goto send_next_target;
641                         }
642
643 #ifndef STRICT_RFC
644                         if (ForceType == CLIENT_SERVICE &&
645                             (Conn_Options(Client_Conn(Client_NextHop(cl)))
646                              & CONN_RFC1459)) {
647                                 /* SQUERY command but RFC 1459 link: convert
648                                  * request to PRIVMSG command */
649                                 Req->command = "PRIVMSG";
650                         }
651 #endif
652                         if (Client_HasMode(cl, 'b') &&
653                             !Client_HasMode(from, 'R') &&
654                             !Client_HasMode(from, 'o') &&
655                             !(Client_Type(from) == CLIENT_SERVER) &&
656                             !(Client_Type(from) == CLIENT_SERVICE)) {
657                                 if (SendErrors && !IRC_WriteErrClient(from,
658                                                 ERR_NONONREG_MSG,
659                                                 Client_ID(from), Client_ID(cl)))
660                                         return DISCONNECTED;
661                                 goto send_next_target;
662                         }
663
664                         if (Client_HasMode(cl, 'C')) {
665                                 cl2chan = Channel_FirstChannelOf(cl);
666                                 while (cl2chan) {
667                                         chan = Channel_GetChannel(cl2chan);
668                                         if (Channel_IsMemberOf(chan, from))
669                                                 break;
670                                         cl2chan = Channel_NextChannelOf(cl, cl2chan);
671                                 }
672                                 if (!cl2chan) {
673                                         if (SendErrors && !IRC_WriteErrClient(
674                                             from, ERR_NOTONSAMECHANNEL_MSG,
675                                             Client_ID(from), Client_ID(cl)))
676                                                 return DISCONNECTED;
677                                         goto send_next_target;
678                                 }
679                         }
680
681                         if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
682                             && Client_HasMode(cl, 'a')) {
683                                 /* Target is away */
684                                 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
685                                                         Client_ID(from),
686                                                         Client_ID(cl),
687                                                         Client_Away(cl)))
688                                         return DISCONNECTED;
689                         }
690                         if (Client_Conn(from) > NONE) {
691                                 Conn_UpdateIdle(Client_Conn(from));
692                         }
693                         if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
694                                                       Req->command, Client_ID(cl),
695                                                       message))
696                                 return DISCONNECTED;
697                 } else if (ForceType != CLIENT_SERVICE
698                            && (chan = Channel_Search(currentTarget))) {
699                         if (!Channel_Write(chan, from, Client, Req->command,
700                                            SendErrors, message))
701                                         return DISCONNECTED;
702                 } else if (ForceType != CLIENT_SERVICE
703                         /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
704                            && strchr("$#", currentTarget[0])
705                            && strchr(currentTarget, '.')) {
706                         /* targetmask */
707                         if (!Send_Message_Mask(from, Req->command, currentTarget,
708                                                message, SendErrors))
709                                 return DISCONNECTED;
710                 } else {
711                         if (!SendErrors)
712                                 return CONNECTED;
713                         if (!IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
714                                                 Client_ID(from), currentTarget))
715                                 return DISCONNECTED;
716                 }
717
718         send_next_target:
719                 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
720                 if (currentTarget)
721                         Conn_SetPenalty(Client_Conn(Client), 1);
722         }
723
724         return CONNECTED;
725 } /* Send_Message */
726
727 static bool
728 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
729                   char * message, bool SendErrors)
730 {
731         CLIENT *cl;
732         bool client_match;
733         char *mask = targetMask + 1;
734         const char *check_wildcards;
735
736         cl = NULL;
737
738         if (!Client_HasMode(from, 'o')) {
739                 if (!SendErrors)
740                         return true;
741                 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
742                                           Client_ID(from));
743         }
744
745         /*
746          * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
747          * dot (".") and no wildcards ("*", "?") following the last one.
748          */
749         check_wildcards = strrchr(targetMask, '.');
750         assert(check_wildcards != NULL);
751         if (check_wildcards &&
752                 check_wildcards[strcspn(check_wildcards, "*?")])
753         {
754                 if (!SendErrors)
755                         return true;
756                 return IRC_WriteErrClient(from, ERR_WILDTOPLEVEL, targetMask);
757         }
758
759         /* #: hostmask, see RFC 2812, sec. 3.3.1 */
760         if (targetMask[0] == '#') {
761                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
762                         if (Client_Type(cl) != CLIENT_USER)
763                                 continue;
764                         client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
765                         if (client_match)
766                                 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
767                                                 command, Client_ID(cl), message))
768                                         return false;
769                 }
770         } else {
771                 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
772                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
773                         if (Client_Type(cl) != CLIENT_USER)
774                                 continue;
775                         client_match = MatchCaseInsensitive(mask,
776                                         Client_ID(Client_Introducer(cl)));
777                         if (client_match)
778                                 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
779                                                 command, Client_ID(cl), message))
780                                         return false;
781                 }
782         }
783         return CONNECTED;
784 } /* Send_Message_Mask */
785
786 /* -eof- */