1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
|
/**
* @file connection.c Connection API
* @ingroup core
*/
/* purple
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#define _PURPLE_CONNECTION_C_
#include "internal.h"
#include "account.h"
#include "blist.h"
#include "connection.h"
#include "dbus-maybe.h"
#include "debug.h"
#include "log.h"
#include "notify.h"
#include "prefs.h"
#include "proxy.h"
#include "request.h"
#include "server.h"
#include "signals.h"
#include "util.h"
#define KEEPALIVE_INTERVAL 30
static GList *connections = NULL;
static GList *connections_connecting = NULL;
static PurpleConnectionUiOps *connection_ui_ops = NULL;
static int connections_handle;
static gboolean
send_keepalive(gpointer data)
{
PurpleConnection *gc = data;
PurplePluginProtocolInfo *prpl_info;
/* Only send keep-alives if we haven't heard from the
* server in a while.
*/
if ((time(NULL) - gc->last_received) < KEEPALIVE_INTERVAL)
return TRUE;
prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl);
if (prpl_info->keepalive)
prpl_info->keepalive(gc);
return TRUE;
}
static void
update_keepalive(PurpleConnection *gc, gboolean on)
{
PurplePluginProtocolInfo *prpl_info = NULL;
if (gc != NULL && gc->prpl != NULL)
prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl);
if (!prpl_info || !prpl_info->keepalive)
return;
if (on && !gc->keepalive)
{
purple_debug_info("connection", "Activating keepalive.\n");
gc->keepalive = purple_timeout_add_seconds(KEEPALIVE_INTERVAL, send_keepalive, gc);
}
else if (!on && gc->keepalive > 0)
{
purple_debug_info("connection", "Deactivating keepalive.\n");
purple_timeout_remove(gc->keepalive);
gc->keepalive = 0;
}
}
void
purple_connection_new(PurpleAccount *account, gboolean regist, const char *password)
{
_purple_connection_new(account, regist, password);
}
void
_purple_connection_new(PurpleAccount *account, gboolean regist, const char *password)
{
PurpleConnection *gc;
PurplePlugin *prpl;
PurplePluginProtocolInfo *prpl_info;
g_return_if_fail(account != NULL);
if (!purple_account_is_disconnected(account))
return;
prpl = purple_find_prpl(purple_account_get_protocol_id(account));
if (prpl != NULL)
prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
else {
gchar *message;
message = g_strdup_printf(_("Missing protocol plugin for %s"),
purple_account_get_username(account));
purple_notify_error(NULL, regist ? _("Registration Error") :
_("Connection Error"), message, NULL);
g_free(message);
return;
}
if (regist)
{
if (prpl_info->register_user == NULL)
return;
}
else
{
if (((password == NULL) || (*password == '\0')) &&
!(prpl_info->options & OPT_PROTO_NO_PASSWORD) &&
!(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL))
{
purple_debug_error("connection", "Cannot connect to account %s without "
"a password.\n", purple_account_get_username(account));
return;
}
}
gc = g_new0(PurpleConnection, 1);
PURPLE_DBUS_REGISTER_POINTER(gc, PurpleConnection);
gc->prpl = prpl;
if ((password != NULL) && (*password != '\0'))
gc->password = g_strdup(password);
purple_connection_set_account(gc, account);
purple_connection_set_state(gc, PURPLE_CONNECTING);
connections = g_list_append(connections, gc);
purple_account_set_connection(account, gc);
purple_signal_emit(purple_connections_get_handle(), "signing-on", gc);
if (regist)
{
purple_debug_info("connection", "Registering. gc = %p\n", gc);
/* set this so we don't auto-reconnect after registering */
gc->wants_to_die = TRUE;
prpl_info->register_user(account);
}
else
{
purple_debug_info("connection", "Connecting. gc = %p\n", gc);
purple_signal_emit(purple_accounts_get_handle(), "account-connecting", account);
prpl_info->login(account);
}
}
void
purple_connection_new_unregister(PurpleAccount *account, const char *password, PurpleAccountUnregistrationCb cb, void *user_data)
{
_purple_connection_new_unregister(account, password, cb, user_data);
}
void
_purple_connection_new_unregister(PurpleAccount *account, const char *password, PurpleAccountUnregistrationCb cb, void *user_data)
{
/* Lots of copy/pasted code to avoid API changes. You might want to integrate that into the previous function when posssible. */
PurpleConnection *gc;
PurplePlugin *prpl;
PurplePluginProtocolInfo *prpl_info;
g_return_if_fail(account != NULL);
prpl = purple_find_prpl(purple_account_get_protocol_id(account));
if (prpl != NULL)
prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
else {
gchar *message;
message = g_strdup_printf(_("Missing protocol plugin for %s"),
purple_account_get_username(account));
purple_notify_error(NULL, _("Unregistration Error"), message, NULL);
g_free(message);
return;
}
if (!purple_account_is_disconnected(account)) {
prpl_info->unregister_user(account, cb, user_data);
return;
}
if (((password == NULL) || (*password == '\0')) &&
!(prpl_info->options & OPT_PROTO_NO_PASSWORD) &&
!(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL))
{
purple_debug_error("connection", "Cannot connect to account %s without "
"a password.\n", purple_account_get_username(account));
return;
}
gc = g_new0(PurpleConnection, 1);
PURPLE_DBUS_REGISTER_POINTER(gc, PurpleConnection);
gc->prpl = prpl;
if ((password != NULL) && (*password != '\0'))
gc->password = g_strdup(password);
purple_connection_set_account(gc, account);
purple_connection_set_state(gc, PURPLE_CONNECTING);
connections = g_list_append(connections, gc);
purple_account_set_connection(account, gc);
purple_signal_emit(purple_connections_get_handle(), "signing-on", gc);
purple_debug_info("connection", "Unregistering. gc = %p\n", gc);
prpl_info->unregister_user(account, cb, user_data);
}
void
purple_connection_destroy(PurpleConnection *gc)
{
_purple_connection_destroy(gc);
}
void
_purple_connection_destroy(PurpleConnection *gc)
{
PurpleAccount *account;
GSList *buddies;
PurplePluginProtocolInfo *prpl_info = NULL;
gboolean remove = FALSE;
g_return_if_fail(gc != NULL);
account = purple_connection_get_account(gc);
purple_debug_info("connection", "Disconnecting connection %p\n", gc);
if (purple_connection_get_state(gc) != PURPLE_CONNECTING)
remove = TRUE;
purple_signal_emit(purple_connections_get_handle(), "signing-off", gc);
while (gc->buddy_chats)
{
PurpleConversation *b = gc->buddy_chats->data;
gc->buddy_chats = g_slist_remove(gc->buddy_chats, b);
purple_conv_chat_left(PURPLE_CONV_CHAT(b));
}
update_keepalive(gc, FALSE);
purple_proxy_connect_cancel_with_handle(gc);
prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl);
if (prpl_info->close)
(prpl_info->close)(gc);
/* Clear out the proto data that was freed in the prpl close method*/
buddies = purple_find_buddies(account, NULL);
while (buddies != NULL) {
PurpleBuddy *buddy = buddies->data;
purple_buddy_set_protocol_data(buddy, NULL);
buddies = g_slist_delete_link(buddies, buddies);
}
connections = g_list_remove(connections, gc);
purple_connection_set_state(gc, PURPLE_DISCONNECTED);
if (remove)
purple_blist_remove_account(account);
purple_signal_emit(purple_connections_get_handle(), "signed-off", gc);
purple_account_request_close_with_account(account);
purple_request_close_with_handle(gc);
purple_notify_close_with_handle(gc);
purple_debug_info("connection", "Destroying connection %p\n", gc);
purple_account_set_connection(account, NULL);
g_free(gc->password);
g_free(gc->display_name);
if (gc->disconnect_timeout > 0)
purple_timeout_remove(gc->disconnect_timeout);
PURPLE_DBUS_UNREGISTER_POINTER(gc);
g_free(gc);
}
/*
* d:)->-<
*
* d:O-\-<
*
* d:D-/-<
*
* d8D->-< DANCE!
*/
void
purple_connection_set_state(PurpleConnection *gc, PurpleConnectionState state)
{
PurpleConnectionUiOps *ops;
g_return_if_fail(gc != NULL);
if (gc->state == state)
return;
gc->state = state;
ops = purple_connections_get_ui_ops();
if (gc->state == PURPLE_CONNECTING) {
connections_connecting = g_list_append(connections_connecting, gc);
}
else {
connections_connecting = g_list_remove(connections_connecting, gc);
}
if (gc->state == PURPLE_CONNECTED) {
PurpleAccount *account;
PurplePresence *presence;
account = purple_connection_get_account(gc);
presence = purple_account_get_presence(account);
/* Set the time the account came online */
purple_presence_set_login_time(presence, time(NULL));
if (purple_prefs_get_bool("/purple/logging/log_system"))
{
PurpleLog *log = purple_account_get_log(account, TRUE);
if (log != NULL)
{
char *msg = g_strdup_printf(_("+++ %s signed on"),
purple_account_get_username(account));
purple_log_write(log, PURPLE_MESSAGE_SYSTEM,
purple_account_get_username(account),
purple_presence_get_login_time(presence),
msg);
g_free(msg);
}
}
if (ops != NULL && ops->connected != NULL)
ops->connected(gc);
purple_blist_add_account(account);
purple_signal_emit(purple_connections_get_handle(), "signed-on", gc);
purple_signal_emit_return_1(purple_connections_get_handle(), "autojoin", gc);
serv_set_permit_deny(gc);
update_keepalive(gc, TRUE);
}
else if (gc->state == PURPLE_DISCONNECTED) {
PurpleAccount *account = purple_connection_get_account(gc);
if (purple_prefs_get_bool("/purple/logging/log_system"))
{
PurpleLog *log = purple_account_get_log(account, FALSE);
if (log != NULL)
{
char *msg = g_strdup_printf(_("+++ %s signed off"),
purple_account_get_username(account));
purple_log_write(log, PURPLE_MESSAGE_SYSTEM,
purple_account_get_username(account), time(NULL),
msg);
g_free(msg);
}
}
purple_account_destroy_log(account);
if (ops != NULL && ops->disconnected != NULL)
ops->disconnected(gc);
}
}
void
purple_connection_set_account(PurpleConnection *gc, PurpleAccount *account)
{
g_return_if_fail(gc != NULL);
g_return_if_fail(account != NULL);
gc->account = account;
}
void
purple_connection_set_display_name(PurpleConnection *gc, const char *name)
{
g_return_if_fail(gc != NULL);
g_free(gc->display_name);
gc->display_name = g_strdup(name);
}
void
purple_connection_set_protocol_data(PurpleConnection *connection, void *proto_data) {
g_return_if_fail(connection != NULL);
connection->proto_data = proto_data;
}
PurpleConnectionState
purple_connection_get_state(const PurpleConnection *gc)
{
g_return_val_if_fail(gc != NULL, PURPLE_DISCONNECTED);
return gc->state;
}
PurpleAccount *
purple_connection_get_account(const PurpleConnection *gc)
{
g_return_val_if_fail(gc != NULL, NULL);
return gc->account;
}
PurplePlugin *
purple_connection_get_prpl(const PurpleConnection *gc)
{
g_return_val_if_fail(gc != NULL, NULL);
return gc->prpl;
}
const char *
purple_connection_get_password(const PurpleConnection *gc)
{
g_return_val_if_fail(gc != NULL, NULL);
return gc->password ? gc->password : gc->account->password;
}
const char *
purple_connection_get_display_name(const PurpleConnection *gc)
{
g_return_val_if_fail(gc != NULL, NULL);
return gc->display_name;
}
void *
purple_connection_get_protocol_data(const PurpleConnection *connection) {
g_return_val_if_fail(connection != NULL, NULL);
return connection->proto_data;
}
void
purple_connection_update_progress(PurpleConnection *gc, const char *text,
size_t step, size_t count)
{
PurpleConnectionUiOps *ops;
g_return_if_fail(gc != NULL);
g_return_if_fail(text != NULL);
g_return_if_fail(step < count);
g_return_if_fail(count > 1);
ops = purple_connections_get_ui_ops();
if (ops != NULL && ops->connect_progress != NULL)
ops->connect_progress(gc, text, step, count);
}
void
purple_connection_notice(PurpleConnection *gc, const char *text)
{
PurpleConnectionUiOps *ops;
g_return_if_fail(gc != NULL);
g_return_if_fail(text != NULL);
ops = purple_connections_get_ui_ops();
if (ops != NULL && ops->notice != NULL)
ops->notice(gc, text);
}
static gboolean
purple_connection_disconnect_cb(gpointer data)
{
PurpleAccount *account;
PurpleConnection *gc;
char *password;
account = data;
gc = purple_account_get_connection(account);
if (gc != NULL)
gc->disconnect_timeout = 0;
password = g_strdup(purple_account_get_password(account));
purple_account_disconnect(account);
purple_account_set_password(account, password);
g_free(password);
return FALSE;
}
void
purple_connection_error(PurpleConnection *gc, const char *text)
{
/* prpls that have not been updated to use disconnection reasons will
* be setting wants_to_die before calling this function, so choose
* PURPLE_CONNECTION_ERROR_OTHER_ERROR (which is fatal) if it's true,
* and PURPLE_CONNECTION_ERROR_NETWORK_ERROR (which isn't) if not. See
* the documentation in connection.h.
*/
PurpleConnectionError reason = gc->wants_to_die
? PURPLE_CONNECTION_ERROR_OTHER_ERROR
: PURPLE_CONNECTION_ERROR_NETWORK_ERROR;
purple_connection_error_reason (gc, reason, text);
}
void
purple_connection_error_reason (PurpleConnection *gc,
PurpleConnectionError reason,
const char *description)
{
PurpleConnectionUiOps *ops;
g_return_if_fail(gc != NULL);
/* This sanity check relies on PURPLE_CONNECTION_ERROR_OTHER_ERROR
* being the last member of the PurpleConnectionError enum in
* connection.h; if other reasons are added after it, this check should
* be updated.
*/
if (reason > PURPLE_CONNECTION_ERROR_OTHER_ERROR) {
purple_debug_error("connection",
"purple_connection_error_reason: reason %u isn't a "
"valid reason\n", reason);
reason = PURPLE_CONNECTION_ERROR_OTHER_ERROR;
}
if (description == NULL) {
purple_debug_error("connection", "purple_connection_error_reason called with NULL description\n");
description = _("Unknown error");
}
/* If we've already got one error, we don't need any more */
if (gc->disconnect_timeout > 0)
return;
gc->wants_to_die = purple_connection_error_is_fatal (reason);
purple_debug_info("connection", "Connection error on %p (reason: %u description: %s)\n",
gc, reason, description);
ops = purple_connections_get_ui_ops();
if (ops != NULL)
{
if (ops->report_disconnect_reason != NULL)
ops->report_disconnect_reason (gc, reason, description);
if (ops->report_disconnect != NULL)
ops->report_disconnect (gc, description);
}
purple_signal_emit(purple_connections_get_handle(), "connection-error",
gc, reason, description);
gc->disconnect_timeout = purple_timeout_add(0, purple_connection_disconnect_cb,
purple_connection_get_account(gc));
}
void
purple_connection_ssl_error (PurpleConnection *gc,
PurpleSslErrorType ssl_error)
{
PurpleConnectionError reason;
switch (ssl_error) {
case PURPLE_SSL_HANDSHAKE_FAILED:
reason = PURPLE_CONNECTION_ERROR_ENCRYPTION_ERROR;
break;
case PURPLE_SSL_CONNECT_FAILED:
reason = PURPLE_CONNECTION_ERROR_NETWORK_ERROR;
break;
case PURPLE_SSL_CERTIFICATE_INVALID:
/* TODO: maybe PURPLE_SSL_* should be more specific? */
reason = PURPLE_CONNECTION_ERROR_CERT_OTHER_ERROR;
break;
default:
g_assert_not_reached ();
reason = PURPLE_CONNECTION_ERROR_CERT_OTHER_ERROR;
}
purple_connection_error_reason (gc, reason,
purple_ssl_strerror(ssl_error));
}
gboolean
purple_connection_error_is_fatal (PurpleConnectionError reason)
{
switch (reason)
{
case PURPLE_CONNECTION_ERROR_NETWORK_ERROR:
case PURPLE_CONNECTION_ERROR_ENCRYPTION_ERROR:
return FALSE;
case PURPLE_CONNECTION_ERROR_INVALID_USERNAME:
case PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED:
case PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE:
case PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT:
case PURPLE_CONNECTION_ERROR_NAME_IN_USE:
case PURPLE_CONNECTION_ERROR_INVALID_SETTINGS:
case PURPLE_CONNECTION_ERROR_OTHER_ERROR:
case PURPLE_CONNECTION_ERROR_CERT_NOT_PROVIDED:
case PURPLE_CONNECTION_ERROR_CERT_UNTRUSTED:
case PURPLE_CONNECTION_ERROR_CERT_EXPIRED:
case PURPLE_CONNECTION_ERROR_CERT_NOT_ACTIVATED:
case PURPLE_CONNECTION_ERROR_CERT_HOSTNAME_MISMATCH:
case PURPLE_CONNECTION_ERROR_CERT_FINGERPRINT_MISMATCH:
case PURPLE_CONNECTION_ERROR_CERT_SELF_SIGNED:
case PURPLE_CONNECTION_ERROR_CERT_OTHER_ERROR:
return TRUE;
default:
g_return_val_if_reached(TRUE);
}
}
void
purple_connections_disconnect_all(void)
{
GList *l;
PurpleConnection *gc;
while ((l = purple_connections_get_all()) != NULL) {
gc = l->data;
gc->wants_to_die = TRUE;
purple_account_disconnect(gc->account);
}
}
GList *
purple_connections_get_all(void)
{
return connections;
}
GList *
purple_connections_get_connecting(void)
{
return connections_connecting;
}
void
purple_connections_set_ui_ops(PurpleConnectionUiOps *ops)
{
connection_ui_ops = ops;
}
PurpleConnectionUiOps *
purple_connections_get_ui_ops(void)
{
return connection_ui_ops;
}
void
purple_connections_init(void)
{
void *handle = purple_connections_get_handle();
purple_signal_register(handle, "signing-on",
purple_marshal_VOID__POINTER, NULL, 1,
purple_value_new(PURPLE_TYPE_SUBTYPE,
PURPLE_SUBTYPE_CONNECTION));
purple_signal_register(handle, "signed-on",
purple_marshal_VOID__POINTER, NULL, 1,
purple_value_new(PURPLE_TYPE_SUBTYPE,
PURPLE_SUBTYPE_CONNECTION));
purple_signal_register(handle, "signing-off",
purple_marshal_VOID__POINTER, NULL, 1,
purple_value_new(PURPLE_TYPE_SUBTYPE,
PURPLE_SUBTYPE_CONNECTION));
purple_signal_register(handle, "signed-off",
purple_marshal_VOID__POINTER, NULL, 1,
purple_value_new(PURPLE_TYPE_SUBTYPE,
PURPLE_SUBTYPE_CONNECTION));
purple_signal_register(handle, "connection-error",
purple_marshal_VOID__POINTER_INT_POINTER, NULL, 3,
purple_value_new(PURPLE_TYPE_SUBTYPE,
PURPLE_SUBTYPE_CONNECTION),
purple_value_new(PURPLE_TYPE_ENUM),
purple_value_new(PURPLE_TYPE_STRING));
purple_signal_register(handle, "autojoin",
purple_marshal_BOOLEAN__POINTER, NULL, 1,
purple_value_new(PURPLE_TYPE_SUBTYPE,
PURPLE_SUBTYPE_CONNECTION));
}
void
purple_connections_uninit(void)
{
purple_signals_unregister_by_instance(purple_connections_get_handle());
}
void *
purple_connections_get_handle(void)
{
return &connections_handle;
}
|