1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* Some glue to put the IRC and the IM stuff together. */ |
---|
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 | #include "dcc.h" |
---|
28 | |
---|
29 | /* IM->IRC callbacks: Simple IM/buddy-related stuff. */ |
---|
30 | |
---|
31 | static const struct irc_user_funcs irc_user_im_funcs; |
---|
32 | |
---|
33 | static gboolean bee_irc_user_new( bee_t *bee, bee_user_t *bu ) |
---|
34 | { |
---|
35 | irc_user_t *iu; |
---|
36 | char nick[MAX_NICK_LENGTH+1], *s; |
---|
37 | |
---|
38 | memset( nick, 0, MAX_NICK_LENGTH + 1 ); |
---|
39 | strcpy( nick, nick_get( bu->ic->acc, bu->handle ) ); |
---|
40 | |
---|
41 | bu->ui_data = iu = irc_user_new( (irc_t*) bee->ui_data, nick ); |
---|
42 | iu->bu = bu; |
---|
43 | |
---|
44 | if( ( s = strchr( bu->handle, '@' ) ) ) |
---|
45 | { |
---|
46 | iu->host = g_strdup( s + 1 ); |
---|
47 | iu->user = g_strndup( bu->handle, s - bu->handle ); |
---|
48 | } |
---|
49 | else if( bu->ic->acc->server ) |
---|
50 | { |
---|
51 | iu->host = g_strdup( bu->ic->acc->server ); |
---|
52 | iu->user = g_strdup( bu->handle ); |
---|
53 | |
---|
54 | /* s/ /_/ ... important for AOL screennames */ |
---|
55 | for( s = iu->user; *s; s ++ ) |
---|
56 | if( *s == ' ' ) |
---|
57 | *s = '_'; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | iu->host = g_strdup( bu->ic->acc->prpl->name ); |
---|
62 | iu->user = g_strdup( bu->handle ); |
---|
63 | } |
---|
64 | |
---|
65 | if( set_getbool( &bee->set, "private" ) ) |
---|
66 | iu->flags |= IRC_USER_PRIVATE; |
---|
67 | |
---|
68 | if( bu->flags & BEE_USER_LOCAL ) |
---|
69 | { |
---|
70 | char *s = set_getstr( &bee->set, "handle_unknown" ); |
---|
71 | |
---|
72 | if( strcmp( s, "add_private" ) == 0 ) |
---|
73 | iu->flags |= IRC_USER_PRIVATE; |
---|
74 | else if( strcmp( s, "add_channel" ) == 0 ) |
---|
75 | iu->flags &= ~IRC_USER_PRIVATE; |
---|
76 | } |
---|
77 | |
---|
78 | iu->f = &irc_user_im_funcs; |
---|
79 | //iu->last_typing_notice = 0; |
---|
80 | |
---|
81 | return TRUE; |
---|
82 | } |
---|
83 | |
---|
84 | static gboolean bee_irc_user_free( bee_t *bee, bee_user_t *bu ) |
---|
85 | { |
---|
86 | return irc_user_free( bee->ui_data, (irc_user_t *) bu->ui_data ); |
---|
87 | } |
---|
88 | |
---|
89 | static gboolean bee_irc_user_status( bee_t *bee, bee_user_t *bu, bee_user_t *old ) |
---|
90 | { |
---|
91 | irc_t *irc = bee->ui_data; |
---|
92 | irc_user_t *iu = bu->ui_data; |
---|
93 | |
---|
94 | /* Do this outside the if below since away state can change without |
---|
95 | the online state changing. */ |
---|
96 | iu->flags &= ~IRC_USER_AWAY; |
---|
97 | if( bu->flags & BEE_USER_AWAY || !( bu->flags & BEE_USER_ONLINE ) ) |
---|
98 | iu->flags |= IRC_USER_AWAY; |
---|
99 | |
---|
100 | if( ( bu->flags & BEE_USER_ONLINE ) != ( old->flags & BEE_USER_ONLINE ) ) |
---|
101 | { |
---|
102 | if( bu->flags & BEE_USER_ONLINE ) |
---|
103 | { |
---|
104 | if( g_hash_table_lookup( irc->watches, iu->key ) ) |
---|
105 | irc_send_num( irc, 600, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
106 | iu->host, (int) time( NULL ), "logged online" ); |
---|
107 | } |
---|
108 | else |
---|
109 | { |
---|
110 | if( g_hash_table_lookup( irc->watches, iu->key ) ) |
---|
111 | irc_send_num( irc, 601, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
112 | iu->host, (int) time( NULL ), "logged offline" ); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | /* Reset this one since the info may have changed. */ |
---|
117 | iu->away_reply_timeout = 0; |
---|
118 | |
---|
119 | bee_irc_channel_update( irc, NULL, iu ); |
---|
120 | |
---|
121 | return TRUE; |
---|
122 | } |
---|
123 | |
---|
124 | void bee_irc_channel_update( irc_t *irc, irc_channel_t *ic, irc_user_t *iu ) |
---|
125 | { |
---|
126 | struct irc_control_channel *icc; |
---|
127 | GSList *l; |
---|
128 | gboolean show; |
---|
129 | |
---|
130 | if( ic == NULL ) |
---|
131 | { |
---|
132 | for( l = irc->channels; l; l = l->next ) |
---|
133 | { |
---|
134 | ic = l->data; |
---|
135 | /* TODO: Just add a type flag or so.. */ |
---|
136 | if( ic->f == irc->default_channel->f ) |
---|
137 | bee_irc_channel_update( irc, ic, iu ); |
---|
138 | } |
---|
139 | return; |
---|
140 | } |
---|
141 | if( iu == NULL ) |
---|
142 | { |
---|
143 | for( l = irc->users; l; l = l->next ) |
---|
144 | { |
---|
145 | iu = l->data; |
---|
146 | if( iu->bu ) |
---|
147 | bee_irc_channel_update( irc, ic, l->data ); |
---|
148 | } |
---|
149 | return; |
---|
150 | } |
---|
151 | |
---|
152 | icc = ic->data; |
---|
153 | |
---|
154 | if( !( iu->bu->flags & BEE_USER_ONLINE ) ) |
---|
155 | show = FALSE; |
---|
156 | else if( icc->type == IRC_CC_TYPE_DEFAULT ) |
---|
157 | show = TRUE; |
---|
158 | else if( icc->type == IRC_CC_TYPE_GROUP ) |
---|
159 | show = iu->bu->group == icc->group; |
---|
160 | else if( icc->type == IRC_CC_TYPE_ACCOUNT ) |
---|
161 | show = iu->bu->ic->acc == icc->account; |
---|
162 | |
---|
163 | if( !show ) |
---|
164 | { |
---|
165 | irc_channel_del_user( ic, iu, FALSE, NULL ); |
---|
166 | } |
---|
167 | else |
---|
168 | { |
---|
169 | irc_channel_add_user( ic, iu ); |
---|
170 | |
---|
171 | if( set_getbool( &irc->b->set, "away_devoice" ) ) |
---|
172 | irc_channel_user_set_mode( ic, iu, ( iu->bu->flags & BEE_USER_AWAY ) ? |
---|
173 | 0 : IRC_CHANNEL_USER_VOICE ); |
---|
174 | else |
---|
175 | irc_channel_user_set_mode( ic, iu, 0 ); |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | static gboolean bee_irc_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, time_t sent_at ) |
---|
180 | { |
---|
181 | irc_t *irc = bee->ui_data; |
---|
182 | irc_channel_t *ic = irc->default_channel; |
---|
183 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
184 | char *dst, *prefix = NULL; |
---|
185 | char *wrapped, *ts = NULL; |
---|
186 | |
---|
187 | if( sent_at > 0 && set_getbool( &irc->b->set, "display_timestamps" ) ) |
---|
188 | ts = irc_format_timestamp( irc, sent_at ); |
---|
189 | |
---|
190 | if( iu->flags & IRC_USER_PRIVATE ) |
---|
191 | { |
---|
192 | dst = irc->user->nick; |
---|
193 | prefix = ts; |
---|
194 | ts = NULL; |
---|
195 | } |
---|
196 | else |
---|
197 | { |
---|
198 | dst = ic->name; |
---|
199 | prefix = g_strdup_printf( "%s%s%s", irc->user->nick, set_getstr( &bee->set, "to_char" ), ts ? : "" ); |
---|
200 | } |
---|
201 | |
---|
202 | wrapped = word_wrap( msg, 425 ); |
---|
203 | irc_send_msg( iu, "PRIVMSG", dst, wrapped, prefix ); |
---|
204 | |
---|
205 | g_free( wrapped ); |
---|
206 | g_free( prefix ); |
---|
207 | g_free( ts ); |
---|
208 | |
---|
209 | return TRUE; |
---|
210 | } |
---|
211 | |
---|
212 | static gboolean bee_irc_user_typing( bee_t *bee, bee_user_t *bu, uint32_t flags ) |
---|
213 | { |
---|
214 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
215 | |
---|
216 | if( set_getbool( &bee->set, "typing_notice" ) ) |
---|
217 | irc_send_msg_f( (irc_user_t *) bu->ui_data, "PRIVMSG", irc->user->nick, |
---|
218 | "\001TYPING %d\001", ( flags >> 8 ) & 3 ); |
---|
219 | else |
---|
220 | return FALSE; |
---|
221 | |
---|
222 | return TRUE; |
---|
223 | } |
---|
224 | |
---|
225 | static gboolean bee_irc_user_nick_hint( bee_t *bee, bee_user_t *bu, const char *hint ); |
---|
226 | |
---|
227 | static gboolean bee_irc_user_fullname( bee_t *bee, bee_user_t *bu ) |
---|
228 | { |
---|
229 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
230 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
231 | char *s; |
---|
232 | |
---|
233 | if( iu->fullname != iu->nick ) |
---|
234 | g_free( iu->fullname ); |
---|
235 | iu->fullname = g_strdup( bu->fullname ); |
---|
236 | |
---|
237 | /* Strip newlines (unlikely, but IRC-unfriendly so they must go) |
---|
238 | TODO(wilmer): Do the same with away msgs again! */ |
---|
239 | for( s = iu->fullname; *s; s ++ ) |
---|
240 | if( isspace( *s ) ) *s = ' '; |
---|
241 | |
---|
242 | if( ( bu->ic->flags & OPT_LOGGED_IN ) && set_getbool( &bee->set, "display_namechanges" ) ) |
---|
243 | { |
---|
244 | char *msg = g_strdup_printf( "<< \002BitlBee\002 - Changed name to `%s' >>", iu->fullname ); |
---|
245 | irc_send_msg( iu, "NOTICE", irc->user->nick, msg, NULL ); |
---|
246 | } |
---|
247 | |
---|
248 | s = set_getstr( &bu->ic->acc->set, "nick_source" ); |
---|
249 | if( strcmp( s, "handle" ) != 0 ) |
---|
250 | { |
---|
251 | char *name = g_strdup( bu->fullname ); |
---|
252 | |
---|
253 | if( strcmp( s, "first_name" ) == 0 ) |
---|
254 | { |
---|
255 | int i; |
---|
256 | for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {} |
---|
257 | name[i] = '\0'; |
---|
258 | } |
---|
259 | |
---|
260 | bee_irc_user_nick_hint( bee, bu, name ); |
---|
261 | |
---|
262 | g_free( name ); |
---|
263 | } |
---|
264 | |
---|
265 | return TRUE; |
---|
266 | } |
---|
267 | |
---|
268 | static gboolean bee_irc_user_nick_hint( bee_t *bee, bee_user_t *bu, const char *hint ) |
---|
269 | { |
---|
270 | irc_user_t *iu = bu->ui_data; |
---|
271 | char newnick[MAX_NICK_LENGTH+1], *translit; |
---|
272 | |
---|
273 | if( bu->flags & BEE_USER_ONLINE ) |
---|
274 | /* Ignore if the user is visible already. */ |
---|
275 | return TRUE; |
---|
276 | |
---|
277 | if( nick_saved( bu->ic->acc, bu->handle ) ) |
---|
278 | /* The user already assigned a nickname to this person. */ |
---|
279 | return TRUE; |
---|
280 | |
---|
281 | /* Credits to Josay_ in #bitlbee for this idea. //TRANSLIT should |
---|
282 | do lossy/approximate conversions, so letters with accents don't |
---|
283 | just get stripped. Note that it depends on LC_CTYPE being set to |
---|
284 | something other than C/POSIX. */ |
---|
285 | translit = g_convert( hint, -1, "ASCII//TRANSLIT//IGNORE", "UTF-8", |
---|
286 | NULL, NULL, NULL ); |
---|
287 | |
---|
288 | strncpy( newnick, translit ? : hint, MAX_NICK_LENGTH ); |
---|
289 | newnick[MAX_NICK_LENGTH] = 0; |
---|
290 | g_free( translit ); |
---|
291 | |
---|
292 | /* Some processing to make sure this string is a valid IRC nickname. */ |
---|
293 | nick_strip( newnick ); |
---|
294 | if( set_getbool( &bee->set, "lcnicks" ) ) |
---|
295 | nick_lc( newnick ); |
---|
296 | |
---|
297 | if( strcmp( iu->nick, newnick ) != 0 ) |
---|
298 | { |
---|
299 | /* Only do this if newnick is different from the current one. |
---|
300 | If rejoining a channel, maybe we got this nick already |
---|
301 | (and dedupe would only add an underscore. */ |
---|
302 | nick_dedupe( bu->ic->acc, bu->handle, newnick ); |
---|
303 | irc_user_set_nick( iu, newnick ); |
---|
304 | } |
---|
305 | |
---|
306 | return TRUE; |
---|
307 | } |
---|
308 | |
---|
309 | static gboolean bee_irc_user_group( bee_t *bee, bee_user_t *bu ) |
---|
310 | { |
---|
311 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
312 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
313 | |
---|
314 | bee_irc_channel_update( irc, NULL, iu ); |
---|
315 | |
---|
316 | return TRUE; |
---|
317 | } |
---|
318 | |
---|
319 | /* IRC->IM calls */ |
---|
320 | |
---|
321 | static gboolean bee_irc_user_privmsg_cb( gpointer data, gint fd, b_input_condition cond ); |
---|
322 | |
---|
323 | static gboolean bee_irc_user_privmsg( irc_user_t *iu, const char *msg ) |
---|
324 | { |
---|
325 | const char *away; |
---|
326 | |
---|
327 | if( iu->bu == NULL ) |
---|
328 | return FALSE; |
---|
329 | |
---|
330 | if( ( away = irc_user_get_away( iu ) ) && |
---|
331 | time( NULL ) >= iu->away_reply_timeout ) |
---|
332 | { |
---|
333 | irc_send_num( iu->irc, 301, "%s :%s", iu->nick, away ); |
---|
334 | iu->away_reply_timeout = time( NULL ) + |
---|
335 | set_getint( &iu->irc->b->set, "away_reply_timeout" ); |
---|
336 | } |
---|
337 | |
---|
338 | if( set_getbool( &iu->irc->b->set, "paste_buffer" ) ) |
---|
339 | { |
---|
340 | int delay; |
---|
341 | |
---|
342 | if( iu->pastebuf == NULL ) |
---|
343 | iu->pastebuf = g_string_new( msg ); |
---|
344 | else |
---|
345 | { |
---|
346 | b_event_remove( iu->pastebuf_timer ); |
---|
347 | g_string_append_printf( iu->pastebuf, "\n%s", msg ); |
---|
348 | } |
---|
349 | |
---|
350 | if( ( delay = set_getint( &iu->irc->b->set, "paste_buffer_delay" ) ) <= 5 ) |
---|
351 | delay *= 1000; |
---|
352 | |
---|
353 | iu->pastebuf_timer = b_timeout_add( delay, bee_irc_user_privmsg_cb, iu ); |
---|
354 | |
---|
355 | return TRUE; |
---|
356 | } |
---|
357 | else |
---|
358 | return bee_user_msg( iu->irc->b, iu->bu, msg, 0 ); |
---|
359 | } |
---|
360 | |
---|
361 | static gboolean bee_irc_user_privmsg_cb( gpointer data, gint fd, b_input_condition cond ) |
---|
362 | { |
---|
363 | irc_user_t *iu = data; |
---|
364 | |
---|
365 | bee_user_msg( iu->irc->b, iu->bu, iu->pastebuf->str, 0 ); |
---|
366 | |
---|
367 | g_string_free( iu->pastebuf, TRUE ); |
---|
368 | iu->pastebuf = 0; |
---|
369 | iu->pastebuf_timer = 0; |
---|
370 | |
---|
371 | return FALSE; |
---|
372 | } |
---|
373 | |
---|
374 | static gboolean bee_irc_user_ctcp( irc_user_t *iu, char *const *ctcp ) |
---|
375 | { |
---|
376 | if( ctcp[1] && g_strcasecmp( ctcp[0], "DCC" ) == 0 |
---|
377 | && g_strcasecmp( ctcp[1], "SEND" ) == 0 ) |
---|
378 | { |
---|
379 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request ) |
---|
380 | { |
---|
381 | file_transfer_t *ft = dcc_request( iu->bu->ic, ctcp ); |
---|
382 | if ( ft ) |
---|
383 | iu->bu->ic->acc->prpl->transfer_request( iu->bu->ic, ft, iu->bu->handle ); |
---|
384 | |
---|
385 | return TRUE; |
---|
386 | } |
---|
387 | } |
---|
388 | else if( g_strcasecmp( ctcp[0], "TYPING" ) == 0 ) |
---|
389 | { |
---|
390 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->send_typing && ctcp[1] ) |
---|
391 | { |
---|
392 | int st = ctcp[1][0]; |
---|
393 | if( st >= '0' && st <= '2' ) |
---|
394 | { |
---|
395 | st <<= 8; |
---|
396 | iu->bu->ic->acc->prpl->send_typing( iu->bu->ic, iu->bu->handle, st ); |
---|
397 | } |
---|
398 | |
---|
399 | return TRUE; |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | return FALSE; |
---|
404 | } |
---|
405 | |
---|
406 | static const struct irc_user_funcs irc_user_im_funcs = { |
---|
407 | bee_irc_user_privmsg, |
---|
408 | bee_irc_user_ctcp, |
---|
409 | }; |
---|
410 | |
---|
411 | |
---|
412 | /* IM->IRC: Groupchats */ |
---|
413 | const struct irc_channel_funcs irc_channel_im_chat_funcs; |
---|
414 | |
---|
415 | static gboolean bee_irc_chat_new( bee_t *bee, struct groupchat *c ) |
---|
416 | { |
---|
417 | irc_t *irc = bee->ui_data; |
---|
418 | irc_channel_t *ic; |
---|
419 | char *topic; |
---|
420 | GSList *l; |
---|
421 | int i; |
---|
422 | |
---|
423 | /* Try to find a channel that expects to receive a groupchat. |
---|
424 | This flag is set by groupchat_stub_invite(). */ |
---|
425 | for( l = irc->channels; l; l = l->next ) |
---|
426 | { |
---|
427 | ic = l->data; |
---|
428 | if( ic->flags & IRC_CHANNEL_CHAT_PICKME ) |
---|
429 | break; |
---|
430 | } |
---|
431 | |
---|
432 | /* If we found none, just generate some stupid name. */ |
---|
433 | if( l == NULL ) for( i = 0; i <= 999; i ++ ) |
---|
434 | { |
---|
435 | char name[16]; |
---|
436 | sprintf( name, "#chat_%03d", i ); |
---|
437 | if( ( ic = irc_channel_new( irc, name ) ) ) |
---|
438 | break; |
---|
439 | } |
---|
440 | |
---|
441 | if( ic == NULL ) |
---|
442 | return FALSE; |
---|
443 | |
---|
444 | c->ui_data = ic; |
---|
445 | ic->data = c; |
---|
446 | |
---|
447 | topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); |
---|
448 | irc_channel_set_topic( ic, topic, irc->root ); |
---|
449 | g_free( topic ); |
---|
450 | |
---|
451 | return TRUE; |
---|
452 | } |
---|
453 | |
---|
454 | static gboolean bee_irc_chat_free( bee_t *bee, struct groupchat *c ) |
---|
455 | { |
---|
456 | irc_channel_t *ic = c->ui_data; |
---|
457 | |
---|
458 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
459 | irc_channel_printf( ic, "Cleaning up channel, bye!" ); |
---|
460 | |
---|
461 | /* irc_channel_free( ic ); */ |
---|
462 | |
---|
463 | irc_channel_del_user( ic, ic->irc->user, FALSE, "Chatroom closed by server" ); |
---|
464 | ic->data = NULL; |
---|
465 | |
---|
466 | return TRUE; |
---|
467 | } |
---|
468 | |
---|
469 | static gboolean bee_irc_chat_log( bee_t *bee, struct groupchat *c, const char *text ) |
---|
470 | { |
---|
471 | irc_channel_t *ic = c->ui_data; |
---|
472 | |
---|
473 | irc_channel_printf( ic, "%s", text ); |
---|
474 | |
---|
475 | return TRUE; |
---|
476 | } |
---|
477 | |
---|
478 | static gboolean bee_irc_chat_msg( bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *msg, time_t sent_at ) |
---|
479 | { |
---|
480 | irc_t *irc = bee->ui_data; |
---|
481 | irc_user_t *iu = bu->ui_data; |
---|
482 | irc_channel_t *ic = c->ui_data; |
---|
483 | char *ts = NULL; |
---|
484 | |
---|
485 | if( sent_at > 0 && set_getbool( &bee->set, "display_timestamps" ) ) |
---|
486 | ts = irc_format_timestamp( irc, sent_at ); |
---|
487 | |
---|
488 | irc_send_msg( iu, "PRIVMSG", ic->name, msg, ts ); |
---|
489 | g_free( ts ); |
---|
490 | |
---|
491 | return TRUE; |
---|
492 | } |
---|
493 | |
---|
494 | static gboolean bee_irc_chat_add_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
495 | { |
---|
496 | irc_t *irc = bee->ui_data; |
---|
497 | |
---|
498 | irc_channel_add_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data ); |
---|
499 | |
---|
500 | return TRUE; |
---|
501 | } |
---|
502 | |
---|
503 | static gboolean bee_irc_chat_remove_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
504 | { |
---|
505 | irc_t *irc = bee->ui_data; |
---|
506 | |
---|
507 | irc_channel_del_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data, FALSE, NULL ); |
---|
508 | |
---|
509 | return TRUE; |
---|
510 | } |
---|
511 | |
---|
512 | static gboolean bee_irc_chat_topic( bee_t *bee, struct groupchat *c, const char *new, bee_user_t *bu ) |
---|
513 | { |
---|
514 | irc_t *irc = bee->ui_data; |
---|
515 | irc_user_t *iu; |
---|
516 | |
---|
517 | if( bu == NULL ) |
---|
518 | iu = irc->root; |
---|
519 | else if( bu == bee->user ) |
---|
520 | iu = irc->user; |
---|
521 | else |
---|
522 | iu = bu->ui_data; |
---|
523 | |
---|
524 | irc_channel_set_topic( c->ui_data, new, iu ); |
---|
525 | |
---|
526 | return TRUE; |
---|
527 | } |
---|
528 | |
---|
529 | static gboolean bee_irc_chat_name_hint( bee_t *bee, struct groupchat *c, const char *name ) |
---|
530 | { |
---|
531 | irc_t *irc = bee->ui_data; |
---|
532 | irc_channel_t *ic = c->ui_data; |
---|
533 | char stripped[MAX_NICK_LENGTH+1], *full_name; |
---|
534 | |
---|
535 | /* Don't rename a channel if the user's in it already. */ |
---|
536 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
537 | return FALSE; |
---|
538 | |
---|
539 | strncpy( stripped, name, MAX_NICK_LENGTH ); |
---|
540 | stripped[MAX_NICK_LENGTH] = '\0'; |
---|
541 | nick_strip( stripped ); |
---|
542 | if( set_getbool( &bee->set, "lcnicks" ) ) |
---|
543 | nick_lc( stripped ); |
---|
544 | |
---|
545 | full_name = g_strdup_printf( "&%s", stripped ); |
---|
546 | |
---|
547 | if( stripped[0] && irc_channel_by_name( irc, full_name ) == NULL ) |
---|
548 | { |
---|
549 | g_free( ic->name ); |
---|
550 | ic->name = full_name; |
---|
551 | } |
---|
552 | else |
---|
553 | { |
---|
554 | g_free( full_name ); |
---|
555 | } |
---|
556 | |
---|
557 | return TRUE; |
---|
558 | } |
---|
559 | |
---|
560 | /* IRC->IM */ |
---|
561 | static gboolean bee_irc_channel_chat_privmsg_cb( gpointer data, gint fd, b_input_condition cond ); |
---|
562 | |
---|
563 | static gboolean bee_irc_channel_chat_privmsg( irc_channel_t *ic, const char *msg ) |
---|
564 | { |
---|
565 | struct groupchat *c = ic->data; |
---|
566 | |
---|
567 | if( c == NULL ) |
---|
568 | return FALSE; |
---|
569 | else if( set_getbool( &ic->irc->b->set, "paste_buffer" ) ) |
---|
570 | { |
---|
571 | int delay; |
---|
572 | |
---|
573 | if( ic->pastebuf == NULL ) |
---|
574 | ic->pastebuf = g_string_new( msg ); |
---|
575 | else |
---|
576 | { |
---|
577 | b_event_remove( ic->pastebuf_timer ); |
---|
578 | g_string_append_printf( ic->pastebuf, "\n%s", msg ); |
---|
579 | } |
---|
580 | |
---|
581 | if( ( delay = set_getint( &ic->irc->b->set, "paste_buffer_delay" ) ) <= 5 ) |
---|
582 | delay *= 1000; |
---|
583 | |
---|
584 | ic->pastebuf_timer = b_timeout_add( delay, bee_irc_channel_chat_privmsg_cb, ic ); |
---|
585 | |
---|
586 | return TRUE; |
---|
587 | } |
---|
588 | else |
---|
589 | bee_chat_msg( ic->irc->b, c, msg, 0 ); |
---|
590 | |
---|
591 | return TRUE; |
---|
592 | } |
---|
593 | |
---|
594 | static gboolean bee_irc_channel_chat_privmsg_cb( gpointer data, gint fd, b_input_condition cond ) |
---|
595 | { |
---|
596 | irc_channel_t *ic = data; |
---|
597 | |
---|
598 | bee_chat_msg( ic->irc->b, ic->data, ic->pastebuf->str, 0 ); |
---|
599 | |
---|
600 | g_string_free( ic->pastebuf, TRUE ); |
---|
601 | ic->pastebuf = 0; |
---|
602 | ic->pastebuf_timer = 0; |
---|
603 | |
---|
604 | return FALSE; |
---|
605 | } |
---|
606 | |
---|
607 | static gboolean bee_irc_channel_chat_join( irc_channel_t *ic ) |
---|
608 | { |
---|
609 | char *acc_s, *room; |
---|
610 | account_t *acc; |
---|
611 | |
---|
612 | if( strcmp( set_getstr( &ic->set, "chat_type" ), "room" ) != 0 ) |
---|
613 | return TRUE; |
---|
614 | |
---|
615 | if( ( acc_s = set_getstr( &ic->set, "account" ) ) && |
---|
616 | ( room = set_getstr( &ic->set, "room" ) ) && |
---|
617 | ( acc = account_get( ic->irc->b, acc_s ) ) && |
---|
618 | acc->ic && acc->prpl->chat_join ) |
---|
619 | { |
---|
620 | char *nick; |
---|
621 | |
---|
622 | if( !( nick = set_getstr( &ic->set, "nick" ) ) ) |
---|
623 | nick = ic->irc->user->nick; |
---|
624 | |
---|
625 | ic->flags |= IRC_CHANNEL_CHAT_PICKME; |
---|
626 | acc->prpl->chat_join( acc->ic, room, nick, NULL ); |
---|
627 | ic->flags &= ~IRC_CHANNEL_CHAT_PICKME; |
---|
628 | |
---|
629 | return FALSE; |
---|
630 | } |
---|
631 | else |
---|
632 | { |
---|
633 | irc_send_num( ic->irc, 403, "%s :Can't join channel, account offline?", ic->name ); |
---|
634 | return FALSE; |
---|
635 | } |
---|
636 | } |
---|
637 | |
---|
638 | static gboolean bee_irc_channel_chat_part( irc_channel_t *ic, const char *msg ) |
---|
639 | { |
---|
640 | struct groupchat *c = ic->data; |
---|
641 | |
---|
642 | if( c && c->ic->acc->prpl->chat_leave ) |
---|
643 | c->ic->acc->prpl->chat_leave( c ); |
---|
644 | |
---|
645 | return TRUE; |
---|
646 | } |
---|
647 | |
---|
648 | static gboolean bee_irc_channel_chat_topic( irc_channel_t *ic, const char *new ) |
---|
649 | { |
---|
650 | struct groupchat *c = ic->data; |
---|
651 | |
---|
652 | if( c == NULL ) |
---|
653 | return FALSE; |
---|
654 | |
---|
655 | if( c->ic->acc->prpl->chat_topic == NULL ) |
---|
656 | irc_send_num( ic->irc, 482, "%s :IM network does not support channel topics", ic->name ); |
---|
657 | else |
---|
658 | { |
---|
659 | /* TODO: Need more const goodness here, sigh */ |
---|
660 | char *topic = g_strdup( new ); |
---|
661 | c->ic->acc->prpl->chat_topic( c, topic ); |
---|
662 | g_free( topic ); |
---|
663 | return TRUE; |
---|
664 | } |
---|
665 | |
---|
666 | return FALSE; |
---|
667 | } |
---|
668 | |
---|
669 | static gboolean bee_irc_channel_chat_invite( irc_channel_t *ic, irc_user_t *iu ) |
---|
670 | { |
---|
671 | struct groupchat *c = ic->data; |
---|
672 | bee_user_t *bu = iu->bu; |
---|
673 | |
---|
674 | if( bu == NULL ) |
---|
675 | return FALSE; |
---|
676 | |
---|
677 | if( c ) |
---|
678 | { |
---|
679 | if( iu->bu->ic != c->ic ) |
---|
680 | irc_send_num( ic->irc, 482, "%s :Can't mix different IM networks in one groupchat", ic->name ); |
---|
681 | else if( c->ic->acc->prpl->chat_invite ) |
---|
682 | c->ic->acc->prpl->chat_invite( c, iu->bu->handle, NULL ); |
---|
683 | else |
---|
684 | irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name ); |
---|
685 | } |
---|
686 | else if( bu->ic->acc->prpl->chat_with && |
---|
687 | strcmp( set_getstr( &ic->set, "chat_type" ), "groupchat" ) == 0 ) |
---|
688 | { |
---|
689 | ic->flags |= IRC_CHANNEL_CHAT_PICKME; |
---|
690 | iu->bu->ic->acc->prpl->chat_with( bu->ic, bu->handle ); |
---|
691 | ic->flags &= ~IRC_CHANNEL_CHAT_PICKME; |
---|
692 | } |
---|
693 | else |
---|
694 | { |
---|
695 | irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name ); |
---|
696 | } |
---|
697 | |
---|
698 | return TRUE; |
---|
699 | } |
---|
700 | |
---|
701 | static char *set_eval_room_account( set_t *set, char *value ); |
---|
702 | |
---|
703 | static gboolean bee_irc_channel_init( irc_channel_t *ic ) |
---|
704 | { |
---|
705 | set_add( &ic->set, "account", NULL, set_eval_room_account, ic ); |
---|
706 | set_add( &ic->set, "chat_type", "groupchat", NULL, ic ); |
---|
707 | set_add( &ic->set, "nick", NULL, NULL, ic ); |
---|
708 | set_add( &ic->set, "room", NULL, NULL, ic ); |
---|
709 | |
---|
710 | return TRUE; |
---|
711 | } |
---|
712 | |
---|
713 | static char *set_eval_room_account( set_t *set, char *value ) |
---|
714 | { |
---|
715 | struct irc_channel *ic = set->data; |
---|
716 | account_t *acc; |
---|
717 | |
---|
718 | if( !( acc = account_get( ic->irc->b, value ) ) ) |
---|
719 | return SET_INVALID; |
---|
720 | else if( !acc->prpl->chat_join ) |
---|
721 | { |
---|
722 | irc_usermsg( ic->irc, "Named chatrooms not supported on that account." ); |
---|
723 | return SET_INVALID; |
---|
724 | } |
---|
725 | |
---|
726 | return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user ); |
---|
727 | } |
---|
728 | |
---|
729 | static gboolean bee_irc_channel_free( irc_channel_t *ic ) |
---|
730 | { |
---|
731 | set_del( &ic->set, "account" ); |
---|
732 | set_del( &ic->set, "chat_type" ); |
---|
733 | set_del( &ic->set, "nick" ); |
---|
734 | set_del( &ic->set, "room" ); |
---|
735 | |
---|
736 | return TRUE; |
---|
737 | } |
---|
738 | |
---|
739 | const struct irc_channel_funcs irc_channel_im_chat_funcs = { |
---|
740 | bee_irc_channel_chat_privmsg, |
---|
741 | bee_irc_channel_chat_join, |
---|
742 | bee_irc_channel_chat_part, |
---|
743 | bee_irc_channel_chat_topic, |
---|
744 | bee_irc_channel_chat_invite, |
---|
745 | |
---|
746 | bee_irc_channel_init, |
---|
747 | bee_irc_channel_free, |
---|
748 | }; |
---|
749 | |
---|
750 | |
---|
751 | /* IM->IRC: File transfers */ |
---|
752 | static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size ) |
---|
753 | { |
---|
754 | return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size ); |
---|
755 | } |
---|
756 | |
---|
757 | static gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft ) |
---|
758 | { |
---|
759 | return dccs_recv_start( ft ); |
---|
760 | } |
---|
761 | |
---|
762 | static void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft ) |
---|
763 | { |
---|
764 | return dcc_close( ft ); |
---|
765 | } |
---|
766 | |
---|
767 | static void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file ) |
---|
768 | { |
---|
769 | dcc_file_transfer_t *df = file->priv; |
---|
770 | |
---|
771 | if( file->bytes_transferred >= file->file_size ) |
---|
772 | dcc_finish( file ); |
---|
773 | else |
---|
774 | df->proto_finished = TRUE; |
---|
775 | } |
---|
776 | |
---|
777 | const struct bee_ui_funcs irc_ui_funcs = { |
---|
778 | bee_irc_user_new, |
---|
779 | bee_irc_user_free, |
---|
780 | bee_irc_user_fullname, |
---|
781 | bee_irc_user_nick_hint, |
---|
782 | bee_irc_user_group, |
---|
783 | bee_irc_user_status, |
---|
784 | bee_irc_user_msg, |
---|
785 | bee_irc_user_typing, |
---|
786 | |
---|
787 | bee_irc_chat_new, |
---|
788 | bee_irc_chat_free, |
---|
789 | bee_irc_chat_log, |
---|
790 | bee_irc_chat_msg, |
---|
791 | bee_irc_chat_add_user, |
---|
792 | bee_irc_chat_remove_user, |
---|
793 | bee_irc_chat_topic, |
---|
794 | bee_irc_chat_name_hint, |
---|
795 | |
---|
796 | bee_irc_ft_in_start, |
---|
797 | bee_irc_ft_out_start, |
---|
798 | bee_irc_ft_close, |
---|
799 | bee_irc_ft_finished, |
---|
800 | }; |
---|