app_hasnewvoicemail.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * HasVoicemail application
  5. * Changes Copyright (c) 2004 Todd Freeman <freeman@andrews.edu>
  6. *
  7. * 95% based on HasNewVoicemail by:
  8. *
  9. * Copyright (c) 2003 Tilghman Lesher. All rights reserved.
  10. *
  11. * Tilghman Lesher <asterisk-hasnewvoicemail-app@the-tilghman.com>
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  25. * EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <sys/types.h>
  34. #include <asterisk/file.h>
  35. #include <asterisk/logger.h>
  36. #include <asterisk/channel.h>
  37. #include <asterisk/pbx.h>
  38. #include <asterisk/module.h>
  39. #include <asterisk/lock.h>
  40. #include <asterisk/utils.h>
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <string.h>
  44. #include <stdlib.h>
  45. #include <dirent.h>
  46. #include "../astconf.h"
  47. static char *tdesc = "Indicator for whether a voice mailbox has messages in a given folder.";
  48. static char *app_hasvoicemail = "HasVoicemail";
  49. static char *hasvoicemail_synopsis = "Conditionally branches to priority + 101";
  50. static char *hasvoicemail_descrip =
  51. "HasVoicemail(vmbox[@context][:folder][|varname])\n"
  52. " Branches to priority + 101, if there is voicemail in folder indicated."
  53. " Optionally sets <varname> to the number of messages in that folder."
  54. " Assumes folder of INBOX if not specified.\n";
  55. static char *app_hasnewvoicemail = "HasNewVoicemail";
  56. static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101";
  57. static char *hasnewvoicemail_descrip =
  58. "HasNewVoicemail(vmbox[@context][|varname])\n"
  59. " Branches to priority + 101, if there is voicemail in folder INBOX."
  60. " Optionally sets <varname> to the number of messages in that folder.\n";
  61. STANDARD_LOCAL_USER;
  62. LOCAL_USER_DECL;
  63. static int hasvoicemail_exec(struct ast_channel *chan, void *data)
  64. {
  65. int res=0;
  66. struct localuser *u;
  67. char vmpath[256], *temps, *input, *varname = NULL, *vmbox, *vmfolder = "INBOX", *context = "default";
  68. DIR *vmdir;
  69. struct dirent *vment;
  70. int vmcount = 0;
  71. if (!data) {
  72. ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[@context][:folder]|varname)\n");
  73. return -1;
  74. }
  75. LOCAL_USER_ADD(u);
  76. input = ast_strdupa((char *)data);
  77. if (input) {
  78. temps = input;
  79. if ((temps = strsep(&input, "|"))) {
  80. if (input && !ast_strlen_zero(input))
  81. varname = input;
  82. input = temps;
  83. }
  84. if ((temps = strsep(&input, ":"))) {
  85. if (input && !ast_strlen_zero(input))
  86. vmfolder = input;
  87. input = temps;
  88. }
  89. if ((vmbox = strsep(&input, "@")))
  90. if (input && !ast_strlen_zero(input))
  91. context = input;
  92. if (!vmbox)
  93. vmbox = input;
  94. snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, vmbox, vmfolder);
  95. if (!(vmdir = opendir(vmpath))) {
  96. ast_log(LOG_NOTICE, "Voice mailbox %s at %s does not exist\n", vmbox, vmpath);
  97. } else {
  98. /* No matter what the format of VM, there will always be a .txt file for each message. */
  99. while ((vment = readdir(vmdir)))
  100. if (!strncmp(vment->d_name + 7,".txt",4))
  101. vmcount++;
  102. closedir(vmdir);
  103. }
  104. /* Set the count in the channel variable */
  105. if (varname) {
  106. char tmp[12];
  107. snprintf(tmp, sizeof(tmp), "%d", vmcount);
  108. pbx_builtin_setvar_helper(chan, varname, tmp);
  109. }
  110. if (vmcount > 0) {
  111. /* Branch to the next extension */
  112. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
  113. chan->priority += 100;
  114. } else
  115. ast_log(LOG_WARNING, "VM box %s@%s has new voicemail, but extension %s, priority %d doesn't exist\n", vmbox, context, chan->exten, chan->priority + 101);
  116. }
  117. } else {
  118. ast_log(LOG_ERROR, "Out of memory error\n");
  119. }
  120. LOCAL_USER_REMOVE(u);
  121. return res;
  122. }
  123. int unload_module(void)
  124. {
  125. int res;
  126. STANDARD_HANGUP_LOCALUSERS;
  127. res = ast_unregister_application(app_hasvoicemail);
  128. res |= ast_unregister_application(app_hasnewvoicemail);
  129. return res;
  130. }
  131. int load_module(void)
  132. {
  133. int res;
  134. res = ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
  135. res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
  136. return res;
  137. }
  138. char *description(void)
  139. {
  140. return tdesc;
  141. }
  142. int usecount(void)
  143. {
  144. int res;
  145. STANDARD_USECOUNT(res);
  146. return res;
  147. }
  148. char *key()
  149. {
  150. return ASTERISK_GPL_KEY;
  151. }