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
30 #include "conn-func.h"
34 #include "irc-write.h"
36 #define SEND_TO_USER 1
37 #define SEND_TO_SERVER 2
39 static const char *Get_Prefix PARAMS((CLIENT *Target, CLIENT *Client));
40 static void cb_writeStrServersPrefixFlag PARAMS((CLIENT *Client,
41 CLIENT *Prefix, void *Buffer));
42 static void Send_Marked_Connections PARAMS((CLIENT *Prefix, const char *Buffer));
45 * Send an error message to a client and enforce a penalty time.
47 * @param Client The target client.
48 * @param Format Format string.
49 * @return CONNECTED or DISCONNECTED.
53 IRC_WriteErrClient( CLIENT *Client, const char *Format, ... )
56 IRC_WriteErrClient( Client, Format, va_alist )
65 assert(Client != NULL);
66 assert(Format != NULL);
73 vsnprintf(buffer, 1000, Format, ap);
76 IRC_SetPenalty(Client, 2);
77 return IRC_WriteStrClientPrefix(Client, Client_ThisServer(),
82 * Send a message to a client.
84 * @param Client The target client.
85 * @param Format Format string.
86 * @return CONNECTED or DISCONNECTED.
90 IRC_WriteStrClient( CLIENT *Client, const char *Format, ... )
93 IRC_WriteStrClient( Client, Format, va_alist )
102 assert(Client != NULL);
103 assert(Format != NULL);
106 va_start(ap, Format);
110 vsnprintf(buffer, 1000, Format, ap);
113 return IRC_WriteStrClientPrefix(Client, Client_ThisServer(),
118 * Send a message to a client using a specific prefix.
120 * @param Client The target client.
121 * @param Prefix The prefix to use.
122 * @param Format Format string.
123 * @return CONNECTED or DISCONNECTED.
127 IRC_WriteStrClientPrefix(CLIENT *Client, CLIENT *Prefix, const char *Format, ...)
130 IRC_WriteStrClientPrefix(Client, Prefix, Format, va_alist)
137 /* send text to local and remote clients */
142 assert( Client != NULL );
143 assert( Format != NULL );
144 assert( Prefix != NULL );
147 va_start( ap, Format );
151 vsnprintf( buffer, 1000, Format, ap );
154 return Conn_WriteStr(Client_Conn(Client_NextHop(Client)), ":%s %s",
155 Get_Prefix(Client_NextHop(Client), Prefix), buffer);
159 * Send a message to all client in a channel.
161 * The message is only sent once per remote server.
163 * @param Client The sending client, excluded while forwarding the message.
164 * @param Channel The target channel.
165 * @param Remote If not set, the message is sent to local clients only.
166 * @param Format Format string.
170 IRC_WriteStrChannel(CLIENT *Client, CHANNEL *Chan, bool Remote,
171 const char *Format, ...)
174 IRC_WriteStrChannel(Client, Chan, Remote, Format, va_alist)
185 assert( Client != NULL );
186 assert( Format != NULL );
189 va_start( ap, Format );
193 vsnprintf( buffer, 1000, Format, ap );
196 IRC_WriteStrChannelPrefix(Client, Chan, Client_ThisServer(),
197 Remote, "%s", buffer);
201 * Send a message to all client in a channel using a specific prefix.
203 * The message is only sent once per remote server.
205 * @param Client The sending client, excluded while forwarding the message.
206 * @param Channel The target channel.
207 * @param Prefix The prefix to use.
208 * @param Remote If not set, the message is sent to local clients only.
209 * @param Format Format string.
213 IRC_WriteStrChannelPrefix(CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix,
214 bool Remote, const char *Format, ...)
217 IRC_WriteStrChannelPrefix(Client, Chan, Prefix, Remote, Format, va_alist)
232 assert( Client != NULL );
233 assert( Chan != NULL );
234 assert( Prefix != NULL );
235 assert( Format != NULL );
238 va_start( ap, Format );
242 vsnprintf( buffer, 1000, Format, ap );
247 cl2chan = Channel_FirstMember( Chan );
249 c = Channel_GetClient( cl2chan );
251 if (Client_Conn(c) <= NONE)
253 else if(Client_Type(c) == CLIENT_SERVER)
257 c = Client_NextHop(c);
259 if(c && c != Client) {
260 /* Ok, another Client */
261 conn = Client_Conn(c);
262 if (Client_Type(c) == CLIENT_SERVER)
263 Conn_SetFlag(conn, SEND_TO_SERVER);
265 Conn_SetFlag(conn, SEND_TO_USER);
267 cl2chan = Channel_NextMember(Chan, cl2chan);
269 Send_Marked_Connections(Prefix, buffer);
273 * Send a message to all the servers in the network.
275 * @param Client The sending client, excluded while forwarding the message.
276 * @param Format Format string.
280 IRC_WriteStrServers(CLIENT *ExceptOf, const char *Format, ...)
283 IRC_WriteStrServers(ExceptOf, Format, va_alist)
292 assert( Format != NULL );
295 va_start( ap, Format );
299 vsnprintf( buffer, 1000, Format, ap );
302 IRC_WriteStrServersPrefix(ExceptOf, Client_ThisServer(), "%s", buffer);
306 * Send a message to all the servers in the network using a specific prefix.
308 * @param Client The sending client, excluded while forwarding the message.
309 * @param Prefix The prefix to use.
310 * @param Format Format string.
314 IRC_WriteStrServersPrefix(CLIENT *ExceptOf, CLIENT *Prefix,
315 const char *Format, ...)
318 IRC_WriteStrServersPrefix(ExceptOf, Prefix, Format, va_alist)
328 assert( Format != NULL );
329 assert( Prefix != NULL );
332 va_start( ap, Format );
336 vsnprintf( buffer, 1000, Format, ap );
339 IRC_WriteStrServersPrefixFlag( ExceptOf, Prefix, '\0', "%s", buffer );
343 * Send a message to all the servers in the network using a specific prefix
344 * and matching a "client flag".
346 * @param Client The sending client, excluded while forwarding the message.
347 * @param Prefix The prefix to use.
348 * @param Flag Client flag that must be set on the target.
349 * @param Format Format string.
353 IRC_WriteStrServersPrefixFlag(CLIENT *ExceptOf, CLIENT *Prefix, char Flag,
354 const char *Format, ...)
357 IRC_WriteStrServersPrefixFlag(ExceptOf, Prefix, Flag, Format, va_alist)
368 assert( Format != NULL );
369 assert( Prefix != NULL );
372 va_start( ap, Format );
376 vsnprintf( buffer, 1000, Format, ap );
379 IRC_WriteStrServersPrefixFlag_CB(ExceptOf, Prefix, Flag,
380 cb_writeStrServersPrefixFlag, buffer);
384 * Send a message to all the servers in the network using a specific prefix
385 * and matching a "client flag" using a callback function.
387 * @param Client The sending client, excluded while forwarding the message.
388 * @param Prefix The prefix to use.
389 * @param Flag Client flag that must be set on the target.
390 * @param callback Callback function.
391 * @param Format Format string.
394 IRC_WriteStrServersPrefixFlag_CB(CLIENT *ExceptOf, CLIENT *Prefix, char Flag,
395 void (*callback)(CLIENT *, CLIENT *, void *), void *cb_data)
401 if (Client_Type(c) == CLIENT_SERVER && Client_Conn(c) > NONE &&
402 c != Client_ThisServer() && c != ExceptOf) {
403 /* Found a target server, do the flags match? */
404 if (Flag == '\0' || Client_HasFlag(c, Flag))
405 callback(c, Prefix, cb_data);
412 * Send a message to all "related" clients.
414 * Related clients are the one that share one ore more channels with the client
415 * sending this message.
417 * The message is only sent once per remote server.
419 * @param Client The sending client, excluded while forwarding the message.
420 * @param Prefix The prefix to use.
421 * @param Remote If not set, the message is sent to local clients only.
422 * @param Format Format string.
426 IRC_WriteStrRelatedPrefix(CLIENT *Client, CLIENT *Prefix, bool Remote,
427 const char *Format, ...)
430 IRC_WriteStrRelatedPrefix(Client, Prefix, Remote, Format, va_alist)
438 CL2CHAN *chan_cl2chan, *cl2chan;
445 assert( Client != NULL );
446 assert( Prefix != NULL );
447 assert( Format != NULL );
450 va_start( ap, Format );
454 vsnprintf( buffer, 1000, Format, ap );
459 chan_cl2chan = Channel_FirstChannelOf( Client );
460 while( chan_cl2chan )
462 chan = Channel_GetChannel( chan_cl2chan );
463 cl2chan = Channel_FirstMember( chan );
466 c = Channel_GetClient( cl2chan );
469 if( Client_Conn( c ) <= NONE ) c = NULL;
470 else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
472 if( c ) c = Client_NextHop( c );
474 if( c && ( c != Client ))
476 conn = Client_Conn( c );
477 if( Client_Type( c ) == CLIENT_SERVER ) Conn_SetFlag( conn, SEND_TO_SERVER );
478 else Conn_SetFlag( conn, SEND_TO_USER );
480 cl2chan = Channel_NextMember( chan, cl2chan );
483 chan_cl2chan = Channel_NextChannelOf( Client, chan_cl2chan );
485 Send_Marked_Connections(Prefix, buffer);
486 } /* IRC_WriteStrRelatedPrefix */
489 * Send WALLOPS message.
491 * @param Client The sending client, excluded while forwarding the message.
492 * @param From The (remote) sender of the message.
493 * @param Format Format string.
497 IRC_SendWallops(CLIENT *Client, CLIENT *From, const char *Format, ...)
500 IRC_SendWallops(Client, From, Format, va_alist )
512 va_start(ap, Format);
516 vsnprintf(msg, 1000, Format, ap);
519 for (to=Client_First(); to != NULL; to=Client_Next(to)) {
520 if (Client_Conn(to) == NONE) /* no local connection */
523 switch (Client_Type(to)) {
525 if (Client_HasMode(to, 'w'))
526 IRC_WriteStrClientPrefix(to, From,
531 IRC_WriteStrClientPrefix(to, From,
536 } /* IRC_SendWallops */
539 * Set a "penalty time" for an IRC client.
541 * Note: penalty times are never set for server links!
543 * @param Client The client.
544 * @param Seconds The additional "penalty time" to enforce.
547 IRC_SetPenalty( CLIENT *Client, time_t Seconds )
551 assert( Client != NULL );
552 assert( Seconds > 0 );
554 if( Client_Type( Client ) == CLIENT_SERVER ) return;
556 c = Client_Conn( Client );
558 Conn_SetPenalty(c, Seconds);
559 } /* IRC_SetPenalty */
562 Get_Prefix(CLIENT *Target, CLIENT *Client)
564 assert (Target != NULL);
565 assert (Client != NULL);
567 if (Client_Type(Target) == CLIENT_SERVER)
568 return Client_ID(Client);
570 return Client_MaskCloaked(Client);
574 cb_writeStrServersPrefixFlag(CLIENT *Client, CLIENT *Prefix, void *Buffer)
576 IRC_WriteStrClientPrefix(Client, Prefix, "%s", Buffer);
577 } /* cb_writeStrServersPrefixFlag */
580 * Send a message to all marked connections using a specific prefix.
582 * @param Prefix The prefix to use.
583 * @param Buffer The message to send.
586 Send_Marked_Connections(CLIENT *Prefix, const char *Buffer)
590 assert(Prefix != NULL);
591 assert(Buffer != NULL);
594 while (conn != NONE) {
595 if (Conn_Flag(conn) == SEND_TO_SERVER)
596 Conn_WriteStr(conn, ":%s %s",
597 Client_ID(Prefix), Buffer);
598 else if (Conn_Flag(conn) == SEND_TO_USER)
599 Conn_WriteStr(conn, ":%s %s",
600 Client_MaskCloaked(Prefix), Buffer);
601 conn = Conn_Next(conn);