[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 | |
---|
[b3117f2] | 24 | #include "bitlbee.h" |
---|
[e5d8d21] | 25 | #include "help.h" |
---|
[b3117f2] | 26 | |
---|
[0ac1a375] | 27 | #include <stdarg.h> |
---|
| 28 | |
---|
[796da03] | 29 | #include <glib.h> |
---|
| 30 | #include <purple.h> |
---|
| 31 | |
---|
| 32 | GSList *purple_connections; |
---|
| 33 | |
---|
[0ac1a375] | 34 | /* This makes me VERY sad... :-( But some libpurple callbacks come in without |
---|
| 35 | any context so this is the only way to get that. Don't want to support |
---|
| 36 | libpurple in daemon mode anyway. */ |
---|
| 37 | static irc_t *local_irc; |
---|
| 38 | |
---|
[7da726b] | 39 | static struct im_connection *purple_ic_by_pa( PurpleAccount *pa ) |
---|
[860ba6a] | 40 | { |
---|
| 41 | GSList *i; |
---|
| 42 | |
---|
| 43 | for( i = purple_connections; i; i = i->next ) |
---|
| 44 | if( ((struct im_connection *)i->data)->proto_data == pa ) |
---|
| 45 | return i->data; |
---|
| 46 | |
---|
| 47 | return NULL; |
---|
| 48 | } |
---|
| 49 | |
---|
[7da726b] | 50 | static struct im_connection *purple_ic_by_gc( PurpleConnection *gc ) |
---|
| 51 | { |
---|
| 52 | return purple_ic_by_pa( purple_connection_get_account( gc ) ); |
---|
| 53 | } |
---|
| 54 | |
---|
[796da03] | 55 | static void purple_init( account_t *acc ) |
---|
| 56 | { |
---|
[45a19e5] | 57 | PurplePlugin *prpl = purple_plugins_find_with_id( (char*) acc->prpl->data ); |
---|
[0f7ee7e5] | 58 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
| 59 | GList *i; |
---|
[0cbef26] | 60 | |
---|
[0f7ee7e5] | 61 | for( i = pi->protocol_options; i; i = i->next ) |
---|
| 62 | { |
---|
| 63 | PurpleAccountOption *o = i->data; |
---|
| 64 | const char *name; |
---|
| 65 | char *def = NULL; |
---|
| 66 | set_eval eval = NULL; |
---|
| 67 | set_t *s; |
---|
| 68 | |
---|
| 69 | name = purple_account_option_get_setting( o ); |
---|
| 70 | |
---|
| 71 | switch( purple_account_option_get_type( o ) ) |
---|
| 72 | { |
---|
| 73 | case PURPLE_PREF_STRING: |
---|
| 74 | def = g_strdup( purple_account_option_get_default_string( o ) ); |
---|
| 75 | break; |
---|
| 76 | |
---|
| 77 | case PURPLE_PREF_INT: |
---|
| 78 | def = g_strdup_printf( "%d", purple_account_option_get_default_int( o ) ); |
---|
| 79 | eval = set_eval_int; |
---|
| 80 | break; |
---|
| 81 | |
---|
| 82 | case PURPLE_PREF_BOOLEAN: |
---|
| 83 | if( purple_account_option_get_default_bool( o ) ) |
---|
| 84 | def = g_strdup( "true" ); |
---|
| 85 | else |
---|
| 86 | def = g_strdup( "false" ); |
---|
| 87 | eval = set_eval_bool; |
---|
| 88 | break; |
---|
| 89 | |
---|
| 90 | default: |
---|
| 91 | fprintf( stderr, "Setting with unknown type: %s (%d)\n", name, purple_account_option_get_type( o ) ); |
---|
[b3117f2] | 92 | name = NULL; |
---|
[0f7ee7e5] | 93 | } |
---|
| 94 | |
---|
[b3117f2] | 95 | if( name != NULL ) |
---|
[0f7ee7e5] | 96 | { |
---|
| 97 | s = set_add( &acc->set, name, def, eval, acc ); |
---|
| 98 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 99 | g_free( def ); |
---|
| 100 | } |
---|
| 101 | } |
---|
[796da03] | 102 | } |
---|
| 103 | |
---|
[b74b287] | 104 | static void purple_sync_settings( account_t *acc, PurpleAccount *pa ) |
---|
| 105 | { |
---|
| 106 | PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id ); |
---|
| 107 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
| 108 | GList *i; |
---|
| 109 | |
---|
| 110 | for( i = pi->protocol_options; i; i = i->next ) |
---|
| 111 | { |
---|
| 112 | PurpleAccountOption *o = i->data; |
---|
| 113 | const char *name; |
---|
| 114 | set_t *s; |
---|
| 115 | |
---|
| 116 | name = purple_account_option_get_setting( o ); |
---|
| 117 | s = set_find( &acc->set, name ); |
---|
| 118 | if( s->value == NULL ) |
---|
| 119 | continue; |
---|
| 120 | |
---|
| 121 | switch( purple_account_option_get_type( o ) ) |
---|
| 122 | { |
---|
| 123 | case PURPLE_PREF_STRING: |
---|
| 124 | purple_account_set_string( pa, name, set_getstr( &acc->set, name ) ); |
---|
| 125 | break; |
---|
| 126 | |
---|
| 127 | case PURPLE_PREF_INT: |
---|
| 128 | purple_account_set_int( pa, name, set_getint( &acc->set, name ) ); |
---|
| 129 | break; |
---|
| 130 | |
---|
| 131 | case PURPLE_PREF_BOOLEAN: |
---|
| 132 | purple_account_set_bool( pa, name, set_getbool( &acc->set, name ) ); |
---|
| 133 | break; |
---|
| 134 | |
---|
| 135 | default: |
---|
| 136 | break; |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
[796da03] | 141 | static void purple_login( account_t *acc ) |
---|
| 142 | { |
---|
| 143 | struct im_connection *ic = imcb_new( acc ); |
---|
[860ba6a] | 144 | PurpleAccount *pa; |
---|
[796da03] | 145 | |
---|
[0ac1a375] | 146 | if( local_irc != NULL && local_irc != acc->irc ) |
---|
[6967d01] | 147 | { |
---|
[0ac1a375] | 148 | irc_usermsg( acc->irc, "Daemon mode detected. Do *not* try to use libpurple in daemon mode! " |
---|
| 149 | "Please use inetd or ForkDaemon mode instead." ); |
---|
[6967d01] | 150 | return; |
---|
| 151 | } |
---|
[0ac1a375] | 152 | local_irc = acc->irc; |
---|
[6967d01] | 153 | |
---|
[796da03] | 154 | /* For now this is needed in the _connected() handlers if using |
---|
| 155 | GLib event handling, to make sure we're not handling events |
---|
| 156 | on dead connections. */ |
---|
| 157 | purple_connections = g_slist_prepend( purple_connections, ic ); |
---|
| 158 | |
---|
[cd741d8] | 159 | ic->proto_data = pa = purple_account_new( acc->user, (char*) acc->prpl->data ); |
---|
[860ba6a] | 160 | purple_account_set_password( pa, acc->pass ); |
---|
[b74b287] | 161 | purple_sync_settings( acc, pa ); |
---|
[860ba6a] | 162 | |
---|
| 163 | purple_account_set_enabled( pa, "BitlBee", TRUE ); |
---|
[796da03] | 164 | } |
---|
| 165 | |
---|
| 166 | static void purple_logout( struct im_connection *ic ) |
---|
| 167 | { |
---|
[db4cd40] | 168 | PurpleAccount *pa = ic->proto_data; |
---|
| 169 | |
---|
| 170 | purple_account_set_enabled( pa, "BitlBee", FALSE ); |
---|
[796da03] | 171 | purple_connections = g_slist_remove( purple_connections, ic ); |
---|
[b74b287] | 172 | purple_accounts_remove( pa ); |
---|
[796da03] | 173 | } |
---|
| 174 | |
---|
| 175 | static int purple_buddy_msg( struct im_connection *ic, char *who, char *message, int flags ) |
---|
| 176 | { |
---|
[389f7be] | 177 | PurpleConversation *conv; |
---|
| 178 | |
---|
| 179 | if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_IM, |
---|
| 180 | who, ic->proto_data ) ) == NULL ) |
---|
| 181 | { |
---|
| 182 | conv = purple_conversation_new( PURPLE_CONV_TYPE_IM, |
---|
| 183 | ic->proto_data, who ); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | purple_conv_im_send( purple_conversation_get_im_data( conv ), message ); |
---|
[0cbef26] | 187 | |
---|
| 188 | return 1; |
---|
[796da03] | 189 | } |
---|
| 190 | |
---|
| 191 | static GList *purple_away_states( struct im_connection *ic ) |
---|
| 192 | { |
---|
[ec5e57d] | 193 | PurpleAccount *pa = ic->proto_data; |
---|
| 194 | GList *st, *ret = NULL; |
---|
| 195 | |
---|
| 196 | for( st = purple_account_get_status_types( pa ); st; st = st->next ) |
---|
| 197 | ret = g_list_append( ret, (void*) purple_status_type_get_name( st->data ) ); |
---|
| 198 | |
---|
| 199 | return ret; |
---|
[796da03] | 200 | } |
---|
| 201 | |
---|
| 202 | static void purple_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
| 203 | { |
---|
[ec5e57d] | 204 | PurpleAccount *pa = ic->proto_data; |
---|
| 205 | GList *status_types = purple_account_get_status_types( pa ), *st; |
---|
| 206 | PurpleStatusType *pst = NULL; |
---|
| 207 | |
---|
| 208 | for( st = status_types; st; st = st->next ) |
---|
| 209 | { |
---|
| 210 | pst = st->data; |
---|
| 211 | |
---|
| 212 | if( g_strcasecmp( state_txt, purple_status_type_get_name( pst ) ) == 0 ) |
---|
| 213 | break; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | purple_account_set_status( pa, st ? purple_status_type_get_id( pst ) : "away", |
---|
| 217 | TRUE, "message", message, NULL ); |
---|
[796da03] | 218 | } |
---|
| 219 | |
---|
| 220 | static void purple_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 221 | { |
---|
[b3117f2] | 222 | PurpleBuddy *pb; |
---|
| 223 | |
---|
| 224 | pb = purple_buddy_new( (PurpleAccount*) ic->proto_data, who, NULL ); |
---|
| 225 | purple_blist_add_buddy( pb, NULL, NULL, NULL ); |
---|
| 226 | purple_account_add_buddy( (PurpleAccount*) ic->proto_data, pb ); |
---|
[796da03] | 227 | } |
---|
| 228 | |
---|
| 229 | static void purple_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 230 | { |
---|
[b3117f2] | 231 | PurpleBuddy *pb; |
---|
| 232 | |
---|
| 233 | pb = purple_find_buddy( (PurpleAccount*) ic->proto_data, who ); |
---|
| 234 | if( pb != NULL ) |
---|
| 235 | { |
---|
| 236 | purple_account_remove_buddy( (PurpleAccount*) ic->proto_data, pb, NULL ); |
---|
| 237 | purple_blist_remove_buddy( pb ); |
---|
| 238 | } |
---|
[796da03] | 239 | } |
---|
| 240 | |
---|
| 241 | static void purple_keepalive( struct im_connection *ic ) |
---|
| 242 | { |
---|
| 243 | } |
---|
| 244 | |
---|
[487f555] | 245 | static int purple_send_typing( struct im_connection *ic, char *who, int flags ) |
---|
[796da03] | 246 | { |
---|
[487f555] | 247 | PurpleTypingState state = PURPLE_NOT_TYPING; |
---|
| 248 | PurpleConversation *conv; |
---|
| 249 | |
---|
| 250 | if( flags & OPT_TYPING ) |
---|
| 251 | state = PURPLE_TYPING; |
---|
| 252 | else if( flags & OPT_THINKING ) |
---|
| 253 | state = PURPLE_TYPED; |
---|
| 254 | |
---|
| 255 | if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_IM, |
---|
| 256 | who, ic->proto_data ) ) == NULL ) |
---|
| 257 | { |
---|
| 258 | purple_conv_im_set_typing_state( purple_conversation_get_im_data( conv ), state ); |
---|
| 259 | return 1; |
---|
| 260 | } |
---|
| 261 | else |
---|
| 262 | { |
---|
| 263 | return 0; |
---|
| 264 | } |
---|
[796da03] | 265 | } |
---|
| 266 | |
---|
[860ba6a] | 267 | static void purple_ui_init(); |
---|
| 268 | |
---|
| 269 | static PurpleCoreUiOps bee_core_uiops = |
---|
| 270 | { |
---|
| 271 | NULL, |
---|
| 272 | NULL, |
---|
| 273 | purple_ui_init, |
---|
| 274 | NULL, |
---|
| 275 | }; |
---|
| 276 | |
---|
| 277 | static void prplcb_conn_progress( PurpleConnection *gc, const char *text, size_t step, size_t step_count ) |
---|
| 278 | { |
---|
[0cbef26] | 279 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
| 280 | |
---|
| 281 | imcb_log( ic, "%s", text ); |
---|
[860ba6a] | 282 | } |
---|
| 283 | |
---|
| 284 | static void prplcb_conn_connected( PurpleConnection *gc ) |
---|
| 285 | { |
---|
[d250b2a] | 286 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
| 287 | |
---|
| 288 | imcb_connected( ic ); |
---|
| 289 | |
---|
| 290 | if( gc->flags & PURPLE_CONNECTION_HTML ) |
---|
| 291 | ic->flags |= OPT_DOES_HTML; |
---|
[860ba6a] | 292 | } |
---|
| 293 | |
---|
| 294 | static void prplcb_conn_disconnected( PurpleConnection *gc ) |
---|
| 295 | { |
---|
[0cbef26] | 296 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
| 297 | |
---|
| 298 | if( ic != NULL ) |
---|
[b74b287] | 299 | { |
---|
[0cbef26] | 300 | imc_logout( ic, TRUE ); |
---|
[b74b287] | 301 | } |
---|
[860ba6a] | 302 | } |
---|
| 303 | |
---|
| 304 | static void prplcb_conn_notice( PurpleConnection *gc, const char *text ) |
---|
| 305 | { |
---|
[0cbef26] | 306 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
| 307 | |
---|
| 308 | if( ic != NULL ) |
---|
| 309 | imcb_log( ic, "%s", text ); |
---|
[860ba6a] | 310 | } |
---|
| 311 | |
---|
| 312 | static void prplcb_conn_report_disconnect_reason( PurpleConnection *gc, PurpleConnectionError reason, const char *text ) |
---|
| 313 | { |
---|
[0cbef26] | 314 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
| 315 | |
---|
[860ba6a] | 316 | /* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login, |
---|
| 317 | should probably handle that. */ |
---|
[0cbef26] | 318 | if( ic != NULL ) |
---|
| 319 | imcb_error( ic, "%s", text ); |
---|
[860ba6a] | 320 | } |
---|
| 321 | |
---|
| 322 | static PurpleConnectionUiOps bee_conn_uiops = |
---|
| 323 | { |
---|
| 324 | prplcb_conn_progress, |
---|
| 325 | prplcb_conn_connected, |
---|
| 326 | prplcb_conn_disconnected, |
---|
| 327 | prplcb_conn_notice, |
---|
| 328 | NULL, |
---|
| 329 | NULL, |
---|
| 330 | NULL, |
---|
| 331 | prplcb_conn_report_disconnect_reason, |
---|
| 332 | }; |
---|
| 333 | |
---|
[7da726b] | 334 | static void prplcb_blist_new( PurpleBlistNode *node ) |
---|
| 335 | { |
---|
| 336 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
| 337 | |
---|
[db4cd40] | 338 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
[7da726b] | 339 | { |
---|
[db4cd40] | 340 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
| 341 | |
---|
| 342 | if( ic == NULL ) |
---|
| 343 | return; |
---|
| 344 | |
---|
[7da726b] | 345 | imcb_add_buddy( ic, bud->name, NULL ); |
---|
| 346 | if( bud->server_alias ) |
---|
[4524f66] | 347 | { |
---|
| 348 | imcb_rename_buddy( ic, bud->name, bud->server_alias ); |
---|
[7da726b] | 349 | imcb_buddy_nick_hint( ic, bud->name, bud->server_alias ); |
---|
[4524f66] | 350 | } |
---|
[7da726b] | 351 | } |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | static void prplcb_blist_update( PurpleBuddyList *list, PurpleBlistNode *node ) |
---|
| 355 | { |
---|
| 356 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
| 357 | |
---|
[db4cd40] | 358 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
[7da726b] | 359 | { |
---|
[db4cd40] | 360 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
[4f103ea] | 361 | PurpleStatus *as; |
---|
| 362 | int flags = 0; |
---|
| 363 | |
---|
[db4cd40] | 364 | if( ic == NULL ) |
---|
| 365 | return; |
---|
| 366 | |
---|
[4524f66] | 367 | if( bud->server_alias ) |
---|
| 368 | imcb_rename_buddy( ic, bud->name, bud->server_alias ); |
---|
| 369 | |
---|
[4f103ea] | 370 | flags |= purple_presence_is_online( bud->presence ) ? OPT_LOGGED_IN : 0; |
---|
| 371 | flags |= purple_presence_is_available( bud->presence ) ? 0 : OPT_AWAY; |
---|
| 372 | |
---|
| 373 | as = purple_presence_get_active_status( bud->presence ); |
---|
| 374 | |
---|
| 375 | imcb_buddy_status( ic, bud->name, flags, purple_status_get_name( as ), |
---|
| 376 | purple_status_get_attr_string( as, "message" ) ); |
---|
[7da726b] | 377 | } |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | static void prplcb_blist_remove( PurpleBuddyList *list, PurpleBlistNode *node ) |
---|
| 381 | { |
---|
[3e7b640] | 382 | /* |
---|
[7da726b] | 383 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
| 384 | |
---|
[db4cd40] | 385 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
[0cbef26] | 386 | { |
---|
[db4cd40] | 387 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
| 388 | |
---|
| 389 | if( ic == NULL ) |
---|
| 390 | return; |
---|
| 391 | |
---|
[0cbef26] | 392 | imcb_remove_buddy( ic, bud->name, NULL ); |
---|
| 393 | } |
---|
[b3117f2] | 394 | */ |
---|
[7da726b] | 395 | } |
---|
| 396 | |
---|
| 397 | static PurpleBlistUiOps bee_blist_uiops = |
---|
| 398 | { |
---|
| 399 | NULL, |
---|
| 400 | prplcb_blist_new, |
---|
| 401 | NULL, |
---|
| 402 | prplcb_blist_update, |
---|
| 403 | prplcb_blist_remove, |
---|
| 404 | }; |
---|
| 405 | |
---|
[d250b2a] | 406 | static void prplcb_conv_im( PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime ) |
---|
| 407 | { |
---|
| 408 | struct im_connection *ic = purple_ic_by_pa( conv->account ); |
---|
[3e7b640] | 409 | PurpleBuddy *buddy; |
---|
[d250b2a] | 410 | |
---|
[389f7be] | 411 | /* ..._SEND means it's an outgoing message, no need to echo those. */ |
---|
[3e7b640] | 412 | if( flags & PURPLE_MESSAGE_SEND ) |
---|
| 413 | return; |
---|
| 414 | |
---|
| 415 | buddy = purple_find_buddy( conv->account, who ); |
---|
| 416 | if( buddy != NULL ) |
---|
| 417 | who = purple_buddy_get_contact_alias( buddy ); |
---|
| 418 | |
---|
| 419 | imcb_buddy_msg( ic, (char*) who, (char*) message, 0, mtime ); |
---|
[d250b2a] | 420 | } |
---|
| 421 | |
---|
[860ba6a] | 422 | static PurpleConversationUiOps bee_conv_uiops = |
---|
| 423 | { |
---|
| 424 | NULL, /* create_conversation */ |
---|
| 425 | NULL, /* destroy_conversation */ |
---|
| 426 | NULL, /* write_chat */ |
---|
[d250b2a] | 427 | prplcb_conv_im, /* write_im */ |
---|
[e046390] | 428 | NULL, /* write_conv */ |
---|
[860ba6a] | 429 | NULL, /* chat_add_users */ |
---|
| 430 | NULL, /* chat_rename_user */ |
---|
| 431 | NULL, /* chat_remove_users */ |
---|
| 432 | NULL, /* chat_update_user */ |
---|
| 433 | NULL, /* present */ |
---|
| 434 | NULL, /* has_focus */ |
---|
| 435 | NULL, /* custom_smiley_add */ |
---|
| 436 | NULL, /* custom_smiley_write */ |
---|
| 437 | NULL, /* custom_smiley_close */ |
---|
| 438 | NULL, /* send_confirm */ |
---|
| 439 | }; |
---|
| 440 | |
---|
[0ac1a375] | 441 | struct prplcb_request_action_data |
---|
| 442 | { |
---|
| 443 | void *user_data, *bee_data; |
---|
| 444 | PurpleRequestActionCb yes, no; |
---|
| 445 | int yes_i, no_i; |
---|
| 446 | }; |
---|
| 447 | |
---|
| 448 | static void prplcb_request_action_yes( void *data ) |
---|
| 449 | { |
---|
| 450 | struct prplcb_request_action_data *pqad = data; |
---|
| 451 | |
---|
| 452 | pqad->yes( pqad->user_data, pqad->yes_i ); |
---|
| 453 | g_free( pqad ); |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | static void prplcb_request_action_no( void *data ) |
---|
| 457 | { |
---|
| 458 | struct prplcb_request_action_data *pqad = data; |
---|
| 459 | |
---|
| 460 | pqad->no( pqad->user_data, pqad->no_i ); |
---|
| 461 | g_free( pqad ); |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | static void *prplcb_request_action( const char *title, const char *primary, const char *secondary, |
---|
| 465 | int default_action, PurpleAccount *account, const char *who, |
---|
| 466 | PurpleConversation *conv, void *user_data, size_t action_count, |
---|
| 467 | va_list actions ) |
---|
| 468 | { |
---|
| 469 | struct prplcb_request_action_data *pqad; |
---|
| 470 | int i; |
---|
| 471 | char *q; |
---|
| 472 | |
---|
| 473 | pqad = g_new0( struct prplcb_request_action_data, 1 ); |
---|
| 474 | |
---|
| 475 | for( i = 0; i < action_count; i ++ ) |
---|
| 476 | { |
---|
| 477 | char *caption; |
---|
| 478 | void *fn; |
---|
| 479 | |
---|
| 480 | caption = va_arg( actions, char* ); |
---|
| 481 | fn = va_arg( actions, void* ); |
---|
| 482 | |
---|
| 483 | if( strcmp( caption, "Accept" ) == 0 ) |
---|
| 484 | { |
---|
| 485 | pqad->yes = fn; |
---|
| 486 | pqad->yes_i = i; |
---|
| 487 | } |
---|
| 488 | else if( strcmp( caption, "Reject" ) == 0 ) |
---|
| 489 | { |
---|
| 490 | pqad->no = fn; |
---|
| 491 | pqad->no_i = i; |
---|
| 492 | } |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | pqad->user_data = user_data; |
---|
| 496 | |
---|
| 497 | q = g_strdup_printf( "Request: %s\n\n%s\n\n%s", title, primary, secondary ); |
---|
| 498 | pqad->bee_data = query_add( local_irc, purple_ic_by_pa( account ), q, |
---|
| 499 | prplcb_request_action_yes, prplcb_request_action_no, pqad ); |
---|
| 500 | |
---|
| 501 | g_free( q ); |
---|
[e5d8d21] | 502 | |
---|
| 503 | return pqad; |
---|
[0ac1a375] | 504 | } |
---|
| 505 | |
---|
| 506 | static PurpleRequestUiOps bee_request_uiops = |
---|
| 507 | { |
---|
| 508 | NULL, |
---|
| 509 | NULL, |
---|
| 510 | prplcb_request_action, |
---|
| 511 | NULL, |
---|
| 512 | NULL, |
---|
| 513 | NULL, |
---|
| 514 | NULL, |
---|
| 515 | }; |
---|
| 516 | |
---|
[0cbef26] | 517 | static void prplcb_debug_print( PurpleDebugLevel level, const char *category, const char *arg_s ) |
---|
| 518 | { |
---|
[6967d01] | 519 | fprintf( stderr, "DEBUG %s: %s", category, arg_s ); |
---|
[0cbef26] | 520 | } |
---|
| 521 | |
---|
| 522 | static PurpleDebugUiOps bee_debug_uiops = |
---|
| 523 | { |
---|
| 524 | prplcb_debug_print, |
---|
| 525 | }; |
---|
| 526 | |
---|
[4164e62] | 527 | static guint prplcb_ev_timeout_add( guint interval, GSourceFunc func, gpointer udata ) |
---|
| 528 | { |
---|
| 529 | return b_timeout_add( interval, (b_event_handler) func, udata ); |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | static guint prplcb_ev_input_add( int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer udata ) |
---|
| 533 | { |
---|
| 534 | return b_input_add( fd, cond | B_EV_FLAG_FORCE_REPEAT, (b_event_handler) func, udata ); |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | static gboolean prplcb_ev_remove( guint id ) |
---|
| 538 | { |
---|
| 539 | b_event_remove( (gint) id ); |
---|
| 540 | return TRUE; |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | static PurpleEventLoopUiOps glib_eventloops = |
---|
| 544 | { |
---|
| 545 | prplcb_ev_timeout_add, |
---|
| 546 | prplcb_ev_remove, |
---|
| 547 | prplcb_ev_input_add, |
---|
| 548 | prplcb_ev_remove, |
---|
| 549 | }; |
---|
| 550 | |
---|
[860ba6a] | 551 | static void purple_ui_init() |
---|
| 552 | { |
---|
[7da726b] | 553 | purple_blist_set_ui_ops( &bee_blist_uiops ); |
---|
[860ba6a] | 554 | purple_connections_set_ui_ops( &bee_conn_uiops ); |
---|
| 555 | purple_conversations_set_ui_ops( &bee_conv_uiops ); |
---|
[0ac1a375] | 556 | purple_request_set_ui_ops( &bee_request_uiops ); |
---|
[0cbef26] | 557 | //purple_debug_set_ui_ops( &bee_debug_uiops ); |
---|
[860ba6a] | 558 | } |
---|
| 559 | |
---|
[796da03] | 560 | void purple_initmodule() |
---|
| 561 | { |
---|
[cd741d8] | 562 | struct prpl funcs; |
---|
[796da03] | 563 | GList *prots; |
---|
[e5d8d21] | 564 | GString *help; |
---|
[796da03] | 565 | |
---|
[e046390] | 566 | if( B_EV_IO_READ != PURPLE_INPUT_READ || |
---|
| 567 | B_EV_IO_WRITE != PURPLE_INPUT_WRITE ) |
---|
| 568 | { |
---|
| 569 | /* FIXME FIXME FIXME FIXME FIXME :-) */ |
---|
| 570 | exit( 1 ); |
---|
| 571 | } |
---|
| 572 | |
---|
[796da03] | 573 | purple_util_set_user_dir("/tmp"); |
---|
| 574 | purple_debug_set_enabled(FALSE); |
---|
| 575 | purple_core_set_ui_ops(&bee_core_uiops); |
---|
| 576 | purple_eventloop_set_ui_ops(&glib_eventloops); |
---|
| 577 | if( !purple_core_init( "BitlBee") ) |
---|
| 578 | { |
---|
| 579 | /* Initializing the core failed. Terminate. */ |
---|
| 580 | fprintf( stderr, "libpurple initialization failed.\n" ); |
---|
| 581 | abort(); |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | /* This seems like stateful shit we don't want... */ |
---|
| 585 | purple_set_blist(purple_blist_new()); |
---|
| 586 | purple_blist_load(); |
---|
| 587 | |
---|
| 588 | /* Meh? */ |
---|
| 589 | purple_prefs_load(); |
---|
| 590 | |
---|
[cd741d8] | 591 | memset( &funcs, 0, sizeof( funcs ) ); |
---|
| 592 | funcs.login = purple_login; |
---|
| 593 | funcs.init = purple_init; |
---|
| 594 | funcs.logout = purple_logout; |
---|
| 595 | funcs.buddy_msg = purple_buddy_msg; |
---|
| 596 | funcs.away_states = purple_away_states; |
---|
| 597 | funcs.set_away = purple_set_away; |
---|
| 598 | funcs.add_buddy = purple_add_buddy; |
---|
| 599 | funcs.remove_buddy = purple_remove_buddy; |
---|
| 600 | funcs.keepalive = purple_keepalive; |
---|
| 601 | funcs.send_typing = purple_send_typing; |
---|
| 602 | funcs.handle_cmp = g_strcasecmp; |
---|
| 603 | |
---|
[e5d8d21] | 604 | help = g_string_new("BitlBee libpurple module supports the following IM protocols:\n"); |
---|
| 605 | |
---|
[796da03] | 606 | for( prots = purple_plugins_get_protocols(); prots; prots = prots->next ) |
---|
| 607 | { |
---|
| 608 | PurplePlugin *prot = prots->data; |
---|
[cd741d8] | 609 | struct prpl *ret; |
---|
[796da03] | 610 | |
---|
[cd741d8] | 611 | ret = g_memdup( &funcs, sizeof( funcs ) ); |
---|
| 612 | ret->name = ret->data = prot->info->id; |
---|
| 613 | if( strncmp( ret->name, "prpl-", 5 ) == 0 ) |
---|
| 614 | ret->name += 5; |
---|
[796da03] | 615 | register_protocol( ret ); |
---|
[cd741d8] | 616 | |
---|
[e5d8d21] | 617 | g_string_append_printf( help, "\n* %s (%s)", ret->name, prot->info->name ); |
---|
| 618 | |
---|
[cd741d8] | 619 | if( g_strcasecmp( prot->info->id, "prpl-aim" ) == 0 ) |
---|
| 620 | { |
---|
| 621 | ret = g_memdup( &funcs, sizeof( funcs ) ); |
---|
| 622 | ret->name = "oscar"; |
---|
| 623 | ret->data = prot->info->id; |
---|
| 624 | register_protocol( ret ); |
---|
| 625 | } |
---|
[796da03] | 626 | } |
---|
[e5d8d21] | 627 | |
---|
| 628 | help_add_mem( &global.help, "purple", help->str ); |
---|
| 629 | g_string_free( help, TRUE ); |
---|
[796da03] | 630 | } |
---|