Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
res_mwi_external_ami.c File Reference

AMI wrapper for external MWI. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/res_mwi_external.h"
#include "asterisk/manager.h"
Include dependency graph for res_mwi_external_ami.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static int mwi_mailbox_delete (struct mansession *s, const struct message *m)
 
static int mwi_mailbox_get (struct mansession *s, const struct message *m)
 
static int mwi_mailbox_update (struct mansession *s, const struct message *m)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "AMI support for external MWI" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND - 5, .requires = "res_mwi_external", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

AMI wrapper for external MWI.

Author
Richard Mudgett rmudg.nosp@m.ett@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

See Also:

Definition in file res_mwi_external_ami.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 370 of file res_mwi_external_ami.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 370 of file res_mwi_external_ami.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 370 of file res_mwi_external_ami.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 348 of file res_mwi_external_ami.c.

References ast_manager_register_xml, AST_MODFLAG_DEFAULT, AST_MODPRI_CHANNEL_DEPEND, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, ASTERISK_GPL_KEY, EVENT_FLAG_CALL, EVENT_FLAG_REPORTING, mwi_mailbox_delete(), mwi_mailbox_get(), mwi_mailbox_update(), and unload_module().

349 {
350  int res;
351 
352  res = 0;
356  if (res) {
357  unload_module();
359  }
360 
362 }
static int mwi_mailbox_get(struct mansession *s, const struct message *m)
#define EVENT_FLAG_CALL
Definition: manager.h:72
static int unload_module(void)
static int mwi_mailbox_update(struct mansession *s, const struct message *m)
static int mwi_mailbox_delete(struct mansession *s, const struct message *m)
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define EVENT_FLAG_REPORTING
Definition: manager.h:80
#define ast_manager_register_xml(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:186

◆ mwi_mailbox_delete()

static int mwi_mailbox_delete ( struct mansession s,
const struct message m 
)
static

Definition at line 244 of file res_mwi_external_ami.c.

References ast_free, ast_mwi_mailbox_delete(), ast_mwi_mailbox_delete_by_regex(), ast_regex_string_to_regex_pattern(), ast_str_buffer(), ast_str_create, ast_strlen_zero, astman_get_header(), astman_send_ack(), astman_send_error(), astman_send_error_va(), and NULL.

Referenced by load_module().

245 {
246  const char *mailbox_id = astman_get_header(m, "Mailbox");
247 
248  if (ast_strlen_zero(mailbox_id)) {
249  astman_send_error(s, m, "Missing mailbox parameter in request");
250  return 0;
251  }
252 
253  if (*mailbox_id == '/') {
254  struct ast_str *regex_string;
255 
256  regex_string = ast_str_create(strlen(mailbox_id) + 1);
257  if (!regex_string) {
258  astman_send_error(s, m, "Memory Allocation Failure");
259  return 0;
260  }
261 
262  /* Make "/regex/" into "regex" */
263  if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
264  astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
265  ast_free(regex_string);
266  return 0;
267  }
268 
270  ast_free(regex_string);
271  } else {
272  ast_mwi_mailbox_delete(mailbox_id);
273  }
274 
275  astman_send_ack(s, m, NULL);
276  return 0;
277 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition: manager.c:3191
#define NULL
Definition: resample.c:96
void astman_send_error_va(struct mansession *s, const struct message *m, const char *fmt,...)
Send error in manager transaction (with va_args support)
Definition: manager.c:3164
const char * astman_get_header(const struct message *m, char *var)
Get header from mananger transaction.
Definition: manager.c:2820
#define ast_strlen_zero(foo)
Definition: strings.h:52
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
int ast_mwi_mailbox_delete(const char *mailbox_id)
Delete matching external MWI object.
int ast_regex_string_to_regex_pattern(const char *regex_string, struct ast_str **regex_pattern)
Given a string regex_string in the form of "/regex/", convert it into the form of "regex"...
Definition: main/utils.c:1931
#define ast_free(a)
Definition: astmm.h:182
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:3159
int ast_mwi_mailbox_delete_by_regex(const char *regex)
Delete all external MWI objects selected by the regular expression.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:620

◆ mwi_mailbox_get()

static int mwi_mailbox_get ( struct mansession s,
const struct message m 
)
static

