Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions | Variables
res_stasis_mailbox.c File Reference
#include "asterisk.h"
#include "asterisk/astdb.h"
#include "asterisk/astobj2.h"
#include "asterisk/module.h"
#include "asterisk/stasis_app_impl.h"
#include "asterisk/stasis_app_mailbox.h"
#include "asterisk/res_mwi_external.h"
Include dependency graph for res_stasis_mailbox.c:

Go to the source code of this file.

Macros

#define MAILBOX_BUCKETS   37
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static struct ast_jsonmailbox_to_json (const struct ast_mwi_mailbox_object *mailbox)
 
enum stasis_mailbox_result stasis_app_mailbox_delete (const char *name)
 Delete a mailbox controlled by ARI. More...
 
enum stasis_mailbox_result stasis_app_mailbox_to_json (const char *name, struct ast_json **json)
 Convert mailbox to JSON. More...
 
int stasis_app_mailbox_update (const char *name, int old_messages, int new_messages)
 Changes the state of a mailbox. More...
 
struct ast_jsonstasis_app_mailboxes_to_json ()
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS , .description = "Stasis application mailbox support" , .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 = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_stasis,res_mwi_external" }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Macro Definition Documentation

◆ MAILBOX_BUCKETS

#define MAILBOX_BUCKETS   37

Number of hash buckets for mailboxes

Definition at line 35 of file res_stasis_mailbox.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 159 of file res_stasis_mailbox.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 159 of file res_stasis_mailbox.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 159 of file res_stasis_mailbox.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 144 of file res_stasis_mailbox.c.

References AST_MODULE_LOAD_SUCCESS.

Referenced by unload_module().

145 {
147 }

◆ mailbox_to_json()

static struct ast_json* mailbox_to_json ( const struct ast_mwi_mailbox_object mailbox)
static

Definition at line 37 of file res_stasis_mailbox.c.

References ast_json_pack(), ast_mwi_mailbox_get_id(), ast_mwi_mailbox_get_msgs_new(), and ast_mwi_mailbox_get_msgs_old().

Referenced by stasis_app_mailbox_to_json(), and stasis_app_mailboxes_to_json().

39 {
40  return ast_json_pack("{s: s, s: i, s: i}",
41  "name", ast_mwi_mailbox_get_id(mailbox),
42  "old_messages", ast_mwi_mailbox_get_msgs_old(mailbox),
43  "new_messages", ast_mwi_mailbox_get_msgs_new(mailbox));
44 }
const char * ast_mwi_mailbox_get_id(const struct ast_mwi_mailbox_object *mailbox)
Get mailbox id.
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
unsigned int ast_mwi_mailbox_get_msgs_new(const struct ast_mwi_mailbox_object *mailbox)
Get the number of new messages.
unsigned int ast_mwi_mailbox_get_msgs_old(const struct ast_mwi_mailbox_object *mailbox)
Get the number of old messages.

◆ stasis_app_mailbox_delete()

enum stasis_mailbox_result stasis_app_mailbox_delete ( const char *  name)

Delete a mailbox controlled by ARI.

Parameters
namethe name of the ARI controlled mailbox
Returns
a stasis mailbox application result

Definition at line 122 of file res_stasis_mailbox.c.

References ast_mwi_mailbox_delete(), ast_mwi_mailbox_get(), ast_mwi_mailbox_unref, mailbox, NULL, STASIS_MAILBOX_ERROR, STASIS_MAILBOX_MISSING, and STASIS_MAILBOX_OK.

Referenced by ast_ari_mailboxes_delete().

124 {
125  const struct ast_mwi_mailbox_object *mailbox;
126 
127  /* Make sure the mailbox actually exists before we delete it */
128  mailbox = ast_mwi_mailbox_get(name);
129  if (!mailbox) {
130  return STASIS_MAILBOX_MISSING;
131  }
132 
133  ast_mwi_mailbox_unref(mailbox);
134  mailbox = NULL;
135 
136  /* Now delete the mailbox */
138  return STASIS_MAILBOX_ERROR;
139  }
140 
141  return STASIS_MAILBOX_OK;
142 }
#define NULL
Definition: resample.c:96
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
const struct ast_mwi_mailbox_object * ast_mwi_mailbox_get(const char *mailbox_id)
Get matching external MWI object.
int ast_mwi_mailbox_delete(const char *mailbox_id)
Delete matching external MWI object.
static const char name[]
Definition: cdr_mysql.c:74
#define ast_mwi_mailbox_unref(mailbox)
Convienience unref function for mailbox object.

◆ stasis_app_mailbox_to_json()

enum stasis_mailbox_result stasis_app_mailbox_to_json ( const char *  name,
struct ast_json **  json 
)

Convert mailbox to JSON.

Parameters
namethe name of the mailbox
jsonIf the query is successful, this pointer at this address will be set to the JSON representation of the mailbox
Returns
stasis mailbox result code indicating success or failure and cause
NULL on error.

