pua_xmpp.c

Go to the documentation of this file.
00001 /*
00002  * $Id: pua_xmpp.c 1666 2007-03-02 13:40:09Z anca_vamanu $
00003  *
00004  * pua_xmpp module - presence SIP - XMPP Gateway
00005  *
00006  * Copyright (C) 2007 Voice Sistem S.R.L.
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * --------
00026  *  2007-03-29  initial version (anca)
00027  */
00028 
00029 /*! \file
00030  * \brief Kamailio presence gateway: SIP/SIMPLE -- XMPP (pua_xmpp)
00031  */
00032 
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <libxml/parser.h>
00037 #include <time.h>
00038 
00039 #include "../../sr_module.h"
00040 #include "../../dprint.h"
00041 #include "../../str.h"
00042 #include "../../pt.h"
00043 #include "../../mem/mem.h"
00044 #include "../../mem/shm_mem.h"
00045 #include "../../parser/parse_expires.h"
00046 #include "../../parser/msg_parser.h"
00047 #include "../tm/tm_load.h"
00048 #include "../xmpp/xmpp_api.h"
00049 #include "../pua/pua_bind.h"
00050 
00051 #include "pua_xmpp.h"
00052 #include "xmpp2simple.h"
00053 #include "simple2xmpp.h"
00054 #include "request_winfo.h"
00055 
00056 MODULE_VERSION
00057 
00058 struct tm_binds tmb;
00059 
00060 /* functions imported from pua module*/
00061 pua_api_t pua;
00062 send_publish_t pua_send_publish;
00063 send_subscribe_t pua_send_subscribe;
00064 query_dialog_t pua_is_dialog;
00065 
00066 /* functions imported from xmpp module*/
00067 xmpp_api_t xmpp_api;
00068 xmpp_send_xsubscribe_f xmpp_subscribe;
00069 xmpp_send_xnotify_f xmpp_notify;
00070 xmpp_send_xpacket_f xmpp_packet;
00071 xmpp_translate_uri_f duri_sip_xmpp;
00072 xmpp_translate_uri_f euri_sip_xmpp;
00073 xmpp_translate_uri_f duri_xmpp_sip;
00074 xmpp_translate_uri_f euri_xmpp_sip;
00075 
00076 /* libxml wrapper functions */
00077 xmlNodeGetAttrContentByName_t XMLNodeGetAttrContentByName;
00078 xmlDocGetNodeByName_t XMLDocGetNodeByName;
00079 xmlNodeGetNodeByName_t XMLNodeGetNodeByName;
00080 xmlNodeGetNodeContentByName_t XMLNodeGetNodeContentByName;
00081 
00082 str server_address= {0, 0};
00083 
00084 /** module functions */
00085 
00086 static int mod_init(void);
00087 static int child_init(int);
00088 
00089 static int fixup_pua_xmpp(void** param, int param_no);
00090 
00091 static cmd_export_t cmds[]=
00092 {
00093    {"pua_xmpp_notify", (cmd_function)Notify2Xmpp,  0, 0, 0, REQUEST_ROUTE},
00094    {"pua_xmpp_req_winfo", (cmd_function)request_winfo, 2, fixup_pua_xmpp, 0, REQUEST_ROUTE},
00095    {0, 0, 0, 0, 0, 0}
00096 };
00097 
00098 static param_export_t params[]={
00099    {"server_address",     STR_PARAM,   &server_address   },
00100    {0,         0,    0  }
00101 };
00102 
00103 /*! \brief module exports */
00104 struct module_exports exports= {
00105    "pua_xmpp",       /* module name */
00106    DEFAULT_DLFLAGS,  /* dlopen flags */
00107    cmds,          /* exported functions */
00108    params,           /* exported  parameters */
00109    0,             /* exported statistics */
00110    0,             /* exported MI functions*/
00111    0,             /* exported pseudo-variables */
00112    0,             /* extra processes */
00113    mod_init,         /* module initialization function */
00114    0,             /* response handling function */
00115    0,             /* destroy function */
00116    child_init        /* per-child init function */
00117 };
00118 
00119 /*! \brief
00120  * init module function
00121  */
00122 static int mod_init(void)
00123 {
00124    load_tm_f  load_tm;
00125    bind_pua_t bind_pua;
00126    bind_xmpp_t bind_xmpp;
00127    bind_libxml_t bind_libxml;
00128    libxml_api_t libxml_api;
00129 
00130    /* check if compulsory parameter server_address is set */
00131    if(server_address.s== NULL)
00132    {
00133       LM_ERR("compulsory 'server_address' parameter not set!");
00134       return -1;
00135    }
00136    server_address.len= strlen(server_address.s);
00137 
00138    /* import the TM auto-loading function */
00139    if((load_tm=(load_tm_f)find_export("load_tm", 0, 0))==NULL)
00140    {
00141       LM_ERR("can't import load_tm\n");
00142       return -1;
00143    }
00144    /* let the auto-loading function load all TM stuff */
00145 
00146    if(load_tm(&tmb)==-1)
00147    {
00148       LM_ERR("can't load tm functions\n");
00149       return -1;
00150    }
00151 
00152    /* bind libxml wrapper functions */
00153    if((bind_libxml= (bind_libxml_t)find_export("bind_libxml_api", 1, 0))== NULL)
00154    {
00155       LM_ERR("can't import bind_libxml_api\n");
00156       return -1;
00157    }
00158    if(bind_libxml(&libxml_api)< 0)
00159    {
00160       LM_ERR("can not bind libxml api\n");
00161       return -1;
00162    }
00163    XMLNodeGetAttrContentByName= libxml_api.xmlNodeGetAttrContentByName;
00164    XMLDocGetNodeByName= libxml_api.xmlDocGetNodeByName;
00165    XMLNodeGetNodeByName= libxml_api.xmlNodeGetNodeByName;
00166    XMLNodeGetNodeContentByName= libxml_api.xmlNodeGetNodeContentByName;
00167 
00168    if(XMLNodeGetAttrContentByName== NULL || XMLDocGetNodeByName== NULL ||
00169       XMLNodeGetNodeByName== NULL || XMLNodeGetNodeContentByName== NULL)
00170    {
00171       LM_ERR("libxml wrapper functions could not be bound\n");
00172       return -1;
00173    }
00174 
00175 
00176    /* bind xmpp */
00177    bind_xmpp= (bind_xmpp_t)find_export("bind_xmpp", 0,0);
00178    if (!bind_xmpp)
00179    {
00180       LM_ERR("Can't bind to the XMPP module.\n");
00181       return -1;
00182    }
00183    if(bind_xmpp(&xmpp_api)< 0)
00184    {
00185       LM_ERR("Can't bind to the XMPP module.\n");
00186       return -1;
00187    }
00188    if(xmpp_api.xsubscribe== NULL)
00189    {
00190       LM_ERR("Could not import xsubscribe from the XMPP module. Version mismatch?\n");
00191       return -1;
00192    }
00193    xmpp_subscribe= xmpp_api.xsubscribe;
00194 
00195    if(xmpp_api.xnotify== NULL)
00196    {
00197       LM_ERR("Could not import xnotify from the XMPP module. Version mismatch?\n");
00198       return -1;
00199    }
00200    xmpp_notify= xmpp_api.xnotify;
00201    
00202    if(xmpp_api.xpacket== NULL)
00203    {
00204       LM_ERR("Could not import xnotify from the XMPP module. Version mismatch?\n");
00205       return -1;
00206    }
00207    xmpp_packet= xmpp_api.xpacket;
00208 
00209    if(xmpp_api.register_callback== NULL)
00210    {
00211       LM_ERR("Could not import register_callback"
00212             " to xmpp\n");
00213       return -1;
00214    }
00215    if(xmpp_api.register_callback(XMPP_RCV_PRESENCE, pres_Xmpp2Sip, NULL)< 0)
00216    {
00217       LM_ERR("ERROR while registering callback"
00218             " to xmpp\n");
00219       return -1;
00220    }
00221    if(xmpp_api.decode_uri_sip_xmpp== NULL)
00222    {
00223       LM_ERR("Could not import decode_uri_sip_xmpp"
00224             " from xmpp\n");
00225       return -1;
00226    }
00227    duri_sip_xmpp= xmpp_api.decode_uri_sip_xmpp;
00228 
00229    if(xmpp_api.encode_uri_sip_xmpp== NULL)
00230    {
00231       LM_ERR("Could not import encode_uri_sip_xmpp"
00232             " from xmpp\n");
00233       return -1;
00234    }
00235    euri_sip_xmpp= xmpp_api.encode_uri_sip_xmpp;
00236 
00237    if(xmpp_api.decode_uri_xmpp_sip== NULL)
00238    {
00239       LM_ERR("Could not import decode_uri_xmpp_sip"
00240             " from xmpp\n");
00241       return -1;
00242    }
00243    duri_xmpp_sip= xmpp_api.decode_uri_xmpp_sip;
00244 
00245    if(xmpp_api.encode_uri_xmpp_sip== NULL)
00246    {
00247       LM_ERR("Could not import encode_uri_xmpp_sip"
00248             " from xmpp\n");
00249       return -1;
00250    }
00251    euri_xmpp_sip= xmpp_api.encode_uri_xmpp_sip;
00252 
00253    /* bind pua */
00254    bind_pua= (bind_pua_t)find_export("bind_pua", 1,0);
00255    if (!bind_pua)
00256    {
00257       LM_ERR("Can't bind to PUA module\n");
00258       return -1;
00259    }
00260    
00261    if (bind_pua(&pua) < 0)
00262    {
00263       LM_ERR("Can't bind to PUA module\n");
00264       return -1;
00265    }
00266    if(pua.send_publish == NULL)
00267    {
00268       LM_ERR("Could not import send_publish() in module PUA. Version mismatch?\n");
00269       return -1;
00270    }
00271    pua_send_publish= pua.send_publish;
00272 
00273    if(pua.send_subscribe == NULL)
00274    {
00275       LM_ERR("Could not import send_publish() in module PUA. Version mismatch?\n");
00276       return -1;
00277    }
00278    pua_send_subscribe= pua.send_subscribe;
00279    
00280    if(pua.is_dialog == NULL)
00281    {
00282       LM_ERR("Could not import send_subscribe() in module PUA. Version mismatch?\n");
00283       return -1;
00284    }
00285    pua_is_dialog= pua.is_dialog;
00286 
00287    if(pua.register_puacb(XMPP_INITIAL_SUBS, Sipreply2Xmpp, NULL)< 0)
00288    {
00289       LM_ERR("Could not register PUA callback\n");
00290       return -1;
00291    }  
00292 
00293    return 0;
00294 }
00295 
00296 static int child_init(int rank)
00297 {
00298    LM_DBG("child [%d]  pid [%d]\n", rank, getpid());
00299    return 0;
00300 }
00301 
00302 static int fixup_pua_xmpp(void** param, int param_no)
00303 {
00304    pv_elem_t *model;
00305    str s;
00306    if(*param)
00307    {
00308       s.s = (char*)(*param); s.len = strlen(s.s);
00309       if(pv_parse_format(&s, &model)<0)
00310       {
00311          LM_ERR("wrong format[%s]\n",(char*)(*param));
00312          return E_UNSPEC;
00313       }
00314          
00315       *param = (void*)model;
00316       return 0;
00317    }
00318    LM_ERR("null format\n");
00319    return E_UNSPEC;
00320 }
00321 

Generated on Thu May 24 08:00:53 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6