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 | return NULL; |
---|
177 | } |
---|
178 | |
---|
179 | static void purple_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
180 | { |
---|
181 | } |
---|
182 | |
---|
183 | static void purple_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
184 | { |
---|
185 | } |
---|
186 | |
---|
187 | static void purple_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
188 | { |
---|
189 | } |
---|
190 | |
---|
191 | static void purple_keepalive( struct im_connection *ic ) |
---|
192 | { |
---|
193 | } |
---|
194 | |
---|
195 | static int purple_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
196 | { |
---|
197 | return 1; |
---|
198 | } |
---|
199 | |
---|
200 | static void purple_ui_init(); |
---|
201 | |
---|
202 | static PurpleCoreUiOps bee_core_uiops = |
---|
203 | { |
---|
204 | NULL, |
---|
205 | NULL, |
---|
206 | purple_ui_init, |
---|
207 | NULL, |
---|
208 | }; |
---|
209 | |
---|
210 | static void prplcb_conn_progress( PurpleConnection *gc, const char *text, size_t step, size_t step_count ) |
---|
211 | { |
---|
212 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
213 | |
---|
214 | imcb_log( ic, "%s", text ); |
---|
215 | } |
---|
216 | |
---|
217 | static void prplcb_conn_connected( PurpleConnection *gc ) |
---|
218 | { |
---|
219 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
220 | |
---|
221 | imcb_connected( ic ); |
---|
222 | |
---|
223 | if( gc->flags & PURPLE_CONNECTION_HTML ) |
---|
224 | ic->flags |= OPT_DOES_HTML; |
---|
225 | } |
---|
226 | |
---|
227 | static void prplcb_conn_disconnected( PurpleConnection *gc ) |
---|
228 | { |
---|
229 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
230 | |
---|
231 | if( ic != NULL ) |
---|
232 | { |
---|
233 | imc_logout( ic, TRUE ); |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | static void prplcb_conn_notice( PurpleConnection *gc, const char *text ) |
---|
238 | { |
---|
239 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
240 | |
---|
241 | if( ic != NULL ) |
---|
242 | imcb_log( ic, "%s", text ); |
---|
243 | } |
---|
244 | |
---|
245 | static void prplcb_conn_report_disconnect_reason( PurpleConnection *gc, PurpleConnectionError reason, const char *text ) |
---|
246 | { |
---|
247 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
248 | |
---|
249 | /* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login, |
---|
250 | should probably handle that. */ |
---|
251 | if( ic != NULL ) |
---|
252 | imcb_error( ic, "%s", text ); |
---|
253 | } |
---|
254 | |
---|
255 | static PurpleConnectionUiOps bee_conn_uiops = |
---|
256 | { |
---|
257 | prplcb_conn_progress, |
---|
258 | prplcb_conn_connected, |
---|
259 | prplcb_conn_disconnected, |
---|
260 | prplcb_conn_notice, |
---|
261 | NULL, |
---|
262 | NULL, |
---|
263 | NULL, |
---|
264 | prplcb_conn_report_disconnect_reason, |
---|
265 | }; |
---|
266 | |
---|
267 | static void prplcb_blist_new( PurpleBlistNode *node ) |
---|
268 | { |
---|
269 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
270 | |
---|
271 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
272 | { |
---|
273 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
274 | |
---|
275 | if( ic == NULL ) |
---|
276 | return; |
---|
277 | |
---|
278 | imcb_add_buddy( ic, bud->name, NULL ); |
---|
279 | if( bud->server_alias ) |
---|
280 | imcb_buddy_nick_hint( ic, bud->name, bud->server_alias ); |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | static void prplcb_blist_update( PurpleBuddyList *list, PurpleBlistNode *node ) |
---|
285 | { |
---|
286 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
287 | |
---|
288 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
289 | { |
---|
290 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
291 | PurpleStatus *as; |
---|
292 | int flags = 0; |
---|
293 | |
---|
294 | if( ic == NULL ) |
---|
295 | return; |
---|
296 | |
---|
297 | flags |= purple_presence_is_online( bud->presence ) ? OPT_LOGGED_IN : 0; |
---|
298 | flags |= purple_presence_is_available( bud->presence ) ? 0 : OPT_AWAY; |
---|
299 | |
---|
300 | as = purple_presence_get_active_status( bud->presence ); |
---|
301 | |
---|
302 | imcb_buddy_status( ic, bud->name, flags, purple_status_get_name( as ), |
---|
303 | purple_status_get_attr_string( as, "message" ) ); |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | static void prplcb_blist_remove( 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 | |
---|
315 | if( ic == NULL ) |
---|
316 | return; |
---|
317 | |
---|
318 | imcb_remove_buddy( ic, bud->name, NULL ); |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | static PurpleBlistUiOps bee_blist_uiops = |
---|
323 | { |
---|
324 | NULL, |
---|
325 | prplcb_blist_new, |
---|
326 | NULL, |
---|
327 | prplcb_blist_update, |
---|
328 | prplcb_blist_remove, |
---|
329 | }; |
---|
330 | |
---|
331 | static void prplcb_conv_im( PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime ) |
---|
332 | { |
---|
333 | struct im_connection *ic = purple_ic_by_pa( conv->account ); |
---|
334 | |
---|
335 | /* ..._SEND means it's an outgoing message, no need to echo those. */ |
---|
336 | if( !( flags & PURPLE_MESSAGE_SEND ) ) |
---|
337 | imcb_buddy_msg( ic, (char*) who, (char*) message, 0, mtime ); |
---|
338 | } |
---|
339 | |
---|
340 | static PurpleConversationUiOps bee_conv_uiops = |
---|
341 | { |
---|
342 | NULL, /* create_conversation */ |
---|
343 | NULL, /* destroy_conversation */ |
---|
344 | NULL, /* write_chat */ |
---|
345 | prplcb_conv_im, /* write_im */ |
---|
346 | NULL, /* write_conv */ |
---|
347 | NULL, /* chat_add_users */ |
---|
348 | NULL, /* chat_rename_user */ |
---|
349 | NULL, /* chat_remove_users */ |
---|
350 | NULL, /* chat_update_user */ |
---|
351 | NULL, /* present */ |
---|
352 | NULL, /* has_focus */ |
---|
353 | NULL, /* custom_smiley_add */ |
---|
354 | NULL, /* custom_smiley_write */ |
---|
355 | NULL, /* custom_smiley_close */ |
---|
356 | NULL, /* send_confirm */ |
---|
357 | NULL, |
---|
358 | NULL, |
---|
359 | NULL, |
---|
360 | NULL |
---|
361 | }; |
---|
362 | |
---|
363 | static void prplcb_debug_print( PurpleDebugLevel level, const char *category, const char *arg_s ) |
---|
364 | { |
---|
365 | printf( "DEBUG %s: %s", category, arg_s ); |
---|
366 | } |
---|
367 | |
---|
368 | static PurpleDebugUiOps bee_debug_uiops = |
---|
369 | { |
---|
370 | prplcb_debug_print, |
---|
371 | }; |
---|
372 | |
---|
373 | static guint prplcb_ev_timeout_add( guint interval, GSourceFunc func, gpointer udata ) |
---|
374 | { |
---|
375 | return b_timeout_add( interval, (b_event_handler) func, udata ); |
---|
376 | } |
---|
377 | |
---|
378 | static guint prplcb_ev_input_add( int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer udata ) |
---|
379 | { |
---|
380 | return b_input_add( fd, cond | B_EV_FLAG_FORCE_REPEAT, (b_event_handler) func, udata ); |
---|
381 | } |
---|
382 | |
---|
383 | static gboolean prplcb_ev_remove( guint id ) |
---|
384 | { |
---|
385 | b_event_remove( (gint) id ); |
---|
386 | return TRUE; |
---|
387 | } |
---|
388 | |
---|
389 | static PurpleEventLoopUiOps glib_eventloops = |
---|
390 | { |
---|
391 | prplcb_ev_timeout_add, |
---|
392 | prplcb_ev_remove, |
---|
393 | prplcb_ev_input_add, |
---|
394 | prplcb_ev_remove, |
---|
395 | }; |
---|
396 | |
---|
397 | static void purple_ui_init() |
---|
398 | { |
---|
399 | purple_blist_set_ui_ops( &bee_blist_uiops ); |
---|
400 | purple_connections_set_ui_ops( &bee_conn_uiops ); |
---|
401 | purple_conversations_set_ui_ops( &bee_conv_uiops ); |
---|
402 | //purple_debug_set_ui_ops( &bee_debug_uiops ); |
---|
403 | } |
---|
404 | |
---|
405 | void purple_initmodule() |
---|
406 | { |
---|
407 | GList *prots; |
---|
408 | |
---|
409 | if( B_EV_IO_READ != PURPLE_INPUT_READ || |
---|
410 | B_EV_IO_WRITE != PURPLE_INPUT_WRITE ) |
---|
411 | { |
---|
412 | /* FIXME FIXME FIXME FIXME FIXME :-) */ |
---|
413 | exit( 1 ); |
---|
414 | } |
---|
415 | |
---|
416 | purple_util_set_user_dir("/tmp"); |
---|
417 | purple_debug_set_enabled(FALSE); |
---|
418 | purple_core_set_ui_ops(&bee_core_uiops); |
---|
419 | purple_eventloop_set_ui_ops(&glib_eventloops); |
---|
420 | if( !purple_core_init( "BitlBee") ) |
---|
421 | { |
---|
422 | /* Initializing the core failed. Terminate. */ |
---|
423 | fprintf( stderr, "libpurple initialization failed.\n" ); |
---|
424 | abort(); |
---|
425 | } |
---|
426 | |
---|
427 | /* This seems like stateful shit we don't want... */ |
---|
428 | purple_set_blist(purple_blist_new()); |
---|
429 | purple_blist_load(); |
---|
430 | |
---|
431 | /* Meh? */ |
---|
432 | purple_prefs_load(); |
---|
433 | |
---|
434 | for( prots = purple_plugins_get_protocols(); prots; prots = prots->next ) |
---|
435 | { |
---|
436 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
437 | PurplePlugin *prot = prots->data; |
---|
438 | |
---|
439 | ret->name = prot->info->id; |
---|
440 | ret->login = purple_login; |
---|
441 | ret->init = purple_init; |
---|
442 | ret->logout = purple_logout; |
---|
443 | ret->buddy_msg = purple_buddy_msg; |
---|
444 | ret->away_states = purple_away_states; |
---|
445 | ret->set_away = purple_set_away; |
---|
446 | ret->add_buddy = purple_add_buddy; |
---|
447 | ret->remove_buddy = purple_remove_buddy; |
---|
448 | ret->keepalive = purple_keepalive; |
---|
449 | ret->send_typing = purple_send_typing; |
---|
450 | ret->handle_cmp = g_strcasecmp; |
---|
451 | |
---|
452 | register_protocol( ret ); |
---|
453 | } |
---|
454 | } |
---|