Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Enumerations | Functions | Variables
res_mwi_external.c File Reference

Core external MWI support. More...

#include "asterisk.h"
#include "asterisk/app.h"
#include "asterisk/mwi.h"
#include "asterisk/module.h"
#include "asterisk/res_mwi_external.h"
#include "asterisk/sorcery.h"
#include "asterisk/cli.h"
Include dependency graph for res_mwi_external.c:

Go to the source code of this file.

Data Structures

struct  ast_mwi_mailbox_object
 

Macros

#define FORMAT_MAILBOX_HDR   "%6s %6s %s\n"
 
#define FORMAT_MAILBOX_ROW   "%6u %6u %s\n"
 
#define MWI_ASTDB_PREFIX   "mwi_external"
 
#define MWI_MAILBOX_TYPE   "mailboxes"
 

Enumerations

enum  folder_map { FOLDER_INVALID = 0, FOLDER_INBOX = 1, FOLDER_OLD = 2 }
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
struct ast_mwi_mailbox_objectast_mwi_mailbox_alloc (const char *mailbox_id)
 Allocate an external MWI object. More...
 
struct ast_mwi_mailbox_objectast_mwi_mailbox_copy (const struct ast_mwi_mailbox_object *mailbox)
 Copy the external MWI counts object. More...
 
int ast_mwi_mailbox_delete (const char *mailbox_id)
 Delete matching external MWI object. More...
 
int ast_mwi_mailbox_delete_all (void)
 Delete all external MWI objects. More...
 
int ast_mwi_mailbox_delete_by_regex (const char *regex)
 Delete all external MWI objects selected by the regular expression. More...
 
const struct ast_mwi_mailbox_objectast_mwi_mailbox_get (const char *mailbox_id)
 Get matching external MWI object. More...
 
struct ao2_containerast_mwi_mailbox_get_all (void)
 Get all external MWI objects. More...
 
struct ao2_containerast_mwi_mailbox_get_by_regex (const char *regex)
 Get all external MWI objects selected by the regular expression. More...
 
const char * ast_mwi_mailbox_get_id (const struct ast_mwi_mailbox_object *mailbox)
 Get mailbox id. More...
 
unsigned int ast_mwi_mailbox_get_msgs_new (const struct ast_mwi_mailbox_object *mailbox)
 Get the number of new messages. More...
 
unsigned int ast_mwi_mailbox_get_msgs_old (const struct ast_mwi_mailbox_object *mailbox)
 Get the number of old messages. More...
 
void ast_mwi_mailbox_set_msgs_new (struct ast_mwi_mailbox_object *mailbox, unsigned int num_msgs)
 Set the number of new messages. More...
 
void ast_mwi_mailbox_set_msgs_old (struct ast_mwi_mailbox_object *mailbox, unsigned int num_msgs)
 Set the number of old messages. More...
 
int ast_mwi_mailbox_update (struct ast_mwi_mailbox_object *mailbox)
 Update the external MWI counts with the given object. More...
 
static int load_module (void)
 
static enum folder_map mwi_folder_map (const char *folder)
 
static int mwi_has_voicemail (const char *mailboxes, const char *folder)
 
static int mwi_inboxcount (const char *mailboxes, int *newmsgs, int *oldmsgs)
 
static int mwi_inboxcount2 (const char *mailboxes, int *urgentmsgs, int *newmsgs, int *oldmsgs)
 
static void mwi_initial_events (void)
 
static void mwi_mailbox_delete (struct ast_mwi_mailbox_object *mailbox)
 
static void mwi_mailbox_delete_all (struct ao2_container *mailboxes)
 
static int mwi_messagecount (const char *mailbox_id, const char *folder)
 
static void mwi_observe_delete (const void *obj)
 
static void mwi_observe_update (const void *obj)
 
static void mwi_post_event (const struct ast_mwi_mailbox_object *mailbox)
 
static int mwi_sorcery_init (void)
 
static void * mwi_sorcery_object_alloc (const char *id)
 Internal function to allocate a mwi object. More...
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "Core external MWI resource" , .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, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const struct ast_sorcery_observer mwi_observers
 
static struct ast_sorcerymwi_sorcery
 
static const struct ast_vm_functions vm_table
 

Detailed Description

Core external MWI support.

The module manages the persistent message counts cache and supplies an API to allow the protocol specific modules to control the counts or a subset.

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.c.

Macro Definition Documentation

◆ FORMAT_MAILBOX_HDR

