X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Firc-write.c;h=8d09f9bcf8eb271672d32de78199fd18e41e26a3;hp=16aac9a081859aa27904e116252d009ba02d3176;hb=b8482fd3cfdb429aec75575958f4d5d4e9ae22df;hpb=2a7dd06ebd9cc72d45a6a4becdbef5213d7b7800 diff --git a/src/ngircd/irc-write.c b/src/ngircd/irc-write.c index 16aac9a0..8d09f9bc 100644 --- a/src/ngircd/irc-write.c +++ b/src/ngircd/irc-write.c @@ -1,6 +1,6 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001-2008 Alexander Barton (alex@barton.de) + * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,7 +16,6 @@ * Sending IRC commands over the network */ -#include "imp.h" #include #ifdef PROTOTYPES # include @@ -24,26 +23,64 @@ # include #endif #include -#include -#include "defines.h" #include "conn-func.h" #include "channel.h" -#include "exp.h" #include "irc-write.h" - #define SEND_TO_USER 1 #define SEND_TO_SERVER 2 - static const char *Get_Prefix PARAMS((CLIENT *Target, CLIENT *Client)); static void cb_writeStrServersPrefixFlag PARAMS((CLIENT *Client, CLIENT *Prefix, void *Buffer)); -static bool Send_Marked_Connections PARAMS((CLIENT *Prefix, const char *Buffer)); +static void Send_Marked_Connections PARAMS((CLIENT *Prefix, const char *Buffer)); + +/** + * Send an error message to a client and enforce a penalty time. + * + * @param Client The target client. + * @param Format Format string. + * @return CONNECTED or DISCONNECTED. + */ +#ifdef PROTOTYPES +GLOBAL bool +IRC_WriteErrClient( CLIENT *Client, const char *Format, ... ) +#else +GLOBAL bool +IRC_WriteErrClient( Client, Format, va_alist ) +CLIENT *Client; +const char *Format; +va_dcl +#endif +{ + char buffer[1000]; + va_list ap; + + assert(Client != NULL); + assert(Format != NULL); + +#ifdef PROTOTYPES + va_start(ap, Format); +#else + va_start(ap); +#endif + vsnprintf(buffer, sizeof(buffer), Format, ap); + va_end(ap); + IRC_SetPenalty(Client, 2); + return IRC_WriteStrClientPrefix(Client, Client_ThisServer(), + "%s", buffer); +} +/** + * Send a message to a client. + * + * @param Client The target client. + * @param Format Format string. + * @return CONNECTED or DISCONNECTED. + */ #ifdef PROTOTYPES GLOBAL bool IRC_WriteStrClient( CLIENT *Client, const char *Format, ... ) @@ -56,27 +93,31 @@ va_dcl #endif { char buffer[1000]; - bool ok = CONNECTED; va_list ap; - assert( Client != NULL ); - assert( Format != NULL ); + assert(Client != NULL); + assert(Format != NULL); #ifdef PROTOTYPES - va_start( ap, Format ); + va_start(ap, Format); #else - va_start( ap ); + va_start(ap); #endif - vsnprintf( buffer, 1000, Format, ap ); - va_end( ap ); - - /* to the client itself */ - ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), "%s", buffer ); - - return ok; -} /* IRC_WriteStrClient */ + vsnprintf(buffer, sizeof(buffer), Format, ap); + va_end(ap); + return IRC_WriteStrClientPrefix(Client, Client_ThisServer(), + "%s", buffer); +} +/** + * Send a message to a client using a specific prefix. + * + * @param Client The target client. + * @param Prefix The prefix to use. + * @param Format Format string. + * @return CONNECTED or DISCONNECTED. + */ #ifdef PROTOTYPES GLOBAL bool IRC_WriteStrClientPrefix(CLIENT *Client, CLIENT *Prefix, const char *Format, ...) @@ -103,20 +144,29 @@ va_dcl #else va_start( ap ); #endif - vsnprintf( buffer, 1000, Format, ap ); + vsnprintf(buffer, sizeof(buffer), Format, ap); va_end( ap ); return Conn_WriteStr(Client_Conn(Client_NextHop(Client)), ":%s %s", Get_Prefix(Client_NextHop(Client), Prefix), buffer); -} /* IRC_WriteStrClientPrefix */ - +} +/** + * Send a message to all client in a channel. + * + * The message is only sent once per remote server. + * + * @param Client The sending client, excluded while forwarding the message. + * @param Channel The target channel. + * @param Remote If not set, the message is sent to local clients only. + * @param Format Format string. + */ #ifdef PROTOTYPES -GLOBAL bool +GLOBAL void IRC_WriteStrChannel(CLIENT *Client, CHANNEL *Chan, bool Remote, const char *Format, ...) #else -GLOBAL bool +GLOBAL void IRC_WriteStrChannel(Client, Chan, Remote, Format, va_alist) CLIENT *Client; CHANNEL *Chan; @@ -136,24 +186,30 @@ va_dcl #else va_start( ap ); #endif - vsnprintf( buffer, 1000, Format, ap ); + vsnprintf(buffer, sizeof(buffer), Format, ap); va_end( ap ); - return IRC_WriteStrChannelPrefix( Client, Chan, Client_ThisServer( ), Remote, "%s", buffer ); -} /* IRC_WriteStrChannel */ - - + IRC_WriteStrChannelPrefix(Client, Chan, Client_ThisServer(), + Remote, "%s", buffer); +} /** - * send message to all clients in the same channel, but only send message - * once per remote server. + * Send a message to all client in a channel using a specific prefix. + * + * The message is only sent once per remote server. + * + * @param Client The sending client, excluded while forwarding the message. + * @param Channel The target channel. + * @param Prefix The prefix to use. + * @param Remote If not set, the message is sent to local clients only. + * @param Format Format string. */ #ifdef PROTOTYPES -GLOBAL bool +GLOBAL void IRC_WriteStrChannelPrefix(CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix, bool Remote, const char *Format, ...) #else -GLOBAL bool +GLOBAL void IRC_WriteStrChannelPrefix(Client, Chan, Prefix, Remote, Format, va_alist) CLIENT *Client; CHANNEL *Chan; @@ -179,35 +235,42 @@ va_dcl #else va_start( ap ); #endif - vsnprintf( buffer, 1000, Format, ap ); + vsnprintf(buffer, sizeof(buffer), Format, ap); va_end( ap ); Conn_ClearFlags( ); cl2chan = Channel_FirstMember( Chan ); - while( cl2chan ) - { + while(cl2chan) { c = Channel_GetClient( cl2chan ); - if( ! Remote ) - { - if( Client_Conn( c ) <= NONE ) c = NULL; - else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL; + if (!Remote) { + if (Client_Conn(c) <= NONE) + c = NULL; + else if(Client_Type(c) == CLIENT_SERVER) + c = NULL; } - if( c ) c = Client_NextHop( c ); + if(c) + c = Client_NextHop(c); - if( c && ( c != Client )) - { + if(c && c != Client) { /* Ok, another Client */ - conn = Client_Conn( c ); - if( Client_Type( c ) == CLIENT_SERVER ) Conn_SetFlag( conn, SEND_TO_SERVER ); - else Conn_SetFlag( conn, SEND_TO_USER ); + conn = Client_Conn(c); + if (Client_Type(c) == CLIENT_SERVER) + Conn_SetFlag(conn, SEND_TO_SERVER); + else + Conn_SetFlag(conn, SEND_TO_USER); } - cl2chan = Channel_NextMember( Chan, cl2chan ); + cl2chan = Channel_NextMember(Chan, cl2chan); } - return Send_Marked_Connections(Prefix, buffer); -} /* IRC_WriteStrChannelPrefix */ - + Send_Marked_Connections(Prefix, buffer); +} +/** + * Send a message to all the servers in the network. + * + * @param Client The sending client, excluded while forwarding the message. + * @param Format Format string. + */ #ifdef PROTOTYPES GLOBAL void IRC_WriteStrServers(CLIENT *ExceptOf, const char *Format, ...) @@ -229,13 +292,19 @@ va_dcl #else va_start( ap ); #endif - vsnprintf( buffer, 1000, Format, ap ); + vsnprintf(buffer, sizeof(buffer), Format, ap); va_end( ap ); - IRC_WriteStrServersPrefix( ExceptOf, Client_ThisServer( ), "%s", buffer ); -} /* IRC_WriteStrServers */ - + IRC_WriteStrServersPrefix(ExceptOf, Client_ThisServer(), "%s", buffer); +} +/** + * Send a message to all the servers in the network using a specific prefix. + * + * @param Client The sending client, excluded while forwarding the message. + * @param Prefix The prefix to use. + * @param Format Format string. + */ #ifdef PROTOTYPES GLOBAL void IRC_WriteStrServersPrefix(CLIENT *ExceptOf, CLIENT *Prefix, @@ -260,13 +329,21 @@ va_dcl #else va_start( ap ); #endif - vsnprintf( buffer, 1000, Format, ap ); + vsnprintf(buffer, sizeof(buffer), Format, ap); va_end( ap ); IRC_WriteStrServersPrefixFlag( ExceptOf, Prefix, '\0', "%s", buffer ); -} /* IRC_WriteStrServersPrefix */ - +} +/** + * Send a message to all the servers in the network using a specific prefix + * and matching a "client flag". + * + * @param Client The sending client, excluded while forwarding the message. + * @param Prefix The prefix to use. + * @param Flag Client flag that must be set on the target. + * @param Format Format string. + */ #ifdef PROTOTYPES GLOBAL void IRC_WriteStrServersPrefixFlag(CLIENT *ExceptOf, CLIENT *Prefix, char Flag, @@ -292,14 +369,23 @@ va_dcl #else va_start( ap ); #endif - vsnprintf( buffer, 1000, Format, ap ); + vsnprintf(buffer, sizeof(buffer), Format, ap); va_end( ap ); IRC_WriteStrServersPrefixFlag_CB(ExceptOf, Prefix, Flag, cb_writeStrServersPrefixFlag, buffer); -} /* IRC_WriteStrServersPrefixFlag */ - +} +/** + * Send a message to all the servers in the network using a specific prefix + * and matching a "client flag" using a callback function. + * + * @param Client The sending client, excluded while forwarding the message. + * @param Prefix The prefix to use. + * @param Flag Client flag that must be set on the target. + * @param callback Callback function. + * @param Format Format string. + */ GLOBAL void IRC_WriteStrServersPrefixFlag_CB(CLIENT *ExceptOf, CLIENT *Prefix, char Flag, void (*callback)(CLIENT *, CLIENT *, void *), void *cb_data) @@ -311,24 +397,32 @@ IRC_WriteStrServersPrefixFlag_CB(CLIENT *ExceptOf, CLIENT *Prefix, char Flag, if (Client_Type(c) == CLIENT_SERVER && Client_Conn(c) > NONE && c != Client_ThisServer() && c != ExceptOf) { /* Found a target server, do the flags match? */ - if (Flag == '\0' || strchr(Client_Flags(c), Flag)) + if (Flag == '\0' || Client_HasFlag(c, Flag)) callback(c, Prefix, cb_data); } c = Client_Next(c); } -} /* IRC_WriteStrServersPrefixFlag */ - +} /** - * send message to all clients that are in the same channels as the client sending this message. - * only send message once per reote server. + * Send a message to all "related" clients. + * + * Related clients are the one that share one ore more channels with the client + * sending this message. + * + * The message is only sent once per remote server. + * + * @param Client The sending client, excluded while forwarding the message. + * @param Prefix The prefix to use. + * @param Remote If not set, the message is sent to local clients only. + * @param Format Format string. */ #ifdef PROTOTYPES -GLOBAL bool +GLOBAL void IRC_WriteStrRelatedPrefix(CLIENT *Client, CLIENT *Prefix, bool Remote, const char *Format, ...) #else -GLOBAL bool +GLOBAL void IRC_WriteStrRelatedPrefix(Client, Prefix, Remote, Format, va_alist) CLIENT *Client; CLIENT *Prefix; @@ -353,7 +447,7 @@ va_dcl #else va_start( ap ); #endif - vsnprintf( buffer, 1000, Format, ap ); + vsnprintf(buffer, sizeof(buffer), Format, ap); va_end( ap ); Conn_ClearFlags( ); @@ -384,13 +478,16 @@ va_dcl chan_cl2chan = Channel_NextChannelOf( Client, chan_cl2chan ); } - return Send_Marked_Connections(Prefix, buffer); + Send_Marked_Connections(Prefix, buffer); } /* IRC_WriteStrRelatedPrefix */ - /** * Send WALLOPS message. - */ + * + * @param Client The sending client, excluded while forwarding the message. + * @param From The (remote) sender of the message. + * @param Format Format string. +*/ #ifdef PROTOTYPES GLOBAL void IRC_SendWallops(CLIENT *Client, CLIENT *From, const char *Format, ...) @@ -412,7 +509,7 @@ va_dcl #else va_start(ap); #endif - vsnprintf(msg, 1000, Format, ap); + vsnprintf(msg, sizeof(msg), Format, ap); va_end(ap); for (to=Client_First(); to != NULL; to=Client_Next(to)) { @@ -434,22 +531,31 @@ va_dcl } } /* IRC_SendWallops */ - +/** + * Set a "penalty time" for an IRC client. + * + * Note: penalty times are never set for server links or remote clients! + * + * @param Client The client. + * @param Seconds The additional "penalty time" to enforce. + */ GLOBAL void -IRC_SetPenalty( CLIENT *Client, time_t Seconds ) +IRC_SetPenalty(CLIENT *Client, time_t Seconds) { CONN_ID c; - assert( Client != NULL ); - assert( Seconds > 0 ); + assert(Client != NULL); + assert(Seconds > 0); - if( Client_Type( Client ) == CLIENT_SERVER ) return; + if (Client_Type(Client) == CLIENT_SERVER) + return; - c = Client_Conn( Client ); - if (c > NONE) - Conn_SetPenalty(c, Seconds); -} /* IRC_SetPenalty */ + c = Client_Conn(Client); + if (c <= NONE) + return; + Conn_SetPenalty(c, Seconds); +} /* IRC_SetPenalty */ static const char * Get_Prefix(CLIENT *Target, CLIENT *Client) @@ -463,19 +569,22 @@ Get_Prefix(CLIENT *Target, CLIENT *Client) return Client_MaskCloaked(Client); } /* Get_Prefix */ - static void cb_writeStrServersPrefixFlag(CLIENT *Client, CLIENT *Prefix, void *Buffer) { IRC_WriteStrClientPrefix(Client, Prefix, "%s", Buffer); } /* cb_writeStrServersPrefixFlag */ - -static bool +/** + * Send a message to all marked connections using a specific prefix. + * + * @param Prefix The prefix to use. + * @param Buffer The message to send. + */ +static void Send_Marked_Connections(CLIENT *Prefix, const char *Buffer) { CONN_ID conn; - bool ok = CONNECTED; assert(Prefix != NULL); assert(Buffer != NULL); @@ -483,17 +592,13 @@ Send_Marked_Connections(CLIENT *Prefix, const char *Buffer) conn = Conn_First(); while (conn != NONE) { if (Conn_Flag(conn) == SEND_TO_SERVER) - ok = Conn_WriteStr(conn, ":%s %s", - Client_ID(Prefix), Buffer); + Conn_WriteStr(conn, ":%s %s", + Client_ID(Prefix), Buffer); else if (Conn_Flag(conn) == SEND_TO_USER) - ok = Conn_WriteStr(conn, ":%s %s", - Client_MaskCloaked(Prefix), Buffer); - if (!ok) - break; - conn = Conn_Next( conn ); + Conn_WriteStr(conn, ":%s %s", + Client_MaskCloaked(Prefix), Buffer); + conn = Conn_Next(conn); } - return ok; } - /* -eof- */