Definition at line 147 of file res_mwi_external_ami.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_container_alloc_list, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, AO2_ITERATOR_UNLINK, ao2_link, ao2_ref, ast_free, ast_mwi_mailbox_get(), ast_mwi_mailbox_get_by_regex(), ast_mwi_mailbox_get_id(), ast_mwi_mailbox_get_msgs_new(), ast_mwi_mailbox_get_msgs_old(), ast_mwi_mailbox_unref, ast_regex_string_to_regex_pattern(), ast_str_buffer(), ast_str_create, ast_strlen_zero, astman_append(), astman_get_header(), astman_send_error(), astman_send_error_va(), astman_send_list_complete_end(), astman_send_list_complete_start(), astman_send_listack(), id, mailbox, mailboxes, and NULL.

Referenced by load_module().

148 {
149  char id_text[256];
150  const char *id;
151  const char *mailbox_id = astman_get_header(m, "Mailbox");
152  const struct ast_mwi_mailbox_object *mailbox;
153  struct ao2_container *mailboxes;
154  unsigned count;
155  struct ao2_iterator iter;
156 
157  if (ast_strlen_zero(mailbox_id)) {
158  astman_send_error(s, m, "Missing mailbox parameter in request");
159  return 0;
160  }
161 
162  if (*mailbox_id == '/') {
163  struct ast_str *regex_string;
164 
165  regex_string = ast_str_create(strlen(mailbox_id) + 1);
166  if (!regex_string) {
167  astman_send_error(s, m, "Memory Allocation Failure");
168  return 0;
169  }
170 
171  /* Make "/regex/" into "regex" */
172  if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
173  astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
174  ast_free(regex_string);
175  return 0;
176  }
177 
178  mailboxes = ast_mwi_mailbox_get_by_regex(ast_str_buffer(regex_string));
179  ast_free(regex_string);
180  } else {
182  if (mailboxes) {
183  mailbox = ast_mwi_mailbox_get(mailbox_id);
184  if (mailbox) {
185  if (!ao2_link(mailboxes, (void *) mailbox)) {
186  ao2_ref(mailboxes, -1);
187  mailboxes = NULL;
188  }
189  ast_mwi_mailbox_unref(mailbox);
190  }
191  }
192  }
193  if (!mailboxes) {
194  astman_send_error(s, m, "Mailbox container creation failure");
195  return 0;
196  }
197 
198  astman_send_listack(s, m, "Mailboxes will follow", "start");
199 
200  id = astman_get_header(m, "ActionID");
201  if (!ast_strlen_zero(id)) {
202  snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
203  } else {
204  id_text[0] = '\0';
205  }
206 
207  /* Output mailbox list. */
208  count = 0;
209  iter = ao2_iterator_init(mailboxes, AO2_ITERATOR_UNLINK);
210  for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
211  ++count;
212  astman_append(s,
213  "Event: MWIGet\r\n"
214  "Mailbox: %s\r\n"
215  "OldMessages: %u\r\n"
216  "NewMessages: %u\r\n"
217  "%s"
218  "\r\n",
219  ast_mwi_mailbox_get_id(mailbox),
222  id_text);
223  }
224  ao2_iterator_destroy(&iter);
225  ao2_ref(mailboxes, -1);
226 
227  astman_send_list_complete_start(s, m, "MWIGetComplete", count);
229 
230  return 0;
231 }
void astman_append(struct mansession *s, const char *fmt,...)
Definition: manager.c:3080
const char * ast_mwi_mailbox_get_id(const struct ast_mwi_mailbox_object *mailbox)
Get mailbox id.
void astman_send_list_complete_start(struct mansession *s, const struct message *m, const char *event_name, int count)
Start the list complete event.
Definition: manager.c:3237
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
#define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn)
Definition: astobj2.h:1335
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define NULL
Definition: resample.c:96
void astman_send_error_va(struct mansession *s, const struct message *m, const char *fmt,...)
Send error in manager transaction (with va_args support)
Definition: manager.c:3164
const char * astman_get_header(const struct message *m, char *var)
Get header from mananger transaction.
Definition: manager.c:2820
#define ast_strlen_zero(foo)
Definition: strings.h:52
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
void astman_send_list_complete_end(struct mansession *s)
End the list complete event.
Definition: manager.c:3245
const struct ast_mwi_mailbox_object * ast_mwi_mailbox_get(const char *mailbox_id)
Get matching external MWI object.
#define ao2_ref(o, delta)
Definition: astobj2.h:464
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
int ast_regex_string_to_regex_pattern(const char *regex_string, struct ast_str **regex_pattern)
Given a string regex_string in the form of "/regex/", convert it into the form of "regex"...
Definition: main/utils.c:1931
#define ast_free(a)
Definition: astmm.h:182
unsigned int ast_mwi_mailbox_get_msgs_new(const struct ast_mwi_mailbox_object *mailbox)
Get the number of new messages.
#define ast_mwi_mailbox_unref(mailbox)
Convienience unref function for mailbox object.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
enum queue_result id
Definition: app_queue.c:1507
Generic container type.
unsigned int ast_mwi_mailbox_get_msgs_old(const struct ast_mwi_mailbox_object *mailbox)
Get the number of old messages.
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:3159
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
struct ao2_container * ast_mwi_mailbox_get_by_regex(const char *regex)
Get all external MWI objects selected by the regular expression.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:620
void astman_send_listack(struct mansession *s, const struct message *m, char *msg, char *listflag)
Send ack in manager transaction to begin a list.
Definition: manager.c:3201
#define ao2_link(container, obj)
Definition: astobj2.h:1549