#define FORMAT_MAILBOX_HDR   "%6s %6s %s\n"

Definition at line 650 of file res_mwi_external.c.

◆ FORMAT_MAILBOX_ROW

#define FORMAT_MAILBOX_ROW   "%6u %6u %s\n"

Definition at line 651 of file res_mwi_external.c.

◆ MWI_ASTDB_PREFIX

#define MWI_ASTDB_PREFIX   "mwi_external"

Define to include CLI commands to manipulate the external MWI mailboxes. Useful for testing the module functionality.

Definition at line 72 of file res_mwi_external.c.

Referenced by mwi_sorcery_init().

◆ MWI_MAILBOX_TYPE

#define MWI_MAILBOX_TYPE   "mailboxes"

Enumeration Type Documentation

◆ folder_map

enum folder_map
Enumerator
FOLDER_INVALID 
FOLDER_INBOX 
FOLDER_OLD 

Definition at line 326 of file res_mwi_external.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 957 of file res_mwi_external.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 957 of file res_mwi_external.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 957 of file res_mwi_external.c.

◆ ast_mwi_mailbox_alloc()

struct ast_mwi_mailbox_object* ast_mwi_mailbox_alloc ( const char *  mailbox_id)

Allocate an external MWI object.

Since
12.1.0
Parameters
mailbox_idName of mailbox.
Return values
objecton success. The object is an ao2 object.
NULLon error.

Definition at line 196 of file res_mwi_external.c.

References ast_sorcery_alloc(), ast_strlen_zero, MWI_MAILBOX_TYPE, and NULL.

Referenced by mwi_mailbox_update(), and stasis_app_mailbox_update().

197 {
198  if (ast_strlen_zero(mailbox_id)) {
199  return NULL;
200  }
201 
202  return ast_sorcery_alloc(mwi_sorcery, MWI_MAILBOX_TYPE, mailbox_id);
203 }
static struct ast_sorcery * mwi_sorcery
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define MWI_MAILBOX_TYPE
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744

◆ ast_mwi_mailbox_copy()

struct ast_mwi_mailbox_object* ast_mwi_mailbox_copy ( const struct ast_mwi_mailbox_object mailbox)

Copy the external MWI counts object.

Since
12.1.0
Parameters
mailboxWhat to copy.
Return values
copyon success. The object is an ao2 object.
NULLon error.

Definition at line 205 of file res_mwi_external.c.

References ast_sorcery_copy().

206 {
207  return ast_sorcery_copy(mwi_sorcery, mailbox);
208 }
static struct ast_sorcery * mwi_sorcery
void * ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
Create a copy of an object.
Definition: sorcery.c:1778

◆ ast_mwi_mailbox_delete()

int ast_mwi_mailbox_delete ( const char *  mailbox_id)

Delete matching external MWI object.

Since
12.1.0
Parameters
mailbox_idName of mailbox to delete.
Return values
0on success.
-1on error.

Definition at line 310 of file res_mwi_external.c.

References ast_mwi_mailbox_get(), ast_mwi_mailbox_unref, ast_strlen_zero, mailbox, and mwi_mailbox_delete().

Referenced by mwi_mailbox_delete(), and stasis_app_mailbox_delete().

311 {
312  const struct ast_mwi_mailbox_object *mailbox;
313 
314  if (ast_strlen_zero(mailbox_id)) {
315  return -1;
316  }
317 
318  mailbox = ast_mwi_mailbox_get(mailbox_id);
319  if (mailbox) {
320  mwi_mailbox_delete((struct ast_mwi_mailbox_object *) mailbox);
321  ast_mwi_mailbox_unref(mailbox);
322  }
323  return 0;
324 }
const struct ast_mwi_mailbox_object * ast_mwi_mailbox_get(const char *mailbox_id)
Get matching external MWI object.
#define ast_strlen_zero(foo)
Definition: strings.h:52
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
static void mwi_mailbox_delete(struct ast_mwi_mailbox_object *mailbox)
#define ast_mwi_mailbox_unref(mailbox)
Convienience unref function for mailbox object.

◆ ast_mwi_mailbox_delete_all()

int ast_mwi_mailbox_delete_all ( void  )

Delete all external MWI objects.

Since
12.1.0
Return values
0on success.
-1on error.

Definition at line 286 of file res_mwi_external.c.

References ao2_ref, ast_mwi_mailbox_get_all(), mailboxes, and mwi_mailbox_delete_all().

