simple2xmpp.c

Go to the documentation of this file.
00001 /*
00002  * $Id: simple2xmpp.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 <libxml/parser.h>
00036 
00037 #include "../../ut.h"
00038 #include "../../cmpapi.h"
00039 #include "../../dprint.h"
00040 #include "../../parser/msg_parser.h"
00041 #include "../../parser/parse_from.h"
00042 #include "../../parser/parse_expires.h"
00043 #include "../../parser/parse_content.h"
00044 #include "../../parser/parse_fline.h"
00045 #include "../../mem/mem.h"
00046 #include "pua_xmpp.h"
00047 #include "simple2xmpp.h"
00048 
00049 int winfo2xmpp(str* to_uri, str* body, str* id);
00050 int build_xmpp_content(str* to_uri, str* from_uri, str* body, str* id, int is_terminated);
00051 
00052 int Notify2Xmpp(struct sip_msg* msg, char* s1, char* s2)
00053 {
00054    struct to_body *pto, TO, *pfrom= NULL;
00055    str to_uri;
00056    char* uri= NULL;
00057    str from_uri;
00058    struct hdr_field* hdr= NULL;
00059    str body;
00060    xmlDocPtr doc= NULL;
00061    int is_terminated= 0;
00062    str id;
00063    ua_pres_t dialog;
00064    int event_flag= 0;
00065 
00066    memset(&dialog, 0, sizeof(ua_pres_t));
00067    
00068    LM_DBG("start...\n\n");
00069 
00070    if( parse_headers(msg,HDR_EOH_F, 0)==-1 )
00071    {
00072       LM_ERR("parsing headers\n");
00073       return -1;
00074    }
00075    if((!msg->event ) ||(msg->event->body.len<=0))
00076    {
00077       LM_ERR("Missing event header field value\n");
00078       return -1;
00079    }
00080 
00081    if( msg->to==NULL || msg->to->body.s==NULL)
00082    {
00083       LM_ERR("cannot parse TO header\n");
00084       return -1;
00085    }
00086 
00087    if(msg->to->parsed != NULL)
00088    {
00089       pto = (struct to_body*)msg->to->parsed;
00090       LM_ERR("'To' header ALREADY PARSED:<%.*s>\n",pto->uri.len,pto->uri.s);
00091    }
00092    else
00093    {
00094       memset( &TO , 0, sizeof(TO) );
00095       parse_to(msg->to->body.s,msg->to->body.s + msg->to->body.len + 1, &TO);
00096       if(TO.uri.len <= 0) 
00097       {
00098          LM_ERR("'To' header NOT parsed\n");
00099          return -1;
00100       }
00101       pto = &TO;
00102    }
00103 
00104    dialog.watcher_uri= &pto->uri;
00105    
00106    uri=(char*)pkg_malloc(sizeof(char)*( pto->uri.len+1));
00107    if(uri== NULL)
00108    {
00109       LM_ERR("no more memory\n");
00110       return -1;
00111    }
00112    memcpy(uri, pto->uri.s, pto->uri.len);
00113    uri[pto->uri.len]= '\0';
00114    to_uri.s= duri_sip_xmpp(uri);
00115    if(to_uri.s== NULL)
00116    {
00117       LM_ERR("while decoding sip uri in xmpp\n");
00118       return -1;  
00119    }  
00120    to_uri.len= strlen(to_uri.s);
00121    pkg_free(uri);
00122 
00123     if (pto->tag_value.s==NULL || pto->tag_value.len==0 )
00124    {  
00125       LM_ERR("to tag value not parsed\n");
00126       goto error;
00127    }
00128    id=  pto->tag_value;
00129    dialog.from_tag= id;
00130 
00131    if( msg->callid==NULL || msg->callid->body.s==NULL)
00132    {
00133       LM_ERR("cannot parse callid header\n");
00134       goto error;
00135    }
00136    dialog.call_id = msg->callid->body;
00137 
00138    if (!msg->from || !msg->from->body.s)
00139    {
00140       LM_ERR("ERROR cannot find 'from' header!\n");
00141       goto error;
00142    }
00143    if (msg->from->parsed == NULL)
00144    {
00145       LM_ERR("'From' header not parsed\n");
00146       /* parsing from header */
00147       if ( parse_from_header( msg )<0 ) 
00148       {
00149          LM_ERR("ERROR cannot parse From header\n");
00150          goto error;
00151       }
00152    }
00153    pfrom = (struct to_body*)msg->from->parsed;
00154    dialog.pres_uri= &pfrom->uri;
00155    
00156    uri=(char*)pkg_malloc(sizeof(char)*( pfrom->uri.len+1));
00157    if(uri== NULL)
00158    {
00159       LM_ERR("no more memory\n");
00160       goto error;
00161    }
00162    memcpy(uri, pfrom->uri.s, pfrom->uri.len);
00163    uri[pfrom->uri.len]= '\0';
00164    
00165    from_uri.s= euri_sip_xmpp(uri);
00166    if(from_uri.s== NULL)
00167    {
00168       LM_ERR("while encoding sip uri in xmpp\n");
00169       goto error;
00170    }  
00171    from_uri.len= strlen(from_uri.s);
00172    pkg_free(uri);
00173    
00174    if( pfrom->tag_value.s ==NULL || pfrom->tag_value.len == 0)
00175    {
00176       LM_ERR("no from tag value present\n");
00177       goto error;
00178    }
00179 
00180    dialog.to_tag= pfrom->tag_value;
00181    dialog.flag|= XMPP_SUBSCRIBE;
00182    if(msg->event->body.len== 8 && 
00183          (strncmp(msg->event->body.s,"presence",8 )==0))
00184       event_flag|= PRESENCE_EVENT;
00185    else
00186    if(msg->event->body.len== 14 && 
00187          (strncmp(msg->event->body.s,"presence.winfo",14 )==0))
00188       event_flag|= PWINFO_EVENT;
00189    else
00190    {
00191       LM_ERR("wrong event\n");
00192       goto error;
00193    }  
00194    dialog.event= event_flag;
00195    
00196    if(pua_is_dialog(&dialog)< 0) // verify if within a stored dialog
00197    {
00198       LM_ERR("Notify in a non existing dialog\n");
00199       goto error;
00200    }
00201    /*constructing the xml body*/
00202    if(get_content_length(msg) == 0 )
00203    {
00204       body.s= NULL;
00205       body.len= 0;
00206    }  
00207    else
00208    {
00209       body.s=get_body(msg);
00210       if (body.s== NULL) 
00211       {
00212          LM_ERR("cannot extract body from msg\n");
00213          goto error;
00214       }
00215       body.len = get_content_length( msg );
00216    }
00217 
00218    /* treat the two cases: event= presence & event=presence.winfo */ 
00219    if(event_flag & PRESENCE_EVENT)
00220    {  
00221       LM_DBG("PRESENCE\n");
00222       hdr = msg->headers;
00223       while (hdr!= NULL)
00224       {
00225          if(cmp_hdrname_strzn(&hdr->name, "Subscription-State", 18)==0)
00226             break;
00227          hdr = hdr->next;
00228       }
00229       if(hdr && strncmp(hdr->body.s,"terminated", 10)== 0)
00230       {
00231          /* chack if reason timeout => don't send notification */
00232          if(strncmp(hdr->body.s+11,"reason=timeout", 14)== 0)
00233          {
00234             LM_DBG("Received Notification with state"
00235                "terminated; reason= timeout=> don't send notification\n");
00236             return 1;
00237          }  
00238          is_terminated= 1;
00239 
00240       }
00241    
00242       if(build_xmpp_content(&to_uri, &from_uri, &body, &id, is_terminated)< 0)
00243       {
00244          LM_ERR("in function build_xmpp_content\n");  
00245          goto error;
00246       }
00247       xmlFreeDoc(doc);
00248    }  
00249    else
00250    {  
00251       if(event_flag & PWINFO_EVENT)
00252       {
00253          LM_DBG("PRESENCE.WINFO\n");
00254          hdr = msg->headers;
00255          while (hdr!= NULL)
00256          {
00257             if(cmp_hdrname_strzn(&hdr->name, "Subscription-State", 18)==0)
00258                break;
00259             hdr = hdr->next;
00260          }
00261          if(hdr && strncmp(hdr->body.s,"terminated", 10)== 0)
00262          {
00263             LM_DBG("Notify for presence.winfo with" 
00264                " Subscription-State terminated- should not translate\n");
00265             goto error;
00266          }
00267          if(winfo2xmpp(&to_uri, &body, &id)< 0)
00268          {
00269             LM_ERR("while sending subscription\n");
00270             goto error;
00271          }
00272 
00273       }
00274       else
00275       {
00276          LM_ERR("Missing or unsupported event header field value\n");
00277          goto error;
00278       }
00279 
00280    }
00281    return 1;
00282 
00283 error:
00284    if(doc)
00285       xmlFreeDoc(doc);
00286    return 0;
00287 }
00288 
00289 int build_xmpp_content(str* to_uri, str* from_uri, str* body, str* id,
00290       int is_terminated)
00291 {
00292    xmlDocPtr sip_doc= NULL;
00293    xmlDocPtr doc= NULL;
00294    xmlNodePtr xmpp_root= NULL;
00295    xmlNodePtr sip_root= NULL;
00296    xmlNodePtr new_node= NULL;
00297    xmlNodePtr node = NULL;
00298    xmlBufferPtr buffer= NULL;
00299    xmlAttrPtr attr= NULL;
00300    char* basic= NULL, *priority= NULL, *note= NULL;
00301    str xmpp_msg;
00302 
00303    LM_DBG("start...\n");
00304 
00305    /* creating the xml doc for the xmpp message*/
00306    doc= xmlNewDoc(0);
00307    if(doc== NULL)
00308    {
00309       LM_ERR("when creating new xml doc\n");
00310       goto error;
00311    }
00312    xmpp_root = xmlNewNode(NULL, BAD_CAST "presence");
00313    if(xmpp_root==0)
00314    {
00315       LM_ERR("when adding new node- presence\n");
00316       goto error;
00317    }
00318    xmlDocSetRootElement(doc, xmpp_root);
00319 
00320    attr= xmlNewProp(xmpp_root, BAD_CAST "to", BAD_CAST to_uri->s);
00321    if(attr== NULL)
00322    {
00323       LM_ERR("while adding new attribute\n");
00324       goto error;
00325    }
00326    attr= xmlNewProp(xmpp_root, BAD_CAST "from", BAD_CAST from_uri->s);
00327    if(attr== NULL)
00328    {
00329       LM_ERR("while adding new attribute\n");
00330       goto error;
00331    }
00332    if(is_terminated)
00333    {  
00334       attr=  xmlNewProp(xmpp_root, BAD_CAST "type", BAD_CAST "unsubscribed");
00335       if(attr== NULL)
00336       {
00337          LM_ERR("while adding new attribute\n");
00338          goto error;
00339       }
00340       goto done;
00341    }
00342    if(body->s== NULL)
00343    {
00344       attr=  xmlNewProp(xmpp_root, BAD_CAST "type", BAD_CAST "unavailable");
00345       if(attr== NULL)
00346       {
00347          LM_ERR("while adding new attribute\n");
00348          goto error;
00349       }
00350       goto done;
00351    }
00352 
00353    /*extractiong the information from the sip message body*/
00354    sip_doc= xmlParseMemory(body->s, body->len);
00355    if(sip_doc== NULL)
00356    {
00357       LM_ERR("while parsing xml memory\n");
00358       return -1;
00359    }
00360    sip_root= XMLDocGetNodeByName(sip_doc, "presence", NULL);
00361    if(sip_root== NULL)
00362    {
00363       LM_ERR("while extracting 'presence' node\n");
00364       goto error;
00365    }
00366    
00367    node = XMLNodeGetNodeByName(sip_root, "basic", NULL);
00368    if(node== NULL)
00369    {
00370       LM_ERR("while extracting status basic node\n");
00371       goto error;
00372    }
00373    basic= (char*)xmlNodeGetContent(node);
00374    if(basic== NULL)
00375    {
00376       LM_ERR("while extracting status basic node content\n");
00377       goto error;
00378    }
00379    if(xmlStrcasecmp( (unsigned char*)basic,(unsigned char*) "closed")==0 )
00380    {
00381       attr= xmlNewProp(xmpp_root, BAD_CAST "type", BAD_CAST "unavailable");
00382       if(attr== NULL)
00383       {
00384          LM_ERR("while adding node attr\n");
00385          xmlFree(basic);
00386          goto error;
00387       }
00388       xmlFree(basic);
00389       goto done;
00390    }/* else the status is open so no type attr should be added */
00391 
00392    xmlFree(basic);
00393    /* addind show node */
00394    node= XMLNodeGetNodeByName(sip_root, "note", NULL);
00395    if(node== NULL)
00396    {
00397       LM_DBG("No note node found\n");
00398       node= XMLNodeGetNodeByName(sip_root, "person", NULL);
00399       if(node== NULL)
00400       {
00401          LM_DBG("No person node found\n");
00402          goto done;
00403       }
00404       node= XMLNodeGetNodeByName(node, "note", NULL);
00405       if(node== NULL)
00406       {  
00407          LM_DBG("Person node has no note node\n");
00408          goto done;
00409       }  
00410    }  
00411    note= (char*)xmlNodeGetContent(node);
00412    if(note== NULL)
00413    {
00414       LM_ERR("while extracting note node content\n");
00415       goto error;
00416    }
00417 
00418    if(xmlStrcasecmp((unsigned char*)note, (unsigned char*)"away")== 0)
00419    {
00420       new_node = xmlNewChild(xmpp_root, NULL, BAD_CAST "show",
00421             BAD_CAST "away");
00422       if(new_node== NULL)
00423       {
00424          LM_ERR("while adding node show: away\n");
00425          goto error;
00426       }  
00427    }
00428    else
00429       if(xmlStrcasecmp((unsigned char*)note, (unsigned char*)"busy")== 0)
00430       {
00431          new_node = xmlNewChild(xmpp_root, NULL, BAD_CAST "show"
00432                , BAD_CAST "xa");
00433          if(new_node== NULL)
00434          {
00435             LM_ERR("while adding node show: away\n");
00436             goto error;
00437          }  
00438       }
00439 
00440       /*
00441       if(xmlStrcasecmp((unsigned char*)note, (unsigned char*)"on the phone")== 0)
00442       {
00443          new_node = xmlNewChild(xmpp_root, NULL, BAD_CAST "show", BAD_CAST "chat");
00444          if(new_node== NULL)
00445          {
00446             LM_ERR("while adding node show: chat\n");
00447             goto error;
00448          }  
00449       }
00450       else 
00451          if(xmlStrcasecmp((unsigned char*)note, (unsigned char*)"idle")== 0)
00452          {
00453             new_node = xmlNewChild(xmpp_root, NULL, BAD_CAST "show", BAD_CAST "idle");
00454             if(new_node== NULL)
00455             {
00456                LM_ERR("while adding node: idle\n");
00457                goto error;
00458             }  
00459          }
00460          else */  
00461             if((xmlStrcasecmp((unsigned char*)note,
00462                (unsigned char*)"dnd")== 0)||
00463                (xmlStrcasecmp((unsigned char*)note,
00464                   (unsigned char*)"do not disturb")== 0))
00465             {
00466                new_node = xmlNewChild(xmpp_root, NULL, BAD_CAST "show",
00467                      BAD_CAST "dnd");
00468                if(new_node== NULL)
00469                {
00470                   LM_ERR("while adding node show: dnd\n");
00471                   goto error;
00472                }     
00473             }
00474 
00475    
00476    /* adding status node */
00477    new_node = xmlNewChild(xmpp_root, NULL, BAD_CAST "status", BAD_CAST note);
00478    if(new_node== NULL)
00479    {
00480       LM_ERR("while adding node status\n");
00481       goto error;
00482    }  
00483    
00484    xmlFree(note);
00485    note= NULL;
00486 
00487    /* adding priotity node*/
00488    node= XMLNodeGetNodeByName(sip_root, "contact", NULL);
00489    if(node== NULL)
00490    {
00491       LM_DBG("No contact node found\n");
00492    }
00493    else
00494    {
00495       priority= XMLNodeGetAttrContentByName(node, "priority");
00496       if(priority== NULL)
00497          LM_DBG("No priority attribute found\n");
00498       else
00499       {
00500          new_node= xmlNewChild(xmpp_root, NULL, BAD_CAST "priority",
00501                BAD_CAST priority);
00502          if(sip_root== NULL)
00503          {
00504             LM_ERR("while adding node\n");
00505             xmlFree(priority);
00506             goto error;
00507          }
00508          xmlFree(priority);
00509       }
00510    }
00511 
00512 done:
00513    buffer= xmlBufferCreate();
00514    if(buffer== NULL)
00515    {
00516       LM_ERR("while adding creating new buffer\n");
00517       goto error;
00518    }
00519    
00520    xmpp_msg.len= xmlNodeDump(buffer, doc, xmpp_root, 1,1);
00521    if(xmpp_msg.len== -1)
00522    {
00523       LM_ERR("while dumping node\n");
00524       goto error;
00525    }
00526    xmpp_msg.s= (char*)xmlBufferContent( buffer);
00527    if(xmpp_msg.s==  NULL)
00528    {
00529       LM_ERR("while extracting buffer content\n");
00530       goto error;
00531    }
00532    
00533    LM_DBG("xmpp_msg: %.*s\n",xmpp_msg.len, xmpp_msg.s);
00534    if( xmpp_notify(from_uri, to_uri, &xmpp_msg, id)< 0)
00535    {
00536       LM_ERR("while sending xmpp_notify\n");
00537       goto error;
00538    }
00539 
00540    xmlBufferFree(buffer);
00541    xmlCleanupParser();
00542    xmlMemoryDump();
00543 
00544    if(sip_doc)
00545       xmlFreeDoc(sip_doc);
00546    if(doc)
00547       xmlFreeDoc(doc);
00548    return 0;
00549 
00550 error:
00551    if(sip_doc)
00552       xmlFreeDoc(sip_doc);
00553    if(note)
00554       xmlFree(note);
00555    if(buffer)
00556       xmlBufferFree(buffer);
00557    xmlCleanupParser();
00558    xmlMemoryDump();
00559 
00560    return -1;
00561 
00562 }
00563 
00564 
00565 int winfo2xmpp(str* to_uri, str* body, str* id)
00566 {
00567    xmlAttrPtr attr= NULL;
00568    str xmpp_msg;
00569    char* watcher= NULL ;
00570    str from_uri;
00571    xmlDocPtr notify_doc= NULL;
00572    xmlDocPtr doc= NULL;
00573    xmlNodePtr pidf_root= NULL;
00574    xmlNodePtr root_node= NULL;
00575    xmlNodePtr node= NULL;
00576    xmlBufferPtr buffer= NULL;
00577 
00578    LM_DBG("start...\n");
00579    notify_doc= xmlParseMemory(body->s, body->len);
00580    if(notify_doc== NULL)
00581    {
00582       LM_ERR("while parsing xml memory\n");
00583       return -1;
00584    }
00585    pidf_root= XMLDocGetNodeByName(notify_doc, "watcherinfo", NULL);
00586    if(pidf_root== NULL)
00587    {
00588       LM_ERR("while extracting 'presence' node\n");
00589       goto error;
00590    }
00591    
00592    node = XMLNodeGetNodeByName(pidf_root, "watcher", NULL);
00593 
00594    for (; node!=NULL; node = node->next)
00595    {     
00596       if( xmlStrcasecmp(node->name,(unsigned char*)"watcher"))
00597          continue;
00598 
00599       watcher= (char*)xmlNodeGetContent(node->children); 
00600       if(watcher== NULL)
00601       {
00602          LM_ERR("while extracting watcher node content\n");
00603          goto error;
00604       }
00605       from_uri.s= euri_sip_xmpp(watcher);
00606       if(from_uri.s== NULL)
00607       {
00608          LM_ERR("while encoding sip uri in xmpp\n");
00609          goto error;
00610       }  
00611       from_uri.len= strlen(from_uri.s);
00612       xmlFree(watcher);
00613       watcher= NULL;
00614 
00615       doc= xmlNewDoc( 0 );
00616       if(doc== NULL)
00617       {
00618          LM_ERR("when creating new xml doc\n");
00619          goto error;
00620       }
00621       root_node = xmlNewNode(NULL, BAD_CAST "presence");
00622       if(root_node== NULL)
00623       {
00624          LM_ERR("when adding new node\n");
00625          goto error;
00626       }
00627       xmlDocSetRootElement(doc, root_node);
00628 
00629       attr= xmlNewProp(root_node, BAD_CAST "to", BAD_CAST to_uri->s);
00630       if(attr== NULL)
00631       {
00632          LM_ERR("while adding attribute to_uri\n");
00633          goto error;
00634       }
00635       attr= xmlNewProp(root_node, BAD_CAST "from", BAD_CAST from_uri.s);
00636       if(attr== NULL)
00637       {
00638          LM_ERR("while adding attribute from_uri\n");
00639          goto error;
00640       }
00641       attr= xmlNewProp(root_node, BAD_CAST "type", BAD_CAST "subscribe");
00642       if(attr== NULL)
00643       {
00644          LM_ERR("while adding attribute type\n");
00645          goto error;
00646       }
00647       buffer= xmlBufferCreate();
00648       if(buffer== NULL)
00649       {
00650          LM_ERR("while adding creating new buffer\n");
00651          goto error;
00652       }
00653       
00654       xmpp_msg.len= xmlNodeDump(buffer, doc, root_node, 1,1);
00655       if(xmpp_msg.len== -1)
00656       {
00657          LM_ERR("while dumping node\n");
00658          goto error;
00659       }
00660       xmpp_msg.s= (char*)xmlBufferContent( buffer);
00661       if(xmpp_msg.s==  NULL)
00662       {
00663          LM_ERR("while extracting buffer content\n");
00664          goto error;
00665       }
00666    
00667       LM_DBG("xmpp_msg: %.*s\n",xmpp_msg.len, xmpp_msg.s);
00668       
00669       if( xmpp_subscribe(&from_uri, to_uri, &xmpp_msg, id)< 0)
00670       {
00671          LM_ERR("while sending xmpp_subscribe\n");
00672          goto error;
00673       }
00674       xmlBufferFree(buffer);
00675       buffer= NULL;
00676       xmlFreeDoc(doc);
00677       doc= NULL;
00678    }
00679 
00680    xmlFreeDoc(notify_doc);
00681    xmlCleanupParser();
00682    xmlMemoryDump();
00683    return 0;
00684 
00685 error:
00686 
00687    if(doc)
00688       xmlFreeDoc(doc);
00689    if(notify_doc)
00690       xmlFreeDoc(notify_doc);
00691    if(watcher)
00692       xmlFree(watcher);
00693    if(buffer)
00694       xmlBufferFree(buffer);
00695    xmlCleanupParser();
00696    xmlMemoryDump();
00697 
00698    return -1;
00699 
00700 }
00701 
00702 char* get_error_reason(int code, str* reason)
00703 {
00704    char* err_cond= NULL;
00705 
00706    err_cond= (char*)pkg_malloc(50* sizeof(char));
00707    if(err_cond== NULL)
00708    {
00709       LM_ERR("no more memory\n");
00710       return NULL;
00711    }
00712    
00713    switch( code )
00714    {
00715       case 300:   { strcpy(err_cond, "redirect");           break;}
00716       case 301:   { strcpy(err_cond, "gone");            break;}
00717       case 302:   { strcpy(err_cond, "redirect");           break;}
00718       case 305:   { strcpy(err_cond, "redirect");           break;}
00719       case 380:   { strcpy(err_cond, "not-acceptable");        break;}
00720       case 400:   { strcpy(err_cond, "bad-request");        break;}
00721       case 401:   { strcpy(err_cond, "not-authorized");        break;}
00722       case 402:   { strcpy(err_cond, "payment-required");         break;}
00723       case 403:   { strcpy(err_cond, "forbidden");       break;}
00724       case 404:   { strcpy(err_cond, "item-not-found");        break;}
00725       case 405:   { strcpy(err_cond, "not-allowed");        break;}
00726       case 406:   { strcpy(err_cond, "not-acceptable");        break;}
00727       case 407:   { strcpy(err_cond, "registration-required");    break;}
00728       case 408:   { strcpy(err_cond, "service-unavailable");      break;}
00729       case 410:   { strcpy(err_cond, "gone");            break;}
00730       case 413:   { strcpy(err_cond, "bad-request");        break;}
00731       case 414:   { strcpy(err_cond, "bad-request");        break;}
00732       case 415:   { strcpy(err_cond, "bad-request");        break;}
00733       case 416:   { strcpy(err_cond, "bad-request");        break;}
00734       case 420:   { strcpy(err_cond, "bad-request");        break;}
00735       case 421:   { strcpy(err_cond, "bad-request");        break;}
00736       case 423:   { strcpy(err_cond, "bad-request");        break;}
00737       case 480:   { strcpy(err_cond, "recipient-unavailable");    break;}
00738       case 481:   { strcpy(err_cond, "item-not-found");        break;}
00739       case 482:   { strcpy(err_cond, "not-acceptable");        break;}
00740       case 483:   { strcpy(err_cond, "not-acceptable");        break;}
00741       case 484:   { strcpy(err_cond, "jid-malformed");         break;}
00742       case 485:   { strcpy(err_cond, "item-not-found");        break;}
00743       case 488:   { strcpy(err_cond, "not-acceptable");        break;}
00744       case 491:   { strcpy(err_cond, "unexpected-request");    break;}
00745       case 500:   { strcpy(err_cond, "internal-server-error");    break;}
00746       case 501:   { strcpy(err_cond, "feature-not-implemented");     break;}
00747       case 502:   { strcpy(err_cond, "remote-server-not-found");     break;}
00748       case 503:   { strcpy(err_cond, "service-unavailable");      break;}
00749       case 504:   { strcpy(err_cond, "remote-server-timeout");    break;}
00750       case 505:   { strcpy(err_cond, "not-acceptable");        break;}
00751       case 513:   { strcpy(err_cond, "bad-request");        break;}
00752       case 600:   { strcpy(err_cond, "service-unavailable");      break;}
00753       case 603:   { strcpy(err_cond, "service-unavailable");      break;}
00754       case 604:   { strcpy(err_cond, "item-not-found");        break;}
00755       case 606:   { strcpy(err_cond, "not-acceptable");        break;}
00756       default: { strcpy(err_cond, "not-acceptable");        break;}
00757    }
00758 
00759    return err_cond;
00760 }  
00761 
00762 
00763 int Sipreply2Xmpp(ua_pres_t* hentity, struct sip_msg * msg) 
00764 {
00765    char* uri;
00766    /* named according to the direction of the message in xmpp*/
00767    str from_uri;
00768    str to_uri;
00769    xmlDocPtr doc= NULL;
00770    xmlNodePtr root_node= NULL, node = NULL;
00771    xmlAttrPtr attr= NULL;
00772    str xmpp_msg;
00773    int code;
00774    str reason;
00775    char* err_reason= NULL;
00776    xmlBufferPtr buffer= NULL;
00777    
00778    LM_DBG("start..\n");
00779    uri=(char*)pkg_malloc(sizeof(char)*( hentity->watcher_uri->len+1));
00780    if(uri== NULL)
00781    {
00782       LM_ERR("no more memory\n");
00783       goto error;
00784    }
00785    memcpy(uri, hentity->watcher_uri->s, hentity->watcher_uri->len);
00786    uri[hentity->watcher_uri->len]= '\0';
00787    to_uri.s= duri_sip_xmpp(uri);
00788    if(to_uri.s== NULL)
00789    {
00790       LM_ERR("whil decoding sip uri in xmpp\n");
00791       goto error; 
00792    }  
00793 
00794    to_uri.len= strlen(to_uri.s);
00795    pkg_free(uri);
00796    
00797    uri=(char*)pkg_malloc(sizeof(char)*( hentity->pres_uri->len+1));
00798    if(uri== NULL)
00799    {
00800       LM_ERR("no more memory\n");
00801       goto error;
00802    }
00803    memcpy(uri, hentity->pres_uri->s, hentity->pres_uri->len);
00804    uri[hentity->pres_uri->len]= '\0';
00805    from_uri.s= euri_sip_xmpp(uri);
00806    if(from_uri.s== NULL)
00807    {
00808       LM_ERR("while encoding sip uri in xmpp\n");
00809       goto error;
00810    }
00811 
00812    from_uri.len= strlen(from_uri.s);
00813    pkg_free(uri);
00814 
00815    doc= xmlNewDoc(BAD_CAST "1.0");
00816    if(doc==0)
00817       goto error;
00818    root_node = xmlNewNode(NULL, BAD_CAST "presence");
00819    
00820    if(root_node==0)
00821       goto error;
00822    xmlDocSetRootElement(doc, root_node);
00823 
00824    attr= xmlNewProp(root_node, BAD_CAST "to", BAD_CAST to_uri.s);
00825    if(attr== NULL)
00826    {
00827       LM_ERR("while adding attribute to\n");
00828       goto error;
00829    }
00830    attr= xmlNewProp(root_node, BAD_CAST "from", BAD_CAST from_uri.s);
00831    if(attr== NULL)
00832    {
00833       LM_ERR("while adding attribute from\n");
00834       goto error;
00835    }
00836 
00837    if(msg== FAKED_REPLY)
00838    {
00839       code = 408;
00840       reason.s= "Request Timeout";
00841       reason.len= strlen(reason.s)- 1;
00842    }
00843    else
00844    {
00845       code= msg->first_line.u.reply.statuscode;
00846       reason= msg->first_line.u.reply.reason;
00847    }
00848 
00849    LM_DBG(" to_uri= %s\n\t from_uri= %s\n",
00850          to_uri.s, from_uri.s);
00851 
00852    if(code>=300)
00853    {
00854       LM_DBG(" error code(>= 300)\n");
00855       err_reason= get_error_reason(code, &reason);
00856       if(err_reason== NULL)
00857       {
00858          LM_ERR("couldn't get response phrase\n");
00859          goto error;
00860       }
00861    
00862       attr= xmlNewProp(root_node, BAD_CAST "type", BAD_CAST "error");
00863       if(attr== NULL)
00864       {
00865          LM_ERR("while adding new attribute\n");
00866          goto error;
00867       }
00868       node= xmlNewChild(root_node, 0, BAD_CAST  "error", 0 );
00869       if(node== NULL)
00870       {
00871          LM_ERR("while adding new node\n");
00872          goto error;
00873       }  
00874       node= xmlNewChild(node, 0,  BAD_CAST err_reason, 0 );
00875       if(node== NULL)
00876       {
00877          LM_ERR("while adding new node\n");
00878          goto error;
00879       }  
00880 
00881       attr= xmlNewProp(node, BAD_CAST "xmlns", 
00882             BAD_CAST "urn:ietf:params:xml:ns:xmpp-stanzas");
00883       if(attr== NULL)
00884       {
00885          LM_ERR("while adding new attribute\n");
00886          goto error;
00887       }
00888 
00889    }
00890    else
00891       if(code>=200 )
00892       {
00893          LM_DBG(" 2xx code\n");
00894          attr= xmlNewProp(root_node, BAD_CAST "type", BAD_CAST "subscribed");
00895          if(attr== NULL)
00896          {
00897             LM_ERR("while adding new attribute\n");
00898             goto error;
00899          }
00900       }
00901 
00902       buffer= xmlBufferCreate();
00903       if(buffer== NULL)
00904       {
00905          LM_ERR("while adding creating new buffer\n");
00906          goto error;
00907       }
00908       
00909       xmpp_msg.len= xmlNodeDump(buffer, doc, root_node, 1,1);
00910       if(xmpp_msg.len== -1)
00911       {
00912          LM_ERR("while dumping node\n");
00913          goto error;
00914       }
00915       xmpp_msg.s= (char*)xmlBufferContent( buffer);
00916       if(xmpp_msg.s==  NULL)
00917       {
00918          LM_ERR("while extracting buffer content\n");
00919          goto error;
00920       }
00921    
00922 
00923    LM_DBG("xmpp_msg: %.*s\n",xmpp_msg.len, xmpp_msg.s);
00924    
00925    if(xmpp_packet(&from_uri, &to_uri, &xmpp_msg, &hentity->to_tag)< 0)
00926    {
00927       LM_ERR("while sending xmpp_reply_to_subscribe\n");
00928       goto error;
00929    }
00930    if(err_reason)
00931       pkg_free(err_reason);
00932    xmlFreeDoc(doc);
00933    
00934    return 0;
00935 
00936 error:
00937 
00938    if(doc)
00939       xmlFreeDoc(doc);
00940    if(err_reason)
00941       pkg_free(err_reason);
00942    return -1;
00943 
00944 }
00945 

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