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