pua_xmpp: init printbuf_len and detect when no uri is provided
[sip-router] / src / modules / pua_xmpp / request_winfo.c
1 /*
2  * pua_xmpp module - presence SIP - XMPP Gateway
3  *
4  * Copyright (C) 2007 Voice Sistem S.R.L.
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * Kamailio is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License 
19  * along with this program; if not, write to the Free Software 
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 /*! \file
25  * \brief Kamailio pua_xmpp :: Winfo support
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <libxml/parser.h>
31
32 #include "pua_xmpp.h"
33 #include "request_winfo.h"
34
35 #define PRINTBUF_SIZE 256
36
37 int request_winfo(struct sip_msg* msg, char* uri, char* expires)
38 {
39         subs_info_t subs;
40         struct sip_uri puri;
41         int printbuf_len = 0;
42         char buffer[PRINTBUF_SIZE];
43         str uri_str;
44
45         memset(&puri, 0, sizeof(struct sip_uri));
46         if(uri)
47         {
48                 printbuf_len = PRINTBUF_SIZE-1;
49                 if(pv_printf(msg, (pv_elem_t*)uri, buffer, &printbuf_len)<0)
50                 {
51                         LM_ERR("cannot print the format\n");
52                         return -1;
53                 }
54                 if(parse_uri(buffer, printbuf_len, &puri)!=0)
55                 {
56                         LM_ERR("bad owner SIP address!\n");
57                         goto error;
58                 } else {
59                         LM_DBG("using user id [%.*s]\n", printbuf_len,
60                                         buffer);
61                 }
62         } 
63         if(printbuf_len == 0 || puri.user.len<=0 || puri.user.s==NULL
64                         || puri.host.len<=0 || puri.host.s==NULL)
65         {
66                 LM_ERR("bad owner URI!\n");
67                 goto error;
68         }
69         uri_str.s= buffer;
70         uri_str.len=  printbuf_len;
71         LM_DBG("uri= %.*s:\n", uri_str.len, uri_str.s);
72
73         memset(&subs, 0, sizeof(subs_info_t));
74         
75         subs.pres_uri= &uri_str;
76
77         subs.watcher_uri= &uri_str;
78
79         subs.contact= &server_address;
80         
81         if(strncmp(expires, "0", 1 )== 0)
82         {
83                 subs.expires= 0;
84         }
85         else
86         {
87                 subs.expires= -1;
88         
89         }
90         /* -1 - for a subscription with no time limit */
91         /*  0  -for unsubscribe */
92
93         subs.source_flag |= XMPP_SUBSCRIBE;
94         subs.event= PWINFO_EVENT;
95
96         if(pua_send_subscribe(&subs)< 0)
97         {
98                 LM_ERR("while sending subscribe\n");
99                 goto error;
100         }
101
102         return 1;
103
104 error:
105         return 0;
106 }
107