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