source: protocols/purple/purple.c @ 4524f66

Last change on this file since 4524f66 was 4524f66, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-10-12T22:37:28Z

Store real names in /whois.

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