Asterisk - The Open Source Telephony Project GIT-master-a358458
res_pjsip_mwi_body_generator.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * Mark Michelson <mmichelson@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/res_pjsip.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
41static 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
57static 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
73static 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
80static void mwi_destroy_body(void *body)
81{
82 struct ast_str **mwi = body;
83
84 ast_free(*mwi);
85 ast_free(mwi);
86}
87
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
98static int load_module(void)
99{
102 }
104}
105
106static int unload_module(void)
107{
109 return 0;
110}
111
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",
const char * str
Definition: app_jack.c:147
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:317
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODPRI_CHANNEL_DEPEND
Definition: module.h:326
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static void mwi_to_string(void *body, struct ast_str **str)
static void mwi_destroy_body(void *body)
static int load_module(void)
static struct ast_sip_pubsub_body_generator mwi_generator
static int unload_module(void)
#define MWI_TYPE
#define MWI_SUBTYPE
static void * mwi_allocate_body(void *data)
static int mwi_generate_body_content(void *body, void *data)
void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator)
Unregister a body generator with the pubsub core.
int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator)
Register a body generator with the pubsub core.
#define AST_SIP_MESSAGE_ACCUMULATOR
#define NULL
Definition: resample.c:96
String manipulation functions.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
Message counter used for message-summary XML bodies.
Pubsub body generator.
const char * type
Content type In "plain/text", "plain" is the type.
Support for dynamic strings.
Definition: strings.h:623