res_mwi_external_ami.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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_append(s,
  211. "Event: MWIGetComplete\r\n"
  212. "EventList: Complete\r\n"
  213. "ListItems: %u\r\n"
  214. "%s"
  215. "\r\n", count, id_text);
  216. return 0;
  217. }
  218. /*!
  219. * \internal
  220. * \brief Delete the requested mailboxes.
  221. * \since 12.1.0
  222. *
  223. * \param s AMI session.
  224. * \param m AMI message.
  225. *
  226. * \retval 0 to keep AMI connection.
  227. * \retval -1 to disconnect AMI connection.
  228. */
  229. static int mwi_mailbox_delete(struct mansession *s, const struct message *m)
  230. {
  231. const char *mailbox_id = astman_get_header(m, "Mailbox");
  232. if (ast_strlen_zero(mailbox_id)) {
  233. astman_send_error(s, m, "Missing mailbox parameter in request");
  234. return 0;
  235. }
  236. if (*mailbox_id == '/') {
  237. struct ast_str *regex_string;
  238. regex_string = ast_str_create(strlen(mailbox_id) + 1);
  239. if (!regex_string) {
  240. astman_send_error(s, m, "Memory Allocation Failure");
  241. return 0;
  242. }
  243. /* Make "/regex/" into "regex" */
  244. if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
  245. astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
  246. ast_free(regex_string);
  247. return 0;
  248. }
  249. ast_mwi_mailbox_delete_by_regex(ast_str_buffer(regex_string));
  250. ast_free(regex_string);
  251. } else {
  252. ast_mwi_mailbox_delete(mailbox_id);
  253. }
  254. astman_send_ack(s, m, NULL);
  255. return 0;
  256. }
  257. /*!
  258. * \internal
  259. * \brief Update the specified mailbox.
  260. * \since 12.1.0
  261. *
  262. * \param s AMI session.
  263. * \param m AMI message.
  264. *
  265. * \retval 0 to keep AMI connection.
  266. * \retval -1 to disconnect AMI connection.
  267. */
  268. static int mwi_mailbox_update(struct mansession *s, const struct message *m)
  269. {
  270. const char *mailbox_id = astman_get_header(m, "Mailbox");
  271. const char *msgs_old = astman_get_header(m, "OldMessages");
  272. const char *msgs_new = astman_get_header(m, "NewMessages");
  273. struct ast_mwi_mailbox_object *mailbox;
  274. unsigned int num_old;
  275. unsigned int num_new;
  276. if (ast_strlen_zero(mailbox_id)) {
  277. astman_send_error(s, m, "Missing mailbox parameter in request");
  278. return 0;
  279. }
  280. num_old = 0;
  281. if (!ast_strlen_zero(msgs_old)) {
  282. if (sscanf(msgs_old, "%u", &num_old) != 1) {
  283. astman_send_error_va(s, m, "Invalid OldMessages: %s", msgs_old);
  284. return 0;
  285. }
  286. }
  287. num_new = 0;
  288. if (!ast_strlen_zero(msgs_new)) {
  289. if (sscanf(msgs_new, "%u", &num_new) != 1) {
  290. astman_send_error_va(s, m, "Invalid NewMessages: %s", msgs_new);
  291. return 0;
  292. }
  293. }
  294. mailbox = ast_mwi_mailbox_alloc(mailbox_id);
  295. if (!mailbox) {
  296. astman_send_error(s, m, "Mailbox object creation failure");
  297. return 0;
  298. }
  299. /* Update external mailbox. */
  300. ast_mwi_mailbox_set_msgs_old(mailbox, num_old);
  301. ast_mwi_mailbox_set_msgs_new(mailbox, num_new);
  302. if (ast_mwi_mailbox_update(mailbox)) {
  303. astman_send_error(s, m, "Update attempt failed");
  304. } else {
  305. astman_send_ack(s, m, NULL);
  306. }
  307. ast_mwi_mailbox_unref(mailbox);
  308. return 0;
  309. }
  310. static int unload_module(void)
  311. {
  312. ast_manager_unregister("MWIGet");
  313. ast_manager_unregister("MWIDelete");
  314. ast_manager_unregister("MWIUpdate");
  315. /* Must be done last */
  316. ast_mwi_external_unref();
  317. return 0;
  318. }
  319. static int load_module(void)
  320. {
  321. int res;
  322. /* Must be done first */
  323. ast_mwi_external_ref();
  324. res = 0;
  325. res |= ast_manager_register_xml_core("MWIGet", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING, mwi_mailbox_get);
  326. res |= ast_manager_register_xml_core("MWIDelete", EVENT_FLAG_CALL, mwi_mailbox_delete);
  327. res |= ast_manager_register_xml_core("MWIUpdate", EVENT_FLAG_CALL, mwi_mailbox_update);
  328. if (res) {
  329. unload_module();
  330. return AST_MODULE_LOAD_DECLINE;
  331. }
  332. return AST_MODULE_LOAD_SUCCESS;
  333. }
  334. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "AMI support for external MWI",
  335. .support_level = AST_MODULE_SUPPORT_CORE,
  336. .load = load_module,
  337. .unload = unload_module,
  338. );