2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
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.
16 * Sending IRC commands over the network
27 #include "conn-func.h"
30 #include "irc-write.h"
32 #define SEND_TO_USER 1
33 #define SEND_TO_SERVER 2
35 static const char *Get_Prefix PARAMS((CLIENT *Target, CLIENT *Client));
36 static void cb_writeStrServersPrefixFlag PARAMS((CLIENT *Client,
37 CLIENT *Prefix, void *Buffer));
38 static void Send_Marked_Connections PARAMS((CLIENT *Prefix, const char *Buffer));
41 * Send an error message to a client and enforce a penalty time.
43 * @param Client The target client.
44 * @param Format Format string.
45 * @return CONNECTED or DISCONNECTED.
49 IRC_WriteErrClient( CLIENT *Client, const char *Format, ... )
52 IRC_WriteErrClient( Client, Format, va_alist )
61 assert(Client != NULL);
62 assert(Format != NULL);
69 vsnprintf(buffer, sizeof(buffer), Format, ap);
72 IRC_SetPenalty(Client, 2);
73 return IRC_WriteStrClientPrefix(Client, Client_ThisServer(),
78 * Send a message to a client.
80 * @param Client The target client.
81 * @param Format Format string.
82 * @return CONNECTED or DISCONNECTED.
86 IRC_WriteStrClient( CLIENT *Client, const char *Format, ... )
89 IRC_WriteStrClient( Client, Format, va_alist )
98 assert(Client != NULL);
99 assert(Format != NULL);
102 va_start(ap, Format);
106 vsnprintf(buffer, sizeof(buffer), Format, ap);
109 return IRC_WriteStrClientPrefix(Client, Client_ThisServer(),
114 * Send a message to a client using a specific prefix.
116 * @param Client The target client.
117 * @param Prefix The prefix to use.
118 * @param Format Format string.
119 * @return CONNECTED or DISCONNECTED.
123 IRC_WriteStrClientPrefix(CLIENT *Client, CLIENT *Prefix, const char *Format, ...)
126 IRC_WriteStrClientPrefix(Client, Prefix, Format, va_alist)
133 /* send text to local and remote clients */
138 assert( Client != NULL );
139 assert( Format != NULL );
140 assert( Prefix != NULL );
143 va_start( ap, Format );
147 vsnprintf(buffer, sizeof(buffer), Format, ap);
150 return Conn_WriteStr(Client_Conn(Client_NextHop(Client)), ":%s %s",
151 Get_Prefix(Client_NextHop(Client), Prefix), buffer);
155 * Send a message to all client in a channel.
157 * The message is only sent once per remote server.
159 * @param Client The sending client, excluded while forwarding the message.
160 * @param Channel The target channel.
161 * @param Remote If not set, the message is sent to local clients only.
162 * @param Format Format string.
166 IRC_WriteStrChannel(CLIENT *Client, CHANNEL *Chan, bool Remote,
167 const char *Format, ...)
170 IRC_WriteStrChannel(Client, Chan, Remote, Format, va_alist)
181 assert( Client != NULL );
182 assert( Format != NULL );
185 va_start( ap, Format );
189 vsnprintf(buffer, sizeof(buffer), Format, ap);
192 IRC_WriteStrChannelPrefix(Client, Chan, Client_ThisServer(),
193 Remote, "%s", buffer);
197 * Send a message to all client in a channel using a specific prefix.
199 * The message is only sent once per remote server.
201 * @param Client The sending client, excluded while forwarding the message.
202 * @param Channel The target channel.
203 * @param Prefix The prefix to use.
204 * @param Remote If not set, the message is sent to local clients only.
205 * @param Format Format string.
209 IRC_WriteStrChannelPrefix(CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix,
210 bool Remote, const char *Format, ...)
213 IRC_WriteStrChannelPrefix(Client, Chan, Prefix, Remote, Format, va_alist)
228 assert( Client != NULL );
229 assert( Chan != NULL );
230 assert( Prefix != NULL );
231 assert( Format != NULL );
234 va_start( ap, Format );
238 vsnprintf(buffer, sizeof(buffer), Format, ap);
243 cl2chan = Channel_FirstMember( Chan );
245 c = Channel_GetClient( cl2chan );
247 if (Client_Conn(c) <= NONE)
249 else if(Client_Type(c) == CLIENT_SERVER)
253 c = Client_NextHop(c);
255 if(c && c != Client) {
256 /* Ok, another Client */
257 conn = Client_Conn(c);
258 if (Client_Type(c) == CLIENT_SERVER)
259 Conn_SetFlag(conn, SEND_TO_SERVER);
261 Conn_SetFlag(conn, SEND_TO_USER);
263 cl2chan = Channel_NextMember(Chan, cl2chan);
265 Send_Marked_Connections(Prefix, buffer);
269 * Send a message to all the servers in the network.
271 * @param Client The sending client, excluded while forwarding the message.
272 * @param Format Format string.
276 IRC_WriteStrServers(CLIENT *ExceptOf, const char *Format, ...)
279 IRC_WriteStrServers(ExceptOf, Format, va_alist)
288 assert( Format != NULL );
291 va_start( ap, Format );
295 vsnprintf(buffer, sizeof(buffer), Format, ap);
298 IRC_WriteStrServersPrefix(ExceptOf, Client_ThisServer(), "%s", buffer);
302 * Send a message to all the servers in the network using a specific prefix.
304 * @param Client The sending client, excluded while forwarding the message.
305 * @param Prefix The prefix to use.
306 * @param Format Format string.
310 IRC_WriteStrServersPrefix(CLIENT *ExceptOf, CLIENT *Prefix,
311 const char *Format, ...)
314 IRC_WriteStrServersPrefix(ExceptOf, Prefix, Format, va_alist)
324 assert( Format != NULL );
325 assert( Prefix != NULL );
328 va_start( ap, Format );
332 vsnprintf(buffer, sizeof(buffer), Format, ap);
335 IRC_WriteStrServersPrefixFlag( ExceptOf, Prefix, '\0', "%s", buffer );
339 * Send a message to all the servers in the network using a specific prefix
340 * and matching a "client flag".
342 * @param Client The sending client, excluded while forwarding the message.
343 * @param Prefix The prefix to use.
344 * @param Flag Client flag that must be set on the target.
345 * @param Format Format string.
349 IRC_WriteStrServersPrefixFlag(CLIENT *ExceptOf, CLIENT *Prefix, char Flag,
350 const char *Format, ...)
353 IRC_WriteStrServersPrefixFlag(ExceptOf, Prefix, Flag, Format, va_alist)
364 assert( Format != NULL );
365 assert( Prefix != NULL );
368 va_start( ap, Format );
372 vsnprintf(buffer, sizeof(buffer), Format, ap);
375 IRC_WriteStrServersPrefixFlag_CB(ExceptOf, Prefix, Flag,
376 cb_writeStrServersPrefixFlag, buffer);
380 * Send a message to all the servers in the network using a specific prefix
381 * and matching a "client flag" using a callback function.
383 * @param Client The sending client, excluded while forwarding the message.
384 * @param Prefix The prefix to use.
385 * @param Flag Client flag that must be set on the target.
386 * @param callback Callback function.
387 * @param Format Format string.
390 IRC_WriteStrServersPrefixFlag_CB(CLIENT *ExceptOf, CLIENT *Prefix, char Flag,
391 void (*callback)(CLIENT *, CLIENT *, void *), void *cb_data)
397 if (Client_Type(c) == CLIENT_SERVER && Client_Conn(c) > NONE &&
398 c != Client_ThisServer() && c != ExceptOf) {
399 /* Found a target server, do the flags match? */
400 if (Flag == '\0' || Client_HasFlag(c, Flag))
401 callback(c, Prefix, cb_data);
408 * Send a message to all "related" clients.
410 * Related clients are the one that share one ore more channels with the client
411 * sending this message.
413 * The message is only sent once per remote server.
415 * @param Client The sending client, excluded while forwarding the message.
416 * @param Prefix The prefix to use.
417 * @param Remote If not set, the message is sent to local clients only.
418 * @param Format Format string.
422 IRC_WriteStrRelatedPrefix(CLIENT *Client, CLIENT *Prefix, bool Remote,
423 const char *Format, ...)
426 IRC_WriteStrRelatedPrefix(Client, Prefix, Remote, Format, va_alist)
434 CL2CHAN *chan_cl2chan, *cl2chan;
441 assert( Client != NULL );
442 assert( Prefix != NULL );
443 assert( Format != NULL );
446 va_start( ap, Format );
450 vsnprintf(buffer, sizeof(buffer), Format, ap);
455 chan_cl2chan = Channel_FirstChannelOf( Client );
456 while( chan_cl2chan )
458 chan = Channel_GetChannel( chan_cl2chan );
459 cl2chan = Channel_FirstMember( chan );
462 c = Channel_GetClient( cl2chan );
465 if( Client_Conn( c ) <= NONE ) c = NULL;
466 else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
468 if( c ) c = Client_NextHop( c );
470 if( c && ( c != Client ))
472 conn = Client_Conn( c );
473 if( Client_Type( c ) == CLIENT_SERVER ) Conn_SetFlag( conn, SEND_TO_SERVER );
474 else Conn_SetFlag( conn, SEND_TO_USER );
476 cl2chan = Channel_NextMember( chan, cl2chan );
479 chan_cl2chan = Channel_NextChannelOf( Client, chan_cl2chan );
481 Send_Marked_Connections(Prefix, buffer);
482 } /* IRC_WriteStrRelatedPrefix */
485 * Send WALLOPS message.
487 * @param Client The sending client, excluded while forwarding the message.
488 * @param From The (remote) sender of the message.
489 * @param Format Format string.
493 IRC_SendWallops(CLIENT *Client, CLIENT *From, const char *Format, ...)
496 IRC_SendWallops(Client, From, Format, va_alist )
508 va_start(ap, Format);
512 vsnprintf(msg, sizeof(msg), Format, ap);
515 for (to=Client_First(); to != NULL; to=Client_Next(to)) {
516 if (Client_Conn(to) == NONE) /* no local connection */
519 switch (Client_Type(to)) {
521 if (Client_HasMode(to, 'w'))
522 IRC_WriteStrClientPrefix(to, From,
527 IRC_WriteStrClientPrefix(to, From,
532 } /* IRC_SendWallops */
535 * Set a "penalty time" for an IRC client.
537 * Note: penalty times are never set for server links or remote clients!
539 * @param Client The client.
540 * @param Seconds The additional "penalty time" to enforce.
543 IRC_SetPenalty(CLIENT *Client, time_t Seconds)
547 assert(Client != NULL);
550 if (Client_Type(Client) == CLIENT_SERVER)
553 c = Client_Conn(Client);
557 Conn_SetPenalty(c, Seconds);
558 } /* IRC_SetPenalty */
561 Get_Prefix(CLIENT *Target, CLIENT *Client)
563 assert (Target != NULL);
564 assert (Client != NULL);
566 if (Client_Type(Target) == CLIENT_SERVER)
567 return Client_ID(Client);
569 return Client_MaskCloaked(Client);
573 cb_writeStrServersPrefixFlag(CLIENT *Client, CLIENT *Prefix, void *Buffer)
575 IRC_WriteStrClientPrefix(Client, Prefix, "%s", Buffer);
576 } /* cb_writeStrServersPrefixFlag */
579 * Send a message to all marked connections using a specific prefix.
581 * @param Prefix The prefix to use.
582 * @param Buffer The message to send.
585 Send_Marked_Connections(CLIENT *Prefix, const char *Buffer)
589 assert(Prefix != NULL);
590 assert(Buffer != NULL);
593 while (conn != NONE) {
594 if (Conn_Flag(conn) == SEND_TO_SERVER)
595 Conn_WriteStr(conn, ":%s %s",
596 Client_ID(Prefix), Buffer);
597 else if (Conn_Flag(conn) == SEND_TO_USER)
598 Conn_WriteStr(conn, ":%s %s",
599 Client_MaskCloaked(Prefix), Buffer);
600 conn = Conn_Next(conn);