Debian Codegraph @

res/res_pjsip_mwi_body_generator.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * Mark Michelson <[email protected]>
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/res_pjsip.h"
33 #include "asterisk/res_pjsip_pubsub.h"
34 #include "asterisk/res_pjsip_body_generator_types.h"
35 #include "asterisk/module.h"
36 #include "asterisk/strings.h"
37
38 #define MWI_TYPE "application"
39 #define MWI_SUBTYPE "simple-message-summary"
40
41 static void *mwi_allocate_body(void *data)
42 {
43 struct ast_str **mwi_str;
44
45 mwi_str = ast_malloc(sizeof(*mwi_str));
46 if (!mwi_str) {
47 return NULL;
48 }
49 *mwi_str = ast_str_create(128);
50 if (!*mwi_str) {
51 ast_free(mwi_str);
52 return NULL;
53 }
54 return mwi_str;
55 }
56
57 static int mwi_generate_body_content(void *body, void *data)
58 {
59 struct ast_str **mwi = body;
60 struct ast_sip_message_accumulator *counter = data;
61
62 ast_str_append(mwi, 0, "Messages-Waiting: %s\r\n",
63 counter->new_msgs ? "yes" : "no");
64 if (!ast_strlen_zero(counter->message_account)) {
65 ast_str_append(mwi, 0, "Message-Account: %s\r\n", counter->message_account);
66 }
67 ast_str_append(mwi, 0, "Voice-Message: %d/%d (0/0)\r\n",
68 counter->new_msgs, counter->old_msgs);
69
70 return 0;
71 }
72
73 static void mwi_to_string(void *body, struct ast_str **str)
74 {
75 struct ast_str **mwi = body;
76
77 ast_str_set(str, 0, "%s", ast_str_buffer(*mwi));
78 }
79
80 static void mwi_destroy_body(void *body)
81 {
82 struct ast_str **mwi = body;
83
84 ast_free(*mwi);
85 ast_free(mwi);
86 }
87
88 static struct ast_sip_pubsub_body_generator mwi_generator = {
89 .type = MWI_TYPE,
90 .subtype = MWI_SUBTYPE,
91 .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
92 .allocate_body = mwi_allocate_body,
93 .generate_body_content = mwi_generate_body_content,
94 .to_string = mwi_to_string,
95 .destroy_body = mwi_destroy_body,
96 };
97
98 static int load_module(void)
99 {
100 if (ast_sip_pubsub_register_body_generator(&mwi_generator)) {
101 return AST_MODULE_LOAD_DECLINE;
102 }
103 return AST_MODULE_LOAD_SUCCESS;
104 }
105
106 static int unload_module(void)
107 {
108 ast_sip_pubsub_unregister_body_generator(&mwi_generator);
109 return 0;
110 }
111
112 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP MWI resource",
113 .support_level = AST_MODULE_SUPPORT_CORE,
114 .load = load_module,
115 .unload = unload_module,
116 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
117 .requires = "res_pjsip,res_pjsip_pubsub",
118 );