miniclient.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 <unistd.h>
00021 #include <glib.h>
00022 #include <errno.h>
00023 #include <string.h>
00024 #include <signal.h>
00025 
00026 #include "../../dprint.h"
00027 #include "../../str.h"
00028 
00029 #include <libpurple/account.h>
00030 #include <libpurple/accountopt.h>
00031 #include <libpurple/conversation.h>
00032 #include <libpurple/connection.h>
00033 #include <libpurple/core.h>
00034 #include <libpurple/debug.h>
00035 #include <libpurple/eventloop.h>
00036 #include <libpurple/ft.h>
00037 #include <libpurple/log.h>
00038 #include <libpurple/notify.h>
00039 #include <libpurple/plugin.h>
00040 #include <libpurple/prefs.h>
00041 #include <libpurple/prpl.h>
00042 #include <libpurple/pounce.h>
00043 #include <libpurple/savedstatuses.h>
00044 #include <libpurple/sound.h>
00045 #include <libpurple/status.h>
00046 #include <libpurple/util.h>
00047 #include <libpurple/whiteboard.h>
00048 #include <libpurple/xmlnode.h>
00049 
00050 #include "miniclient.h"
00051 #include "defines.h"
00052 #include "purple.h"
00053 #include "mapping.h"
00054 #include "clientsig.h"
00055 #include "clientpipe.h"
00056 #include "clientops.h"
00057 #include "hashtable.h"
00058 
00059 PurpleProxyInfo *proxy = NULL;
00060 extern str httpProxy_host;
00061 extern int httpProxy_port;
00062 
00063 
00064 /**
00065  * The following eventloop functions are used in both pidgin and purple-text. If your
00066  * application uses glib mainloop, you can safely use this verbatim.
00067  */
00068 #define PURPLE_GLIB_READ_COND  (G_IO_IN | G_IO_HUP | G_IO_ERR)
00069 #define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
00070 
00071 static void io_destroy(gpointer data) {
00072    g_free(data);
00073 }
00074 
00075 static gboolean io_invoke(GIOChannel *source, GIOCondition condition, gpointer data) {
00076    PurpleGLibIOClosure *closure = data;
00077    PurpleInputCondition purple_cond = 0;
00078 
00079    if (condition & PURPLE_GLIB_READ_COND)
00080       purple_cond |= PURPLE_INPUT_READ;
00081    if (condition & PURPLE_GLIB_WRITE_COND)
00082       purple_cond |= PURPLE_INPUT_WRITE;
00083 
00084    closure->function(closure->data, g_io_channel_unix_get_fd(source), purple_cond);
00085 
00086    return TRUE;
00087 }
00088 
00089 static guint input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function, gpointer data) {
00090    PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1);
00091    GIOChannel *channel;
00092    GIOCondition cond = 0;
00093 
00094    closure->function = function;
00095    closure->data = data;
00096 
00097    if (condition & PURPLE_INPUT_READ)
00098       cond |= PURPLE_GLIB_READ_COND;
00099    if (condition & PURPLE_INPUT_WRITE)
00100       cond |= PURPLE_GLIB_WRITE_COND;
00101 
00102    channel = g_io_channel_unix_new(fd);
00103    g_io_channel_set_encoding(channel, NULL, NULL);
00104    closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, io_invoke, closure, io_destroy);
00105 
00106    g_io_channel_unref(channel);
00107    return closure->result;
00108 }
00109 
00110 static PurpleEventLoopUiOps glib_eventloops = {
00111    g_timeout_add,
00112    g_source_remove,
00113    input_add,
00114    g_source_remove,
00115    NULL,
00116 #if GLIB_CHECK_VERSION(2,14,0)
00117    g_timeout_add_seconds,
00118 #else
00119    NULL,
00120 #endif
00121 
00122    /* padding */
00123    NULL,
00124    NULL,
00125    NULL
00126 };
00127 /*** End of the eventloop functions. ***/
00128 
00129 static PurpleConversationUiOps conv_uiops = {
00130    NULL,                      /* create_conversation  */
00131    NULL,                      /* destroy_conversation */
00132    NULL,                   /* write_chat           */
00133    NULL,                    /* write_im             */
00134    write_conv,                /* write_conv           */
00135    NULL,                      /* chat_add_users       */
00136    NULL,                      /* chat_rename_user     */
00137    NULL,                      /* chat_remove_users    */
00138    NULL,                      /* chat_update_user     */
00139    NULL,                      /* present              */
00140    NULL,                      /* has_focus            */
00141    NULL,                      /* custom_smiley_add    */
00142    NULL,                      /* custom_smiley_write  */
00143    NULL,                      /* custom_smiley_close  */
00144    NULL,                      /* send_confirm         */
00145    NULL,
00146    NULL,
00147    NULL,
00148    NULL
00149 };
00150 
00151 static void ui_init(void) {
00152    purple_conversations_set_ui_ops(&conv_uiops);
00153 }
00154 
00155 static PurpleCoreUiOps core_uiops = {
00156    NULL,
00157    NULL,
00158    ui_init,
00159    NULL,
00160 
00161    /* padding */
00162    NULL,
00163    NULL,
00164    NULL,
00165    NULL
00166 };
00167 
00168 static void init_libpurple(int fd) {
00169    /* Set a custom user directory (optional) */
00170    purple_util_set_user_dir(USER_DIRECTORY);
00171 
00172    purple_debug_set_enabled(FALSE);
00173 
00174    purple_core_set_ui_ops(&core_uiops);
00175 
00176    purple_eventloop_set_ui_ops(&glib_eventloops);
00177 
00178    purple_plugins_add_search_path(PLUGIN_PATH);
00179    
00180    purple_input_add(fd, PURPLE_INPUT_READ, pipe_reader, NULL);
00181    
00182 
00183    if (!purple_core_init(UI_ID)) {
00184       /* Initializing the core failed. Terminate. */
00185       LM_ERR("libpurple initialization failed.\n");
00186       abort();
00187    }
00188 
00189    purple_set_blist(purple_blist_new());
00190    purple_blist_load();
00191 
00192    purple_prefs_load();
00193 
00194    purple_plugins_load_saved(PLUGIN_PREF);
00195 
00196    purple_pounces_load();
00197 }
00198 
00199 void miniclient_start(int fd) {
00200    LM_DBG("starting miniclient... \n");
00201 
00202    GMainLoop *loop = g_main_loop_new(NULL, FALSE);
00203 
00204    /* libpurple's built-in DNS resolution forks termination ignore */
00205    signal(SIGCHLD, SIG_IGN);
00206 
00207    LM_DBG("initializing libpurple...\n");
00208    init_libpurple(fd);
00209    LM_DBG("libpurple initialized successfully...\n");
00210    
00211    if (httpProxy_host.len > 0) {
00212       proxy = purple_proxy_info_new();
00213       purple_proxy_info_set_type(proxy, PURPLE_PROXY_HTTP);
00214       purple_proxy_info_set_host(proxy, httpProxy_host.s);
00215       purple_proxy_info_set_port(proxy, httpProxy_port);
00216    }
00217 
00218    hashtable_init();
00219    client_connect_signals();
00220 
00221    g_main_loop_run(loop);
00222 }
00223 

Generated on Wed May 23 20:00:27 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6