pua/add_events.c

Go to the documentation of this file.
00001 /*
00002  * $Id: add_events.c  2007-05-03 15:05:20Z anca_vamanu $
00003  *
00004  * pua module - presence user agent module
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  * initial version 2007-05-03 (anca)
00025  */
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <libxml/parser.h>
00030 
00031 #include "event_list.h"
00032 #include "add_events.h"
00033 #include "pua.h"
00034 #include "pidf.h"
00035 
00036 extern int dlginfo_increase_version;
00037 
00038 int pua_add_events(void)
00039 {
00040    /* add presence */
00041    if(add_pua_event(PRESENCE_EVENT, "presence", "application/pidf+xml", 
00042             pres_process_body)< 0)
00043    {
00044       LM_ERR("while adding event presence\n");
00045       return -1;
00046    }
00047 
00048    /* add dialog */
00049    if (dlginfo_increase_version) {
00050       if(add_pua_event(DIALOG_EVENT, "dialog", "application/dialog-info+xml",
00051                bla_process_body)< 0)
00052       {
00053          LM_ERR("while adding event dialog w/ dlginfo_increase_version\n");
00054          return -1;
00055       }
00056    } else {
00057       if(add_pua_event(DIALOG_EVENT, "dialog", "application/dialog-info+xml",
00058                dlg_process_body)< 0)
00059       {
00060          LM_ERR("while adding event dialog w/o dlginfo_increase_version\n");
00061          return -1;
00062       }
00063    }
00064 
00065    /* add dialog;sla */
00066    if(add_pua_event(BLA_EVENT, "dialog;sla", "application/dialog-info+xml",
00067             bla_process_body)< 0)
00068    {
00069       LM_ERR("while adding event dialog;sla\n");
00070       return -1;
00071    }
00072 
00073    /* add message-summary*/
00074    if(add_pua_event(MSGSUM_EVENT, "message-summary", 
00075             "application/simple-message-summary", mwi_process_body)< 0)
00076    {
00077       LM_ERR("while adding event message-summary\n");
00078       return -1;
00079    }
00080    
00081    /* add presence;winfo */
00082    if(add_pua_event(PWINFO_EVENT, "presence.winfo", NULL, NULL)< 0)
00083    {
00084       LM_ERR("while adding event presence.winfo\n");
00085       return -1;
00086    }
00087    
00088    return 0;
00089 
00090 }  
00091 
00092 int pres_process_body(publ_info_t* publ, str** fin_body, int ver, str** tuple_param)
00093 {
00094 
00095    xmlDocPtr doc= NULL;
00096    xmlNodePtr node= NULL;
00097    char* tuple_id= NULL, *person_id= NULL;
00098    int tuple_id_len= 0;
00099    char buf[50];
00100    str* body= NULL;
00101    int alloc_tuple= 0;
00102    str* tuple= NULL;
00103 
00104    doc= xmlParseMemory(publ->body->s, publ->body->len );
00105    if(doc== NULL)
00106    {
00107       LM_ERR("while parsing xml memory\n");
00108       goto error;
00109    }
00110 
00111    node= xmlDocGetNodeByName(doc, "tuple", NULL);
00112    if(node == NULL)
00113    {
00114       LM_ERR("while extracting tuple node\n");
00115       goto error;
00116    }
00117    tuple_id= xmlNodeGetAttrContentByName(node, "id");
00118    if(tuple_id== NULL)
00119    {
00120       tuple= *(tuple_param);
00121 
00122       if(tuple== NULL)  // generate a tuple_id
00123       {
00124          tuple_id= buf;
00125          tuple_id_len= sprintf(tuple_id, "%p", publ);
00126          tuple_id[tuple_id_len]= '\0'; 
00127 
00128          tuple=(str*)pkg_malloc(sizeof(str));
00129          if(tuple== NULL)
00130          {
00131             LM_ERR("No more memory\n");
00132             goto error;
00133          }
00134          tuple->s= (char*)pkg_malloc(tuple_id_len* sizeof(char));
00135          if(tuple->s== NULL)
00136          {
00137             LM_ERR("NO more memory\n");
00138             goto error;
00139          }
00140          memcpy(tuple->s, tuple_id, tuple_id_len);
00141          tuple->len= tuple_id_len;
00142 
00143          *tuple_param= tuple;
00144          alloc_tuple= 1;
00145 
00146          LM_DBG("allocated tuple_id\n\n");
00147 
00148       }
00149       else
00150       {
00151          tuple_id= buf;
00152          tuple_id_len= tuple->len;
00153          memcpy(tuple_id, tuple->s, tuple_id_len);
00154          tuple_id[tuple_id_len]= '\0';
00155       }  
00156       /* add tuple id */
00157       if(!xmlNewProp(node, BAD_CAST "id", BAD_CAST tuple_id))
00158       {
00159          LM_ERR("while extracting xml"
00160                   " node\n");
00161          goto error;
00162       }
00163    }
00164    else
00165    {
00166       if(tuple== NULL)
00167       {
00168          strcpy(buf, tuple_id);
00169          xmlFree(tuple_id);
00170          tuple_id= buf;
00171          tuple_id_len= strlen(tuple_id);
00172       
00173          tuple=(str*)pkg_malloc(sizeof(str));
00174          if(tuple== NULL)
00175          {
00176             LM_ERR("No more memory\n");
00177             goto error;
00178          }
00179          tuple->s= (char*)pkg_malloc(tuple_id_len* sizeof(char));
00180          if(tuple->s== NULL)
00181          {
00182             LM_ERR("NO more memory\n");
00183             goto error;
00184          }
00185          memcpy(tuple->s, tuple_id, tuple_id_len);
00186          tuple->len= tuple_id_len;
00187          alloc_tuple= 1;
00188 
00189       }  
00190    }  
00191 
00192    node= xmlDocGetNodeByName(doc, "person", NULL);
00193    if(node)
00194    {
00195       LM_DBG("found person node\n");
00196       person_id= xmlNodeGetAttrContentByName(node, "id");
00197       if(person_id== NULL)
00198       {  
00199          if(!xmlNewProp(node, BAD_CAST "id", BAD_CAST tuple_id))
00200          {
00201             LM_ERR("while extracting xml"
00202                   " node\n");
00203             goto error;
00204          }
00205       }
00206       else
00207       {
00208          xmlFree(person_id);
00209       }
00210    }  
00211    body= (str*)pkg_malloc(sizeof(str));
00212    if(body== NULL)
00213    {
00214       LM_ERR("NO more memory left\n");
00215       goto error;
00216    }
00217    memset(body, 0, sizeof(str));
00218    xmlDocDumpFormatMemory(doc,(xmlChar**)(void*)&body->s, &body->len, 1);  
00219    if(body->s== NULL || body->len== 0)
00220    {
00221       LM_ERR("while dumping xml format\n");
00222       goto error;
00223    }  
00224    xmlFreeDoc(doc);
00225    doc= NULL;
00226    
00227    *fin_body= body;
00228    xmlMemoryDump();
00229    xmlCleanupParser();
00230    return 1;
00231 
00232 error:
00233    if(doc)
00234       xmlFreeDoc(doc);
00235    if(body)
00236       pkg_free(body);
00237    if(tuple && alloc_tuple)
00238    {
00239       if(tuple->s)
00240          pkg_free(tuple->s);
00241       pkg_free(tuple);
00242       tuple= NULL;
00243    }
00244    return -1;
00245 
00246 }  
00247 
00248 int bla_process_body(publ_info_t* publ, str** fin_body, int ver, str** tuple)
00249 {
00250    xmlNodePtr node= NULL;
00251    xmlDocPtr doc= NULL;
00252    char* version;
00253    str* body= NULL;
00254    int len;
00255    str* init_body;
00256 
00257    init_body= publ->body;
00258 
00259    doc= xmlParseMemory(init_body->s, init_body->len );
00260    if(doc== NULL)
00261    {
00262       LM_ERR("while parsing xml memory\n");
00263       goto error;
00264    }
00265    /* change version and state*/
00266    node= xmlDocGetNodeByName(doc, "dialog-info", NULL);
00267    if(node == NULL)
00268    {
00269       LM_ERR("while extracting dialog-info node\n");
00270       goto error;
00271    }
00272    version= int2str(ver,&len);
00273    version[len]= '\0';
00274 
00275    if( xmlSetProp(node, (const xmlChar *)"version",(const xmlChar*)version)== NULL)
00276    {
00277       LM_ERR("while setting version attribute\n");
00278       goto error; 
00279    }
00280    body= (str*)pkg_malloc(sizeof(str));
00281    if(body== NULL)
00282    {
00283       LM_ERR("NO more memory left\n");
00284       goto error;
00285    }
00286    memset(body, 0, sizeof(str));
00287    xmlDocDumpFormatMemory(doc, (xmlChar**)(void*)&body->s, &body->len, 1); 
00288 
00289    xmlFreeDoc(doc);
00290    doc= NULL;
00291    *fin_body= body;  
00292    if(*fin_body== NULL)
00293       LM_DBG("NULL fin_body\n");
00294 
00295    xmlMemoryDump();
00296    xmlCleanupParser();
00297    LM_DBG("successful\n");
00298    return 1;
00299 
00300 error:
00301    if(doc)
00302       xmlFreeDoc(doc);
00303    if(body)
00304       pkg_free(body);
00305    
00306    xmlMemoryDump();
00307    xmlCleanupParser();
00308    return -1;
00309 }
00310 
00311 int mwi_process_body(publ_info_t* publ, str** fin_body, int ver, str** tuple)
00312 {
00313    *fin_body= publ->body;
00314    return 0;
00315 }
00316 
00317 int dlg_process_body(publ_info_t* publ, str** fin_body, int ver, str** tuple)
00318 {
00319    *fin_body= publ->body;
00320    return 0;
00321 }
00322 

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