287 {
288  struct ao2_container *mailboxes;
289 
290  mailboxes = ast_mwi_mailbox_get_all();
291  if (mailboxes) {
292  mwi_mailbox_delete_all(mailboxes);
293  ao2_ref(mailboxes, -1);
294  }
295  return 0;
296 }
static void mwi_mailbox_delete_all(struct ao2_container *mailboxes)
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct ao2_container * ast_mwi_mailbox_get_all(void)
Get all external MWI objects.
Generic container type.

◆ ast_mwi_mailbox_delete_by_regex()

int ast_mwi_mailbox_delete_by_regex ( const char *  regex)

Delete all external MWI objects selected by the regular expression.

Since
12.1.0
Parameters
regexRegular expression in extended syntax. (NULL is same as "")
Note
The provided regex is treated as extended case sensitive.
Return values
0on success.
-1on error.

Definition at line 298 of file res_mwi_external.c.

References ao2_ref, ast_mwi_mailbox_get_by_regex(), mailboxes, and mwi_mailbox_delete_all().

Referenced by mwi_mailbox_delete().

299 {
300  struct ao2_container *mailboxes;
301 
302  mailboxes = ast_mwi_mailbox_get_by_regex(regex);
303  if (mailboxes) {
304  mwi_mailbox_delete_all(mailboxes);
305  ao2_ref(mailboxes, -1);
306  }
307  return 0;
308 }
static void mwi_mailbox_delete_all(struct ao2_container *mailboxes)
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:948
struct ao2_container * ast_mwi_mailbox_get_by_regex(const char *regex)
Get all external MWI objects selected by the regular expression.
Generic container type.

◆ ast_mwi_mailbox_get()

const struct ast_mwi_mailbox_object* ast_mwi_mailbox_get ( const char *  mailbox_id)

Get matching external MWI object.

Since
12.1.0
Parameters
mailbox_idName of mailbox to retrieve.
Return values
requestedmailbox on success. The object is an ao2 object.
NULLon error or no mailbox.
Note
The object must be treated as read-only.

Definition at line 187 of file res_mwi_external.c.

References ast_sorcery_retrieve_by_id(), ast_strlen_zero, MWI_MAILBOX_TYPE, and NULL.

Referenced by ast_mwi_mailbox_delete(), mwi_has_voicemail(), mwi_inboxcount(), mwi_mailbox_get(), mwi_messagecount(), stasis_app_mailbox_delete(), and stasis_app_mailbox_to_json().

188 {
189  if (ast_strlen_zero(mailbox_id)) {
190  return NULL;
191  }
192 
194 }
static struct ast_sorcery * mwi_sorcery
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
#define MWI_MAILBOX_TYPE

◆ ast_mwi_mailbox_get_all()

struct ao2_container* ast_mwi_mailbox_get_all ( void  )

Get all external MWI objects.

Since
12.1.0
Return values
containerof struct ast_mwi_mailbox_object on success.
NULLon error.
Note
The objects in the container must be treated as read-only.

Definition at line 176 of file res_mwi_external.c.

References AST_RETRIEVE_FLAG_ALL, AST_RETRIEVE_FLAG_MULTIPLE, ast_sorcery_retrieve_by_fields(), MWI_MAILBOX_TYPE, and NULL.

Referenced by ast_mwi_mailbox_delete_all(), mwi_initial_events(), and stasis_app_mailboxes_to_json().

177 {
180 }
Perform no matching, return all objects.
Definition: sorcery.h:123
static struct ast_sorcery * mwi_sorcery
Return all matching objects.
Definition: sorcery.h:120
#define NULL
Definition: resample.c:96
#define MWI_MAILBOX_TYPE
void * ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
Retrieve an object or multiple objects using specific fields.
Definition: sorcery.c:1897

◆ ast_mwi_mailbox_get_by_regex()

struct ao2_container* ast_mwi_mailbox_get_by_regex ( const char *  regex)

Get all external MWI objects selected by the regular expression.

Since
12.1.0
Parameters
regexRegular expression in extended syntax. (NULL is same as "")
Note
The provided regex is treated as extended case sensitive.
Return values
containerof struct ast_mwi_mailbox_object on success.
NULLon error.
Note
The objects in the container must be treated as read-only.

Definition at line 182 of file res_mwi_external.c.

References ast_sorcery_retrieve_by_regex(), and MWI_MAILBOX_TYPE.

Referenced by ast_mwi_mailbox_delete_by_regex(), and mwi_mailbox_get().

