source: protocols/purple/purple.c @ d250b2a

Last change on this file since d250b2a was d250b2a, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-10-06T22:26:01Z

Receive messages.

  • Property mode set to 100644
File size: 10.2 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        struct im_connection *ic = purple_ic_by_gc( gc );
213       
214        imcb_connected( ic );
215       
216        if( gc->flags & PURPLE_CONNECTION_HTML )
217                ic->flags |= OPT_DOES_HTML;
218}
219
220static void prplcb_conn_disconnected( PurpleConnection *gc )
221{
222        imc_logout( purple_ic_by_gc( gc ), TRUE );
223}
224
225static void prplcb_conn_notice( PurpleConnection *gc, const char *text )
226{
227        imcb_log( purple_ic_by_gc( gc ), "%s", text );
228}
229
230static void prplcb_conn_report_disconnect_reason( PurpleConnection *gc, PurpleConnectionError reason, const char *text )
231{
232        /* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login,
233           should probably handle that. */
234        imcb_error( purple_ic_by_gc( gc ), "%s", text );
235}
236
237static PurpleConnectionUiOps bee_conn_uiops =
238{
239        prplcb_conn_progress,
240        prplcb_conn_connected,
241        prplcb_conn_disconnected,
242        prplcb_conn_notice,
243        NULL,
244        NULL,
245        NULL,
246        prplcb_conn_report_disconnect_reason,
247};
248
249static void prplcb_blist_new( PurpleBlistNode *node )
250{
251        PurpleBuddy *bud = (PurpleBuddy*) node;
252        struct im_connection *ic = purple_ic_by_pa( bud->account );
253       
254        if( node->type == PURPLE_BLIST_BUDDY_NODE )
255        {
256                imcb_add_buddy( ic, bud->name, NULL );
257                if( bud->server_alias )
258                        imcb_buddy_nick_hint( ic, bud->name, bud->server_alias );
259        }
260}
261
262static void prplcb_blist_update( PurpleBuddyList *list, PurpleBlistNode *node )
263{
264        PurpleBuddy *bud = (PurpleBuddy*) node;
265       
266        if( node->type == PURPLE_BLIST_BUDDY_NODE )
267        {
268                imcb_buddy_status( purple_ic_by_pa( bud->account ), bud->name,
269                                   purple_presence_is_online( bud->presence ) ? OPT_LOGGED_IN : 0,
270                                   NULL, NULL );
271        }
272}
273
274static void prplcb_blist_remove( PurpleBuddyList *list, PurpleBlistNode *node )
275{
276        PurpleBuddy *bud = (PurpleBuddy*) node;
277       
278        if( node->type == PURPLE_BLIST_BUDDY_NODE )
279                imcb_remove_buddy( purple_ic_by_pa( bud->account ), bud->name, NULL );
280}
281
282static PurpleBlistUiOps bee_blist_uiops =
283{
284        NULL,
285        prplcb_blist_new,
286        NULL,
287        prplcb_blist_update,
288        prplcb_blist_remove,
289};
290
291static void prplcb_conv_im( PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime )
292{
293        struct im_connection *ic = purple_ic_by_pa( conv->account );
294       
295        imcb_buddy_msg( ic, (char*) who, (char*) message, 0, mtime );
296}
297
298static PurpleConversationUiOps bee_conv_uiops = 
299{
300        NULL,                      /* create_conversation  */
301        NULL,                      /* destroy_conversation */
302        NULL,                      /* write_chat           */
303        prplcb_conv_im,            /* write_im             */
304        NULL, //null_write_conv,           /* write_conv           */
305        NULL,                      /* chat_add_users       */
306        NULL,                      /* chat_rename_user     */
307        NULL,                      /* chat_remove_users    */
308        NULL,                      /* chat_update_user     */
309        NULL,                      /* present              */
310        NULL,                      /* has_focus            */
311        NULL,                      /* custom_smiley_add    */
312        NULL,                      /* custom_smiley_write  */
313        NULL,                      /* custom_smiley_close  */
314        NULL,                      /* send_confirm         */
315        NULL,
316        NULL,
317        NULL,
318        NULL
319};
320
321static void purple_ui_init()
322{
323        purple_blist_set_ui_ops( &bee_blist_uiops );
324        purple_connections_set_ui_ops( &bee_conn_uiops );
325        purple_conversations_set_ui_ops( &bee_conv_uiops );
326}
327
328void purple_initmodule()
329{
330        GList *prots;
331       
332        purple_util_set_user_dir("/tmp");
333        purple_debug_set_enabled(FALSE);
334        purple_core_set_ui_ops(&bee_core_uiops);
335        purple_eventloop_set_ui_ops(&glib_eventloops);
336        if( !purple_core_init( "BitlBee") )
337        {
338                /* Initializing the core failed. Terminate. */
339                fprintf( stderr, "libpurple initialization failed.\n" );
340                abort();
341        }
342       
343        /* This seems like stateful shit we don't want... */
344        purple_set_blist(purple_blist_new());
345        purple_blist_load();
346       
347        /* Meh? */
348        purple_prefs_load();
349       
350        for( prots = purple_plugins_get_protocols(); prots; prots = prots->next )
351        {
352                struct prpl *ret = g_new0( struct prpl, 1 );
353                PurplePlugin *prot = prots->data;
354               
355                ret->name = prot->info->id;
356                ret->login = purple_login;
357                ret->init = purple_init;
358                ret->logout = purple_logout;
359                ret->buddy_msg = purple_buddy_msg;
360                ret->away_states = purple_away_states;
361                ret->set_away = purple_set_away;
362                ret->add_buddy = purple_add_buddy;
363                ret->remove_buddy = purple_remove_buddy;
364                ret->keepalive = purple_keepalive;
365                ret->send_typing = purple_send_typing;
366                ret->handle_cmp = g_strcasecmp;
367               
368                register_protocol( ret );
369        }
370}
Note: See TracBrowser for help on using the repository browser.