source: protocols/purple/purple.c @ 389f7be

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

Support for sending messages.

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