183 {
185 }
static struct ast_sorcery * mwi_sorcery
struct ao2_container * ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
Retrieve multiple objects using a regular expression on their id.
Definition: sorcery.c:1949
#define MWI_MAILBOX_TYPE
static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:948

◆ ast_mwi_mailbox_get_id()

const char* ast_mwi_mailbox_get_id ( const struct ast_mwi_mailbox_object mailbox)

Get mailbox id.

Since
12.1.0
Parameters
mailboxObject to get id.
Returns
mailbox_id of the object.
Note
This should never return NULL unless there is a bug in sorcery.

Definition at line 210 of file res_mwi_external.c.

References ast_sorcery_object_get_id().

Referenced by mailbox_to_json(), and mwi_mailbox_get().

211 {
212  return ast_sorcery_object_get_id(mailbox);
213 }
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312

◆ ast_mwi_mailbox_get_msgs_new()

unsigned int ast_mwi_mailbox_get_msgs_new ( const struct ast_mwi_mailbox_object mailbox)

Get the number of new messages.

Since
12.1.0
Parameters
mailboxObject to get number of new messages.
Returns
Number of new messages.

Definition at line 215 of file res_mwi_external.c.

References ast_mwi_mailbox_object::msgs_new.

Referenced by mailbox_to_json(), and mwi_mailbox_get().

216 {
217  return mailbox->msgs_new;
218 }

◆ ast_mwi_mailbox_get_msgs_old()

unsigned int ast_mwi_mailbox_get_msgs_old ( const struct ast_mwi_mailbox_object mailbox)

Get the number of old messages.

Since
12.1.0
Parameters
mailboxObject to get number of old messages.
Returns
Number of old messages.

Definition at line 220 of file res_mwi_external.c.

References ast_mwi_mailbox_object::msgs_old.

Referenced by mailbox_to_json(), and mwi_mailbox_get().

221 {
222  return mailbox->msgs_old;
223 }

◆ ast_mwi_mailbox_set_msgs_new()

void ast_mwi_mailbox_set_msgs_new ( struct ast_mwi_mailbox_object mailbox,
unsigned int  num_msgs 
)

Set the number of new messages.

Since
12.1.0
Parameters
mailboxObject to set number of new messages.
num_msgsNumber of messages to set.
Returns
Nothing

Definition at line 225 of file res_mwi_external.c.

References ast_mwi_mailbox_object::msgs_new.

Referenced by mwi_mailbox_update(), and stasis_app_mailbox_update().

226 {
227  mailbox->msgs_new = num_msgs;
228 }

◆ ast_mwi_mailbox_set_msgs_old()

void ast_mwi_mailbox_set_msgs_old ( struct ast_mwi_mailbox_object mailbox,
unsigned int  num_msgs 
)

Set the number of old messages.

Since
12.1.0
Parameters
mailboxObject to set number of old messages.
num_msgsNumber of messages to set.
Returns
Nothing

Definition at line 230 of file res_mwi_external.c.

References ast_mwi_mailbox_object::msgs_old.

Referenced by mwi_mailbox_update(), and stasis_app_mailbox_update().

231 {
232  mailbox->msgs_old = num_msgs;
233 }

◆ ast_mwi_mailbox_update()

int ast_mwi_mailbox_update ( struct ast_mwi_mailbox_object mailbox)

Update the external MWI counts with the given object.

Since
12.1.0
Parameters
mailboxWhat to update.
Return values
0on success.
-1on error.

Definition at line 235 of file res_mwi_external.c.

References ast_mwi_mailbox_unref, ast_sorcery_create(), ast_sorcery_object_get_id(), ast_sorcery_retrieve_by_id(), ast_sorcery_update(), exists(), and MWI_MAILBOX_TYPE.

Referenced by mwi_mailbox_update(), and stasis_app_mailbox_update().

236 {
237  const struct ast_mwi_mailbox_object *exists;
238  int res;
239 
241  ast_sorcery_object_get_id(mailbox));
242  if (exists) {
243  res = ast_sorcery_update(mwi_sorcery, mailbox);
244  ast_mwi_mailbox_unref(exists);
245  } else {
246  res = ast_sorcery_create(mwi_sorcery, mailbox);
247  }
248  return res;
249 }
static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:124
static struct ast_sorcery * mwi_sorcery
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057
#define MWI_MAILBOX_TYPE
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
#define ast_mwi_mailbox_unref(mailbox)
Convienience unref function for mailbox object.
int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
Update an object.
Definition: sorcery.c:2145

