d1b2c6b56fbaccac8cb5fab811b6b5ccba154a2d
[asterisk/asterisk.git] / res / res_pjsip_pidf_digium_body_supplement.c
1 /*
2  * asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Kevin Harwell <kharwell@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <depend>res_pjsip_pubsub</depend>
23         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29 #include <pjsip_simple.h>
30 #include <pjlib.h>
31
32 #include "asterisk/module.h"
33 #include "asterisk/presencestate.h"
34 #include "asterisk/res_pjsip.h"
35 #include "asterisk/res_pjsip_pubsub.h"
36 #include "asterisk/res_pjsip_presence_xml.h"
37 #include "asterisk/res_pjsip_body_generator_types.h"
38
39 static int pidf_supplement_body(void *body, void *data)
40 {
41         struct ast_sip_exten_state_data *state_data = data;
42         pj_xml_node *node;
43
44         if (ast_strlen_zero(state_data->user_agent) ||
45             !strstr(state_data->user_agent, "digium")) {
46                 /* not a digium phone */
47                 return 0;
48         }
49
50         if (!(node = ast_sip_presence_xml_create_node(
51                       state_data->pool, body, "tuple"))) {
52                 ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
53                 return -1;
54         }
55
56         ast_sip_presence_xml_create_attr(
57                 state_data->pool, node, "id", "digium-presence");
58
59         if (!(node = ast_sip_presence_xml_create_node(
60                       state_data->pool, node, "status"))) {
61                 ast_log(LOG_WARNING, "Unable to create PIDF tuple status\n");
62                 return -1;
63         }
64
65         if (!(node = ast_sip_presence_xml_create_node(
66                       state_data->pool, node, "digium_presence"))) {
67                 ast_log(LOG_WARNING, "Unable to create digium presence\n");
68                 return -1;
69         }
70
71         if (!ast_strlen_zero(state_data->presence_message)) {
72                         pj_strdup2(state_data->pool, &node->content,
73                                    state_data->presence_message);
74         }
75
76         ast_sip_presence_xml_create_attr(
77                 state_data->pool, node, "type", ast_presence_state2str(
78                         state_data->presence_state));
79
80         if (!ast_strlen_zero(state_data->presence_subtype)) {
81                 ast_sip_presence_xml_create_attr(
82                         state_data->pool, node, "subtype",
83                         state_data->presence_subtype);
84         }
85
86         return 0;
87 }
88
89 static struct ast_sip_pubsub_body_supplement pidf_supplement = {
90         .type = "application",
91         .subtype = "pidf+xml",
92         .supplement_body = pidf_supplement_body,
93 };
94
95 static int load_module(void)
96 {
97         if (ast_sip_pubsub_register_body_supplement(&pidf_supplement)) {
98                 return AST_MODULE_LOAD_DECLINE;
99         }
100         return AST_MODULE_LOAD_SUCCESS;
101 }
102
103 static int unload_module(void)
104 {
105         ast_sip_pubsub_unregister_body_supplement(&pidf_supplement);
106         return 0;
107 }
108
109 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP PIDF Digium presence supplement",
110                 .load = load_module,
111                 .unload = unload_module,
112                 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
113 );