clientsig.c

Go to the documentation of this file.
00001 /* OpenSER PURPLE MODULE
00002  * 
00003  * Copyright (C) 2008 Atos Worldline
00004  * Contact: Eric PTAK <eric.ptak@atosorigin.com>
00005  *
00006  * This program is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  *
00019  */
00020 #include <glib.h>
00021 
00022 #include "../../dprint.h"
00023 #include "../../mem/mem.h"
00024 
00025 #include "defines.h"
00026 #include "clientsig.h"
00027 #include "purple.h"
00028 #include "purple_sip.h"
00029 #include "mapping.h"
00030 #include "hashtable.h"
00031 #include "utils.h"
00032 
00033 #include <libpurple/account.h>
00034 #include <libpurple/connection.h>
00035 #include <libpurple/status.h>
00036 
00037 static void signed_on(PurpleConnection *gc) {
00038    LM_DBG("signed-on with <%s>\n", gc->account->username);
00039 }
00040 
00041 static void signing_on(PurpleConnection *gc) {
00042    LM_DBG("signing-on with <%s>...\n", gc->account->username);
00043 }
00044 
00045 static void signed_off(PurpleConnection *gc) {
00046    LM_DBG("signed-off with <%s>\n", gc->account->username);
00047    purple_account_set_enabled(gc->account, UI_ID, FALSE);
00048 }
00049 
00050 static void signing_off(PurpleConnection *gc) {
00051    LM_DBG("signing-off with <%s>...\n", gc->account->username);
00052 }
00053 
00054 static void account_error_changed(PurpleAccount *acc, const PurpleConnectionErrorInfo *old_error, const PurpleConnectionErrorInfo *current_error) {
00055    if (current_error) {
00056       LM_DBG("new account error : <%s>\n", current_error->description);
00057    }
00058 }
00059 
00060 static void account_connecting(PurpleAccount *acc) {
00061    LM_DBG("trying to connect with <%s>\n", acc->username);
00062 }
00063 
00064 static void account_enabled(PurpleAccount *acc) {
00065    LM_DBG("account <%s> enabled...\n", acc->username);
00066 }
00067 
00068 static void account_disabled(PurpleAccount *acc) {
00069    LM_DBG("account <%s> disabled...\n", acc->username);
00070 }
00071 
00072 
00073 static void buddy_status_changed(PurpleBuddy *buddy, PurpleStatus *old_status, PurpleStatus *status) {
00074         PurplePlugin *prpl;
00075         PurplePluginProtocolInfo *prpl_info = NULL;
00076    char *sip_from = find_sip_user(buddy->name);
00077    int d = hashtable_get_counter(buddy->name);
00078    PurpleStatusPrimitive primitive;
00079    enum purple_publish_basic basic;
00080    enum purple_publish_activity activity;
00081          char *statustext = NULL, *tmp = NULL, *new;
00082    const char *end;
00083 
00084    LM_DBG("buddy <%s> has changed status\n", buddy->name);
00085    if ((sip_from) && (d>0)) {
00086       primitive = purple_status_type_get_primitive(purple_status_get_type(status));
00087       primitive_parse(primitive, &basic, &activity);
00088 
00089 
00090 //    char *note = purple_status_get_attr_string(status, "message");
00091 
00092            prpl = purple_find_prpl(purple_account_get_protocol_id(buddy->account));
00093 
00094       if (prpl != NULL)
00095          prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
00096 
00097       if (prpl_info && prpl_info->status_text && buddy->account->gc) {
00098          tmp = prpl_info->status_text(buddy);
00099 
00100          if(tmp && !g_utf8_validate(tmp, -1, &end)) {
00101             new = g_strndup(tmp, g_utf8_pointer_to_offset(tmp, end));
00102             g_free(tmp);
00103             tmp = new;
00104          }
00105 
00106          if(tmp) {
00107             g_strdelimit(tmp, "\n", ' ');
00108             purple_str_strip_char(tmp, '\r');
00109          }
00110          statustext = tmp;
00111       }
00112 
00113 
00114       LM_DBG("<%s> translated to <%s>, sending publish (note = %s)\n", buddy->name, sip_from, statustext);
00115       purple_send_sip_publish(sip_from, buddy->name, basic, activity, statustext);
00116 
00117       pkg_free(sip_from);
00118       g_free(statustext);
00119    }
00120 }
00121 
00122 static void buddy_signed_on(PurpleBuddy *buddy) {
00123    char *sip_from;
00124    int d;
00125    LM_DBG("buddy <%s> signed on\n", buddy->name);
00126    sip_from = find_sip_user(buddy->name);
00127    if (sip_from) {
00128       LM_DBG("<%s> translated to <%s>\n", buddy->name, sip_from);
00129    }
00130    else {
00131       LM_DBG("cannot translate <%s>\n", buddy->name);
00132       pkg_free(sip_from);
00133       return;
00134    }
00135    d = hashtable_get_counter(buddy->name);
00136    if (d>0) {
00137       if (purple_send_sip_publish(sip_from, buddy->name, PURPLE_BASIC_OPEN, PURPLE_ACTIVITY_AVAILABLE, NULL) < 0)
00138          LM_ERR("error sending PUBLISH for %s\n", buddy->name);
00139       else 
00140          LM_DBG("<%s> referenced %d times, PUBLISH sent\n", buddy->name, d);
00141       pkg_free(sip_from);
00142    }
00143    else 
00144       LM_DBG("%s is no more referenced, cannot publish\n", buddy->name);
00145    
00146 }
00147 
00148 static void buddy_signed_off(PurpleBuddy *buddy) {
00149    char *sip_from;
00150    int d;
00151    LM_DBG("buddy <%s> signed off\n", buddy->name);
00152    sip_from = find_sip_user(buddy->name);
00153    if (sip_from) {
00154       LM_DBG("<%s> translated to <%s>\n", buddy->name, sip_from);
00155    }
00156    else {
00157       LM_DBG("cannot translate <%s>\n", buddy->name);
00158       pkg_free(sip_from);
00159       return;
00160    }
00161    d = hashtable_get_counter(buddy->name);
00162    if (d>0) {
00163       if (purple_send_sip_publish(sip_from, buddy->name, PURPLE_BASIC_CLOSED, 0, NULL) < 0)
00164          LM_ERR("error sending PUBLISH for %s\n", buddy->name);
00165       else 
00166          LM_DBG("<%s> referenced %d times, PUBLISH sent\n", buddy->name, d);
00167       pkg_free(sip_from);
00168    }
00169    else 
00170       LM_DBG("%s is no more referenced, cannot publish\n", buddy->name);
00171    
00172    
00173 }
00174 
00175 static void buddy_added(PurpleBuddy *buddy) {
00176    LM_DBG("%s added to %s buddy list\n", buddy->name, buddy->account->username);
00177 }
00178 
00179 void client_connect_signals(void) {
00180    static int handle;
00181    purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle,
00182          PURPLE_CALLBACK(signed_on), NULL);
00183    purple_signal_connect(purple_connections_get_handle(), "signed-off", &handle,
00184          PURPLE_CALLBACK(signed_off), NULL);
00185    purple_signal_connect(purple_connections_get_handle(), "signing-on", &handle,
00186          PURPLE_CALLBACK(signing_on), NULL);
00187    purple_signal_connect(purple_connections_get_handle(), "signing-off", &handle,
00188          PURPLE_CALLBACK(signing_off), NULL);
00189 
00190    purple_signal_connect(purple_accounts_get_handle(), "account-error-changed", &handle,
00191          PURPLE_CALLBACK(account_error_changed), NULL);
00192    purple_signal_connect(purple_accounts_get_handle(), "account-connecting", &handle,
00193          PURPLE_CALLBACK(account_connecting), NULL);
00194    purple_signal_connect(purple_accounts_get_handle(), "account-enabled", &handle,
00195          PURPLE_CALLBACK(account_enabled), NULL);
00196    purple_signal_connect(purple_accounts_get_handle(), "account-disabled", &handle,
00197          PURPLE_CALLBACK(account_disabled), NULL);
00198 
00199    purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", &handle,
00200          PURPLE_CALLBACK(buddy_signed_on), NULL);
00201    purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", &handle,
00202          PURPLE_CALLBACK(buddy_signed_off), NULL);
00203    purple_signal_connect(purple_blist_get_handle(), "buddy-status-changed", &handle,
00204          PURPLE_CALLBACK(buddy_status_changed), NULL);
00205    purple_signal_connect(purple_blist_get_handle(), "buddy-added", &handle,
00206          PURPLE_CALLBACK(buddy_added), NULL);
00207 }
00208 
00209 

Generated on Mon May 21 18:00:25 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6