Blame


1 c23199d9 2002-02-27 alex /*
2 c23199d9 2002-02-27 alex * ngIRCd -- The Next Generation IRC Daemon
3 c23199d9 2002-02-27 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 c23199d9 2002-02-27 alex *
5 c23199d9 2002-02-27 alex * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 c23199d9 2002-02-27 alex * der GNU General Public License (GPL), wie von der Free Software Foundation
7 c23199d9 2002-02-27 alex * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 c23199d9 2002-02-27 alex * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 c23199d9 2002-02-27 alex * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 c23199d9 2002-02-27 alex * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11 c23199d9 2002-02-27 alex *
12 117c1df6 2002-03-25 alex * $Id: irc-write.c,v 1.3 2002/03/25 17:13:07 alex Exp $
13 c23199d9 2002-02-27 alex *
14 c23199d9 2002-02-27 alex * irc-write.c: IRC-Texte und Befehle ueber Netzwerk versenden
15 c23199d9 2002-02-27 alex */
16 c23199d9 2002-02-27 alex
17 c23199d9 2002-02-27 alex
18 ca33cbda 2002-03-12 alex #include "portab.h"
19 c23199d9 2002-02-27 alex
20 ca33cbda 2002-03-12 alex #include "imp.h"
21 c23199d9 2002-02-27 alex #include <assert.h>
22 c23199d9 2002-02-27 alex #include <stdarg.h>
23 c23199d9 2002-02-27 alex #include <stdio.h>
24 c23199d9 2002-02-27 alex
25 ca33cbda 2002-03-12 alex #include "defines.h"
26 ca33cbda 2002-03-12 alex
27 ca33cbda 2002-03-12 alex #include "exp.h"
28 c23199d9 2002-02-27 alex #include "irc-write.h"
29 c23199d9 2002-02-27 alex
30 c23199d9 2002-02-27 alex
31 117c1df6 2002-03-25 alex LOCAL CHAR *Get_Prefix( CLIENT *Target, CLIENT *Client );
32 c23199d9 2002-02-27 alex
33 c23199d9 2002-02-27 alex
34 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
35 c23199d9 2002-02-27 alex {
36 c23199d9 2002-02-27 alex CHAR buffer[1000];
37 c23199d9 2002-02-27 alex BOOLEAN ok = CONNECTED;
38 c23199d9 2002-02-27 alex va_list ap;
39 c23199d9 2002-02-27 alex
40 c23199d9 2002-02-27 alex assert( Client != NULL );
41 c23199d9 2002-02-27 alex assert( Format != NULL );
42 c23199d9 2002-02-27 alex
43 c23199d9 2002-02-27 alex va_start( ap, Format );
44 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
45 c23199d9 2002-02-27 alex va_end( ap );
46 c23199d9 2002-02-27 alex
47 c23199d9 2002-02-27 alex /* an den Client selber */
48 c23199d9 2002-02-27 alex ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
49 c23199d9 2002-02-27 alex
50 c23199d9 2002-02-27 alex return ok;
51 c23199d9 2002-02-27 alex } /* IRC_WriteStrClient */
52 c23199d9 2002-02-27 alex
53 c23199d9 2002-02-27 alex
54 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
55 c23199d9 2002-02-27 alex {
56 c23199d9 2002-02-27 alex /* Text an Clients, lokal bzw. remote, senden. */
57 c23199d9 2002-02-27 alex
58 c23199d9 2002-02-27 alex CHAR buffer[1000];
59 c23199d9 2002-02-27 alex va_list ap;
60 c23199d9 2002-02-27 alex
61 c23199d9 2002-02-27 alex assert( Client != NULL );
62 c23199d9 2002-02-27 alex assert( Format != NULL );
63 c23199d9 2002-02-27 alex assert( Prefix != NULL );
64 c23199d9 2002-02-27 alex
65 c23199d9 2002-02-27 alex va_start( ap, Format );
66 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
67 c23199d9 2002-02-27 alex va_end( ap );
68 c23199d9 2002-02-27 alex
69 c23199d9 2002-02-27 alex return Conn_WriteStr( Client_Conn( Client_NextHop( Client )), ":%s %s", Get_Prefix( Client_NextHop( Client ), Prefix ), buffer );
70 c23199d9 2002-02-27 alex } /* IRC_WriteStrClientPrefix */
71 c23199d9 2002-02-27 alex
72 c23199d9 2002-02-27 alex
73 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_WriteStrChannel( CLIENT *Client, CHANNEL *Chan, BOOLEAN Remote, CHAR *Format, ... )
74 c23199d9 2002-02-27 alex {
75 c23199d9 2002-02-27 alex CHAR buffer[1000];
76 c23199d9 2002-02-27 alex va_list ap;
77 c23199d9 2002-02-27 alex
78 c23199d9 2002-02-27 alex assert( Client != NULL );
79 c23199d9 2002-02-27 alex assert( Format != NULL );
80 c23199d9 2002-02-27 alex
81 c23199d9 2002-02-27 alex va_start( ap, Format );
82 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
83 c23199d9 2002-02-27 alex va_end( ap );
84 c23199d9 2002-02-27 alex
85 c23199d9 2002-02-27 alex return IRC_WriteStrChannelPrefix( Client, Chan, Client_ThisServer( ), Remote, buffer );
86 c23199d9 2002-02-27 alex } /* IRC_WriteStrChannel */
87 c23199d9 2002-02-27 alex
88 c23199d9 2002-02-27 alex
89 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_WriteStrChannelPrefix( CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix, BOOLEAN Remote, CHAR *Format, ... )
90 c23199d9 2002-02-27 alex {
91 c23199d9 2002-02-27 alex BOOLEAN sock[MAX_CONNECTIONS], is_server[MAX_CONNECTIONS], ok = CONNECTED;
92 c23199d9 2002-02-27 alex CHAR buffer[1000];
93 c23199d9 2002-02-27 alex CL2CHAN *cl2chan;
94 c23199d9 2002-02-27 alex CLIENT *c;
95 c23199d9 2002-02-27 alex INT s, i;
96 c23199d9 2002-02-27 alex va_list ap;
97 c23199d9 2002-02-27 alex
98 c23199d9 2002-02-27 alex assert( Client != NULL );
99 c23199d9 2002-02-27 alex assert( Chan != NULL );
100 c23199d9 2002-02-27 alex assert( Prefix != NULL );
101 c23199d9 2002-02-27 alex assert( Format != NULL );
102 c23199d9 2002-02-27 alex
103 c23199d9 2002-02-27 alex va_start( ap, Format );
104 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
105 c23199d9 2002-02-27 alex va_end( ap );
106 c23199d9 2002-02-27 alex
107 c23199d9 2002-02-27 alex for( i = 0; i < MAX_CONNECTIONS; i++ ) sock[i] = FALSE;
108 c23199d9 2002-02-27 alex
109 c23199d9 2002-02-27 alex /* An alle Clients, die in den selben Channels sind.
110 c23199d9 2002-02-27 alex * Dabei aber nur einmal je Remote-Server */
111 c23199d9 2002-02-27 alex cl2chan = Channel_FirstMember( Chan );
112 c23199d9 2002-02-27 alex while( cl2chan )
113 c23199d9 2002-02-27 alex {
114 c23199d9 2002-02-27 alex c = Channel_GetClient( cl2chan );
115 c23199d9 2002-02-27 alex if( ! Remote )
116 c23199d9 2002-02-27 alex {
117 c23199d9 2002-02-27 alex if( Client_Conn( c ) <= NONE ) c = NULL;
118 c23199d9 2002-02-27 alex else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
119 c23199d9 2002-02-27 alex }
120 c23199d9 2002-02-27 alex if( c ) c = Client_NextHop( c );
121 c23199d9 2002-02-27 alex
122 c23199d9 2002-02-27 alex if( c && ( c != Client ))
123 c23199d9 2002-02-27 alex {
124 c23199d9 2002-02-27 alex /* Ok, anderer Client */
125 c23199d9 2002-02-27 alex s = Client_Conn( c );
126 c23199d9 2002-02-27 alex assert( s >= 0 );
127 c23199d9 2002-02-27 alex assert( s < MAX_CONNECTIONS );
128 c23199d9 2002-02-27 alex sock[s] = TRUE;
129 c23199d9 2002-02-27 alex if( Client_Type( c ) == CLIENT_SERVER ) is_server[s] = TRUE;
130 c23199d9 2002-02-27 alex else is_server[s] = FALSE;
131 c23199d9 2002-02-27 alex }
132 c23199d9 2002-02-27 alex cl2chan = Channel_NextMember( Chan, cl2chan );
133 c23199d9 2002-02-27 alex }
134 c23199d9 2002-02-27 alex
135 c23199d9 2002-02-27 alex /* Senden ... */
136 c23199d9 2002-02-27 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
137 c23199d9 2002-02-27 alex {
138 c23199d9 2002-02-27 alex if( sock[i] )
139 c23199d9 2002-02-27 alex {
140 c23199d9 2002-02-27 alex if( is_server[i] ) ok = Conn_WriteStr( i, ":%s %s", Client_ID( Prefix ), buffer );
141 c23199d9 2002-02-27 alex else ok = Conn_WriteStr( i, ":%s %s", Client_Mask( Prefix ), buffer );
142 c23199d9 2002-02-27 alex if( ! ok ) break;
143 c23199d9 2002-02-27 alex }
144 c23199d9 2002-02-27 alex }
145 c23199d9 2002-02-27 alex return ok;
146 c23199d9 2002-02-27 alex } /* IRC_WriteStrChannelPrefix */
147 c23199d9 2002-02-27 alex
148 c23199d9 2002-02-27 alex
149 c23199d9 2002-02-27 alex GLOBAL VOID IRC_WriteStrServers( CLIENT *ExceptOf, CHAR *Format, ... )
150 c23199d9 2002-02-27 alex {
151 c23199d9 2002-02-27 alex CHAR buffer[1000];
152 c23199d9 2002-02-27 alex va_list ap;
153 c23199d9 2002-02-27 alex
154 c23199d9 2002-02-27 alex assert( Format != NULL );
155 c23199d9 2002-02-27 alex
156 c23199d9 2002-02-27 alex va_start( ap, Format );
157 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
158 c23199d9 2002-02-27 alex va_end( ap );
159 c23199d9 2002-02-27 alex
160 c23199d9 2002-02-27 alex /* an den Client selber */
161 c23199d9 2002-02-27 alex return IRC_WriteStrServersPrefix( ExceptOf, Client_ThisServer( ), buffer );
162 c23199d9 2002-02-27 alex } /* IRC_WriteStrServers */
163 c23199d9 2002-02-27 alex
164 c23199d9 2002-02-27 alex
165 c23199d9 2002-02-27 alex GLOBAL VOID IRC_WriteStrServersPrefix( CLIENT *ExceptOf, CLIENT *Prefix, CHAR *Format, ... )
166 c23199d9 2002-02-27 alex {
167 c23199d9 2002-02-27 alex CHAR buffer[1000];
168 c23199d9 2002-02-27 alex CLIENT *c;
169 c23199d9 2002-02-27 alex va_list ap;
170 c23199d9 2002-02-27 alex
171 c23199d9 2002-02-27 alex assert( Format != NULL );
172 c23199d9 2002-02-27 alex assert( Prefix != NULL );
173 c23199d9 2002-02-27 alex
174 c23199d9 2002-02-27 alex va_start( ap, Format );
175 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
176 c23199d9 2002-02-27 alex va_end( ap );
177 c23199d9 2002-02-27 alex
178 c23199d9 2002-02-27 alex c = Client_First( );
179 c23199d9 2002-02-27 alex while( c )
180 c23199d9 2002-02-27 alex {
181 c23199d9 2002-02-27 alex if(( Client_Type( c ) == CLIENT_SERVER ) && ( Client_Conn( c ) > NONE ) && ( c != Client_ThisServer( )) && ( c != ExceptOf ))
182 c23199d9 2002-02-27 alex {
183 c23199d9 2002-02-27 alex /* Ziel-Server gefunden */
184 c23199d9 2002-02-27 alex IRC_WriteStrClientPrefix( c, Prefix, buffer );
185 c23199d9 2002-02-27 alex }
186 c23199d9 2002-02-27 alex c = Client_Next( c );
187 c23199d9 2002-02-27 alex }
188 c23199d9 2002-02-27 alex } /* IRC_WriteStrServersPrefix */
189 c23199d9 2002-02-27 alex
190 c23199d9 2002-02-27 alex
191 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_WriteStrRelatedPrefix( CLIENT *Client, CLIENT *Prefix, BOOLEAN Remote, CHAR *Format, ... )
192 c23199d9 2002-02-27 alex {
193 c23199d9 2002-02-27 alex BOOLEAN sock[MAX_CONNECTIONS], is_server[MAX_CONNECTIONS], ok = CONNECTED;
194 c23199d9 2002-02-27 alex CL2CHAN *chan_cl2chan, *cl2chan;
195 c23199d9 2002-02-27 alex CHAR buffer[1000];
196 c23199d9 2002-02-27 alex CHANNEL *chan;
197 c23199d9 2002-02-27 alex va_list ap;
198 c23199d9 2002-02-27 alex CLIENT *c;
199 c23199d9 2002-02-27 alex INT i, s;
200 c23199d9 2002-02-27 alex
201 c23199d9 2002-02-27 alex assert( Client != NULL );
202 c23199d9 2002-02-27 alex assert( Prefix != NULL );
203 c23199d9 2002-02-27 alex assert( Format != NULL );
204 c23199d9 2002-02-27 alex
205 c23199d9 2002-02-27 alex va_start( ap, Format );
206 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
207 c23199d9 2002-02-27 alex va_end( ap );
208 c23199d9 2002-02-27 alex
209 c23199d9 2002-02-27 alex /* initialisieren */
210 c23199d9 2002-02-27 alex for( i = 0; i < MAX_CONNECTIONS; i++ ) sock[i] = FALSE;
211 c23199d9 2002-02-27 alex
212 c23199d9 2002-02-27 alex /* An alle Clients, die in einem Channel mit dem "Ausloeser" sind,
213 c23199d9 2002-02-27 alex * den Text schicken. An Remote-Server aber jeweils nur einmal. */
214 c23199d9 2002-02-27 alex chan_cl2chan = Channel_FirstChannelOf( Client );
215 c23199d9 2002-02-27 alex while( chan_cl2chan )
216 c23199d9 2002-02-27 alex {
217 c23199d9 2002-02-27 alex /* Channel des Users durchsuchen */
218 c23199d9 2002-02-27 alex chan = Channel_GetChannel( chan_cl2chan );
219 c23199d9 2002-02-27 alex cl2chan = Channel_FirstMember( chan );
220 c23199d9 2002-02-27 alex while( cl2chan )
221 c23199d9 2002-02-27 alex {
222 c23199d9 2002-02-27 alex c = Channel_GetClient( cl2chan );
223 c23199d9 2002-02-27 alex if( ! Remote )
224 c23199d9 2002-02-27 alex {
225 c23199d9 2002-02-27 alex if( Client_Conn( c ) <= NONE ) c = NULL;
226 c23199d9 2002-02-27 alex else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
227 c23199d9 2002-02-27 alex }
228 c23199d9 2002-02-27 alex if( c ) c = Client_NextHop( c );
229 c23199d9 2002-02-27 alex
230 c23199d9 2002-02-27 alex if( c && ( c != Client ))
231 c23199d9 2002-02-27 alex {
232 c23199d9 2002-02-27 alex /* Ok, anderer Client */
233 c23199d9 2002-02-27 alex s = Client_Conn( c );
234 c23199d9 2002-02-27 alex assert( s >= 0 );
235 c23199d9 2002-02-27 alex assert( s < MAX_CONNECTIONS );
236 c23199d9 2002-02-27 alex sock[s] = TRUE;
237 c23199d9 2002-02-27 alex if( Client_Type( c ) == CLIENT_SERVER ) is_server[s] = TRUE;
238 c23199d9 2002-02-27 alex else is_server[s] = FALSE;
239 c23199d9 2002-02-27 alex }
240 c23199d9 2002-02-27 alex cl2chan = Channel_NextMember( chan, cl2chan );
241 c23199d9 2002-02-27 alex }
242 c23199d9 2002-02-27 alex
243 c23199d9 2002-02-27 alex /* naechsten Channel */
244 c23199d9 2002-02-27 alex chan_cl2chan = Channel_NextChannelOf( Client, chan_cl2chan );
245 c23199d9 2002-02-27 alex }
246 c23199d9 2002-02-27 alex
247 c23199d9 2002-02-27 alex /* Senden ... */
248 c23199d9 2002-02-27 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
249 c23199d9 2002-02-27 alex {
250 c23199d9 2002-02-27 alex if( sock[i] )
251 c23199d9 2002-02-27 alex {
252 c23199d9 2002-02-27 alex if( is_server[i] ) ok = Conn_WriteStr( i, ":%s %s", Client_ID( Prefix ), buffer );
253 c23199d9 2002-02-27 alex else ok = Conn_WriteStr( i, ":%s %s", Client_Mask( Prefix ), buffer );
254 c23199d9 2002-02-27 alex if( ! ok ) break;
255 c23199d9 2002-02-27 alex }
256 c23199d9 2002-02-27 alex }
257 c23199d9 2002-02-27 alex return ok;
258 c23199d9 2002-02-27 alex } /* IRC_WriteStrRelatedPrefix */
259 c23199d9 2002-02-27 alex
260 c23199d9 2002-02-27 alex
261 117c1df6 2002-03-25 alex LOCAL CHAR *Get_Prefix( CLIENT *Target, CLIENT *Client )
262 c23199d9 2002-02-27 alex {
263 c23199d9 2002-02-27 alex assert( Target != NULL );
264 c23199d9 2002-02-27 alex assert( Client != NULL );
265 c23199d9 2002-02-27 alex
266 c23199d9 2002-02-27 alex if( Client_Type( Target ) == CLIENT_SERVER ) return Client_ID( Client );
267 c23199d9 2002-02-27 alex else return Client_Mask( Client );
268 c23199d9 2002-02-27 alex } /* Get_Prefix */
269 c23199d9 2002-02-27 alex
270 c23199d9 2002-02-27 alex
271 c23199d9 2002-02-27 alex /* -eof- */