◆ mwi_mailbox_update()

static int mwi_mailbox_update ( struct mansession s,
const struct message m 
)
static

Definition at line 290 of file res_mwi_external_ami.c.

References ast_mwi_mailbox_alloc(), ast_mwi_mailbox_set_msgs_new(), ast_mwi_mailbox_set_msgs_old(), ast_mwi_mailbox_unref, ast_mwi_mailbox_update(), ast_strlen_zero, astman_get_header(), astman_send_ack(), astman_send_error(), astman_send_error_va(), mailbox, and NULL.

Referenced by load_module().

291 {
292  const char *mailbox_id = astman_get_header(m, "Mailbox");
293  const char *msgs_old = astman_get_header(m, "OldMessages");
294  const char *msgs_new = astman_get_header(m, "NewMessages");
296  unsigned int num_old;
297  unsigned int num_new;
298 
299  if (ast_strlen_zero(mailbox_id)) {
300  astman_send_error(s, m, "Missing mailbox parameter in request");
301  return 0;
302  }
303 
304  num_old = 0;
305  if (!ast_strlen_zero(msgs_old)) {
306  if (sscanf(msgs_old, "%u", &num_old) != 1) {
307  astman_send_error_va(s, m, "Invalid OldMessages: %s", msgs_old);
308  return 0;
309  }
310  }
311 
312  num_new = 0;
313  if (!ast_strlen_zero(msgs_new)) {
314  if (sscanf(msgs_new, "%u", &num_new) != 1) {
315  astman_send_error_va(s, m, "Invalid NewMessages: %s", msgs_new);
316  return 0;
317  }
318  }
319 
320  mailbox = ast_mwi_mailbox_alloc(mailbox_id);
321  if (!mailbox) {
322  astman_send_error(s, m, "Mailbox object creation failure");
323  return 0;
324  }
325 
326  /* Update external mailbox. */
327  ast_mwi_mailbox_set_msgs_old(mailbox, num_old);
328  ast_mwi_mailbox_set_msgs_new(mailbox, num_new);
329  if (ast_mwi_mailbox_update(mailbox)) {
330  astman_send_error(s, m, "Update attempt failed");
331  } else {
332  astman_send_ack(s, m, NULL);
333  }
334  ast_mwi_mailbox_unref(mailbox);
335 
336  return 0;
337 }
struct ast_mwi_mailbox_object * ast_mwi_mailbox_alloc(const char *mailbox_id)
Allocate an external MWI object.
void ast_mwi_mailbox_set_msgs_new(struct ast_mwi_mailbox_object *mailbox, unsigned int num_msgs)
Set the number of new messages.
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition: manager.c:3191
#define NULL
Definition: resample.c:96
void astman_send_error_va(struct mansession *s, const struct message *m, const char *fmt,...)
Send error in manager transaction (with va_args support)
Definition: manager.c:3164
const char * astman_get_header(const struct message *m, char *var)
Get header from mananger transaction.
Definition: manager.c:2820
#define ast_strlen_zero(foo)
Definition: strings.h:52
int ast_mwi_mailbox_update(struct ast_mwi_mailbox_object *mailbox)
Update the external MWI counts with the given object.
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
#define ast_mwi_mailbox_unref(mailbox)
Convienience unref function for mailbox object.
void ast_mwi_mailbox_set_msgs_old(struct ast_mwi_mailbox_object *mailbox, unsigned int num_msgs)
Set the number of old messages.
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:3159

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 339 of file res_mwi_external_ami.c.

References ast_manager_unregister().

Referenced by load_module().

340 {
341  ast_manager_unregister("MWIGet");
342  ast_manager_unregister("MWIDelete");
343  ast_manager_unregister("MWIUpdate");
344 
345  return 0;
346 }
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition: manager.c:7258

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "AMI support for external MWI" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND - 5, .requires = "res_mwi_external", }
static

Definition at line 370 of file res_mwi_external_ami.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 370 of file res_mwi_external_ami.c.