◆ load_module()

static int load_module ( void  )
static

Definition at line 924 of file res_mwi_external.c.

References ARRAY_LEN, ast_cli_register_multiple, ast_log, AST_MODFLAG_GLOBAL_SYMBOLS, AST_MODFLAG_LOAD_ORDER, AST_MODPRI_CHANNEL_DEPEND, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, ast_sorcery_observer_add(), ast_vm_register, ASTERISK_GPL_KEY, LOG_ERROR, mwi_initial_events(), MWI_MAILBOX_TYPE, mwi_sorcery_init(), and unload_module().

925 {
926  int res;
927 
928  if (mwi_sorcery_init()
930 #if defined(MWI_DEBUG_CLI)
931  || ast_cli_register_multiple(mwi_cli, ARRAY_LEN(mwi_cli))
932 #endif /* defined(MWI_DEBUG_CLI) */
933  ) {
934  unload_module();
936  }
937 
938  /* ast_vm_register may return DECLINE if another module registered for vm */
939  res = ast_vm_register(&vm_table);
940  if (res) {
941  ast_log(LOG_ERROR, "Failure registering as a voicemail provider\n");
942  unload_module();
944  }
945 
946  /* Post initial MWI count events. */
948 
950 }
static int unload_module(void)
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static int mwi_sorcery_init(void)
static struct ast_sorcery * mwi_sorcery
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
#define ast_log
Definition: astobj2.c:42
#define MWI_MAILBOX_TYPE
static const struct ast_vm_functions vm_table
int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Add an observer to a specific object type.
Definition: sorcery.c:2386
#define ast_vm_register(vm_table)
See __ast_vm_register()
#define LOG_ERROR
Definition: logger.h:285
static const struct ast_sorcery_observer mwi_observers
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static void mwi_initial_events(void)

◆ mwi_folder_map()

static enum folder_map mwi_folder_map ( const char *  folder)
static

Definition at line 341 of file res_mwi_external.c.

References ast_strlen_zero, FOLDER_INBOX, FOLDER_INVALID, and FOLDER_OLD.

Referenced by mwi_has_voicemail(), and mwi_messagecount().

342 {
343  enum folder_map which_folder;
344 
345  if (ast_strlen_zero(folder) || !strcasecmp(folder, "INBOX")) {
346  which_folder = FOLDER_INBOX;
347  } else if (!strcasecmp(folder, "Old")) {
348  which_folder = FOLDER_OLD;
349  } else {
350  which_folder = FOLDER_INVALID;
351  }
352  return which_folder;
353 }
#define ast_strlen_zero(foo)
Definition: strings.h:52
folder_map

◆ mwi_has_voicemail()

static int mwi_has_voicemail ( const char *  mailboxes,
const char *  folder 
)
static

Definition at line 407 of file res_mwi_external.c.

References ast_mwi_mailbox_get(), ast_mwi_mailbox_unref, ast_strdupa, FOLDER_INBOX, FOLDER_INVALID, FOLDER_OLD, mailbox, ast_mwi_mailbox_object::msgs_new, ast_mwi_mailbox_object::msgs_old, mwi_folder_map(), parse(), and strsep().

408 {
409  char *parse;
410  char *mailbox_id;
411  enum folder_map which_folder;
412 
413  which_folder = mwi_folder_map(folder);
414  if (which_folder == FOLDER_INVALID) {
415  return 0;
416  }
417 
418  /* For each mailbox in the list. */
419  parse = ast_strdupa(mailboxes);
420  while ((mailbox_id = strsep(&parse, ",&"))) {
421  const struct ast_mwi_mailbox_object *mailbox;
422  int num_msgs;
423 
424  /* Get the specified mailbox. */
425  mailbox = ast_mwi_mailbox_get(mailbox_id);
426  if (!mailbox) {
427  continue;
428  }
429 
430  /* Done if the found mailbox has any messages. */
431  num_msgs = 0;
432  switch (which_folder) {
433  case FOLDER_INVALID:
434  break;
435  case FOLDER_INBOX:
436  num_msgs = mailbox->msgs_new;
437  break;
438  case FOLDER_OLD:
439  num_msgs = mailbox->msgs_old;
440  break;
441  }
442  ast_mwi_mailbox_unref(mailbox);
443  if (num_msgs) {
444  return 1;
445  }
446  }
447 
448  return 0;
449 }
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
const struct ast_mwi_mailbox_object * ast_mwi_mailbox_get(const char *mailbox_id)
Get matching external MWI object.
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static enum folder_map mwi_folder_map(const char *folder)
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define ast_mwi_mailbox_unref(mailbox)
Convienience unref function for mailbox object.
char * strsep(char **str, const char *delims)
folder_map

