res_mwi_external_ami.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Richard Mudgett <rmudgett@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. * \file
  20. * \brief AMI wrapper for external MWI.
  21. *
  22. * \author Richard Mudgett <rmudgett@digium.com>
  23. *
  24. * See Also:
  25. * \arg \ref AstCREDITS
  26. */
  27. /*** MODULEINFO
  28. <depend>res_mwi_external</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. /*** DOCUMENTATION
  32. <manager name="MWIGet" language="en_US">
  33. <synopsis>
  34. Get selected mailboxes with message counts.
  35. </synopsis>
  36. <syntax>
  37. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  38. <parameter name="Mailbox" required="true">
  39. <para>Mailbox ID in the form of
  40. /<replaceable>regex</replaceable>/ for all mailboxes matching the regular
  41. expression. Otherwise it is for a specific mailbox.</para>
  42. </parameter>
  43. </syntax>
  44. <description>
  45. <para>Get a list of mailboxes with their message counts.</para>
  46. </description>
  47. </manager>
  48. <managerEvent language="en_US" name="MWIGet">
  49. <managerEventInstance class="EVENT_FLAG_REPORTING">
  50. <synopsis>
  51. Raised in response to a MWIGet command.
  52. </synopsis>
  53. <syntax>
  54. <parameter name="ActionID" required="false"/>
  55. <parameter name="Mailbox">
  56. <para>Specific mailbox ID.</para>
  57. </parameter>
  58. <parameter name="OldMessages">
  59. <para>The number of old messages in the mailbox.</para>
  60. </parameter>
  61. <parameter name="NewMessages">
  62. <para>The number of new messages in the mailbox.</para>
  63. </parameter>
  64. </syntax>
  65. <see-also>
  66. <ref type="manager">MWIGet</ref>
  67. </see-also>
  68. </managerEventInstance>
  69. </managerEvent>
  70. <managerEvent language="en_US" name="MWIGetComplete">
  71. <managerEventInstance class="EVENT_FLAG_REPORTING">
  72. <synopsis>
  73. Raised in response to a MWIGet command.
  74. </synopsis>
  75. <syntax>
  76. <parameter name="ActionID" required="false"/>
  77. <parameter name="EventList" />
  78. <parameter name="ListItems">
  79. <para>The number of mailboxes reported.</para>
  80. </parameter>
  81. </syntax>
  82. <see-also>
  83. <ref type="manager">MWIGet</ref>
  84. </see-also>
  85. </managerEventInstance>
  86. </managerEvent>
  87. <manager name="MWIDelete" language="en_US">
  88. <synopsis>
  89. Delete selected mailboxes.
  90. </synopsis>
  91. <syntax>
  92. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  93. <xi:include xpointer="xpointer(/docs/manager[@name='MWIGet']/syntax/parameter[@name='Mailbox'])" />
  94. </syntax>
  95. <description>
  96. <para>Delete the specified mailboxes.</para>
  97. </description>
  98. </manager>
  99. <manager name="MWIUpdate" language="en_US">
  100. <synopsis>
  101. Update the mailbox message counts.
  102. </synopsis>
  103. <syntax>
  104. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  105. <parameter name="Mailbox" required="true">
  106. <para>Specific mailbox ID.</para>
  107. </parameter>
  108. <parameter name="OldMessages">
  109. <para>The number of old messages in the mailbox. Defaults
  110. to zero if missing.</para>
  111. </parameter>
  112. <parameter name="NewMessages">
  113. <para>The number of new messages in the mailbox. Defaults
  114. to zero if missing.</para>
  115. </parameter>
  116. </syntax>
  117. <description>
  118. <para>Update the mailbox message counts.</para>
  119. </description>
  120. </manager>
  121. ***/
  122. #include "asterisk.h"
  123. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  124. #include "asterisk/module.h"
  125. #include "asterisk/res_mwi_external.h"
  126. #include "asterisk/manager.h"
  127. /* ------------------------------------------------------------------- */
  128. /*!
  129. * \internal
  130. * \brief Get the requested mailboxes.
  131. * \since 12.1.0
  132. *
  133. * \param s AMI session.
  134. * \param m AMI message.
  135. *
  136. * \retval 0 to keep AMI connection.
  137. * \retval -1 to disconnect AMI connection.
  138. */
  139. static int mwi_mailbox_get(struct mansession *s, const struct message *m)
  140. {
  141. char id_text[256];
  142. const char *id;
  143. const char *mailbox_id = astman_get_header(m, "Mailbox");
  144. const struct ast_mwi_mailbox_object *mailbox;
  145. struct ao2_container *mailboxes;
  146. unsigned count;
  147. struct ao2_iterator iter;
  148. if (ast_strlen_zero(mailbox_id)) {
  149. astman_send_error(s, m, "Missing mailbox parameter in request");
  150. return 0;
  151. }
  152. if (*mailbox_id == '/') {
  153. struct ast_str *regex_string;
  154. regex_string = ast_str_create(strlen(mailbox_id) + 1);
  155. if (!regex_string) {
  156. astman_send_error(s, m, "Memory Allocation Failure");
  157. return 0;
  158. }
  159. /* Make "/regex/" into "regex" */
  160. if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
  161. astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
  162. ast_free(regex_string);
  163. return 0;
  164. }
  165. mailboxes = ast_mwi_mailbox_get_by_regex(ast_str_buffer(regex_string));
  166. ast_free(regex_string);
  167. } else {
  168. mailboxes = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
  169. if (mailboxes) {
  170. mailbox = ast_mwi_mailbox_get(mailbox_id);
  171. if (mailbox) {
  172. if (!ao2_link(mailboxes, (void *) mailbox)) {
  173. ao2_ref(mailboxes, -1);
  174. mailboxes = NULL;
  175. }
  176. ast_mwi_mailbox_unref(mailbox);
  177. }
  178. }
  179. }
  180. if (!mailboxes) {
  181. astman_send_error(s, m, "Mailbox container creation failure");
  182. return 0;
  183. }
  184. astman_send_listack(s, m, "Mailboxes will follow", "start");
  185. id = astman_get_header(m, "ActionID");
  186. if (!ast_strlen_zero(id)) {
  187. snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
  188. } else {
  189. id_text[0] = '\0';
  190. }
  191. /* Output mailbox list. */
  192. count = 0;
  193. iter = ao2_iterator_init(mailboxes, AO2_ITERATOR_UNLINK);
  194. for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
  195. ++count;
  196. astman_append(s,
  197. "Event: MWIGet\r\n"
  198. "Mailbox: %s\r\n"
  199. "OldMessages: %u\r\n"
  200. "NewMessages: %u\r\n"
  201. "%s"
  202. "\r\n",
  203. ast_mwi_mailbox_get_id(mailbox),
  204. ast_mwi_mailbox_get_msgs_old(mailbox),
  205. ast_mwi_mailbox_get_msgs_new(mailbox),
  206. id_text);
  207. }
  208. ao2_iterator_destroy(&iter);
  209. ao2_ref(mailboxes, -1);
  210. astman_send_list_complete_start(s, m, "MWIGetComplete", count);
  211. astman_send_list_complete_end(s);
  212. return 0;
  213. }
  214. /*!
  215. * \internal
  216. * \brief Delete the requested mailboxes.
  217. * \since 12.1.0
  218. *
  219. * \param s AMI session.
  220. * \param m AMI message.
  221. *
  222. * \retval 0 to keep AMI connection.
  223. * \retval -1 to disconnect AMI connection.
  224. */
  225. static int mwi_mailbox_delete(struct mansession *s, const struct message *m)
  226. {
  227. const char *mailbox_id = astman_get_header(m, "Mailbox");
  228. if (ast_strlen_zero(mailbox_id)) {
  229. astman_send_error(s, m, "Missing mailbox parameter in request");
  230. return 0;
  231. }
  232. if (*mailbox_id == '/') {
  233. struct ast_str *regex_string;
  234. regex_string = ast_str_create(strlen(mailbox_id) + 1);
  235. if (!regex_string) {
  236. astman_send_error(s, m, "Memory Allocation Failure");
  237. return 0;
  238. }
  239. /* Make "/regex/" into "regex" */
  240. if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
  241. astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
  242. ast_free(regex_string);
  243. return 0;
  244. }
  245. ast_mwi_mailbox_delete_by_regex(ast_str_buffer(regex_string));
  246. ast_free(regex_string);
  247. } else {
  248. ast_mwi_mailbox_delete(mailbox_id);
  249. }
  250. astman_send_ack(s, m, NULL);
  251. return 0;
  252. }
  253. /*!
  254. * \internal
  255. * \brief Update the specified mailbox.
  256. * \since 12.1.0
  257. *
  258. * \param s AMI session.
  259. * \param m AMI message.
  260. *
  261. * \retval 0 to keep AMI connection.
  262. * \retval -1 to disconnect AMI connection.
  263. */
  264. static int mwi_mailbox_update(struct mansession *s, const struct message *m)
  265. {
  266. const char *mailbox_id = astman_get_header(m, "Mailbox");
  267. const char *msgs_old = astman_get_header(m, "OldMessages");
  268. const char *msgs_new = astman_get_header(m, "NewMessages");
  269. struct ast_mwi_mailbox_object *mailbox;
  270. unsigned int num_old;
  271. unsigned int num_new;
  272. if (ast_strlen_zero(mailbox_id)) {
  273. astman_send_error(s, m, "Missing mailbox parameter in request");
  274. return 0;
  275. }
  276. num_old = 0;
  277. if (!ast_strlen_zero(msgs_old)) {
  278. if (sscanf(msgs_old, "%u", &num_old) != 1) {
  279. astman_send_error_va(s, m, "Invalid OldMessages: %s", msgs_old);
  280. return 0;
  281. }
  282. }
  283. num_new = 0;
  284. if (!ast_strlen_zero(msgs_new)) {
  285. if (sscanf(msgs_new, "%u", &num_new) != 1) {
  286. astman_send_error_va(s, m, "Invalid NewMessages: %s", msgs_new);
  287. return 0;
  288. }
  289. }
  290. mailbox = ast_mwi_mailbox_alloc(mailbox_id);
  291. if (!mailbox) {
  292. astman_send_error(s, m, "Mailbox object creation failure");
  293. return 0;
  294. }
  295. /* Update external mailbox. */
  296. ast_mwi_mailbox_set_msgs_old(mailbox, num_old);
  297. ast_mwi_mailbox_set_msgs_new(mailbox, num_new);
  298. if (ast_mwi_mailbox_update(mailbox)) {
  299. astman_send_error(s, m, "Update attempt failed");
  300. } else {
  301. astman_send_ack(s, m, NULL);
  302. }
  303. ast_mwi_mailbox_unref(mailbox);
  304. return 0;
  305. }
  306. static int unload_module(void)
  307. {
  308. ast_manager_unregister("MWIGet");
  309. ast_manager_unregister("MWIDelete");
  310. ast_manager_unregister("MWIUpdate");
  311. /* Must be done last */
  312. ast_mwi_external_unref();
  313. return 0;
  314. }
  315. static int load_module(void)
  316. {
  317. int res;
  318. /* Must be done first */
  319. ast_mwi_external_ref();
  320. res = 0;
  321. res |= ast_manager_register_xml("MWIGet", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING, mwi_mailbox_get);
  322. res |= ast_manager_register_xml("MWIDelete", EVENT_FLAG_CALL, mwi_mailbox_delete);
  323. res |= ast_manager_register_xml("MWIUpdate", EVENT_FLAG_CALL, mwi_mailbox_update);
  324. if (res) {
  325. unload_module();
  326. return AST_MODULE_LOAD_DECLINE;
  327. }
  328. return AST_MODULE_LOAD_SUCCESS;
  329. }
  330. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "AMI support for external MWI",
  331. .support_level = AST_MODULE_SUPPORT_CORE,
  332. .load = load_module,
  333. .unload = unload_module,
  334. );