01e91096ad4d8714bd4e0ed8365b5817486b2c3d
[sip-router] / src / modules / presence_xml / add_events.c
1 /*
2  * Copyright (C) 2006 Voice Sistem S.R.L.
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * Kamailio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 /*!
23  * \file
24  * \brief Kamailio Presence_XML :: Several event packages, presence, presence.winfo, dialog;sla
25  * \ingroup presence_xml
26  */
27
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <libxml/parser.h>
33 #include "../../core/parser/parse_content.h"
34 #include "../../core/data_lump_rpl.h"
35 #include "../../core/ut.h"
36 #include "xcap_auth.h"
37 #include "notify_body.h"
38 #include "add_events.h"
39 #include "presence_xml.h"
40
41 extern int disable_presence;
42 extern int disable_winfo;
43 extern int disable_bla;
44 extern int disable_xcapdiff;
45
46 static str pu_415_rpl = str_init("Unsupported media type");
47
48 int xml_add_events(void)
49 {
50         pres_ev_t event;
51
52         if(!disable_presence) {
53                 /* constructing presence event */
54                 memset(&event, 0, sizeof(pres_ev_t));
55                 event.name.s = "presence";
56                 event.name.len = 8;
57
58                 event.content_type.s = "application/pidf+xml";
59                 event.content_type.len = 20;
60
61                 event.type = PUBL_TYPE;
62                 event.req_auth = 1;
63                 event.apply_auth_nbody = pres_apply_auth;
64                 event.get_auth_status = pres_watcher_allowed;
65                 event.agg_nbody = pres_agg_nbody;
66                 event.evs_publ_handl = xml_publ_handl;
67                 event.free_body = free_xml_body;
68                 event.default_expires = 3600;
69                 event.get_rules_doc = pres_get_rules_doc;
70                 event.get_pidf_doc = pres_get_pidf_doc;
71                 if(psapi.add_event(&event) < 0) {
72                         LM_ERR("while adding event presence\n");
73                         return -1;
74                 }
75                 LM_DBG("added 'presence' event to presence module\n");
76         }
77
78         if(!disable_winfo) {
79                 /* constructing presence.winfo event */
80                 memset(&event, 0, sizeof(pres_ev_t));
81                 event.name.s = "presence.winfo";
82                 event.name.len = 14;
83
84                 event.content_type.s = "application/watcherinfo+xml";
85                 event.content_type.len = 27;
86                 event.type = WINFO_TYPE;
87                 event.free_body = free_xml_body;
88                 event.default_expires = 3600;
89
90                 if(psapi.add_event(&event) < 0) {
91                         LM_ERR("while adding event presence.winfo\n");
92                         return -1;
93                 }
94                 LM_DBG("added 'presence.winfo' event to presence module\n");
95         }
96
97         if(!disable_bla) {
98                 /* constructing bla event */
99                 memset(&event, 0, sizeof(pres_ev_t));
100                 event.name.s = "dialog;sla";
101                 event.name.len = 10;
102
103                 event.etag_not_new = 1;
104                 event.evs_publ_handl = xml_publ_handl;
105                 event.content_type.s = "application/dialog-info+xml";
106                 event.content_type.len = 27;
107                 event.type = PUBL_TYPE;
108                 event.free_body = free_xml_body;
109                 event.default_expires = 3600;
110                 if(psapi.add_event(&event) < 0) {
111                         LM_ERR("while adding event dialog;sla\n");
112                         return -1;
113                 }
114                 LM_DBG("added 'dialog;sla' event to presence module\n");
115         }
116
117         if(!disable_xcapdiff) {
118                 /* constructing xcap-diff event */
119                 memset(&event, 0, sizeof(pres_ev_t));
120                 event.name.s = "xcap-diff";
121                 event.name.len = 9;
122
123                 event.content_type.s = "application/xcap-diff+xml";
124                 event.content_type.len = 25;
125
126                 event.type = PUBL_TYPE;
127                 event.default_expires = 3600;
128                 if(psapi.add_event(&event) < 0) {
129                         LM_ERR("while adding event xcap-diff\n");
130                         return -1;
131                 }
132                 LM_DBG("added 'xcap-diff' event to presence module\n");
133         }
134
135         return 0;
136 }
137 /*
138  * in event specific publish handling - only check is good body format
139  */
140 int xml_publ_handl(struct sip_msg *msg)
141 {
142         str body = {0, 0};
143         xmlDocPtr doc = NULL;
144
145         if(get_content_length(msg) == 0)
146                 return 1;
147
148         body.s = get_body(msg);
149         if(body.s == NULL) {
150                 LM_ERR("cannot extract body from msg\n");
151                 goto error;
152         }
153         /* content-length (if present) must be already parsed */
154
155         body.len = get_content_length(msg);
156         doc = xmlParseMemory(body.s, body.len);
157         if(doc == NULL) {
158                 LM_ERR("bad body format\n");
159                 if(slb.freply(msg, 415, &pu_415_rpl) < 0) {
160                         LM_ERR("while sending '415 Unsupported media type' reply\n");
161                 }
162                 goto error;
163         }
164         xmlFreeDoc(doc);
165         xmlCleanupParser();
166         xmlMemoryDump();
167         return 1;
168
169 error:
170         xmlFreeDoc(doc);
171         xmlCleanupParser();
172         xmlMemoryDump();
173         return -1;
174 }