1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* |
---|
8 | * nogaim |
---|
9 | * |
---|
10 | * Gaim without gaim - for BitlBee |
---|
11 | * |
---|
12 | * This file contains functions called by the Gaim IM-modules. It's written |
---|
13 | * from scratch for BitlBee and doesn't contain any code from Gaim anymore |
---|
14 | * (except for the function names). |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | This program is free software; you can redistribute it and/or modify |
---|
19 | it under the terms of the GNU General Public License as published by |
---|
20 | the Free Software Foundation; either version 2 of the License, or |
---|
21 | (at your option) any later version. |
---|
22 | |
---|
23 | This program is distributed in the hope that it will be useful, |
---|
24 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
26 | GNU General Public License for more details. |
---|
27 | |
---|
28 | You should have received a copy of the GNU General Public License with |
---|
29 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
30 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
31 | Suite 330, Boston, MA 02111-1307 USA |
---|
32 | */ |
---|
33 | |
---|
34 | #define BITLBEE_CORE |
---|
35 | #include <ctype.h> |
---|
36 | |
---|
37 | #include "nogaim.h" |
---|
38 | #include "chat.h" |
---|
39 | |
---|
40 | GSList *connections; |
---|
41 | |
---|
42 | #ifdef WITH_PLUGINS |
---|
43 | gboolean load_plugin(char *path) |
---|
44 | { |
---|
45 | void (*init_function) (void); |
---|
46 | |
---|
47 | GModule *mod = g_module_open(path, G_MODULE_BIND_LAZY); |
---|
48 | |
---|
49 | if(!mod) { |
---|
50 | log_message(LOGLVL_ERROR, "Can't find `%s', not loading (%s)\n", path, g_module_error()); |
---|
51 | return FALSE; |
---|
52 | } |
---|
53 | |
---|
54 | if(!g_module_symbol(mod,"init_plugin",(gpointer *) &init_function)) { |
---|
55 | log_message(LOGLVL_WARNING, "Can't find function `init_plugin' in `%s'\n", path); |
---|
56 | return FALSE; |
---|
57 | } |
---|
58 | |
---|
59 | init_function(); |
---|
60 | |
---|
61 | return TRUE; |
---|
62 | } |
---|
63 | |
---|
64 | void load_plugins(void) |
---|
65 | { |
---|
66 | GDir *dir; |
---|
67 | GError *error = NULL; |
---|
68 | |
---|
69 | dir = g_dir_open(global.conf->plugindir, 0, &error); |
---|
70 | |
---|
71 | if (dir) { |
---|
72 | const gchar *entry; |
---|
73 | char *path; |
---|
74 | |
---|
75 | while ((entry = g_dir_read_name(dir))) { |
---|
76 | path = g_build_filename(global.conf->plugindir, entry, NULL); |
---|
77 | if(!path) { |
---|
78 | log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry); |
---|
79 | continue; |
---|
80 | } |
---|
81 | |
---|
82 | load_plugin(path); |
---|
83 | |
---|
84 | g_free(path); |
---|
85 | } |
---|
86 | |
---|
87 | g_dir_close(dir); |
---|
88 | } |
---|
89 | } |
---|
90 | #endif |
---|
91 | |
---|
92 | GList *protocols = NULL; |
---|
93 | |
---|
94 | void register_protocol (struct prpl *p) |
---|
95 | { |
---|
96 | int i; |
---|
97 | gboolean refused = global.conf->protocols != NULL; |
---|
98 | |
---|
99 | for (i = 0; global.conf->protocols && global.conf->protocols[i]; i++) |
---|
100 | { |
---|
101 | if (g_strcasecmp(p->name, global.conf->protocols[i]) == 0) |
---|
102 | refused = FALSE; |
---|
103 | } |
---|
104 | |
---|
105 | if (refused) |
---|
106 | log_message(LOGLVL_WARNING, "Protocol %s disabled\n", p->name); |
---|
107 | else |
---|
108 | protocols = g_list_append(protocols, p); |
---|
109 | } |
---|
110 | |
---|
111 | struct prpl *find_protocol(const char *name) |
---|
112 | { |
---|
113 | GList *gl; |
---|
114 | for (gl = protocols; gl; gl = gl->next) |
---|
115 | { |
---|
116 | struct prpl *proto = gl->data; |
---|
117 | if(!g_strcasecmp(proto->name, name)) |
---|
118 | return proto; |
---|
119 | } |
---|
120 | return NULL; |
---|
121 | } |
---|
122 | |
---|
123 | void nogaim_init() |
---|
124 | { |
---|
125 | extern void msn_initmodule(); |
---|
126 | extern void oscar_initmodule(); |
---|
127 | extern void byahoo_initmodule(); |
---|
128 | extern void jabber_initmodule(); |
---|
129 | |
---|
130 | #ifdef WITH_MSN |
---|
131 | msn_initmodule(); |
---|
132 | #endif |
---|
133 | |
---|
134 | #ifdef WITH_OSCAR |
---|
135 | oscar_initmodule(); |
---|
136 | #endif |
---|
137 | |
---|
138 | #ifdef WITH_YAHOO |
---|
139 | byahoo_initmodule(); |
---|
140 | #endif |
---|
141 | |
---|
142 | #ifdef WITH_JABBER |
---|
143 | jabber_initmodule(); |
---|
144 | #endif |
---|
145 | |
---|
146 | #ifdef WITH_PLUGINS |
---|
147 | load_plugins(); |
---|
148 | #endif |
---|
149 | } |
---|
150 | |
---|
151 | GSList *get_connections() { return connections; } |
---|
152 | |
---|
153 | struct im_connection *imcb_new( account_t *acc ) |
---|
154 | { |
---|
155 | struct im_connection *ic; |
---|
156 | |
---|
157 | ic = g_new0( struct im_connection, 1 ); |
---|
158 | |
---|
159 | ic->bee = acc->bee; |
---|
160 | ic->acc = acc; |
---|
161 | acc->ic = ic; |
---|
162 | |
---|
163 | connections = g_slist_append( connections, ic ); |
---|
164 | |
---|
165 | return( ic ); |
---|
166 | } |
---|
167 | |
---|
168 | void imc_free( struct im_connection *ic ) |
---|
169 | { |
---|
170 | account_t *a; |
---|
171 | |
---|
172 | /* Destroy the pointer to this connection from the account list */ |
---|
173 | for( a = ic->bee->accounts; a; a = a->next ) |
---|
174 | if( a->ic == ic ) |
---|
175 | { |
---|
176 | a->ic = NULL; |
---|
177 | break; |
---|
178 | } |
---|
179 | |
---|
180 | connections = g_slist_remove( connections, ic ); |
---|
181 | g_free( ic ); |
---|
182 | } |
---|
183 | |
---|
184 | static void serv_got_crap( struct im_connection *ic, char *format, ... ) |
---|
185 | { |
---|
186 | va_list params; |
---|
187 | char *text; |
---|
188 | account_t *a; |
---|
189 | |
---|
190 | va_start( params, format ); |
---|
191 | text = g_strdup_vprintf( format, params ); |
---|
192 | va_end( params ); |
---|
193 | |
---|
194 | if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || |
---|
195 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) |
---|
196 | strip_html( text ); |
---|
197 | |
---|
198 | /* Try to find a different connection on the same protocol. */ |
---|
199 | for( a = ic->bee->accounts; a; a = a->next ) |
---|
200 | if( a->prpl == ic->acc->prpl && a->ic != ic ) |
---|
201 | break; |
---|
202 | |
---|
203 | /* If we found one, include the screenname in the message. */ |
---|
204 | if( a ) |
---|
205 | /* FIXME(wilmer): ui_log callback or so */ |
---|
206 | irc_usermsg( ic->bee->ui_data, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text ); |
---|
207 | else |
---|
208 | irc_usermsg( ic->bee->ui_data, "%s - %s", ic->acc->prpl->name, text ); |
---|
209 | |
---|
210 | g_free( text ); |
---|
211 | } |
---|
212 | |
---|
213 | void imcb_log( struct im_connection *ic, char *format, ... ) |
---|
214 | { |
---|
215 | va_list params; |
---|
216 | char *text; |
---|
217 | |
---|
218 | va_start( params, format ); |
---|
219 | text = g_strdup_vprintf( format, params ); |
---|
220 | va_end( params ); |
---|
221 | |
---|
222 | if( ic->flags & OPT_LOGGED_IN ) |
---|
223 | serv_got_crap( ic, "%s", text ); |
---|
224 | else |
---|
225 | serv_got_crap( ic, "Logging in: %s", text ); |
---|
226 | |
---|
227 | g_free( text ); |
---|
228 | } |
---|
229 | |
---|
230 | void imcb_error( struct im_connection *ic, char *format, ... ) |
---|
231 | { |
---|
232 | va_list params; |
---|
233 | char *text; |
---|
234 | |
---|
235 | va_start( params, format ); |
---|
236 | text = g_strdup_vprintf( format, params ); |
---|
237 | va_end( params ); |
---|
238 | |
---|
239 | if( ic->flags & OPT_LOGGED_IN ) |
---|
240 | serv_got_crap( ic, "Error: %s", text ); |
---|
241 | else |
---|
242 | serv_got_crap( ic, "Couldn't log in: %s", text ); |
---|
243 | |
---|
244 | g_free( text ); |
---|
245 | } |
---|
246 | |
---|
247 | static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond ) |
---|
248 | { |
---|
249 | struct im_connection *ic = d; |
---|
250 | |
---|
251 | if( ic->acc->prpl->keepalive ) |
---|
252 | ic->acc->prpl->keepalive( ic ); |
---|
253 | |
---|
254 | return TRUE; |
---|
255 | } |
---|
256 | |
---|
257 | void imcb_connected( struct im_connection *ic ) |
---|
258 | { |
---|
259 | /* MSN servers sometimes redirect you to a different server and do |
---|
260 | the whole login sequence again, so these "late" calls to this |
---|
261 | function should be handled correctly. (IOW, ignored) */ |
---|
262 | if( ic->flags & OPT_LOGGED_IN ) |
---|
263 | return; |
---|
264 | |
---|
265 | imcb_log( ic, "Logged in" ); |
---|
266 | |
---|
267 | ic->keepalive = b_timeout_add( 60000, send_keepalive, ic ); |
---|
268 | ic->flags |= OPT_LOGGED_IN; |
---|
269 | |
---|
270 | /* Necessary to send initial presence status, even if we're not away. */ |
---|
271 | imc_away_send_update( ic ); |
---|
272 | |
---|
273 | /* Apparently we're connected successfully, so reset the |
---|
274 | exponential backoff timer. */ |
---|
275 | ic->acc->auto_reconnect_delay = 0; |
---|
276 | |
---|
277 | /* |
---|
278 | for( c = irc->chatrooms; c; c = c->next ) |
---|
279 | { |
---|
280 | if( c->acc != ic->acc ) |
---|
281 | continue; |
---|
282 | |
---|
283 | if( set_getbool( &c->set, "auto_join" ) ) |
---|
284 | chat_join( irc, c, NULL ); |
---|
285 | } |
---|
286 | */ |
---|
287 | } |
---|
288 | |
---|
289 | gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond ) |
---|
290 | { |
---|
291 | account_t *a = data; |
---|
292 | |
---|
293 | a->reconnect = 0; |
---|
294 | account_on( a->bee, a ); |
---|
295 | |
---|
296 | return( FALSE ); /* Only have to run the timeout once */ |
---|
297 | } |
---|
298 | |
---|
299 | void cancel_auto_reconnect( account_t *a ) |
---|
300 | { |
---|
301 | b_event_remove( a->reconnect ); |
---|
302 | a->reconnect = 0; |
---|
303 | } |
---|
304 | |
---|
305 | void imc_logout( struct im_connection *ic, int allow_reconnect ) |
---|
306 | { |
---|
307 | bee_t *bee = ic->bee; |
---|
308 | account_t *a; |
---|
309 | GSList *l; |
---|
310 | int delay; |
---|
311 | |
---|
312 | /* Nested calls might happen sometimes, this is probably the best |
---|
313 | place to catch them. */ |
---|
314 | if( ic->flags & OPT_LOGGING_OUT ) |
---|
315 | return; |
---|
316 | else |
---|
317 | ic->flags |= OPT_LOGGING_OUT; |
---|
318 | |
---|
319 | imcb_log( ic, "Signing off.." ); |
---|
320 | |
---|
321 | b_event_remove( ic->keepalive ); |
---|
322 | ic->keepalive = 0; |
---|
323 | ic->acc->prpl->logout( ic ); |
---|
324 | b_event_remove( ic->inpa ); |
---|
325 | |
---|
326 | g_free( ic->away ); |
---|
327 | ic->away = NULL; |
---|
328 | |
---|
329 | for( l = bee->users; l; ) |
---|
330 | { |
---|
331 | bee_user_t *bu = l->data; |
---|
332 | GSList *next = l->next; |
---|
333 | |
---|
334 | if( bu->ic == ic ) |
---|
335 | bee_user_free( bee, bu ); |
---|
336 | |
---|
337 | l = next; |
---|
338 | } |
---|
339 | |
---|
340 | //query_del_by_conn( ic->irc, ic ); |
---|
341 | |
---|
342 | for( a = bee->accounts; a; a = a->next ) |
---|
343 | if( a->ic == ic ) |
---|
344 | break; |
---|
345 | |
---|
346 | if( !a ) |
---|
347 | { |
---|
348 | /* Uhm... This is very sick. */ |
---|
349 | } |
---|
350 | else if( allow_reconnect && set_getbool( &bee->set, "auto_reconnect" ) && |
---|
351 | set_getbool( &a->set, "auto_reconnect" ) && |
---|
352 | ( delay = account_reconnect_delay( a ) ) > 0 ) |
---|
353 | { |
---|
354 | imcb_log( ic, "Reconnecting in %d seconds..", delay ); |
---|
355 | a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a ); |
---|
356 | } |
---|
357 | |
---|
358 | imc_free( ic ); |
---|
359 | } |
---|
360 | |
---|
361 | void imcb_ask( struct im_connection *ic, char *msg, void *data, |
---|
362 | query_callback doit, query_callback dont ) |
---|
363 | { |
---|
364 | query_add( (irc_t *) ic->bee->ui_data, ic, msg, doit, dont, data ); |
---|
365 | } |
---|
366 | |
---|
367 | void imcb_add_buddy( struct im_connection *ic, const char *handle, const char *group ) |
---|
368 | { |
---|
369 | bee_user_t *bu; |
---|
370 | bee_t *bee = ic->bee; |
---|
371 | |
---|
372 | if( bee_user_by_handle( bee, ic, handle ) ) |
---|
373 | { |
---|
374 | if( set_getbool( &bee->set, "debug" ) ) |
---|
375 | imcb_log( ic, "User already exists, ignoring add request: %s", handle ); |
---|
376 | |
---|
377 | return; |
---|
378 | |
---|
379 | /* Buddy seems to exist already. Let's ignore this request then... |
---|
380 | Eventually subsequent calls to this function *should* be possible |
---|
381 | when a buddy is in multiple groups. But for now BitlBee doesn't |
---|
382 | even support groups so let's silently ignore this for now. */ |
---|
383 | } |
---|
384 | |
---|
385 | bu = bee_user_new( bee, ic, handle ); |
---|
386 | bu->group = g_strdup( group ); |
---|
387 | } |
---|
388 | |
---|
389 | void imcb_rename_buddy( struct im_connection *ic, const char *handle, const char *fullname ) |
---|
390 | { |
---|
391 | bee_t *bee = ic->bee; |
---|
392 | bee_user_t *bu = bee_user_by_handle( bee, ic, handle ); |
---|
393 | |
---|
394 | if( !bu || !fullname ) return; |
---|
395 | |
---|
396 | if( !bu->fullname || strcmp( bu->fullname, fullname ) != 0 ) |
---|
397 | { |
---|
398 | g_free( bu->fullname ); |
---|
399 | bu->fullname = g_strdup( fullname ); |
---|
400 | |
---|
401 | if( bee->ui->user_fullname ) |
---|
402 | bee->ui->user_fullname( bee, bu ); |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | void imcb_remove_buddy( struct im_connection *ic, const char *handle, char *group ) |
---|
407 | { |
---|
408 | bee_user_free( ic->bee, bee_user_by_handle( ic->bee, ic, handle ) ); |
---|
409 | } |
---|
410 | |
---|
411 | /* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM |
---|
412 | modules to suggest a nickname for a handle. */ |
---|
413 | void imcb_buddy_nick_hint( struct im_connection *ic, const char *handle, const char *nick ) |
---|
414 | { |
---|
415 | #if 0 |
---|
416 | user_t *u = user_findhandle( ic, handle ); |
---|
417 | char newnick[MAX_NICK_LENGTH+1], *orig_nick; |
---|
418 | |
---|
419 | if( u && !u->online && !nick_saved( ic->acc, handle ) ) |
---|
420 | { |
---|
421 | /* Only do this if the person isn't online yet (which should |
---|
422 | be the case if we just added it) and if the user hasn't |
---|
423 | assigned a nickname to this buddy already. */ |
---|
424 | |
---|
425 | strncpy( newnick, nick, MAX_NICK_LENGTH ); |
---|
426 | newnick[MAX_NICK_LENGTH] = 0; |
---|
427 | |
---|
428 | /* Some processing to make sure this string is a valid IRC nickname. */ |
---|
429 | nick_strip( newnick ); |
---|
430 | if( set_getbool( &ic->bee->set, "lcnicks" ) ) |
---|
431 | nick_lc( newnick ); |
---|
432 | |
---|
433 | if( strcmp( u->nick, newnick ) != 0 ) |
---|
434 | { |
---|
435 | /* Only do this if newnick is different from the current one. |
---|
436 | If rejoining a channel, maybe we got this nick already |
---|
437 | (and dedupe would only add an underscore. */ |
---|
438 | nick_dedupe( ic->acc, handle, newnick ); |
---|
439 | |
---|
440 | /* u->nick will be freed halfway the process, so it can't be |
---|
441 | passed as an argument. */ |
---|
442 | orig_nick = g_strdup( u->nick ); |
---|
443 | user_rename( ic->irc, orig_nick, newnick ); |
---|
444 | g_free( orig_nick ); |
---|
445 | } |
---|
446 | } |
---|
447 | #endif |
---|
448 | } |
---|
449 | |
---|
450 | |
---|
451 | struct imcb_ask_cb_data |
---|
452 | { |
---|
453 | struct im_connection *ic; |
---|
454 | char *handle; |
---|
455 | }; |
---|
456 | |
---|
457 | static void imcb_ask_auth_cb_no( void *data ) |
---|
458 | { |
---|
459 | struct imcb_ask_cb_data *cbd = data; |
---|
460 | |
---|
461 | cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle ); |
---|
462 | |
---|
463 | g_free( cbd->handle ); |
---|
464 | g_free( cbd ); |
---|
465 | } |
---|
466 | |
---|
467 | static void imcb_ask_auth_cb_yes( void *data ) |
---|
468 | { |
---|
469 | struct imcb_ask_cb_data *cbd = data; |
---|
470 | |
---|
471 | cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle ); |
---|
472 | |
---|
473 | g_free( cbd->handle ); |
---|
474 | g_free( cbd ); |
---|
475 | } |
---|
476 | |
---|
477 | void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname ) |
---|
478 | { |
---|
479 | struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); |
---|
480 | char *s, *realname_ = NULL; |
---|
481 | |
---|
482 | if( realname != NULL ) |
---|
483 | realname_ = g_strdup_printf( " (%s)", realname ); |
---|
484 | |
---|
485 | s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.", |
---|
486 | handle, realname_ ?: "" ); |
---|
487 | |
---|
488 | g_free( realname_ ); |
---|
489 | |
---|
490 | data->ic = ic; |
---|
491 | data->handle = g_strdup( handle ); |
---|
492 | query_add( (irc_t *) ic->bee->ui_data, ic, s, |
---|
493 | imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data ); |
---|
494 | } |
---|
495 | |
---|
496 | |
---|
497 | static void imcb_ask_add_cb_no( void *data ) |
---|
498 | { |
---|
499 | g_free( ((struct imcb_ask_cb_data*)data)->handle ); |
---|
500 | g_free( data ); |
---|
501 | } |
---|
502 | |
---|
503 | static void imcb_ask_add_cb_yes( void *data ) |
---|
504 | { |
---|
505 | struct imcb_ask_cb_data *cbd = data; |
---|
506 | |
---|
507 | cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL ); |
---|
508 | |
---|
509 | return imcb_ask_add_cb_no( data ); |
---|
510 | } |
---|
511 | |
---|
512 | void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname ) |
---|
513 | { |
---|
514 | struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); |
---|
515 | char *s; |
---|
516 | |
---|
517 | /* TODO: Make a setting for this! */ |
---|
518 | if( bee_user_by_handle( ic->bee, ic, handle ) != NULL ) |
---|
519 | return; |
---|
520 | |
---|
521 | s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle ); |
---|
522 | |
---|
523 | data->ic = ic; |
---|
524 | data->handle = g_strdup( handle ); |
---|
525 | query_add( (irc_t *) ic->bee->ui_data, ic, s, |
---|
526 | imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data ); |
---|
527 | } |
---|
528 | |
---|
529 | void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ) |
---|
530 | { |
---|
531 | #if 0 |
---|
532 | user_t *u; |
---|
533 | |
---|
534 | if( !set_getbool( &ic->bee->set, "typing_notice" ) ) |
---|
535 | return; |
---|
536 | |
---|
537 | if( ( u = user_findhandle( ic, handle ) ) ) |
---|
538 | { |
---|
539 | char buf[256]; |
---|
540 | |
---|
541 | g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 ); |
---|
542 | irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf ); |
---|
543 | } |
---|
544 | #endif |
---|
545 | } |
---|
546 | |
---|
547 | struct bee_user *imcb_buddy_by_handle( struct im_connection *ic, const char *handle ) |
---|
548 | { |
---|
549 | return bee_user_by_handle( ic->bee, ic, handle ); |
---|
550 | } |
---|
551 | |
---|
552 | struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle ) |
---|
553 | { |
---|
554 | #if 0 |
---|
555 | struct groupchat *c; |
---|
556 | |
---|
557 | /* This one just creates the conversation structure, user won't see anything yet */ |
---|
558 | |
---|
559 | if( ic->groupchats ) |
---|
560 | { |
---|
561 | for( c = ic->groupchats; c->next; c = c->next ); |
---|
562 | c = c->next = g_new0( struct groupchat, 1 ); |
---|
563 | } |
---|
564 | else |
---|
565 | ic->groupchats = c = g_new0( struct groupchat, 1 ); |
---|
566 | |
---|
567 | c->ic = ic; |
---|
568 | c->title = g_strdup( handle ); |
---|
569 | c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ ); |
---|
570 | c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); |
---|
571 | |
---|
572 | if( set_getbool( &ic->bee->set, "debug" ) ) |
---|
573 | imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle ); |
---|
574 | |
---|
575 | return c; |
---|
576 | #endif |
---|
577 | return NULL; |
---|
578 | } |
---|
579 | |
---|
580 | void imcb_chat_free( struct groupchat *c ) |
---|
581 | { |
---|
582 | #if 0 |
---|
583 | struct im_connection *ic = c->ic; |
---|
584 | struct groupchat *l; |
---|
585 | GList *ir; |
---|
586 | |
---|
587 | if( set_getbool( &ic->bee->set, "debug" ) ) |
---|
588 | imcb_log( ic, "You were removed from conversation %p", c ); |
---|
589 | |
---|
590 | if( c ) |
---|
591 | { |
---|
592 | if( c->joined ) |
---|
593 | { |
---|
594 | user_t *u, *r; |
---|
595 | |
---|
596 | r = user_find( ic->irc, ic->irc->mynick ); |
---|
597 | irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" ); |
---|
598 | |
---|
599 | u = user_find( ic->irc, ic->irc->nick ); |
---|
600 | irc_kick( ic->irc, u, c->channel, r ); |
---|
601 | /* irc_part( ic->irc, u, c->channel ); */ |
---|
602 | } |
---|
603 | |
---|
604 | /* Find the previous chat in the linked list. */ |
---|
605 | for( l = ic->groupchats; l && l->next != c; l = l->next ); |
---|
606 | |
---|
607 | if( l ) |
---|
608 | l->next = c->next; |
---|
609 | else |
---|
610 | ic->groupchats = c->next; |
---|
611 | |
---|
612 | for( ir = c->in_room; ir; ir = ir->next ) |
---|
613 | g_free( ir->data ); |
---|
614 | g_list_free( c->in_room ); |
---|
615 | g_free( c->channel ); |
---|
616 | g_free( c->title ); |
---|
617 | g_free( c->topic ); |
---|
618 | g_free( c ); |
---|
619 | } |
---|
620 | #endif |
---|
621 | } |
---|
622 | |
---|
623 | void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at ) |
---|
624 | { |
---|
625 | #if 0 |
---|
626 | struct im_connection *ic = c->ic; |
---|
627 | char *wrapped; |
---|
628 | user_t *u; |
---|
629 | |
---|
630 | /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */ |
---|
631 | if( g_strcasecmp( who, ic->acc->user ) == 0 ) |
---|
632 | return; |
---|
633 | |
---|
634 | u = user_findhandle( ic, who ); |
---|
635 | |
---|
636 | if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || |
---|
637 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) |
---|
638 | strip_html( msg ); |
---|
639 | |
---|
640 | wrapped = word_wrap( msg, 425 ); |
---|
641 | if( c && u ) |
---|
642 | { |
---|
643 | irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", wrapped ); |
---|
644 | } |
---|
645 | else |
---|
646 | { |
---|
647 | imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped ); |
---|
648 | } |
---|
649 | g_free( wrapped ); |
---|
650 | #endif |
---|
651 | } |
---|
652 | |
---|
653 | void imcb_chat_log( struct groupchat *c, char *format, ... ) |
---|
654 | { |
---|
655 | #if 0 |
---|
656 | irc_t *irc = c->ic->irc; |
---|
657 | va_list params; |
---|
658 | char *text; |
---|
659 | user_t *u; |
---|
660 | |
---|
661 | va_start( params, format ); |
---|
662 | text = g_strdup_vprintf( format, params ); |
---|
663 | va_end( params ); |
---|
664 | |
---|
665 | u = user_find( irc, irc->mynick ); |
---|
666 | |
---|
667 | irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text ); |
---|
668 | |
---|
669 | g_free( text ); |
---|
670 | #endif |
---|
671 | } |
---|
672 | |
---|
673 | void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at ) |
---|
674 | { |
---|
675 | #if 0 |
---|
676 | struct im_connection *ic = c->ic; |
---|
677 | user_t *u = NULL; |
---|
678 | |
---|
679 | if( who == NULL) |
---|
680 | u = user_find( ic->irc, ic->irc->mynick ); |
---|
681 | else if( g_strcasecmp( who, ic->acc->user ) == 0 ) |
---|
682 | u = user_find( ic->irc, ic->irc->nick ); |
---|
683 | else |
---|
684 | u = user_findhandle( ic, who ); |
---|
685 | |
---|
686 | if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || |
---|
687 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) |
---|
688 | strip_html( topic ); |
---|
689 | |
---|
690 | g_free( c->topic ); |
---|
691 | c->topic = g_strdup( topic ); |
---|
692 | |
---|
693 | if( c->joined && u ) |
---|
694 | irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic ); |
---|
695 | #endif |
---|
696 | } |
---|
697 | |
---|
698 | void imcb_chat_add_buddy( struct groupchat *b, const char *handle ) |
---|
699 | { |
---|
700 | #if 0 |
---|
701 | user_t *u = user_findhandle( b->ic, handle ); |
---|
702 | int me = 0; |
---|
703 | |
---|
704 | if( set_getbool( &b->ic->bee->set, "debug" ) ) |
---|
705 | imcb_log( b->ic, "User %s added to conversation %p", handle, b ); |
---|
706 | |
---|
707 | /* It might be yourself! */ |
---|
708 | if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 ) |
---|
709 | { |
---|
710 | u = user_find( b->ic->irc, b->ic->irc->nick ); |
---|
711 | if( !b->joined ) |
---|
712 | irc_join( b->ic->irc, u, b->channel ); |
---|
713 | b->joined = me = 1; |
---|
714 | } |
---|
715 | |
---|
716 | /* Most protocols allow people to join, even when they're not in |
---|
717 | your contact list. Try to handle that here */ |
---|
718 | if( !u ) |
---|
719 | { |
---|
720 | imcb_add_buddy( b->ic, handle, NULL ); |
---|
721 | u = user_findhandle( b->ic, handle ); |
---|
722 | } |
---|
723 | |
---|
724 | /* Add the handle to the room userlist, if it's not 'me' */ |
---|
725 | if( !me ) |
---|
726 | { |
---|
727 | if( b->joined ) |
---|
728 | irc_join( b->ic->irc, u, b->channel ); |
---|
729 | b->in_room = g_list_append( b->in_room, g_strdup( handle ) ); |
---|
730 | } |
---|
731 | #endif |
---|
732 | } |
---|
733 | |
---|
734 | /* This function is one BIG hack... :-( EREWRITE */ |
---|
735 | void imcb_chat_remove_buddy( struct groupchat *b, const char *handle, const char *reason ) |
---|
736 | { |
---|
737 | #if 0 |
---|
738 | user_t *u; |
---|
739 | int me = 0; |
---|
740 | |
---|
741 | if( set_getbool( &b->ic->bee->set, "debug" ) ) |
---|
742 | imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" ); |
---|
743 | |
---|
744 | /* It might be yourself! */ |
---|
745 | if( g_strcasecmp( handle, b->ic->acc->user ) == 0 ) |
---|
746 | { |
---|
747 | if( b->joined == 0 ) |
---|
748 | return; |
---|
749 | |
---|
750 | u = user_find( b->ic->irc, b->ic->irc->nick ); |
---|
751 | b->joined = 0; |
---|
752 | me = 1; |
---|
753 | } |
---|
754 | else |
---|
755 | { |
---|
756 | u = user_findhandle( b->ic, handle ); |
---|
757 | } |
---|
758 | |
---|
759 | if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) ) |
---|
760 | irc_part( b->ic->irc, u, b->channel ); |
---|
761 | #endif |
---|
762 | } |
---|
763 | |
---|
764 | #if 0 |
---|
765 | static int remove_chat_buddy_silent( struct groupchat *b, const char *handle ) |
---|
766 | { |
---|
767 | GList *i; |
---|
768 | |
---|
769 | /* Find the handle in the room userlist and shoot it */ |
---|
770 | i = b->in_room; |
---|
771 | while( i ) |
---|
772 | { |
---|
773 | if( g_strcasecmp( handle, i->data ) == 0 ) |
---|
774 | { |
---|
775 | g_free( i->data ); |
---|
776 | b->in_room = g_list_remove( b->in_room, i->data ); |
---|
777 | return( 1 ); |
---|
778 | } |
---|
779 | |
---|
780 | i = i->next; |
---|
781 | } |
---|
782 | |
---|
783 | return 0; |
---|
784 | } |
---|
785 | #endif |
---|
786 | |
---|
787 | |
---|
788 | /* Misc. BitlBee stuff which shouldn't really be here */ |
---|
789 | #if 0 |
---|
790 | char *set_eval_away_devoice( set_t *set, char *value ) |
---|
791 | { |
---|
792 | irc_t *irc = set->data; |
---|
793 | int st; |
---|
794 | |
---|
795 | if( !is_bool( value ) ) |
---|
796 | return SET_INVALID; |
---|
797 | |
---|
798 | st = bool2int( value ); |
---|
799 | |
---|
800 | /* Horror.... */ |
---|
801 | |
---|
802 | if( st != set_getbool( &irc->b->set, "away_devoice" ) ) |
---|
803 | { |
---|
804 | char list[80] = ""; |
---|
805 | user_t *u = irc->users; |
---|
806 | int i = 0, count = 0; |
---|
807 | char pm; |
---|
808 | char v[80]; |
---|
809 | |
---|
810 | if( st ) |
---|
811 | pm = '+'; |
---|
812 | else |
---|
813 | pm = '-'; |
---|
814 | |
---|
815 | while( u ) |
---|
816 | { |
---|
817 | if( u->ic && u->online && !u->away ) |
---|
818 | { |
---|
819 | if( ( strlen( list ) + strlen( u->nick ) ) >= 79 ) |
---|
820 | { |
---|
821 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
822 | irc_write( irc, ":%s MODE %s %c%s%s", |
---|
823 | irc->myhost, |
---|
824 | irc->channel, pm, v, list ); |
---|
825 | |
---|
826 | *list = 0; |
---|
827 | count = 0; |
---|
828 | } |
---|
829 | |
---|
830 | sprintf( list + strlen( list ), " %s", u->nick ); |
---|
831 | count ++; |
---|
832 | } |
---|
833 | u = u->next; |
---|
834 | } |
---|
835 | |
---|
836 | /* $v = 'v' x $i */ |
---|
837 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
838 | irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost, |
---|
839 | irc->channel, pm, v, list ); |
---|
840 | } |
---|
841 | |
---|
842 | return value; |
---|
843 | } |
---|
844 | #endif |
---|
845 | |
---|
846 | |
---|
847 | |
---|
848 | /* The plan is to not allow straight calls to prpl functions anymore, but do |
---|
849 | them all from some wrappers. We'll start to define some down here: */ |
---|
850 | |
---|
851 | int imc_chat_msg( struct groupchat *c, char *msg, int flags ) |
---|
852 | { |
---|
853 | char *buf = NULL; |
---|
854 | |
---|
855 | if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
856 | { |
---|
857 | buf = escape_html( msg ); |
---|
858 | msg = buf; |
---|
859 | } |
---|
860 | |
---|
861 | c->ic->acc->prpl->chat_msg( c, msg, flags ); |
---|
862 | g_free( buf ); |
---|
863 | |
---|
864 | return 1; |
---|
865 | } |
---|
866 | |
---|
867 | static char *imc_away_state_find( GList *gcm, char *away, char **message ); |
---|
868 | |
---|
869 | int imc_away_send_update( struct im_connection *ic ) |
---|
870 | { |
---|
871 | char *away, *msg = NULL; |
---|
872 | |
---|
873 | away = set_getstr( &ic->acc->set, "away" ) ? |
---|
874 | : set_getstr( &ic->bee->set, "away" ); |
---|
875 | if( away && *away ) |
---|
876 | { |
---|
877 | GList *m = ic->acc->prpl->away_states( ic ); |
---|
878 | msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL; |
---|
879 | away = imc_away_state_find( m, away, &msg ) ? : m->data; |
---|
880 | } |
---|
881 | else if( ic->acc->flags & ACC_FLAG_STATUS_MESSAGE ) |
---|
882 | { |
---|
883 | away = NULL; |
---|
884 | msg = set_getstr( &ic->acc->set, "status" ) ? |
---|
885 | : set_getstr( &ic->bee->set, "status" ); |
---|
886 | } |
---|
887 | |
---|
888 | ic->acc->prpl->set_away( ic, away, msg ); |
---|
889 | |
---|
890 | return 1; |
---|
891 | } |
---|
892 | |
---|
893 | static char *imc_away_alias_list[8][5] = |
---|
894 | { |
---|
895 | { "Away from computer", "Away", "Extended away", NULL }, |
---|
896 | { "NA", "N/A", "Not available", NULL }, |
---|
897 | { "Busy", "Do not disturb", "DND", "Occupied", NULL }, |
---|
898 | { "Be right back", "BRB", NULL }, |
---|
899 | { "On the phone", "Phone", "On phone", NULL }, |
---|
900 | { "Out to lunch", "Lunch", "Food", NULL }, |
---|
901 | { "Invisible", "Hidden" }, |
---|
902 | { NULL } |
---|
903 | }; |
---|
904 | |
---|
905 | static char *imc_away_state_find( GList *gcm, char *away, char **message ) |
---|
906 | { |
---|
907 | GList *m; |
---|
908 | int i, j; |
---|
909 | |
---|
910 | for( m = gcm; m; m = m->next ) |
---|
911 | if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 ) |
---|
912 | { |
---|
913 | /* At least the Yahoo! module works better if message |
---|
914 | contains no data unless it adds something to what |
---|
915 | we have in state already. */ |
---|
916 | if( strlen( m->data ) == strlen( away ) ) |
---|
917 | *message = NULL; |
---|
918 | |
---|
919 | return m->data; |
---|
920 | } |
---|
921 | |
---|
922 | for( i = 0; *imc_away_alias_list[i]; i ++ ) |
---|
923 | { |
---|
924 | int keep_message; |
---|
925 | |
---|
926 | for( j = 0; imc_away_alias_list[i][j]; j ++ ) |
---|
927 | if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 ) |
---|
928 | { |
---|
929 | keep_message = strlen( away ) != strlen( imc_away_alias_list[i][j] ); |
---|
930 | break; |
---|
931 | } |
---|
932 | |
---|
933 | if( !imc_away_alias_list[i][j] ) /* If we reach the end, this row */ |
---|
934 | continue; /* is not what we want. Next! */ |
---|
935 | |
---|
936 | /* Now find an entry in this row which exists in gcm */ |
---|
937 | for( j = 0; imc_away_alias_list[i][j]; j ++ ) |
---|
938 | { |
---|
939 | for( m = gcm; m; m = m->next ) |
---|
940 | if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 ) |
---|
941 | { |
---|
942 | if( !keep_message ) |
---|
943 | *message = NULL; |
---|
944 | |
---|
945 | return imc_away_alias_list[i][j]; |
---|
946 | } |
---|
947 | } |
---|
948 | |
---|
949 | /* No need to look further, apparently this state doesn't |
---|
950 | have any good alias for this protocol. */ |
---|
951 | break; |
---|
952 | } |
---|
953 | |
---|
954 | return NULL; |
---|
955 | } |
---|
956 | |
---|
957 | void imc_add_allow( struct im_connection *ic, char *handle ) |
---|
958 | { |
---|
959 | if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) |
---|
960 | { |
---|
961 | ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) ); |
---|
962 | } |
---|
963 | |
---|
964 | ic->acc->prpl->add_permit( ic, handle ); |
---|
965 | } |
---|
966 | |
---|
967 | void imc_rem_allow( struct im_connection *ic, char *handle ) |
---|
968 | { |
---|
969 | GSList *l; |
---|
970 | |
---|
971 | if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) |
---|
972 | { |
---|
973 | g_free( l->data ); |
---|
974 | ic->permit = g_slist_delete_link( ic->permit, l ); |
---|
975 | } |
---|
976 | |
---|
977 | ic->acc->prpl->rem_permit( ic, handle ); |
---|
978 | } |
---|
979 | |
---|
980 | void imc_add_block( struct im_connection *ic, char *handle ) |
---|
981 | { |
---|
982 | if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) |
---|
983 | { |
---|
984 | ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) ); |
---|
985 | } |
---|
986 | |
---|
987 | ic->acc->prpl->add_deny( ic, handle ); |
---|
988 | } |
---|
989 | |
---|
990 | void imc_rem_block( struct im_connection *ic, char *handle ) |
---|
991 | { |
---|
992 | GSList *l; |
---|
993 | |
---|
994 | if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) |
---|
995 | { |
---|
996 | g_free( l->data ); |
---|
997 | ic->deny = g_slist_delete_link( ic->deny, l ); |
---|
998 | } |
---|
999 | |
---|
1000 | ic->acc->prpl->rem_deny( ic, handle ); |
---|
1001 | } |
---|
1002 | |
---|
1003 | void imcb_clean_handle( struct im_connection *ic, char *handle ) |
---|
1004 | { |
---|
1005 | /* Accepts a handle and does whatever is necessary to make it |
---|
1006 | BitlBee-friendly. Currently this means removing everything |
---|
1007 | outside 33-127 (ASCII printable excl spaces), @ (only one |
---|
1008 | is allowed) and ! and : */ |
---|
1009 | char out[strlen(handle)+1]; |
---|
1010 | int s, d; |
---|
1011 | |
---|
1012 | s = d = 0; |
---|
1013 | while( handle[s] ) |
---|
1014 | { |
---|
1015 | if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' && |
---|
1016 | ( handle[s] & 0x80 ) == 0 ) |
---|
1017 | { |
---|
1018 | if( handle[s] == '@' ) |
---|
1019 | { |
---|
1020 | /* See if we got an @ already? */ |
---|
1021 | out[d] = 0; |
---|
1022 | if( strchr( out, '@' ) ) |
---|
1023 | continue; |
---|
1024 | } |
---|
1025 | |
---|
1026 | out[d++] = handle[s]; |
---|
1027 | } |
---|
1028 | s ++; |
---|
1029 | } |
---|
1030 | out[d] = handle[s]; |
---|
1031 | |
---|
1032 | strcpy( handle, out ); |
---|
1033 | } |
---|