◆ mwi_inboxcount()

static int mwi_inboxcount ( const char *  mailboxes,
int *  newmsgs,
int *  oldmsgs 
)
static

Definition at line 467 of file res_mwi_external.c.

References ast_mwi_mailbox_get(), ast_mwi_mailbox_unref, ast_strdupa, mailbox, ast_mwi_mailbox_object::msgs_new, ast_mwi_mailbox_object::msgs_old, parse(), and strsep().

Referenced by mwi_inboxcount2().

468 {
469  char *parse;
470  char *mailbox_id;
471 
472  if (!newmsgs && !oldmsgs) {
473  /* Nowhere to accumulate counts */
474  return 0;
475  }
476 
477  /* For each mailbox in the list. */
478  parse = ast_strdupa(mailboxes);
479  while ((mailbox_id = strsep(&parse, ", "))) {
480  const struct ast_mwi_mailbox_object *mailbox;
481 
482  /* Get the specified mailbox. */
483  mailbox = ast_mwi_mailbox_get(mailbox_id);
484  if (!mailbox) {
485  continue;
486  }
487 
488  /* Accumulate the counts. */
489  if (newmsgs) {
490  *newmsgs += mailbox->msgs_new;
491  }
492  if (oldmsgs) {
493  *oldmsgs += mailbox->msgs_old;
494  }
495 
496  ast_mwi_mailbox_unref(mailbox);
497  }
498 
499  return 0;
500 }
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
const struct ast_mwi_mailbox_object * ast_mwi_mailbox_get(const char *mailbox_id)
Get matching external MWI object.
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define ast_mwi_mailbox_unref(mailbox)
Convienience unref function for mailbox object.
char * strsep(char **str, const char *delims)

◆ mwi_inboxcount2()

static int mwi_inboxcount2 ( const char *  mailboxes,
int *  urgentmsgs,
int *  newmsgs,
int *  oldmsgs 
)
static

Definition at line 520 of file res_mwi_external.c.

References mwi_inboxcount().

521 {
522  /*
523  * This module does not support urgentmsgs. Just ignore them.
524  * The global API call has already set the count to zero.
525  */
526  return mwi_inboxcount(mailboxes, newmsgs, oldmsgs);
527 }
static int mwi_inboxcount(const char *mailboxes, int *newmsgs, int *oldmsgs)
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.

◆ mwi_initial_events()

static void mwi_initial_events ( void  )
static

Definition at line 888 of file res_mwi_external.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, AO2_ITERATOR_UNLINK, ao2_ref, ast_mwi_mailbox_get_all(), ast_mwi_mailbox_unref, mailbox, mailboxes, and mwi_post_event().

Referenced by load_module().

889 {
890  struct ao2_container *mailboxes;
891  const struct ast_mwi_mailbox_object *mailbox;
892  struct ao2_iterator iter;
893 
894  /* Get all mailbox counts. */
895  mailboxes = ast_mwi_mailbox_get_all();
896  if (!mailboxes) {
897  return;
898  }
899 
900  /* Post all mailbox counts. */
901  iter = ao2_iterator_init(mailboxes, AO2_ITERATOR_UNLINK);
902  for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
903  mwi_post_event(mailbox);
904  }
905  ao2_iterator_destroy(&iter);
906 
907  ao2_ref(mailboxes, -1);
908 }
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
static void mwi_post_event(const struct ast_mwi_mailbox_object *mailbox)
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct ao2_container * ast_mwi_mailbox_get_all(void)
Get all external MWI objects.
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
#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
Generic container type.
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.

◆ mwi_mailbox_delete()

static void mwi_mailbox_delete ( struct ast_mwi_mailbox_object mailbox)
static

Definition at line 260 of file res_mwi_external.c.

References ast_sorcery_delete().

Referenced by ast_mwi_mailbox_delete(), and mwi_mailbox_delete_all().

261 {
263 }
static struct ast_sorcery * mwi_sorcery
int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
Delete an object.
Definition: sorcery.c:2233

◆ mwi_mailbox_delete_all()

static void mwi_mailbox_delete_all ( struct ao2_container mailboxes)
static