Definition at line 46 of file res_stasis_mailbox.c.

References ast_mwi_mailbox_get(), ast_mwi_mailbox_unref, mailbox, mailbox_to_json(), STASIS_MAILBOX_ERROR, STASIS_MAILBOX_MISSING, and STASIS_MAILBOX_OK.

Referenced by ast_ari_mailboxes_get().

48 {
49  struct ast_json *mailbox_json;
50  const struct ast_mwi_mailbox_object *mailbox;
51 
52  mailbox = ast_mwi_mailbox_get(name);
53  if (!mailbox) {
55  }
56 
57  mailbox_json = mailbox_to_json(mailbox);
58  if (!mailbox_json) {
59  ast_mwi_mailbox_unref(mailbox);
60  return STASIS_MAILBOX_ERROR;
61  }
62 
63  *json = mailbox_json;
64 
65  return STASIS_MAILBOX_OK;
66 }
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
const struct ast_mwi_mailbox_object * ast_mwi_mailbox_get(const char *mailbox_id)
Get matching external MWI object.
static struct ast_json * mailbox_to_json(const struct ast_mwi_mailbox_object *mailbox)
static const char name[]
Definition: cdr_mysql.c:74
#define ast_mwi_mailbox_unref(mailbox)
Convienience unref function for mailbox object.
Abstract JSON element (object, array, string, int, ...).

◆ stasis_app_mailbox_update()

int stasis_app_mailbox_update ( const char *  name,
int  old_messages,
int  new_messages 
)

Changes the state of a mailbox.

Note
Implicitly creates the mailbox.
Parameters
nameThe name of the ARI controlled mailbox
old_messagescount of old (read) messages in the mailbox
new_messagescount of new (unread) messages in the mailbox
Returns
0 if successful
-1 on internal error.

Definition at line 101 of file res_stasis_mailbox.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(), and mailbox.

Referenced by ast_ari_mailboxes_update().

103 {
105  int res = 0;
106 
107  mailbox = ast_mwi_mailbox_alloc(name);
108  if (!mailbox) {
109  return -1;
110  }
111  ast_mwi_mailbox_set_msgs_new(mailbox, new_messages);
112  ast_mwi_mailbox_set_msgs_old(mailbox, old_messages);
113  if (ast_mwi_mailbox_update(mailbox)) {
114  res = -1;
115  }
116 
117  ast_mwi_mailbox_unref(mailbox);
118 
119  return res;
120 }
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.
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
static const char name[]
Definition: cdr_mysql.c:74
#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.

◆ stasis_app_mailboxes_to_json()

struct ast_json* stasis_app_mailboxes_to_json ( void  )

brief Convert mailboxes to json array

Returns
JSON representation of the mailboxes
NULL on error.

Definition at line 68 of file res_stasis_mailbox.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, array(), ast_json_array_append(), ast_json_array_create(), ast_json_unref(), ast_mwi_mailbox_get_all(), ast_mwi_mailbox_unref, mailbox, mailbox_to_json(), mailboxes, and NULL.

Referenced by ast_ari_mailboxes_list().

69 {
71  struct ao2_container *mailboxes;
72  struct ao2_iterator iter;
73  const struct ast_mwi_mailbox_object *mailbox;
74 
75  if (!array) {
76  return NULL;
77  }
78 
79  mailboxes = ast_mwi_mailbox_get_all();
80  if (!mailboxes) {
81  ast_json_unref(array);
82  return NULL;
83  }
84 
85  iter = ao2_iterator_init(mailboxes, 0);
86  for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
87  struct ast_json *appending = mailbox_to_json(mailbox);
88  if (!appending || ast_json_array_append(array, appending)) {
89  /* Failed to append individual mailbox to the array. Abort. */
90  ast_json_unref(array);
91  array = NULL;
92  break;
93  }
94  }
95  ao2_iterator_destroy(&iter);
96  ao2_ref(mailboxes, -1);
97 
98  return array;
99 }
struct ao2_container * ast_mwi_mailbox_get_all(void)
Get all external MWI objects.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define NULL
Definition: resample.c:96
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
static int array(struct ast_channel *chan, const char *cmd, char *var, const char *value)
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
static struct ast_json * mailbox_to_json(const 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
Abstract JSON element (object, array, string, int, ...).
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.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 149 of file res_stasis_mailbox.c.

References AST_MODFLAG_GLOBAL_SYMBOLS, AST_MODULE_INFO(), AST_MODULE_SUPPORT_CORE, ASTERISK_GPL_KEY, and load_module().

150 {
151  return 0;
152 }

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS , .description = "Stasis application mailbox support" , .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 = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_stasis,res_mwi_external" }
static

Definition at line 159 of file res_stasis_mailbox.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 159 of file res_stasis_mailbox.c.