65964350aed3c90edc9ecbcac7f295f99ae0756c
[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;
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                 {
60                         LM_DBG("using user id [%.*s]\n", printbuf_len,
61                                         buffer);
62                 }
63         } 
64         if(puri.user.len<=0 || puri.user.s==NULL
65                         || puri.host.len<=0 || puri.host.s==NULL)
66         {
67                 LM_ERR("bad owner URI!\n");
68                 goto error;
69         }
70         uri_str.s= buffer;
71         uri_str.len=  printbuf_len;
72         LM_DBG("uri= %.*s:\n", uri_str.len, uri_str.s);
73
74         memset(&subs, 0, sizeof(subs_info_t));
75         
76         subs.pres_uri= &uri_str;
77
78         subs.watcher_uri= &uri_str;
79
80         subs.contact= &server_address;
81         
82         if(strncmp(expires, "0", 1 )== 0)
83         {
84                 subs.expires= 0;
85         }
86         else
87         {
88                 subs.expires= -1;
89         
90         }
91         /* -1 - for a subscription with no time limit */
92         /*  0  -for unsubscribe */
93
94         subs.source_flag |= XMPP_SUBSCRIBE;
95         subs.event= PWINFO_EVENT;
96
97         if(pua_send_subscribe(&subs)< 0)
98         {
99                 LM_ERR("while sending subscribe\n");
100                 goto error;
101         }
102
103         return 1;
104
105 error:
106         return 0;
107 }
108