Definition at line 274 of file res_mwi_external.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, AO2_ITERATOR_UNLINK, ast_mwi_mailbox_unref, mailbox, and mwi_mailbox_delete().

Referenced by ast_mwi_mailbox_delete_all(), and ast_mwi_mailbox_delete_by_regex().

275 {
277  struct ao2_iterator iter;
278 
279  iter = ao2_iterator_init(mailboxes, AO2_ITERATOR_UNLINK);
280  for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
281  mwi_mailbox_delete(mailbox);
282  }
283  ao2_iterator_destroy(&iter);
284 }
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
static void mwi_mailbox_delete(struct ast_mwi_mailbox_object *mailbox)
#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
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.

◆ mwi_messagecount()

static int mwi_messagecount ( const char *  mailbox_id,
const char *  folder 
)
static

Definition at line 365 of file res_mwi_external.c.

References ast_mwi_mailbox_get(), ast_mwi_mailbox_unref, FOLDER_INBOX, FOLDER_INVALID, FOLDER_OLD, mailbox, ast_mwi_mailbox_object::msgs_new, ast_mwi_mailbox_object::msgs_old, and mwi_folder_map().

366 {
367  const struct ast_mwi_mailbox_object *mailbox;
368  int num_msgs;
369  enum folder_map which_folder;
370 
371  which_folder = mwi_folder_map(folder);
372  if (which_folder == FOLDER_INVALID) {
373  return 0;
374  }
375 
376  mailbox = ast_mwi_mailbox_get(mailbox_id);
377  if (!mailbox) {
378  return 0;
379  }
380  num_msgs = 0;
381  switch (which_folder) {
382  case FOLDER_INVALID:
383  break;
384  case FOLDER_INBOX:
385  num_msgs = mailbox->msgs_new;
386  break;
387  case FOLDER_OLD:
388  num_msgs = mailbox->msgs_old;
389  break;
390  }
391  ast_mwi_mailbox_unref(mailbox);
392 
393  return num_msgs;
394 }
const struct ast_mwi_mailbox_object * ast_mwi_mailbox_get(const char *mailbox_id)
Get matching external MWI object.
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
static enum folder_map mwi_folder_map(const char *folder)
#define ast_mwi_mailbox_unref(mailbox)
Convienience unref function for mailbox object.
folder_map

◆ mwi_observe_delete()

static void mwi_observe_delete ( const void *  obj)
static

Definition at line 110 of file res_mwi_external.c.

References ast_delete_mwi_state, ast_publish_mwi_state, ast_sorcery_object_get_id(), mailbox, ast_mwi_mailbox_object::msgs_new, ast_mwi_mailbox_object::msgs_old, and NULL.

111 {
112  const struct ast_mwi_mailbox_object *mailbox = obj;
113 
114  if (mailbox->msgs_new || mailbox->msgs_old) {
115  /* Post a count clearing event. */
117  }
118 
119  /* Post a cache remove event. */
121 }
#define NULL
Definition: resample.c:96
#define ast_publish_mwi_state(mailbox, context, new_msgs, old_msgs)
Publish a MWI state update via stasis.
Definition: mwi.h:380
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
#define ast_delete_mwi_state(mailbox, context)
Delete MWI state cached by stasis.
Definition: mwi.h:433

◆ mwi_observe_update()

static void mwi_observe_update ( const void *  obj)
static

Definition at line 98 of file res_mwi_external.c.

References mwi_post_event().

99 {
100  mwi_post_event(obj);
101 }
static void mwi_post_event(const struct ast_mwi_mailbox_object *mailbox)

◆ mwi_post_event()

static void mwi_post_event ( const struct ast_mwi_mailbox_object mailbox)
static

Definition at line 92 of file res_mwi_external.c.

References ast_publish_mwi_state, ast_sorcery_object_get_id(), ast_mwi_mailbox_object::msgs_new, ast_mwi_mailbox_object::msgs_old, and NULL.

Referenced by mwi_initial_events(), and mwi_observe_update().

93 {
95  mailbox->msgs_new, mailbox->msgs_old);
96 }
#define NULL
Definition: resample.c:96
#define ast_publish_mwi_state(mailbox, context, new_msgs, old_msgs)
Publish a MWI state update via stasis.
Definition: mwi.h:380
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312

◆ mwi_sorcery_init()

static int mwi_sorcery_init ( void  )
static

Definition at line 143 of file res_mwi_external.c.

