1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* The IRC-based UI - Sending responses to commands/etc. */ |
---|
8 | |
---|
9 | /* |
---|
10 | This program is free software; you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include "bitlbee.h" |
---|
27 | |
---|
28 | void irc_send_num( irc_t *irc, int code, char *format, ... ) |
---|
29 | { |
---|
30 | char text[IRC_MAX_LINE]; |
---|
31 | va_list params; |
---|
32 | |
---|
33 | va_start( params, format ); |
---|
34 | g_vsnprintf( text, IRC_MAX_LINE, format, params ); |
---|
35 | va_end( params ); |
---|
36 | |
---|
37 | irc_write( irc, ":%s %03d %s %s", irc->root->host, code, irc->user->nick ? : "*", text ); |
---|
38 | } |
---|
39 | |
---|
40 | void irc_send_login( irc_t *irc ) |
---|
41 | { |
---|
42 | irc_send_num( irc, 1, ":Welcome to the BitlBee gateway, %s", irc->user->nick ); |
---|
43 | irc_send_num( irc, 2, ":Host %s is running BitlBee " BITLBEE_VERSION " " ARCH "/" CPU ".", irc->root->host ); |
---|
44 | irc_send_num( irc, 3, ":%s", IRCD_INFO ); |
---|
45 | irc_send_num( irc, 4, "%s %s %s %s", irc->root->host, BITLBEE_VERSION, UMODES UMODES_PRIV, CMODES ); |
---|
46 | irc_send_num( irc, 5, "PREFIX=(ohv)@%%+ CHANTYPES=%s CHANMODES=,,,%s NICKLEN=%d CHANNELLEN=%d " |
---|
47 | "NETWORK=BitlBee SAFELIST CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 " |
---|
48 | ":are supported by this server", |
---|
49 | CTYPES, CMODES, MAX_NICK_LENGTH - 1, MAX_NICK_LENGTH - 1 ); |
---|
50 | irc_send_motd( irc ); |
---|
51 | } |
---|
52 | |
---|
53 | void irc_send_motd( irc_t *irc ) |
---|
54 | { |
---|
55 | char motd[2048]; |
---|
56 | size_t len; |
---|
57 | int fd; |
---|
58 | |
---|
59 | fd = open( global.conf->motdfile, O_RDONLY ); |
---|
60 | if( fd == -1 || ( len = read( fd, motd, sizeof( motd ) - 1 ) ) <= 0 ) |
---|
61 | { |
---|
62 | irc_send_num( irc, 422, ":We don't need MOTDs." ); |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | char linebuf[80]; |
---|
67 | char *add = "", max, *in; |
---|
68 | |
---|
69 | in = motd; |
---|
70 | motd[len] = '\0'; |
---|
71 | linebuf[79] = len = 0; |
---|
72 | max = sizeof( linebuf ) - 1; |
---|
73 | |
---|
74 | irc_send_num( irc, 375, ":- %s Message Of The Day - ", irc->root->host ); |
---|
75 | while( ( linebuf[len] = *(in++) ) ) |
---|
76 | { |
---|
77 | if( linebuf[len] == '\n' || len == max ) |
---|
78 | { |
---|
79 | linebuf[len] = 0; |
---|
80 | irc_send_num( irc, 372, ":- %s", linebuf ); |
---|
81 | len = 0; |
---|
82 | } |
---|
83 | else if( linebuf[len] == '%' ) |
---|
84 | { |
---|
85 | linebuf[len] = *(in++); |
---|
86 | if( linebuf[len] == 'h' ) |
---|
87 | add = irc->root->host; |
---|
88 | else if( linebuf[len] == 'v' ) |
---|
89 | add = BITLBEE_VERSION; |
---|
90 | else if( linebuf[len] == 'n' ) |
---|
91 | add = irc->user->nick; |
---|
92 | else if( linebuf[len] == '\0' ) |
---|
93 | in --; |
---|
94 | else |
---|
95 | add = "%"; |
---|
96 | |
---|
97 | strncpy( linebuf + len, add, max - len ); |
---|
98 | while( linebuf[++len] ); |
---|
99 | } |
---|
100 | else if( len < max ) |
---|
101 | { |
---|
102 | len ++; |
---|
103 | } |
---|
104 | } |
---|
105 | irc_send_num( irc, 376, ":End of MOTD" ); |
---|
106 | } |
---|
107 | |
---|
108 | if( fd != -1 ) |
---|
109 | close( fd ); |
---|
110 | } |
---|
111 | |
---|
112 | void irc_usermsg( irc_t *irc, char *format, ... ) |
---|
113 | { |
---|
114 | irc_channel_t *ic = NULL; |
---|
115 | irc_user_t *iu = irc->root; |
---|
116 | char text[1100]; |
---|
117 | va_list params; |
---|
118 | char *dst; |
---|
119 | |
---|
120 | va_start( params, format ); |
---|
121 | g_vsnprintf( text, sizeof( text ), format, params ); |
---|
122 | va_end( params ); |
---|
123 | |
---|
124 | /* Too similar to bee_irc_user_msg()... */ |
---|
125 | if( iu->last_channel ) |
---|
126 | { |
---|
127 | if( iu->last_channel->flags & IRC_CHANNEL_JOINED ) |
---|
128 | ic = iu->last_channel; |
---|
129 | else if( irc->default_channel->flags & IRC_CHANNEL_JOINED ) |
---|
130 | ic = irc->default_channel; |
---|
131 | } |
---|
132 | |
---|
133 | if( ic ) |
---|
134 | dst = ic->name; |
---|
135 | else |
---|
136 | dst = irc->user->nick; |
---|
137 | |
---|
138 | irc_send_msg( irc->root, "PRIVMSG", dst, text, NULL ); |
---|
139 | } |
---|
140 | |
---|
141 | void irc_send_join( irc_channel_t *ic, irc_user_t *iu ) |
---|
142 | { |
---|
143 | irc_t *irc = ic->irc; |
---|
144 | |
---|
145 | irc_write( irc, ":%s!%s@%s JOIN :%s", iu->nick, iu->user, iu->host, ic->name ); |
---|
146 | |
---|
147 | if( iu == irc->user ) |
---|
148 | { |
---|
149 | irc_write( irc, ":%s MODE %s +%s", irc->root->host, ic->name, ic->mode ); |
---|
150 | irc_send_names( ic ); |
---|
151 | if( ic->topic && *ic->topic ) |
---|
152 | irc_send_topic( ic, FALSE ); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | void irc_send_part( irc_channel_t *ic, irc_user_t *iu, const char *reason ) |
---|
157 | { |
---|
158 | irc_write( ic->irc, ":%s!%s@%s PART %s :%s", iu->nick, iu->user, iu->host, ic->name, reason ? : "" ); |
---|
159 | } |
---|
160 | |
---|
161 | void irc_send_quit( irc_user_t *iu, const char *reason ) |
---|
162 | { |
---|
163 | irc_write( iu->irc, ":%s!%s@%s QUIT :%s", iu->nick, iu->user, iu->host, reason ? : "" ); |
---|
164 | } |
---|
165 | |
---|
166 | void irc_send_kick( irc_channel_t *ic, irc_user_t *iu, irc_user_t *kicker, const char *reason ) |
---|
167 | { |
---|
168 | irc_write( ic->irc, ":%s!%s@%s KICK %s %s :%s", kicker->nick, kicker->user, |
---|
169 | kicker->host, ic->name, iu->nick, reason ? : "" ); |
---|
170 | } |
---|
171 | |
---|
172 | void irc_send_names( irc_channel_t *ic ) |
---|
173 | { |
---|
174 | GSList *l; |
---|
175 | char namelist[385] = ""; |
---|
176 | |
---|
177 | /* RFCs say there is no error reply allowed on NAMES, so when the |
---|
178 | channel is invalid, just give an empty reply. */ |
---|
179 | for( l = ic->users; l; l = l->next ) |
---|
180 | { |
---|
181 | irc_channel_user_t *icu = l->data; |
---|
182 | irc_user_t *iu = icu->iu; |
---|
183 | |
---|
184 | if( strlen( namelist ) + strlen( iu->nick ) > sizeof( namelist ) - 4 ) |
---|
185 | { |
---|
186 | irc_send_num( ic->irc, 353, "= %s :%s", ic->name, namelist ); |
---|
187 | *namelist = 0; |
---|
188 | } |
---|
189 | |
---|
190 | if( icu->flags & IRC_CHANNEL_USER_OP ) |
---|
191 | strcat( namelist, "@" ); |
---|
192 | else if( icu->flags & IRC_CHANNEL_USER_HALFOP ) |
---|
193 | strcat( namelist, "%" ); |
---|
194 | else if( icu->flags & IRC_CHANNEL_USER_VOICE ) |
---|
195 | strcat( namelist, "+" ); |
---|
196 | |
---|
197 | strcat( namelist, iu->nick ); |
---|
198 | strcat( namelist, " " ); |
---|
199 | } |
---|
200 | |
---|
201 | if( *namelist ) |
---|
202 | irc_send_num( ic->irc, 353, "= %s :%s", ic->name, namelist ); |
---|
203 | |
---|
204 | irc_send_num( ic->irc, 366, "%s :End of /NAMES list", ic->name ); |
---|
205 | } |
---|
206 | |
---|
207 | void irc_send_topic( irc_channel_t *ic, gboolean topic_change ) |
---|
208 | { |
---|
209 | if( topic_change && ic->topic_who ) |
---|
210 | { |
---|
211 | irc_write( ic->irc, ":%s TOPIC %s :%s", ic->topic_who, |
---|
212 | ic->name, ic->topic && *ic->topic ? ic->topic : "" ); |
---|
213 | } |
---|
214 | else if( ic->topic ) |
---|
215 | { |
---|
216 | irc_send_num( ic->irc, 332, "%s :%s", ic->name, ic->topic ); |
---|
217 | if( ic->topic_who ) |
---|
218 | irc_send_num( ic->irc, 333, "%s %s %d", |
---|
219 | ic->name, ic->topic_who, (int) ic->topic_time ); |
---|
220 | } |
---|
221 | else |
---|
222 | irc_send_num( ic->irc, 331, "%s :No topic for this channel", ic->name ); |
---|
223 | } |
---|
224 | |
---|
225 | void irc_send_whois( irc_user_t *iu ) |
---|
226 | { |
---|
227 | irc_t *irc = iu->irc; |
---|
228 | |
---|
229 | irc_send_num( irc, 311, "%s %s %s * :%s", |
---|
230 | iu->nick, iu->user, iu->host, iu->fullname ); |
---|
231 | |
---|
232 | if( iu->bu ) |
---|
233 | { |
---|
234 | bee_user_t *bu = iu->bu; |
---|
235 | |
---|
236 | irc_send_num( irc, 312, "%s %s.%s :%s network", iu->nick, bu->ic->acc->user, |
---|
237 | bu->ic->acc->server && *bu->ic->acc->server ? bu->ic->acc->server : "", |
---|
238 | bu->ic->acc->prpl->name ); |
---|
239 | |
---|
240 | if( ( bu->status && *bu->status ) || |
---|
241 | ( bu->status_msg && *bu->status_msg ) ) |
---|
242 | { |
---|
243 | int num = bu->flags & BEE_USER_AWAY ? 301 : 320; |
---|
244 | |
---|
245 | if( bu->status && bu->status_msg ) |
---|
246 | irc_send_num( irc, num, "%s :%s (%s)", iu->nick, bu->status, bu->status_msg ); |
---|
247 | else |
---|
248 | irc_send_num( irc, num, "%s :%s", iu->nick, bu->status ? : bu->status_msg ); |
---|
249 | } |
---|
250 | else if( !( bu->flags & BEE_USER_ONLINE ) ) |
---|
251 | { |
---|
252 | irc_send_num( irc, 301, "%s :%s", iu->nick, "User is offline" ); |
---|
253 | } |
---|
254 | |
---|
255 | if( bu->idle_time || bu->login_time ) |
---|
256 | { |
---|
257 | irc_send_num( irc, 317, "%s %d %d :seconds idle, signon time", |
---|
258 | iu->nick, |
---|
259 | bu->idle_time ? (int) ( time( NULL ) - bu->idle_time ) : 0, |
---|
260 | (int) bu->login_time ); |
---|
261 | } |
---|
262 | } |
---|
263 | else |
---|
264 | { |
---|
265 | irc_send_num( irc, 312, "%s %s :%s", iu->nick, irc->root->host, IRCD_INFO ); |
---|
266 | } |
---|
267 | |
---|
268 | irc_send_num( irc, 318, "%s :End of /WHOIS list", iu->nick ); |
---|
269 | } |
---|
270 | |
---|
271 | void irc_send_who( irc_t *irc, GSList *l, const char *channel ) |
---|
272 | { |
---|
273 | gboolean is_channel = strchr( CTYPES, channel[0] ) != NULL; |
---|
274 | |
---|
275 | while( l ) |
---|
276 | { |
---|
277 | irc_user_t *iu = l->data; |
---|
278 | if( is_channel ) |
---|
279 | iu = ((irc_channel_user_t*)iu)->iu; |
---|
280 | /* TODO(wilmer): Restore away/channel information here */ |
---|
281 | irc_send_num( irc, 352, "%s %s %s %s %s %c :0 %s", |
---|
282 | is_channel ? channel : "*", iu->user, iu->host, irc->root->host, |
---|
283 | iu->nick, iu->flags & IRC_USER_AWAY ? 'G' : 'H', |
---|
284 | iu->fullname ); |
---|
285 | l = l->next; |
---|
286 | } |
---|
287 | |
---|
288 | irc_send_num( irc, 315, "%s :End of /WHO list", channel ); |
---|
289 | } |
---|
290 | |
---|
291 | void irc_send_msg( irc_user_t *iu, const char *type, const char *dst, const char *msg, const char *prefix ) |
---|
292 | { |
---|
293 | char last = 0; |
---|
294 | const char *s = msg, *line = msg; |
---|
295 | char raw_msg[strlen(msg)+1024]; |
---|
296 | |
---|
297 | while( !last ) |
---|
298 | { |
---|
299 | if( *s == '\r' && *(s+1) == '\n' ) |
---|
300 | s++; |
---|
301 | if( *s == '\n' ) |
---|
302 | { |
---|
303 | last = s[1] == 0; |
---|
304 | } |
---|
305 | else |
---|
306 | { |
---|
307 | last = s[0] == 0; |
---|
308 | } |
---|
309 | if( *s == 0 || *s == '\n' ) |
---|
310 | { |
---|
311 | if( g_strncasecmp( line, "/me ", 4 ) == 0 && ( !prefix || !*prefix ) && |
---|
312 | g_strcasecmp( type, "PRIVMSG" ) == 0 ) |
---|
313 | { |
---|
314 | strcpy( raw_msg, "\001ACTION " ); |
---|
315 | strncat( raw_msg, line + 4, s - line - 4 ); |
---|
316 | strcat( raw_msg, "\001" ); |
---|
317 | irc_send_msg_raw( iu, type, dst, raw_msg ); |
---|
318 | } |
---|
319 | else |
---|
320 | { |
---|
321 | *raw_msg = '\0'; |
---|
322 | if( prefix && *prefix ) |
---|
323 | strcpy( raw_msg, prefix ); |
---|
324 | strncat( raw_msg, line, s - line ); |
---|
325 | irc_send_msg_raw( iu, type, dst, raw_msg ); |
---|
326 | } |
---|
327 | line = s + 1; |
---|
328 | } |
---|
329 | s ++; |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | void irc_send_msg_raw( irc_user_t *iu, const char *type, const char *dst, const char *msg ) |
---|
334 | { |
---|
335 | irc_write( iu->irc, ":%s!%s@%s %s %s :%s", |
---|
336 | iu->nick, iu->user, iu->host, type, dst, msg && *msg ? msg : " " ); |
---|
337 | } |
---|
338 | |
---|
339 | void irc_send_msg_f( irc_user_t *iu, const char *type, const char *dst, const char *format, ... ) |
---|
340 | { |
---|
341 | char text[IRC_MAX_LINE]; |
---|
342 | va_list params; |
---|
343 | |
---|
344 | va_start( params, format ); |
---|
345 | g_vsnprintf( text, IRC_MAX_LINE, format, params ); |
---|
346 | va_end( params ); |
---|
347 | |
---|
348 | irc_write( iu->irc, ":%s!%s@%s %s %s :%s", |
---|
349 | iu->nick, iu->user, iu->host, type, dst, text ); |
---|
350 | } |
---|
351 | |
---|
352 | void irc_send_nick( irc_user_t *iu, const char *new ) |
---|
353 | { |
---|
354 | irc_write( iu->irc, ":%s!%s@%s NICK %s", |
---|
355 | iu->nick, iu->user, iu->host, new ); |
---|
356 | } |
---|
357 | |
---|
358 | /* Send an update of a user's mode inside a channel, compared to what it was. */ |
---|
359 | void irc_send_channel_user_mode_diff( irc_channel_t *ic, irc_user_t *iu, |
---|
360 | irc_channel_user_flags_t old, irc_channel_user_flags_t new ) |
---|
361 | { |
---|
362 | char changes[3*(5+strlen(iu->nick))]; |
---|
363 | char from[strlen(ic->irc->root->nick)+strlen(ic->irc->root->user)+strlen(ic->irc->root->host)+3]; |
---|
364 | int n; |
---|
365 | |
---|
366 | *changes = '\0'; n = 0; |
---|
367 | if( ( old & IRC_CHANNEL_USER_OP ) != ( new & IRC_CHANNEL_USER_OP ) ) |
---|
368 | { |
---|
369 | n ++; |
---|
370 | if( new & IRC_CHANNEL_USER_OP ) |
---|
371 | strcat( changes, "+o" ); |
---|
372 | else |
---|
373 | strcat( changes, "-o" ); |
---|
374 | } |
---|
375 | if( ( old & IRC_CHANNEL_USER_HALFOP ) != ( new & IRC_CHANNEL_USER_HALFOP ) ) |
---|
376 | { |
---|
377 | n ++; |
---|
378 | if( new & IRC_CHANNEL_USER_HALFOP ) |
---|
379 | strcat( changes, "+h" ); |
---|
380 | else |
---|
381 | strcat( changes, "-h" ); |
---|
382 | } |
---|
383 | if( ( old & IRC_CHANNEL_USER_VOICE ) != ( new & IRC_CHANNEL_USER_VOICE ) ) |
---|
384 | { |
---|
385 | n ++; |
---|
386 | if( new & IRC_CHANNEL_USER_VOICE ) |
---|
387 | strcat( changes, "+v" ); |
---|
388 | else |
---|
389 | strcat( changes, "-v" ); |
---|
390 | } |
---|
391 | while( n ) |
---|
392 | { |
---|
393 | strcat( changes, " " ); |
---|
394 | strcat( changes, iu->nick ); |
---|
395 | n --; |
---|
396 | } |
---|
397 | |
---|
398 | if( set_getbool( &ic->irc->b->set, "simulate_netsplit" ) ) |
---|
399 | g_snprintf( from, sizeof( from ), "%s", ic->irc->root->host ); |
---|
400 | else |
---|
401 | g_snprintf( from, sizeof( from ), "%s!%s@%s", ic->irc->root->nick, |
---|
402 | ic->irc->root->user, ic->irc->root->host ); |
---|
403 | |
---|
404 | if( *changes ) |
---|
405 | irc_write( ic->irc, ":%s MODE %s %s", from, ic->name, changes ); |
---|
406 | } |
---|
407 | |
---|
408 | void irc_send_invite( irc_user_t *iu, irc_channel_t *ic ) |
---|
409 | { |
---|
410 | irc_t *irc = iu->irc; |
---|
411 | |
---|
412 | irc_write( iu->irc, ":%s!%s@%s INVITE %s :%s", |
---|
413 | iu->nick, iu->user, iu->host, irc->user->nick, ic->name ); |
---|
414 | } |
---|