source: protocols/purple/purple.c @ 7da726b

Last change on this file since 7da726b was 7da726b, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-10-06T21:49:42Z

Getting a contact list and online status now. Time to handle messages.

  • Property mode set to 100644
File size: 9.8 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
31#undef g_io_add_watch
32#undef g_io_add_watch_full
33#undef g_timeout_add
34#undef g_source_remove
35
36/**
37 * The following eventloop functions are used in both pidgin and purple-text. If your
38 * application uses glib mainloop, you can safely use this verbatim.
39 */
40#define PURPLE_GLIB_READ_COND  (G_IO_IN | G_IO_HUP | G_IO_ERR)
41#define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
42
43typedef struct _PurpleGLibIOClosure {
44        PurpleInputFunction function;
45        guint result;
46        gpointer data;
47} PurpleGLibIOClosure;
48
49static struct im_connection *purple_ic_by_pa( PurpleAccount *pa )
50{
51        GSList *i;
52       
53        for( i = purple_connections; i; i = i->next )
54                if( ((struct im_connection *)i->data)->proto_data == pa )
55                        return i->data;
56       
57        return NULL;
58}
59
60static struct im_connection *purple_ic_by_gc( PurpleConnection *gc )
61{
62        return purple_ic_by_pa( purple_connection_get_account( gc ) );
63}
64
65static void purple_glib_io_destroy(gpointer data)
66{
67        g_free(data);
68}
69
70static gboolean purple_glib_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
71{
72        PurpleGLibIOClosure *closure = data;
73        PurpleInputCondition purple_cond = 0;
74
75        if (condition & PURPLE_GLIB_READ_COND)
76                purple_cond |= PURPLE_INPUT_READ;
77        if (condition & PURPLE_GLIB_WRITE_COND)
78                purple_cond |= PURPLE_INPUT_WRITE;
79
80        closure->function(closure->data, g_io_channel_unix_get_fd(source),
81                          purple_cond);
82
83        return TRUE;
84}
85
86static guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function,
87                                                           gpointer data)
88{
89        PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1);
90        GIOChannel *channel;
91        GIOCondition cond = 0;
92
93        closure->function = function;
94        closure->data = data;
95
96        if (condition & PURPLE_INPUT_READ)
97                cond |= PURPLE_GLIB_READ_COND;
98        if (condition & PURPLE_INPUT_WRITE)
99                cond |= PURPLE_GLIB_WRITE_COND;
100
101        channel = g_io_channel_unix_new(fd);
102        closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
103                                              purple_glib_io_invoke, closure, purple_glib_io_destroy);
104
105        g_io_channel_unref(channel);
106        return closure->result;
107}
108
109static PurpleEventLoopUiOps glib_eventloops = 
110{
111        g_timeout_add,
112        g_source_remove,
113        glib_input_add,
114        g_source_remove,
115        NULL,
116#if GLIB_CHECK_VERSION(2,14,0)
117        g_timeout_add_seconds,
118#else
119        NULL,
120#endif
121
122        /* padding */
123        NULL,
124        NULL,
125        NULL
126};
127
128static void purple_init( account_t *acc )
129{
130        /* TODO: Figure out variables to export via set. */
131}
132
133static void purple_login( account_t *acc )
134{
135        struct im_connection *ic = imcb_new( acc );
136        PurpleAccount *pa;
137        PurpleSavedStatus *ps;
138       
139        /* For now this is needed in the _connected() handlers if using
140           GLib event handling, to make sure we're not handling events
141           on dead connections. */
142        purple_connections = g_slist_prepend( purple_connections, ic );
143       
144        pa = purple_account_new( acc->user, acc->prpl->name );
145        purple_account_set_password( pa, acc->pass );
146       
147        ic->proto_data = pa;
148       
149        purple_account_set_enabled( pa, "BitlBee", TRUE );
150       
151        //ps = purple_savedstatus_new( NULL, PURPLE_STATUS_AVAILABLE );
152        //purple_savedstatus_activate_for_account( ps, pa );
153}
154
155static void purple_logout( struct im_connection *ic )
156{
157        purple_connections = g_slist_remove( purple_connections, ic );
158}
159
160static int purple_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
161{
162}
163
164static GList *purple_away_states( struct im_connection *ic )
165{
166        return NULL;
167}
168
169static void purple_set_away( struct im_connection *ic, char *state_txt, char *message )
170{
171}
172
173static void purple_add_buddy( struct im_connection *ic, char *who, char *group )
174{
175}
176
177static void purple_remove_buddy( struct im_connection *ic, char *who, char *group )
178{
179}
180
181static void purple_keepalive( struct im_connection *ic )
182{
183}
184
185static int purple_send_typing( struct im_connection *ic, char *who, int typing )
186{
187}
188
189static void purple_ui_init();
190
191static PurpleCoreUiOps bee_core_uiops = 
192{
193        NULL,
194        NULL,
195        purple_ui_init,
196        NULL,
197
198        /* padding */
199        NULL,
200        NULL,
201        NULL,
202        NULL
203};
204
205static void prplcb_conn_progress( PurpleConnection *gc, const char *text, size_t step, size_t step_count )
206{
207        imcb_log( purple_ic_by_gc( gc ), "%s", text );
208}
209
210static void prplcb_conn_connected( PurpleConnection *gc )
211{
212        imcb_connected( purple_ic_by_gc( gc ) );
213}
214
215static void prplcb_conn_disconnected( PurpleConnection *gc )
216{
217        imc_logout( purple_ic_by_gc( gc ), TRUE );
218}
219
220static void prplcb_conn_notice( PurpleConnection *gc, const char *text )
221{
222        imcb_log( purple_ic_by_gc( gc ), "%s", text );
223}
224
225static void prplcb_conn_report_disconnect_reason( PurpleConnection *gc, PurpleConnectionError reason, const char *text )
226{
227        /* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login,
228           should probably handle that. */
229        imcb_error( purple_ic_by_gc( gc ), "%s", text );
230}
231
232static PurpleConnectionUiOps bee_conn_uiops =
233{
234        prplcb_conn_progress,
235        prplcb_conn_connected,
236        prplcb_conn_disconnected,
237        prplcb_conn_notice,
238        NULL,
239        NULL,
240        NULL,
241        prplcb_conn_report_disconnect_reason,
242};
243
244static void prplcb_blist_new( PurpleBlistNode *node )
245{
246        PurpleBuddy *bud = (PurpleBuddy*) node;
247        struct im_connection *ic = purple_ic_by_pa( bud->account );
248       
249        if( node->type == PURPLE_BLIST_BUDDY_NODE )
250        {
251                imcb_add_buddy( ic, bud->name, NULL );
252                if( bud->server_alias )
253                        imcb_buddy_nick_hint( ic, bud->name, bud->server_alias );
254        }
255}
256
257static void prplcb_blist_update( PurpleBuddyList *list, PurpleBlistNode *node )
258{
259        PurpleBuddy *bud = (PurpleBuddy*) node;
260       
261        if( node->type == PURPLE_BLIST_BUDDY_NODE )
262        {
263                imcb_buddy_status( purple_ic_by_pa( bud->account ), bud->name,
264                                   purple_presence_is_online( bud->presence ) ? OPT_LOGGED_IN : 0,
265                                   NULL, NULL );
266        }
267}
268
269static void prplcb_blist_remove( PurpleBuddyList *list, PurpleBlistNode *node )
270{
271        PurpleBuddy *bud = (PurpleBuddy*) node;
272       
273        if( node->type == PURPLE_BLIST_BUDDY_NODE )
274                imcb_remove_buddy( purple_ic_by_pa( bud->account ), bud->name, NULL );
275}
276
277static PurpleBlistUiOps bee_blist_uiops =
278{
279        NULL,
280        prplcb_blist_new,
281        NULL,
282        prplcb_blist_update,
283        prplcb_blist_remove,
284};
285
286static PurpleConversationUiOps bee_conv_uiops = 
287{
288        NULL,                      /* create_conversation  */
289        NULL,                      /* destroy_conversation */
290        NULL,                      /* write_chat           */
291        NULL,                      /* write_im             */
292        NULL, //null_write_conv,           /* write_conv           */
293        NULL,                      /* chat_add_users       */
294        NULL,                      /* chat_rename_user     */
295        NULL,                      /* chat_remove_users    */
296        NULL,                      /* chat_update_user     */
297        NULL,                      /* present              */
298        NULL,                      /* has_focus            */
299        NULL,                      /* custom_smiley_add    */
300        NULL,                      /* custom_smiley_write  */
301        NULL,                      /* custom_smiley_close  */
302        NULL,                      /* send_confirm         */
303        NULL,
304        NULL,
305        NULL,
306        NULL
307};
308
309static void purple_ui_init()
310{
311        purple_blist_set_ui_ops( &bee_blist_uiops );
312        purple_connections_set_ui_ops( &bee_conn_uiops );
313        purple_conversations_set_ui_ops( &bee_conv_uiops );
314}
315
316void purple_initmodule()
317{
318        GList *prots;
319       
320        purple_util_set_user_dir("/tmp");
321        purple_debug_set_enabled(FALSE);
322        purple_core_set_ui_ops(&bee_core_uiops);
323        purple_eventloop_set_ui_ops(&glib_eventloops);
324        if( !purple_core_init( "BitlBee") )
325        {
326                /* Initializing the core failed. Terminate. */
327                fprintf( stderr, "libpurple initialization failed.\n" );
328                abort();
329        }
330       
331        /* This seems like stateful shit we don't want... */
332        purple_set_blist(purple_blist_new());
333        purple_blist_load();
334       
335        /* Meh? */
336        purple_prefs_load();
337       
338        for( prots = purple_plugins_get_protocols(); prots; prots = prots->next )
339        {
340                struct prpl *ret = g_new0( struct prpl, 1 );
341                PurplePlugin *prot = prots->data;
342               
343                ret->name = prot->info->id;
344                ret->login = purple_login;
345                ret->init = purple_init;
346                ret->logout = purple_logout;
347                ret->buddy_msg = purple_buddy_msg;
348                ret->away_states = purple_away_states;
349                ret->set_away = purple_set_away;
350                ret->add_buddy = purple_add_buddy;
351                ret->remove_buddy = purple_remove_buddy;
352                ret->keepalive = purple_keepalive;
353                ret->send_typing = purple_send_typing;
354                ret->handle_cmp = g_strcasecmp;
355               
356                register_protocol( ret );
357        }
358}
Note: See TracBrowser for help on using the repository browser.