source: protocols/purple/purple.c @ 4164e62

Last change on this file since 4164e62 was 4164e62, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-10-11T10:40:40Z

Fixing a few compiler warnings and cleaning up the last remains of
GLib-specific code.

  • Property mode set to 100644
File size: 10.3 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        /* TODO: Figure out variables to export via set. */
50       
51}
52
53static void purple_login( account_t *acc )
54{
55        struct im_connection *ic = imcb_new( acc );
56        PurpleAccount *pa;
57        //PurpleSavedStatus *ps;
58        //GList *i;
59       
60        /* For now this is needed in the _connected() handlers if using
61           GLib event handling, to make sure we're not handling events
62           on dead connections. */
63        purple_connections = g_slist_prepend( purple_connections, ic );
64       
65        pa = purple_account_new( acc->user, acc->prpl->name );
66        purple_account_set_password( pa, acc->pass );
67       
68        ic->proto_data = pa;
69       
70        purple_account_set_enabled( pa, "BitlBee", TRUE );
71       
72        /*
73        for( i = ((PurplePluginProtocolInfo *)pa->gc->prpl->info->extra_info)->protocol_options; i; i = i->next )
74        {
75                PurpleAccountOption *o = i->data;
76               
77                printf( "%s\n", o->pref_name );
78        }
79        */
80       
81        //ps = purple_savedstatus_new( NULL, PURPLE_STATUS_AVAILABLE );
82        //purple_savedstatus_activate_for_account( ps, pa );
83}
84
85static void purple_logout( struct im_connection *ic )
86{
87        purple_connections = g_slist_remove( purple_connections, ic );
88}
89
90static int purple_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
91{
92        PurpleConversation *conv;
93       
94        if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_IM,
95                                                            who, ic->proto_data ) ) == NULL )
96        {
97                conv = purple_conversation_new( PURPLE_CONV_TYPE_IM,
98                                                ic->proto_data, who );
99        }
100       
101        purple_conv_im_send( purple_conversation_get_im_data( conv ), message );
102       
103        return 1;
104}
105
106static GList *purple_away_states( struct im_connection *ic )
107{
108        return NULL;
109}
110
111static void purple_set_away( struct im_connection *ic, char *state_txt, char *message )
112{
113}
114
115static void purple_add_buddy( struct im_connection *ic, char *who, char *group )
116{
117}
118
119static void purple_remove_buddy( struct im_connection *ic, char *who, char *group )
120{
121}
122
123static void purple_keepalive( struct im_connection *ic )
124{
125}
126
127static int purple_send_typing( struct im_connection *ic, char *who, int typing )
128{
129        return 1;
130}
131
132static void purple_ui_init();
133
134static PurpleCoreUiOps bee_core_uiops = 
135{
136        NULL,
137        NULL,
138        purple_ui_init,
139        NULL,
140
141        /* padding */
142        NULL,
143        NULL,
144        NULL,
145        NULL
146};
147
148static void prplcb_conn_progress( PurpleConnection *gc, const char *text, size_t step, size_t step_count )
149{
150        struct im_connection *ic = purple_ic_by_gc( gc );
151       
152        imcb_log( ic, "%s", text );
153}
154
155static void prplcb_conn_connected( PurpleConnection *gc )
156{
157        struct im_connection *ic = purple_ic_by_gc( gc );
158       
159        imcb_connected( ic );
160       
161        if( gc->flags & PURPLE_CONNECTION_HTML )
162                ic->flags |= OPT_DOES_HTML;
163}
164
165static void prplcb_conn_disconnected( PurpleConnection *gc )
166{
167        struct im_connection *ic = purple_ic_by_gc( gc );
168       
169        if( ic != NULL )
170                imc_logout( ic, TRUE );
171}
172
173static void prplcb_conn_notice( PurpleConnection *gc, const char *text )
174{
175        struct im_connection *ic = purple_ic_by_gc( gc );
176       
177        if( ic != NULL )
178                imcb_log( ic, "%s", text );
179}
180
181static void prplcb_conn_report_disconnect_reason( PurpleConnection *gc, PurpleConnectionError reason, const char *text )
182{
183        struct im_connection *ic = purple_ic_by_gc( gc );
184       
185        /* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login,
186           should probably handle that. */
187        if( ic != NULL )
188                imcb_error( ic, "%s", text );
189}
190
191static PurpleConnectionUiOps bee_conn_uiops =
192{
193        prplcb_conn_progress,
194        prplcb_conn_connected,
195        prplcb_conn_disconnected,
196        prplcb_conn_notice,
197        NULL,
198        NULL,
199        NULL,
200        prplcb_conn_report_disconnect_reason,
201};
202
203static void prplcb_blist_new( PurpleBlistNode *node )
204{
205        PurpleBuddy *bud = (PurpleBuddy*) node;
206        struct im_connection *ic = purple_ic_by_pa( bud->account );
207       
208        if( node->type == PURPLE_BLIST_BUDDY_NODE && ic != NULL )
209        {
210                imcb_add_buddy( ic, bud->name, NULL );
211                if( bud->server_alias )
212                        imcb_buddy_nick_hint( ic, bud->name, bud->server_alias );
213        }
214}
215
216static void prplcb_blist_update( PurpleBuddyList *list, PurpleBlistNode *node )
217{
218        PurpleBuddy *bud = (PurpleBuddy*) node;
219        struct im_connection *ic = purple_ic_by_pa( bud->account );
220       
221        if( node->type == PURPLE_BLIST_BUDDY_NODE && ic != NULL  )
222        {
223                imcb_buddy_status( ic, bud->name,
224                                   purple_presence_is_online( bud->presence ) ? OPT_LOGGED_IN : 0,
225                                   NULL, NULL );
226        }
227}
228
229static void prplcb_blist_remove( PurpleBuddyList *list, PurpleBlistNode *node )
230{
231        PurpleBuddy *bud = (PurpleBuddy*) node;
232        struct im_connection *ic = purple_ic_by_pa( bud->account );
233       
234        if( node->type == PURPLE_BLIST_BUDDY_NODE && ic != NULL  )
235        {
236                imcb_remove_buddy( ic, bud->name, NULL );
237        }
238}
239
240static PurpleBlistUiOps bee_blist_uiops =
241{
242        NULL,
243        prplcb_blist_new,
244        NULL,
245        prplcb_blist_update,
246        prplcb_blist_remove,
247};
248
249static void prplcb_conv_im( PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime )
250{
251        struct im_connection *ic = purple_ic_by_pa( conv->account );
252       
253        /* ..._SEND means it's an outgoing message, no need to echo those. */
254        if( !( flags & PURPLE_MESSAGE_SEND ) )
255                imcb_buddy_msg( ic, (char*) who, (char*) message, 0, mtime );
256}
257
258static PurpleConversationUiOps bee_conv_uiops = 
259{
260        NULL,                      /* create_conversation  */
261        NULL,                      /* destroy_conversation */
262        NULL,                      /* write_chat           */
263        prplcb_conv_im,            /* write_im             */
264        NULL,                      /* write_conv           */
265        NULL,                      /* chat_add_users       */
266        NULL,                      /* chat_rename_user     */
267        NULL,                      /* chat_remove_users    */
268        NULL,                      /* chat_update_user     */
269        NULL,                      /* present              */
270        NULL,                      /* has_focus            */
271        NULL,                      /* custom_smiley_add    */
272        NULL,                      /* custom_smiley_write  */
273        NULL,                      /* custom_smiley_close  */
274        NULL,                      /* send_confirm         */
275        NULL,
276        NULL,
277        NULL,
278        NULL
279};
280
281static void prplcb_debug_print( PurpleDebugLevel level, const char *category, const char *arg_s )
282{
283        printf( "DEBUG %s: %s", category, arg_s );
284}
285
286static PurpleDebugUiOps bee_debug_uiops =
287{
288        prplcb_debug_print,
289};
290
291static guint prplcb_ev_timeout_add( guint interval, GSourceFunc func, gpointer udata )
292{
293        return b_timeout_add( interval, (b_event_handler) func, udata );
294}
295
296static guint prplcb_ev_input_add( int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer udata )
297{
298        return b_input_add( fd, cond | B_EV_FLAG_FORCE_REPEAT, (b_event_handler) func, udata );
299}
300
301static gboolean prplcb_ev_remove( guint id )
302{
303        b_event_remove( (gint) id );
304        return TRUE;
305}
306
307static PurpleEventLoopUiOps glib_eventloops = 
308{
309        prplcb_ev_timeout_add,
310        prplcb_ev_remove,
311        prplcb_ev_input_add,
312        prplcb_ev_remove,
313};
314
315static void purple_ui_init()
316{
317        purple_blist_set_ui_ops( &bee_blist_uiops );
318        purple_connections_set_ui_ops( &bee_conn_uiops );
319        purple_conversations_set_ui_ops( &bee_conv_uiops );
320        //purple_debug_set_ui_ops( &bee_debug_uiops );
321}
322
323void purple_initmodule()
324{
325        GList *prots;
326       
327        if( B_EV_IO_READ != PURPLE_INPUT_READ ||
328            B_EV_IO_WRITE != PURPLE_INPUT_WRITE )
329        {
330                /* FIXME FIXME FIXME FIXME FIXME :-) */
331                exit( 1 );
332        }
333       
334        purple_util_set_user_dir("/tmp");
335        purple_debug_set_enabled(FALSE);
336        purple_core_set_ui_ops(&bee_core_uiops);
337        purple_eventloop_set_ui_ops(&glib_eventloops);
338        if( !purple_core_init( "BitlBee") )
339        {
340                /* Initializing the core failed. Terminate. */
341                fprintf( stderr, "libpurple initialization failed.\n" );
342                abort();
343        }
344       
345        /* This seems like stateful shit we don't want... */
346        purple_set_blist(purple_blist_new());
347        purple_blist_load();
348       
349        /* Meh? */
350        purple_prefs_load();
351       
352        for( prots = purple_plugins_get_protocols(); prots; prots = prots->next )
353        {
354                struct prpl *ret = g_new0( struct prpl, 1 );
355                PurplePlugin *prot = prots->data;
356               
357                ret->name = prot->info->id;
358                ret->login = purple_login;
359                ret->init = purple_init;
360                ret->logout = purple_logout;
361                ret->buddy_msg = purple_buddy_msg;
362                ret->away_states = purple_away_states;
363                ret->set_away = purple_set_away;
364                ret->add_buddy = purple_add_buddy;
365                ret->remove_buddy = purple_remove_buddy;
366                ret->keepalive = purple_keepalive;
367                ret->send_typing = purple_send_typing;
368                ret->handle_cmp = g_strcasecmp;
369               
370                register_protocol( ret );
371        }
372}
Note: See TracBrowser for help on using the repository browser.