[796da03] | 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 | |
---|
| 29 | GSList *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 | |
---|
| 43 | typedef struct _PurpleGLibIOClosure { |
---|
| 44 | PurpleInputFunction function; |
---|
| 45 | guint result; |
---|
| 46 | gpointer data; |
---|
| 47 | } PurpleGLibIOClosure; |
---|
| 48 | |
---|
[7da726b] | 49 | static struct im_connection *purple_ic_by_pa( PurpleAccount *pa ) |
---|
[860ba6a] | 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 | |
---|
[7da726b] | 60 | static struct im_connection *purple_ic_by_gc( PurpleConnection *gc ) |
---|
| 61 | { |
---|
| 62 | return purple_ic_by_pa( purple_connection_get_account( gc ) ); |
---|
| 63 | } |
---|
| 64 | |
---|
[796da03] | 65 | static void purple_glib_io_destroy(gpointer data) |
---|
| 66 | { |
---|
| 67 | g_free(data); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | static 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 | |
---|
| 86 | static 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 | |
---|
| 109 | static 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 | |
---|
| 128 | static void purple_init( account_t *acc ) |
---|
| 129 | { |
---|
[860ba6a] | 130 | /* TODO: Figure out variables to export via set. */ |
---|
[796da03] | 131 | } |
---|
| 132 | |
---|
| 133 | static void purple_login( account_t *acc ) |
---|
| 134 | { |
---|
| 135 | struct im_connection *ic = imcb_new( acc ); |
---|
[860ba6a] | 136 | PurpleAccount *pa; |
---|
| 137 | PurpleSavedStatus *ps; |
---|
[796da03] | 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 | |
---|
[860ba6a] | 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 ); |
---|
[796da03] | 153 | } |
---|
| 154 | |
---|
| 155 | static void purple_logout( struct im_connection *ic ) |
---|
| 156 | { |
---|
| 157 | purple_connections = g_slist_remove( purple_connections, ic ); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | static int purple_buddy_msg( struct im_connection *ic, char *who, char *message, int flags ) |
---|
| 161 | { |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | static GList *purple_away_states( struct im_connection *ic ) |
---|
| 165 | { |
---|
[860ba6a] | 166 | return NULL; |
---|
[796da03] | 167 | } |
---|
| 168 | |
---|
| 169 | static void purple_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
| 170 | { |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | static void purple_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 174 | { |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | static void purple_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 178 | { |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | static void purple_keepalive( struct im_connection *ic ) |
---|
| 182 | { |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | static int purple_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
| 186 | { |
---|
| 187 | } |
---|
| 188 | |
---|
[860ba6a] | 189 | static void purple_ui_init(); |
---|
| 190 | |
---|
| 191 | static 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 | |
---|
| 205 | static 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 | |
---|
| 210 | static void prplcb_conn_connected( PurpleConnection *gc ) |
---|
| 211 | { |
---|
[d250b2a] | 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; |
---|
[860ba6a] | 218 | } |
---|
| 219 | |
---|
| 220 | static void prplcb_conn_disconnected( PurpleConnection *gc ) |
---|
| 221 | { |
---|
| 222 | imc_logout( purple_ic_by_gc( gc ), TRUE ); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | static void prplcb_conn_notice( PurpleConnection *gc, const char *text ) |
---|
| 226 | { |
---|
| 227 | imcb_log( purple_ic_by_gc( gc ), "%s", text ); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | static 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 | |
---|
| 237 | static 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 | |
---|
[7da726b] | 249 | static 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 | |
---|
| 262 | static 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 | |
---|
| 274 | static 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 | |
---|
| 282 | static PurpleBlistUiOps bee_blist_uiops = |
---|
| 283 | { |
---|
| 284 | NULL, |
---|
| 285 | prplcb_blist_new, |
---|
| 286 | NULL, |
---|
| 287 | prplcb_blist_update, |
---|
| 288 | prplcb_blist_remove, |
---|
| 289 | }; |
---|
| 290 | |
---|
[d250b2a] | 291 | static 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 | |
---|
[860ba6a] | 298 | static PurpleConversationUiOps bee_conv_uiops = |
---|
| 299 | { |
---|
| 300 | NULL, /* create_conversation */ |
---|
| 301 | NULL, /* destroy_conversation */ |
---|
| 302 | NULL, /* write_chat */ |
---|
[d250b2a] | 303 | prplcb_conv_im, /* write_im */ |
---|
[860ba6a] | 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 | |
---|
| 321 | static void purple_ui_init() |
---|
| 322 | { |
---|
[7da726b] | 323 | purple_blist_set_ui_ops( &bee_blist_uiops ); |
---|
[860ba6a] | 324 | purple_connections_set_ui_ops( &bee_conn_uiops ); |
---|
| 325 | purple_conversations_set_ui_ops( &bee_conv_uiops ); |
---|
| 326 | } |
---|
| 327 | |
---|
[796da03] | 328 | void 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 | } |
---|