References ast_log, ast_sorcery_apply_default, AST_SORCERY_APPLY_FAIL, ast_sorcery_object_field_register_nodoc, ast_sorcery_object_register, ast_sorcery_open, FLDSET, LOG_ERROR, ast_mwi_mailbox_object::msgs_new, ast_mwi_mailbox_object::msgs_old, MWI_ASTDB_PREFIX, MWI_MAILBOX_TYPE, mwi_sorcery_object_alloc(), NULL, and OPT_UINT_T.

Referenced by load_module().

144 {
145  int res;
146 
148  if (!mwi_sorcery) {
149  ast_log(LOG_ERROR, "MWI external: Sorcery failed to open.\n");
150  return -1;
151  }
152 
153  /* Map the external MWI wizards. */
156  ast_log(LOG_ERROR, "MWI external: Sorcery could not setup wizards.\n");
157  return -1;
158  }
159 
162  if (res) {
163  ast_log(LOG_ERROR, "MWI external: Sorcery could not register object type '%s'.\n",
165  return -1;
166  }
167 
168  /* Define the MWI_MAILBOX_TYPE object fields. */
170  "msgs_new", "0", OPT_UINT_T, 0, FLDSET(struct ast_mwi_mailbox_object, msgs_new));
172  "msgs_old", "0", OPT_UINT_T, 0, FLDSET(struct ast_mwi_mailbox_object, msgs_old));
173  return res ? -1 : 0;
174 }
#define ast_sorcery_object_field_register_nodoc(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object without documentation.
Definition: sorcery.h:987
static struct ast_sorcery * mwi_sorcery
#define NULL
Definition: resample.c:96
#define MWI_ASTDB_PREFIX
#define ast_log
Definition: astobj2.c:42
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
Type for default option handler for unsigned integers.
#define MWI_MAILBOX_TYPE
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition: sorcery.h:838
#define LOG_ERROR
Definition: logger.h:285
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition: sorcery.h:477
static void * mwi_sorcery_object_alloc(const char *id)
Internal function to allocate a mwi object.
#define ast_sorcery_open()
Definition: sorcery.h:408

◆ mwi_sorcery_object_alloc()

static void* mwi_sorcery_object_alloc ( const char *  id)
static

Internal function to allocate a mwi object.

Definition at line 130 of file res_mwi_external.c.

References ast_sorcery_generic_alloc(), and NULL.

Referenced by mwi_sorcery_init().

131 {
132  return ast_sorcery_generic_alloc(sizeof(struct ast_mwi_mailbox_object), NULL);
133 }
#define NULL
Definition: resample.c:96
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition: sorcery.c:1728

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 910 of file res_mwi_external.c.

References ARRAY_LEN, ast_cli_unregister_multiple(), ast_sorcery_observer_remove(), ast_sorcery_unref, ast_vm_unregister(), ast_vm_functions::module_name, MWI_MAILBOX_TYPE, and NULL.

Referenced by load_module().

911 {
913 #if defined(MWI_DEBUG_CLI)
914  ast_cli_unregister_multiple(mwi_cli, ARRAY_LEN(mwi_cli));
915 #endif /* defined(MWI_DEBUG_CLI) */
917 
919  mwi_sorcery = NULL;
920 
921  return 0;
922 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
static struct ast_sorcery * mwi_sorcery
void ast_vm_unregister(const char *module_name)
Unregister the specified voicemail provider.
Definition: main/app.c:473
#define NULL
Definition: resample.c:96
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition: sorcery.h:1502
#define MWI_MAILBOX_TYPE
static const struct ast_vm_functions vm_table
static const struct ast_sorcery_observer mwi_observers
const char * module_name
The name of the module that provides the voicemail functionality.
void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Remove an observer from a specific object type.
Definition: sorcery.c:2418

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "Core external MWI resource" , .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, }
static

Definition at line 957 of file res_mwi_external.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 957 of file res_mwi_external.c.

◆ mwi_observers

const struct ast_sorcery_observer mwi_observers
static
Initial value:
= {
.created = mwi_observe_update,
.updated = mwi_observe_update,
.deleted = mwi_observe_delete,
}
static void mwi_observe_delete(const void *obj)
static void mwi_observe_update(const void *obj)

Definition at line 123 of file res_mwi_external.c.

◆ mwi_sorcery

struct ast_sorcery* mwi_sorcery
static

Definition at line 83 of file res_mwi_external.c.

◆ vm_table

const struct ast_vm_functions vm_table
static

Definition at line 529 of file res_mwi_external.c.