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
29 #include "conn-func.h"
32 #include "irc-write.h"
34 #define SEND_TO_USER 1
35 #define SEND_TO_SERVER 2
37 static const char *Get_Prefix PARAMS((CLIENT *Target, CLIENT *Client));
38 static void cb_writeStrServersPrefixFlag PARAMS((CLIENT *Client,
39 CLIENT *Prefix, void *Buffer));
40 static void Send_Marked_Connections PARAMS((CLIENT *Prefix, const char *Buffer));
43 * Send an error message to a client and enforce a penalty time.
45 * @param Client The target client.
46 * @param Format Format string.
47 * @return CONNECTED or DISCONNECTED.
51 IRC_WriteErrClient( CLIENT *Client, const char *Format, ... )
54 IRC_WriteErrClient( Client, Format, va_alist )
63 assert(Client != NULL);
64 assert(Format != NULL);
71 vsnprintf(buffer, sizeof(buffer), Format, ap);
74 IRC_SetPenalty(Client, 2);
75 return IRC_WriteStrClientPrefix(Client, Client_ThisServer(),
80 * Send a message to a client.
82 * @param Client The target client.
83 * @param Format Format string.
84 * @return CONNECTED or DISCONNECTED.
88 IRC_WriteStrClient( CLIENT *Client, const char *Format, ... )
91 IRC_WriteStrClient( Client, Format, va_alist )
100 assert(Client != NULL);
101 assert(Format != NULL);
104 va_start(ap, Format);
108 vsnprintf(buffer, sizeof(buffer), Format, ap);
111 return IRC_WriteStrClientPrefix(Client, Client_ThisServer(),
116 * Send a message to a client using a specific prefix.
118 * @param Client The target client.
119 * @param Prefix The prefix to use.
120 * @param Format Format string.
121 * @return CONNECTED or DISCONNECTED.
125 IRC_WriteStrClientPrefix(CLIENT *Client, CLIENT *Prefix, const char *Format, ...)
128 IRC_WriteStrClientPrefix(Client, Prefix, Format, va_alist)
135 /* send text to local and remote clients */
140 assert( Client != NULL );
141 assert( Format != NULL );
142 assert( Prefix != NULL );
145 va_start( ap, Format );
149 vsnprintf(buffer, sizeof(buffer), Format, ap);
152 return Conn_WriteStr(Client_Conn(Client_NextHop(Client)), ":%s %s",
153 Get_Prefix(Client_NextHop(Client), Prefix), buffer);
157 * Send a message to all client in a channel.
159 * The message is only sent once per remote server.
161 * @param Client The sending client, excluded while forwarding the message.
162 * @param Channel The target channel.
163 * @param Remote If not set, the message is sent to local clients only.
164 * @param Format Format string.
168 IRC_WriteStrChannel(CLIENT *Client, CHANNEL *Chan, bool Remote,
169 const char *Format, ...)
172 IRC_WriteStrChannel(Client, Chan, Remote, Format, va_alist)
183 assert( Client != NULL );
184 assert( Format != NULL );
187 va_start( ap, Format );
191 vsnprintf(buffer, sizeof(buffer), Format, ap);
194 IRC_WriteStrChannelPrefix(Client, Chan, Client_ThisServer(),
195 Remote, "%s", buffer);
199 * Send a message to all client in a channel using a specific prefix.
201 * The message is only sent once per remote server.
203 * @param Client The sending client, excluded while forwarding the message.
204 * @param Channel The target channel.
205 * @param Prefix The prefix to use.
206 * @param Remote If not set, the message is sent to local clients only.
207 * @param Format Format string.
211 IRC_WriteStrChannelPrefix(CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix,
212 bool Remote, const char *Format, ...)
215 IRC_WriteStrChannelPrefix(Client, Chan, Prefix, Remote, Format, va_alist)
230 assert( Client != NULL );
231 assert( Chan != NULL );
232 assert( Prefix != NULL );
233 assert( Format != NULL );
236 va_start( ap, Format );
240 vsnprintf(buffer, sizeof(buffer), Format, ap);
245 cl2chan = Channel_FirstMember( Chan );
247 c = Channel_GetClient( cl2chan );
249 if (Client_Conn(c) <= NONE)
251 else if(Client_Type(c) == CLIENT_SERVER)
255 c = Client_NextHop(c);
257 if(c && c != Client) {
258 /* Ok, another Client */
259 conn = Client_Conn(c);
260 if (Client_Type(c) == CLIENT_SERVER)
261 Conn_SetFlag(conn, SEND_TO_SERVER);
263 Conn_SetFlag(conn, SEND_TO_USER);
265 cl2chan = Channel_NextMember(Chan, cl2chan);
267 Send_Marked_Connections(Prefix, buffer);
271 * Send a message to all the servers in the network.
273 * @param Client The sending client, excluded while forwarding the message.
274 * @param Format Format string.
278 IRC_WriteStrServers(CLIENT *ExceptOf, const char *Format, ...)
281 IRC_WriteStrServers(ExceptOf, Format, va_alist)
290 assert( Format != NULL );
293 va_start( ap, Format );
297 vsnprintf(buffer, sizeof(buffer), Format, ap);
300 IRC_WriteStrServersPrefix(ExceptOf, Client_ThisServer(), "%s", buffer);
304 * Send a message to all the servers in the network using a specific prefix.
306 * @param Client The sending client, excluded while forwarding the message.
307 * @param Prefix The prefix to use.
308 * @param Format Format string.
312 IRC_WriteStrServersPrefix(CLIENT *ExceptOf, CLIENT *Prefix,
313 const char *Format, ...)
316 IRC_WriteStrServersPrefix(ExceptOf, Prefix, Format, va_alist)
326 assert( Format != NULL );
327 assert( Prefix != NULL );
330 va_start( ap, Format );
334 vsnprintf(buffer, sizeof(buffer), Format, ap);
337 IRC_WriteStrServersPrefixFlag( ExceptOf, Prefix, '\0', "%s", buffer );
341 * Send a message to all the servers in the network using a specific prefix
342 * and matching a "client flag".
344 * @param Client The sending client, excluded while forwarding the message.
345 * @param Prefix The prefix to use.
346 * @param Flag Client flag that must be set on the target.
347 * @param Format Format string.
351 IRC_WriteStrServersPrefixFlag(CLIENT *ExceptOf, CLIENT *Prefix, char Flag,
352 const char *Format, ...)
355 IRC_WriteStrServersPrefixFlag(ExceptOf, Prefix, Flag, Format, va_alist)
366 assert( Format != NULL );
367 assert( Prefix != NULL );
370 va_start( ap, Format );
374 vsnprintf(buffer, sizeof(buffer), Format, ap);
377 IRC_WriteStrServersPrefixFlag_CB(ExceptOf, Prefix, Flag,
378 cb_writeStrServersPrefixFlag, buffer);
382 * Send a message to all the servers in the network using a specific prefix
383 * and matching a "client flag" using a callback function.
385 * @param Client The sending client, excluded while forwarding the message.
386 * @param Prefix The prefix to use.
387 * @param Flag Client flag that must be set on the target.
388 * @param callback Callback function.
389 * @param Format Format string.
392 IRC_WriteStrServersPrefixFlag_CB(CLIENT *ExceptOf, CLIENT *Prefix, char Flag,
393 void (*callback)(CLIENT *, CLIENT *, void *), void *cb_data)
399 if (Client_Type(c) == CLIENT_SERVER && Client_Conn(c) > NONE &&
400 c != Client_ThisServer() && c != ExceptOf) {
401 /* Found a target server, do the flags match? */
402 if (Flag == '\0' || Client_HasFlag(c, Flag))
403 callback(c, Prefix, cb_data);
410 * Send a message to all "related" clients.
412 * Related clients are the one that share one ore more channels with the client
413 * sending this message.
415 * The message is only sent once per remote server.
417 * @param Client The sending client, excluded while forwarding the message.
418 * @param Prefix The prefix to use.
419 * @param Remote If not set, the message is sent to local clients only.
420 * @param Format Format string.
424 IRC_WriteStrRelatedPrefix(CLIENT *Client, CLIENT *Prefix, bool Remote,
425 const char *Format, ...)
428 IRC_WriteStrRelatedPrefix(Client, Prefix, Remote, Format, va_alist)
436 CL2CHAN *chan_cl2chan, *cl2chan;
443 assert( Client != NULL );
444 assert( Prefix != NULL );
445 assert( Format != NULL );
448 va_start( ap, Format );
452 vsnprintf(buffer, sizeof(buffer), Format, ap);
457 chan_cl2chan = Channel_FirstChannelOf( Client );
458 while( chan_cl2chan )
460 chan = Channel_GetChannel( chan_cl2chan );
461 cl2chan = Channel_FirstMember( chan );
464 c = Channel_GetClient( cl2chan );
467 if( Client_Conn( c ) <= NONE ) c = NULL;
468 else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
470 if( c ) c = Client_NextHop( c );
472 if( c && ( c != Client ))
474 conn = Client_Conn( c );
475 if( Client_Type( c ) == CLIENT_SERVER ) Conn_SetFlag( conn, SEND_TO_SERVER );
476 else Conn_SetFlag( conn, SEND_TO_USER );
478 cl2chan = Channel_NextMember( chan, cl2chan );
481 chan_cl2chan = Channel_NextChannelOf( Client, chan_cl2chan );
483 Send_Marked_Connections(Prefix, buffer);
484 } /* IRC_WriteStrRelatedPrefix */
487 * Send WALLOPS message.
489 * @param Client The sending client, excluded while forwarding the message.
490 * @param From The (remote) sender of the message.
491 * @param Format Format string.
495 IRC_SendWallops(CLIENT *Client, CLIENT *From, const char *Format, ...)
498 IRC_SendWallops(Client, From, Format, va_alist )
510 va_start(ap, Format);
514 vsnprintf(msg, sizeof(msg), Format, ap);
517 for (to=Client_First(); to != NULL; to=Client_Next(to)) {
518 if (Client_Conn(to) == NONE) /* no local connection */
521 switch (Client_Type(to)) {
523 if (Client_HasMode(to, 'w'))
524 IRC_WriteStrClientPrefix(to, From,
529 IRC_WriteStrClientPrefix(to, From,
534 } /* IRC_SendWallops */
537 * Set a "penalty time" for an IRC client.
539 * Note: penalty times are never set for server links or remote clients!
541 * @param Client The client.
542 * @param Seconds The additional "penalty time" to enforce.
545 IRC_SetPenalty(CLIENT *Client, time_t Seconds)
549 assert(Client != NULL);
552 if (Client_Type(Client) == CLIENT_SERVER)
555 c = Client_Conn(Client);
559 Conn_SetPenalty(c, Seconds);
560 } /* IRC_SetPenalty */
563 Get_Prefix(CLIENT *Target, CLIENT *Client)
565 assert (Target != NULL);
566 assert (Client != NULL);
568 if (Client_Type(Target) == CLIENT_SERVER)
569 return Client_ID(Client);
571 return Client_MaskCloaked(Client);
575 cb_writeStrServersPrefixFlag(CLIENT *Client, CLIENT *Prefix, void *Buffer)
577 IRC_WriteStrClientPrefix(Client, Prefix, "%s", Buffer);
578 } /* cb_writeStrServersPrefixFlag */
581 * Send a message to all marked connections using a specific prefix.
583 * @param Prefix The prefix to use.
584 * @param Buffer The message to send.
587 Send_Marked_Connections(CLIENT *Prefix, const char *Buffer)
591 assert(Prefix != NULL);
592 assert(Buffer != NULL);
595 while (conn != NONE) {
596 if (Conn_Flag(conn) == SEND_TO_SERVER)
597 Conn_WriteStr(conn, ":%s %s",
598 Client_ID(Prefix), Buffer);
599 else if (Conn_Flag(conn) == SEND_TO_USER)
600 Conn_WriteStr(conn, ":%s %s",
601 Client_MaskCloaked(Prefix), Buffer);
602 conn = Conn_Next(conn);