res_mwi_external_ami.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. #include "asterisk/module.h"
  124. #include "asterisk/res_mwi_external.h"
  125. #include "asterisk/manager.h"
  126. /* ------------------------------------------------------------------- */
  127. /*!
  128. * \internal
  129. * \brief Get the requested mailboxes.
  130. * \since 12.1.0
  131. *
  132. * \param s AMI session.
  133. * \param m AMI message.
  134. *
  135. * \retval 0 to keep AMI connection.
  136. * \retval -1 to disconnect AMI connection.
  137. */
  138. static int mwi_mailbox_get(struct mansession *s, const struct message *m)
  139. {
  140. char id_text[256];
  141. const char *id;
  142. const char *mailbox_id = astman_get_header(m, "Mailbox");
  143. const struct ast_mwi_mailbox_object *mailbox;
  144. struct ao2_container *mailboxes;
  145. unsigned count;
  146. struct ao2_iterator iter;
  147. if (ast_strlen_zero(mailbox_id)) {
  148. astman_send_error(s, m, "Missing mailbox parameter in request");
  149. return 0;
  150. }
  151. if (*mailbox_id == '/') {
  152. struct ast_str *regex_string;
  153. regex_string = ast_str_create(strlen(mailbox_id) + 1);
  154. if (!regex_string) {
  155. astman_send_error(s, m, "Memory Allocation Failure");
  156. return 0;
  157. }
  158. /* Make "/regex/" into "regex" */
  159. if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
  160. astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
  161. ast_free(regex_string);
  162. return 0;
  163. }
  164. mailboxes = ast_mwi_mailbox_get_by_regex(ast_str_buffer(regex_string));
  165. ast_free(regex_string);
  166. } else {
  167. mailboxes = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
  168. if (mailboxes) {
  169. mailbox = ast_mwi_mailbox_get(mailbox_id);
  170. if (mailbox) {
  171. if (!ao2_link(mailboxes, (void *) mailbox)) {
  172. ao2_ref(mailboxes, -1);
  173. mailboxes = NULL;
  174. }
  175. ast_mwi_mailbox_unref(mailbox);
  176. }
  177. }
  178. }
  179. if (!mailboxes) {
  180. astman_send_error(s, m, "Mailbox container creation failure");
  181. return 0;
  182. }
  183. astman_send_listack(s, m, "Mailboxes will follow", "start");
  184. id = astman_get_header(m, "ActionID");
  185. if (!ast_strlen_zero(id)) {
  186. snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
  187. } else {
  188. id_text[0] = '\0';
  189. }
  190. /* Output mailbox list. */
  191. count = 0;
  192. iter = ao2_iterator_init(mailboxes, AO2_ITERATOR_UNLINK);
  193. for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
  194. ++count;
  195. astman_append(s,
  196. "Event: MWIGet\r\n"
  197. "Mailbox: %s\r\n"
  198. "OldMessages: %u\r\n"
  199. "NewMessages: %u\r\n"
  200. "%s"
  201. "\r\n",
  202. ast_mwi_mailbox_get_id(mailbox),
  203. ast_mwi_mailbox_get_msgs_old(mailbox),
  204. ast_mwi_mailbox_get_msgs_new(mailbox),
  205. id_text);
  206. }
  207. ao2_iterator_destroy(&iter);
  208. ao2_ref(mailboxes, -1);
  209. astman_send_list_complete_start(s, m, "MWIGetComplete", count);
  210. astman_send_list_complete_end(s);
  211. return 0;
  212. }
  213. /*!
  214. * \internal
  215. * \brief Delete the requested mailboxes.
  216. * \since 12.1.0
  217. *
  218. * \param s AMI session.
  219. * \param m AMI message.
  220. *
  221. * \retval 0 to keep AMI connection.
  222. * \retval -1 to disconnect AMI connection.
  223. */
  224. static int mwi_mailbox_delete(struct mansession *s, const struct message *m)
  225. {
  226. const char *mailbox_id = astman_get_header(m, "Mailbox");
  227. if (ast_strlen_zero(mailbox_id)) {
  228. astman_send_error(s, m, "Missing mailbox parameter in request");
  229. return 0;
  230. }
  231. if (*mailbox_id == '/') {
  232. struct ast_str *regex_string;
  233. regex_string = ast_str_create(strlen(mailbox_id) + 1);
  234. if (!regex_string) {
  235. astman_send_error(s, m, "Memory Allocation Failure");
  236. return 0;
  237. }
  238. /* Make "/regex/" into "regex" */
  239. if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
  240. astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
  241. ast_free(regex_string);
  242. return 0;
  243. }
  244. ast_mwi_mailbox_delete_by_regex(ast_str_buffer(regex_string));
  245. ast_free(regex_string);
  246. } else {
  247. ast_mwi_mailbox_delete(mailbox_id);
  248. }
  249. astman_send_ack(s, m, NULL);
  250. return 0;
  251. }
  252. /*!
  253. * \internal
  254. * \brief Update the specified mailbox.
  255. * \since 12.1.0
  256. *
  257. * \param s AMI session.
  258. * \param m AMI message.
  259. *
  260. * \retval 0 to keep AMI connection.
  261. * \retval -1 to disconnect AMI connection.
  262. */
  263. static int mwi_mailbox_update(struct mansession *s, const struct message *m)
  264. {
  265. const char *mailbox_id = astman_get_header(m, "Mailbox");
  266. const char *msgs_old = astman_get_header(m, "OldMessages");
  267. const char *msgs_new = astman_get_header(m, "NewMessages");
  268. struct ast_mwi_mailbox_object *mailbox;
  269. unsigned int num_old;
  270. unsigned int num_new;
  271. if (ast_strlen_zero(mailbox_id)) {
  272. astman_send_error(s, m, "Missing mailbox parameter in request");
  273. return 0;
  274. }
  275. num_old = 0;
  276. if (!ast_strlen_zero(msgs_old)) {
  277. if (sscanf(msgs_old, "%u", &num_old) != 1) {
  278. astman_send_error_va(s, m, "Invalid OldMessages: %s", msgs_old);
  279. return 0;
  280. }
  281. }
  282. num_new = 0;
  283. if (!ast_strlen_zero(msgs_new)) {
  284. if (sscanf(msgs_new, "%u", &num_new) != 1) {
  285. astman_send_error_va(s, m, "Invalid NewMessages: %s", msgs_new);
  286. return 0;
  287. }
  288. }
  289. mailbox = ast_mwi_mailbox_alloc(mailbox_id);
  290. if (!mailbox) {
  291. astman_send_error(s, m, "Mailbox object creation failure");
  292. return 0;
  293. }
  294. /* Update external mailbox. */
  295. ast_mwi_mailbox_set_msgs_old(mailbox, num_old);
  296. ast_mwi_mailbox_set_msgs_new(mailbox, num_new);
  297. if (ast_mwi_mailbox_update(mailbox)) {
  298. astman_send_error(s, m, "Update attempt failed");
  299. } else {
  300. astman_send_ack(s, m, NULL);
  301. }
  302. ast_mwi_mailbox_unref(mailbox);
  303. return 0;
  304. }
  305. static int unload_module(void)
  306. {
  307. ast_manager_unregister("MWIGet");
  308. ast_manager_unregister("MWIDelete");
  309. ast_manager_unregister("MWIUpdate");
  310. return 0;
  311. }
  312. static int load_module(void)
  313. {
  314. int res;
  315. res = 0;
  316. res |= ast_manager_register_xml("MWIGet", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING, mwi_mailbox_get);
  317. res |= ast_manager_register_xml("MWIDelete", EVENT_FLAG_CALL, mwi_mailbox_delete);
  318. res |= ast_manager_register_xml("MWIUpdate", EVENT_FLAG_CALL, mwi_mailbox_update);
  319. if (res) {
  320. unload_module();
  321. return AST_MODULE_LOAD_DECLINE;
  322. }
  323. return AST_MODULE_LOAD_SUCCESS;
  324. }
  325. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "AMI support for external MWI",
  326. .support_level = AST_MODULE_SUPPORT_CORE,
  327. .load = load_module,
  328. .unload = unload_module,
  329. .load_pri = AST_MODPRI_CHANNEL_DEPEND - 5,
  330. .requires = "res_mwi_external",
  331. );