1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * libpurple module - Main file * |
---|
5 | * * |
---|
6 | * Copyright 2009 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
7 | * * |
---|
8 | * This program is free software; you can redistribute it and/or modify * |
---|
9 | * it under the terms of the GNU General Public License as published by * |
---|
10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
11 | * (at your option) any later version. * |
---|
12 | * * |
---|
13 | * This program is distributed in the hope that it will be useful, * |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
16 | * GNU General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU General Public License along * |
---|
19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
21 | * * |
---|
22 | \***************************************************************************/ |
---|
23 | |
---|
24 | #include <glib.h> |
---|
25 | #include <purple.h> |
---|
26 | |
---|
27 | #include "bitlbee.h" |
---|
28 | |
---|
29 | GSList *purple_connections; |
---|
30 | |
---|
31 | static struct im_connection *purple_ic_by_pa( PurpleAccount *pa ) |
---|
32 | { |
---|
33 | GSList *i; |
---|
34 | |
---|
35 | for( i = purple_connections; i; i = i->next ) |
---|
36 | if( ((struct im_connection *)i->data)->proto_data == pa ) |
---|
37 | return i->data; |
---|
38 | |
---|
39 | return NULL; |
---|
40 | } |
---|
41 | |
---|
42 | static struct im_connection *purple_ic_by_gc( PurpleConnection *gc ) |
---|
43 | { |
---|
44 | return purple_ic_by_pa( purple_connection_get_account( gc ) ); |
---|
45 | } |
---|
46 | |
---|
47 | static void purple_init( account_t *acc ) |
---|
48 | { |
---|
49 | PurplePlugin *prpl = purple_plugins_find_with_id( acc->prpl->name ); |
---|
50 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
51 | GList *i; |
---|
52 | |
---|
53 | for( i = pi->protocol_options; i; i = i->next ) |
---|
54 | { |
---|
55 | PurpleAccountOption *o = i->data; |
---|
56 | const char *name; |
---|
57 | char *def = NULL; |
---|
58 | set_eval eval = NULL; |
---|
59 | set_t *s; |
---|
60 | |
---|
61 | name = purple_account_option_get_setting( o ); |
---|
62 | |
---|
63 | switch( purple_account_option_get_type( o ) ) |
---|
64 | { |
---|
65 | case PURPLE_PREF_STRING: |
---|
66 | def = g_strdup( purple_account_option_get_default_string( o ) ); |
---|
67 | break; |
---|
68 | |
---|
69 | case PURPLE_PREF_INT: |
---|
70 | def = g_strdup_printf( "%d", purple_account_option_get_default_int( o ) ); |
---|
71 | eval = set_eval_int; |
---|
72 | break; |
---|
73 | |
---|
74 | case PURPLE_PREF_BOOLEAN: |
---|
75 | if( purple_account_option_get_default_bool( o ) ) |
---|
76 | def = g_strdup( "true" ); |
---|
77 | else |
---|
78 | def = g_strdup( "false" ); |
---|
79 | eval = set_eval_bool; |
---|
80 | break; |
---|
81 | |
---|
82 | default: |
---|
83 | fprintf( stderr, "Setting with unknown type: %s (%d)\n", name, purple_account_option_get_type( o ) ); |
---|
84 | } |
---|
85 | |
---|
86 | if( def != NULL ) |
---|
87 | { |
---|
88 | s = set_add( &acc->set, name, def, eval, acc ); |
---|
89 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
90 | g_free( def ); |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | static void purple_sync_settings( account_t *acc, PurpleAccount *pa ) |
---|
96 | { |
---|
97 | PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id ); |
---|
98 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
99 | GList *i; |
---|
100 | |
---|
101 | for( i = pi->protocol_options; i; i = i->next ) |
---|
102 | { |
---|
103 | PurpleAccountOption *o = i->data; |
---|
104 | const char *name; |
---|
105 | set_t *s; |
---|
106 | |
---|
107 | name = purple_account_option_get_setting( o ); |
---|
108 | s = set_find( &acc->set, name ); |
---|
109 | if( s->value == NULL ) |
---|
110 | continue; |
---|
111 | |
---|
112 | switch( purple_account_option_get_type( o ) ) |
---|
113 | { |
---|
114 | case PURPLE_PREF_STRING: |
---|
115 | purple_account_set_string( pa, name, set_getstr( &acc->set, name ) ); |
---|
116 | break; |
---|
117 | |
---|
118 | case PURPLE_PREF_INT: |
---|
119 | purple_account_set_int( pa, name, set_getint( &acc->set, name ) ); |
---|
120 | break; |
---|
121 | |
---|
122 | case PURPLE_PREF_BOOLEAN: |
---|
123 | purple_account_set_bool( pa, name, set_getbool( &acc->set, name ) ); |
---|
124 | break; |
---|
125 | |
---|
126 | default: |
---|
127 | break; |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | static void purple_login( account_t *acc ) |
---|
133 | { |
---|
134 | struct im_connection *ic = imcb_new( acc ); |
---|
135 | PurpleAccount *pa; |
---|
136 | |
---|
137 | /* For now this is needed in the _connected() handlers if using |
---|
138 | GLib event handling, to make sure we're not handling events |
---|
139 | on dead connections. */ |
---|
140 | purple_connections = g_slist_prepend( purple_connections, ic ); |
---|
141 | |
---|
142 | ic->proto_data = pa = purple_account_new( acc->user, acc->prpl->name ); |
---|
143 | purple_account_set_password( pa, acc->pass ); |
---|
144 | purple_sync_settings( acc, pa ); |
---|
145 | |
---|
146 | purple_account_set_enabled( pa, "BitlBee", TRUE ); |
---|
147 | } |
---|
148 | |
---|
149 | static void purple_logout( struct im_connection *ic ) |
---|
150 | { |
---|
151 | PurpleAccount *pa = ic->proto_data; |
---|
152 | |
---|
153 | purple_account_set_enabled( pa, "BitlBee", FALSE ); |
---|
154 | purple_connections = g_slist_remove( purple_connections, ic ); |
---|
155 | purple_accounts_remove( pa ); |
---|
156 | } |
---|
157 | |
---|
158 | static int purple_buddy_msg( struct im_connection *ic, char *who, char *message, int flags ) |
---|
159 | { |
---|
160 | PurpleConversation *conv; |
---|
161 | |
---|
162 | if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_IM, |
---|
163 | who, ic->proto_data ) ) == NULL ) |
---|
164 | { |
---|
165 | conv = purple_conversation_new( PURPLE_CONV_TYPE_IM, |
---|
166 | ic->proto_data, who ); |
---|
167 | } |
---|
168 | |
---|
169 | purple_conv_im_send( purple_conversation_get_im_data( conv ), message ); |
---|
170 | |
---|
171 | return 1; |
---|
172 | } |
---|
173 | |
---|
174 | static GList *purple_away_states( struct im_connection *ic ) |
---|
175 | { |
---|
176 | PurpleAccount *pa = ic->proto_data; |
---|
177 | GList *st, *ret = NULL; |
---|
178 | |
---|
179 | for( st = purple_account_get_status_types( pa ); st; st = st->next ) |
---|
180 | { |
---|
181 | printf( "%s\n", purple_status_type_get_name( st->data ) ); |
---|
182 | ret = g_list_append( ret, (void*) purple_status_type_get_name( st->data ) ); |
---|
183 | } |
---|
184 | |
---|
185 | return ret; |
---|
186 | } |
---|
187 | |
---|
188 | static void purple_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
189 | { |
---|
190 | PurpleAccount *pa = ic->proto_data; |
---|
191 | GList *status_types = purple_account_get_status_types( pa ), *st; |
---|
192 | PurpleStatusType *pst = NULL; |
---|
193 | |
---|
194 | for( st = status_types; st; st = st->next ) |
---|
195 | { |
---|
196 | pst = st->data; |
---|
197 | |
---|
198 | if( g_strcasecmp( state_txt, purple_status_type_get_name( pst ) ) == 0 ) |
---|
199 | break; |
---|
200 | } |
---|
201 | |
---|
202 | purple_account_set_status( pa, st ? purple_status_type_get_id( pst ) : "away", |
---|
203 | TRUE, "message", message, NULL ); |
---|
204 | } |
---|
205 | |
---|
206 | static void purple_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
207 | { |
---|
208 | } |
---|
209 | |
---|
210 | static void purple_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
211 | { |
---|
212 | } |
---|
213 | |
---|
214 | static void purple_keepalive( struct im_connection *ic ) |
---|
215 | { |
---|
216 | } |
---|
217 | |
---|
218 | static int purple_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
219 | { |
---|
220 | return 1; |
---|
221 | } |
---|
222 | |
---|
223 | static void purple_ui_init(); |
---|
224 | |
---|
225 | static PurpleCoreUiOps bee_core_uiops = |
---|
226 | { |
---|
227 | NULL, |
---|
228 | NULL, |
---|
229 | purple_ui_init, |
---|
230 | NULL, |
---|
231 | }; |
---|
232 | |
---|
233 | static void prplcb_conn_progress( PurpleConnection *gc, const char *text, size_t step, size_t step_count ) |
---|
234 | { |
---|
235 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
236 | |
---|
237 | imcb_log( ic, "%s", text ); |
---|
238 | } |
---|
239 | |
---|
240 | static void prplcb_conn_connected( PurpleConnection *gc ) |
---|
241 | { |
---|
242 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
243 | |
---|
244 | imcb_connected( ic ); |
---|
245 | |
---|
246 | if( gc->flags & PURPLE_CONNECTION_HTML ) |
---|
247 | ic->flags |= OPT_DOES_HTML; |
---|
248 | } |
---|
249 | |
---|
250 | static void prplcb_conn_disconnected( PurpleConnection *gc ) |
---|
251 | { |
---|
252 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
253 | |
---|
254 | if( ic != NULL ) |
---|
255 | { |
---|
256 | imc_logout( ic, TRUE ); |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | static void prplcb_conn_notice( PurpleConnection *gc, const char *text ) |
---|
261 | { |
---|
262 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
263 | |
---|
264 | if( ic != NULL ) |
---|
265 | imcb_log( ic, "%s", text ); |
---|
266 | } |
---|
267 | |
---|
268 | static void prplcb_conn_report_disconnect_reason( PurpleConnection *gc, PurpleConnectionError reason, const char *text ) |
---|
269 | { |
---|
270 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
271 | |
---|
272 | /* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login, |
---|
273 | should probably handle that. */ |
---|
274 | if( ic != NULL ) |
---|
275 | imcb_error( ic, "%s", text ); |
---|
276 | } |
---|
277 | |
---|
278 | static PurpleConnectionUiOps bee_conn_uiops = |
---|
279 | { |
---|
280 | prplcb_conn_progress, |
---|
281 | prplcb_conn_connected, |
---|
282 | prplcb_conn_disconnected, |
---|
283 | prplcb_conn_notice, |
---|
284 | NULL, |
---|
285 | NULL, |
---|
286 | NULL, |
---|
287 | prplcb_conn_report_disconnect_reason, |
---|
288 | }; |
---|
289 | |
---|
290 | static void prplcb_blist_new( PurpleBlistNode *node ) |
---|
291 | { |
---|
292 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
293 | |
---|
294 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
295 | { |
---|
296 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
297 | |
---|
298 | if( ic == NULL ) |
---|
299 | return; |
---|
300 | |
---|
301 | imcb_add_buddy( ic, bud->name, NULL ); |
---|
302 | if( bud->server_alias ) |
---|
303 | imcb_buddy_nick_hint( ic, bud->name, bud->server_alias ); |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | static void prplcb_blist_update( PurpleBuddyList *list, PurpleBlistNode *node ) |
---|
308 | { |
---|
309 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
310 | |
---|
311 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
312 | { |
---|
313 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
314 | PurpleStatus *as; |
---|
315 | int flags = 0; |
---|
316 | |
---|
317 | if( ic == NULL ) |
---|
318 | return; |
---|
319 | |
---|
320 | flags |= purple_presence_is_online( bud->presence ) ? OPT_LOGGED_IN : 0; |
---|
321 | flags |= purple_presence_is_available( bud->presence ) ? 0 : OPT_AWAY; |
---|
322 | |
---|
323 | as = purple_presence_get_active_status( bud->presence ); |
---|
324 | |
---|
325 | imcb_buddy_status( ic, bud->name, flags, purple_status_get_name( as ), |
---|
326 | purple_status_get_attr_string( as, "message" ) ); |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | static void prplcb_blist_remove( PurpleBuddyList *list, PurpleBlistNode *node ) |
---|
331 | { |
---|
332 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
333 | |
---|
334 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
335 | { |
---|
336 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
337 | |
---|
338 | if( ic == NULL ) |
---|
339 | return; |
---|
340 | |
---|
341 | imcb_remove_buddy( ic, bud->name, NULL ); |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | static PurpleBlistUiOps bee_blist_uiops = |
---|
346 | { |
---|
347 | NULL, |
---|
348 | prplcb_blist_new, |
---|
349 | NULL, |
---|
350 | prplcb_blist_update, |
---|
351 | prplcb_blist_remove, |
---|
352 | }; |
---|
353 | |
---|
354 | static void prplcb_conv_im( PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime ) |
---|
355 | { |
---|
356 | struct im_connection *ic = purple_ic_by_pa( conv->account ); |
---|
357 | |
---|
358 | /* ..._SEND means it's an outgoing message, no need to echo those. */ |
---|
359 | if( !( flags & PURPLE_MESSAGE_SEND ) ) |
---|
360 | imcb_buddy_msg( ic, (char*) who, (char*) message, 0, mtime ); |
---|
361 | } |
---|
362 | |
---|
363 | static PurpleConversationUiOps bee_conv_uiops = |
---|
364 | { |
---|
365 | NULL, /* create_conversation */ |
---|
366 | NULL, /* destroy_conversation */ |
---|
367 | NULL, /* write_chat */ |
---|
368 | prplcb_conv_im, /* write_im */ |
---|
369 | NULL, /* write_conv */ |
---|
370 | NULL, /* chat_add_users */ |
---|
371 | NULL, /* chat_rename_user */ |
---|
372 | NULL, /* chat_remove_users */ |
---|
373 | NULL, /* chat_update_user */ |
---|
374 | NULL, /* present */ |
---|
375 | NULL, /* has_focus */ |
---|
376 | NULL, /* custom_smiley_add */ |
---|
377 | NULL, /* custom_smiley_write */ |
---|
378 | NULL, /* custom_smiley_close */ |
---|
379 | NULL, /* send_confirm */ |
---|
380 | }; |
---|
381 | |
---|
382 | static void prplcb_debug_print( PurpleDebugLevel level, const char *category, const char *arg_s ) |
---|
383 | { |
---|
384 | printf( "DEBUG %s: %s", category, arg_s ); |
---|
385 | } |
---|
386 | |
---|
387 | static PurpleDebugUiOps bee_debug_uiops = |
---|
388 | { |
---|
389 | prplcb_debug_print, |
---|
390 | }; |
---|
391 | |
---|
392 | static guint prplcb_ev_timeout_add( guint interval, GSourceFunc func, gpointer udata ) |
---|
393 | { |
---|
394 | return b_timeout_add( interval, (b_event_handler) func, udata ); |
---|
395 | } |
---|
396 | |
---|
397 | static guint prplcb_ev_input_add( int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer udata ) |
---|
398 | { |
---|
399 | return b_input_add( fd, cond | B_EV_FLAG_FORCE_REPEAT, (b_event_handler) func, udata ); |
---|
400 | } |
---|
401 | |
---|
402 | static gboolean prplcb_ev_remove( guint id ) |
---|
403 | { |
---|
404 | b_event_remove( (gint) id ); |
---|
405 | return TRUE; |
---|
406 | } |
---|
407 | |
---|
408 | static PurpleEventLoopUiOps glib_eventloops = |
---|
409 | { |
---|
410 | prplcb_ev_timeout_add, |
---|
411 | prplcb_ev_remove, |
---|
412 | prplcb_ev_input_add, |
---|
413 | prplcb_ev_remove, |
---|
414 | }; |
---|
415 | |
---|
416 | static void purple_ui_init() |
---|
417 | { |
---|
418 | purple_blist_set_ui_ops( &bee_blist_uiops ); |
---|
419 | purple_connections_set_ui_ops( &bee_conn_uiops ); |
---|
420 | purple_conversations_set_ui_ops( &bee_conv_uiops ); |
---|
421 | //purple_debug_set_ui_ops( &bee_debug_uiops ); |
---|
422 | } |
---|
423 | |
---|
424 | void purple_initmodule() |
---|
425 | { |
---|
426 | GList *prots; |
---|
427 | |
---|
428 | if( B_EV_IO_READ != PURPLE_INPUT_READ || |
---|
429 | B_EV_IO_WRITE != PURPLE_INPUT_WRITE ) |
---|
430 | { |
---|
431 | /* FIXME FIXME FIXME FIXME FIXME :-) */ |
---|
432 | exit( 1 ); |
---|
433 | } |
---|
434 | |
---|
435 | purple_util_set_user_dir("/tmp"); |
---|
436 | purple_debug_set_enabled(FALSE); |
---|
437 | purple_core_set_ui_ops(&bee_core_uiops); |
---|
438 | purple_eventloop_set_ui_ops(&glib_eventloops); |
---|
439 | if( !purple_core_init( "BitlBee") ) |
---|
440 | { |
---|
441 | /* Initializing the core failed. Terminate. */ |
---|
442 | fprintf( stderr, "libpurple initialization failed.\n" ); |
---|
443 | abort(); |
---|
444 | } |
---|
445 | |
---|
446 | /* This seems like stateful shit we don't want... */ |
---|
447 | purple_set_blist(purple_blist_new()); |
---|
448 | purple_blist_load(); |
---|
449 | |
---|
450 | /* Meh? */ |
---|
451 | purple_prefs_load(); |
---|
452 | |
---|
453 | for( prots = purple_plugins_get_protocols(); prots; prots = prots->next ) |
---|
454 | { |
---|
455 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
456 | PurplePlugin *prot = prots->data; |
---|
457 | |
---|
458 | ret->name = prot->info->id; |
---|
459 | ret->login = purple_login; |
---|
460 | ret->init = purple_init; |
---|
461 | ret->logout = purple_logout; |
---|
462 | ret->buddy_msg = purple_buddy_msg; |
---|
463 | ret->away_states = purple_away_states; |
---|
464 | ret->set_away = purple_set_away; |
---|
465 | ret->add_buddy = purple_add_buddy; |
---|
466 | ret->remove_buddy = purple_remove_buddy; |
---|
467 | ret->keepalive = purple_keepalive; |
---|
468 | ret->send_typing = purple_send_typing; |
---|
469 | ret->handle_cmp = g_strcasecmp; |
---|
470 | |
---|
471 | register_protocol( ret ); |
---|
472 | } |
---|
473 | } |
---|