app_directory.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@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. /*! \file
  19. *
  20. * \brief Provide a directory of extensions
  21. *
  22. * \ingroup applications
  23. */
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/lock.h"
  31. #include "asterisk/file.h"
  32. #include "asterisk/logger.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/config.h"
  37. #include "asterisk/say.h"
  38. #include "asterisk/utils.h"
  39. static char *tdesc = "Extension Directory";
  40. static char *app = "Directory";
  41. static char *synopsis = "Provide directory of voicemail extensions";
  42. static char *descrip =
  43. " Directory(vm-context[|dial-context[|options]]): This application will present\n"
  44. "the calling channel with a directory of extensions from which they can search\n"
  45. "by name. The list of names and corresponding extensions is retrieved from the\n"
  46. "voicemail configuration file, voicemail.conf.\n"
  47. " This applicaiton will immediate exit if one of the following DTMF digits are\n"
  48. "received and the extension to jump to exists:\n"
  49. " 0 - Jump to the 'o' extension, if it exists.\n"
  50. " * - Jump to the 'a' extension, if it exists.\n\n"
  51. " Parameters:\n"
  52. " vm-context - This is the context within voicemail.conf to use for the\n"
  53. " Directory.\n"
  54. " dial-context - This is the dialplan context to use when looking for an\n"
  55. " extension that the user has selected, or when jumping to the\n"
  56. " 'o' or 'a' extension.\n\n"
  57. " Options:\n"
  58. " f - Allow the caller to enter the first name of a user in the directory\n"
  59. " instead of using the last name.\n";
  60. /* For simplicity, I'm keeping the format compatible with the voicemail config,
  61. but i'm open to suggestions for isolating it */
  62. #define VOICEMAIL_CONFIG "voicemail.conf"
  63. /* How many digits to read in */
  64. #define NUMDIGITS 3
  65. STANDARD_LOCAL_USER;
  66. LOCAL_USER_DECL;
  67. static char *convert(char *lastname)
  68. {
  69. char *tmp;
  70. int lcount = 0;
  71. tmp = malloc(NUMDIGITS + 1);
  72. if (tmp) {
  73. while((*lastname > 32) && lcount < NUMDIGITS) {
  74. switch(toupper(*lastname)) {
  75. case '1':
  76. tmp[lcount++] = '1';
  77. break;
  78. case '2':
  79. case 'A':
  80. case 'B':
  81. case 'C':
  82. tmp[lcount++] = '2';
  83. break;
  84. case '3':
  85. case 'D':
  86. case 'E':
  87. case 'F':
  88. tmp[lcount++] = '3';
  89. break;
  90. case '4':
  91. case 'G':
  92. case 'H':
  93. case 'I':
  94. tmp[lcount++] = '4';
  95. break;
  96. case '5':
  97. case 'J':
  98. case 'K':
  99. case 'L':
  100. tmp[lcount++] = '5';
  101. break;
  102. case '6':
  103. case 'M':
  104. case 'N':
  105. case 'O':
  106. tmp[lcount++] = '6';
  107. break;
  108. case '7':
  109. case 'P':
  110. case 'Q':
  111. case 'R':
  112. case 'S':
  113. tmp[lcount++] = '7';
  114. break;
  115. case '8':
  116. case 'T':
  117. case 'U':
  118. case 'V':
  119. tmp[lcount++] = '8';
  120. break;
  121. case '9':
  122. case 'W':
  123. case 'X':
  124. case 'Y':
  125. case 'Z':
  126. tmp[lcount++] = '9';
  127. break;
  128. }
  129. lastname++;
  130. }
  131. tmp[lcount] = '\0';
  132. }
  133. return tmp;
  134. }
  135. /* play name of mailbox owner.
  136. * returns: -1 for bad or missing extension
  137. * '1' for selected entry from directory
  138. * '*' for skipped entry from directory
  139. */
  140. static int play_mailbox_owner(struct ast_channel *chan, char *context, char *dialcontext, char *ext, char *name) {
  141. int res = 0;
  142. int loop = 3;
  143. char fn[256];
  144. char fn2[256];
  145. /* Check for the VoiceMail2 greeting first */
  146. snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/greet",
  147. (char *)ast_config_AST_SPOOL_DIR, context, ext);
  148. /* Otherwise, check for an old-style Voicemail greeting */
  149. snprintf(fn2, sizeof(fn2), "%s/vm/%s/greet",
  150. (char *)ast_config_AST_SPOOL_DIR, ext);
  151. if (ast_fileexists(fn, NULL, chan->language) > 0) {
  152. res = ast_streamfile(chan, fn, chan->language);
  153. if (!res) {
  154. res = ast_waitstream(chan, AST_DIGIT_ANY);
  155. }
  156. ast_stopstream(chan);
  157. } else if (ast_fileexists(fn2, NULL, chan->language) > 0) {
  158. res = ast_streamfile(chan, fn2, chan->language);
  159. if (!res) {
  160. res = ast_waitstream(chan, AST_DIGIT_ANY);
  161. }
  162. ast_stopstream(chan);
  163. } else {
  164. res = ast_say_character_str(chan, !ast_strlen_zero(name) ? name : ext,
  165. AST_DIGIT_ANY, chan->language);
  166. }
  167. while (loop) {
  168. if (!res) {
  169. res = ast_streamfile(chan, "dir-instr", chan->language);
  170. }
  171. if (!res) {
  172. res = ast_waitstream(chan, AST_DIGIT_ANY);
  173. }
  174. if (!res) {
  175. res = ast_waitfordigit(chan, 3000);
  176. }
  177. ast_stopstream(chan);
  178. if (res > -1) {
  179. switch (res) {
  180. case '1':
  181. /* Name selected */
  182. loop = 0;
  183. if (ast_goto_if_exists(chan, dialcontext, ext, 1)) {
  184. ast_log(LOG_WARNING,
  185. "Can't find extension '%s' in context '%s'. "
  186. "Did you pass the wrong context to Directory?\n",
  187. ext, dialcontext);
  188. res = -1;
  189. }
  190. break;
  191. case '*':
  192. /* Skip to next match in list */
  193. loop = 0;
  194. break;
  195. default:
  196. /* Not '1', or '*', so decrement number of tries */
  197. res = 0;
  198. loop--;
  199. break;
  200. } /* end switch */
  201. } /* end if */
  202. else {
  203. /* User hungup, so jump out now */
  204. loop = 0;
  205. }
  206. } /* end while */
  207. return(res);
  208. }
  209. static struct ast_config *realtime_directory(char *context)
  210. {
  211. struct ast_config *cfg;
  212. struct ast_config *rtdata;
  213. struct ast_category *cat;
  214. struct ast_variable *var;
  215. char *mailbox;
  216. char *fullname;
  217. char *hidefromdir;
  218. char tmp[100];
  219. /* Load flat file config. */
  220. cfg = ast_config_load(VOICEMAIL_CONFIG);
  221. if (!cfg) {
  222. /* Loading config failed. */
  223. ast_log(LOG_WARNING, "Loading config failed.\n");
  224. return NULL;
  225. }
  226. /* Get realtime entries, categorized by their mailbox number
  227. and present in the requested context */
  228. rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", "context", context, NULL);
  229. /* if there are no results, just return the entries from the config file */
  230. if (!rtdata)
  231. return cfg;
  232. /* Does the context exist within the config file? If not, make one */
  233. cat = ast_category_get(cfg, context);
  234. if (!cat) {
  235. cat = ast_category_new(context);
  236. if (!cat) {
  237. ast_log(LOG_WARNING, "Out of memory\n");
  238. ast_config_destroy(cfg);
  239. return NULL;
  240. }
  241. ast_category_append(cfg, cat);
  242. }
  243. mailbox = ast_category_browse(rtdata, NULL);
  244. while (mailbox) {
  245. fullname = ast_variable_retrieve(rtdata, mailbox, "fullname");
  246. hidefromdir = ast_variable_retrieve(rtdata, mailbox, "hidefromdir");
  247. snprintf(tmp, sizeof(tmp), "no-password,%s,hidefromdir=%s",
  248. fullname ? fullname : "",
  249. hidefromdir ? hidefromdir : "no");
  250. var = ast_variable_new(mailbox, tmp);
  251. if (var)
  252. ast_variable_append(cat, var);
  253. else
  254. ast_log(LOG_WARNING, "Out of memory adding mailbox '%s'\n", mailbox);
  255. mailbox = ast_category_browse(rtdata, mailbox);
  256. }
  257. ast_config_destroy(rtdata);
  258. return cfg;
  259. }
  260. static int do_directory(struct ast_channel *chan, struct ast_config *cfg, char *context, char *dialcontext, char digit, int last)
  261. {
  262. /* Read in the first three digits.. "digit" is the first digit, already read */
  263. char ext[NUMDIGITS + 1];
  264. char name[80] = "";
  265. struct ast_variable *v;
  266. int res;
  267. int found=0;
  268. int lastuserchoice = 0;
  269. char *start, *pos, *conv,*stringp=NULL;
  270. if (ast_strlen_zero(context)) {
  271. ast_log(LOG_WARNING,
  272. "Directory must be called with an argument "
  273. "(context in which to interpret extensions)\n");
  274. return -1;
  275. }
  276. if (digit == '0') {
  277. if (!ast_goto_if_exists(chan, chan->context, "o", 1) ||
  278. (!ast_strlen_zero(chan->macrocontext) &&
  279. !ast_goto_if_exists(chan, chan->macrocontext, "o", 1))) {
  280. return 0;
  281. } else {
  282. ast_log(LOG_WARNING, "Can't find extension 'o' in current context. "
  283. "Not Exiting the Directory!\n");
  284. res = 0;
  285. }
  286. }
  287. if (digit == '*') {
  288. if (!ast_goto_if_exists(chan, chan->context, "a", 1) ||
  289. (!ast_strlen_zero(chan->macrocontext) &&
  290. !ast_goto_if_exists(chan, chan->macrocontext, "a", 1))) {
  291. return 0;
  292. } else {
  293. ast_log(LOG_WARNING, "Can't find extension 'a' in current context. "
  294. "Not Exiting the Directory!\n");
  295. res = 0;
  296. }
  297. }
  298. memset(ext, 0, sizeof(ext));
  299. ext[0] = digit;
  300. res = 0;
  301. if (ast_readstring(chan, ext + 1, NUMDIGITS - 1, 3000, 3000, "#") < 0) res = -1;
  302. if (!res) {
  303. /* Search for all names which start with those digits */
  304. v = ast_variable_browse(cfg, context);
  305. while(v && !res) {
  306. /* Find all candidate extensions */
  307. while(v) {
  308. /* Find a candidate extension */
  309. start = strdup(v->value);
  310. if (start && !strcasestr(start, "hidefromdir=yes")) {
  311. stringp=start;
  312. strsep(&stringp, ",");
  313. pos = strsep(&stringp, ",");
  314. if (pos) {
  315. ast_copy_string(name, pos, sizeof(name));
  316. /* Grab the last name */
  317. if (last && strrchr(pos,' '))
  318. pos = strrchr(pos, ' ') + 1;
  319. conv = convert(pos);
  320. if (conv) {
  321. if (!strcmp(conv, ext)) {
  322. /* Match! */
  323. found++;
  324. free(conv);
  325. free(start);
  326. break;
  327. }
  328. free(conv);
  329. }
  330. }
  331. free(start);
  332. }
  333. v = v->next;
  334. }
  335. if (v) {
  336. /* We have a match -- play a greeting if they have it */
  337. res = play_mailbox_owner(chan, context, dialcontext, v->name, name);
  338. switch (res) {
  339. case -1:
  340. /* user pressed '1' but extension does not exist, or
  341. * user hungup
  342. */
  343. lastuserchoice = 0;
  344. break;
  345. case '1':
  346. /* user pressed '1' and extensions exists;
  347. play_mailbox_owner will already have done
  348. a goto() on the channel
  349. */
  350. lastuserchoice = res;
  351. break;
  352. case '*':
  353. /* user pressed '*' to skip something found */
  354. lastuserchoice = res;
  355. res = 0;
  356. break;
  357. default:
  358. break;
  359. }
  360. v = v->next;
  361. }
  362. }
  363. if (lastuserchoice != '1') {
  364. if (found)
  365. res = ast_streamfile(chan, "dir-nomore", chan->language);
  366. else
  367. res = ast_streamfile(chan, "dir-nomatch", chan->language);
  368. if (!res)
  369. res = 1;
  370. return res;
  371. }
  372. return 0;
  373. }
  374. return res;
  375. }
  376. static int directory_exec(struct ast_channel *chan, void *data)
  377. {
  378. int res = 0;
  379. struct localuser *u;
  380. struct ast_config *cfg;
  381. int last = 1;
  382. char *context, *dialcontext, *dirintro, *options;
  383. if (ast_strlen_zero(data)) {
  384. ast_log(LOG_WARNING, "Directory requires an argument (context[,dialcontext])\n");
  385. return -1;
  386. }
  387. LOCAL_USER_ADD(u);
  388. context = ast_strdupa(data);
  389. dialcontext = strchr(context, '|');
  390. if (dialcontext) {
  391. *dialcontext = '\0';
  392. dialcontext++;
  393. options = strchr(dialcontext, '|');
  394. if (options) {
  395. *options = '\0';
  396. options++;
  397. if (strchr(options, 'f'))
  398. last = 0;
  399. }
  400. } else
  401. dialcontext = context;
  402. cfg = realtime_directory(context);
  403. if (!cfg) {
  404. LOCAL_USER_REMOVE(u);
  405. return -1;
  406. }
  407. dirintro = ast_variable_retrieve(cfg, context, "directoryintro");
  408. if (ast_strlen_zero(dirintro))
  409. dirintro = ast_variable_retrieve(cfg, "general", "directoryintro");
  410. if (ast_strlen_zero(dirintro)) {
  411. if (last)
  412. dirintro = "dir-intro";
  413. else
  414. dirintro = "dir-intro-fn";
  415. }
  416. if (chan->_state != AST_STATE_UP)
  417. res = ast_answer(chan);
  418. for (;;) {
  419. if (!res)
  420. res = ast_streamfile(chan, dirintro, chan->language);
  421. if (!res)
  422. res = ast_waitstream(chan, AST_DIGIT_ANY);
  423. ast_stopstream(chan);
  424. if (!res)
  425. res = ast_waitfordigit(chan, 5000);
  426. if (res > 0) {
  427. res = do_directory(chan, cfg, context, dialcontext, res, last);
  428. if (res > 0) {
  429. res = ast_waitstream(chan, AST_DIGIT_ANY);
  430. ast_stopstream(chan);
  431. if (res >= 0) {
  432. continue;
  433. }
  434. }
  435. }
  436. break;
  437. }
  438. ast_config_destroy(cfg);
  439. LOCAL_USER_REMOVE(u);
  440. return res;
  441. }
  442. int unload_module(void)
  443. {
  444. int res;
  445. res = ast_unregister_application(app);
  446. STANDARD_HANGUP_LOCALUSERS;
  447. return res;
  448. }
  449. int load_module(void)
  450. {
  451. return ast_register_application(app, directory_exec, synopsis, descrip);
  452. }
  453. char *description(void)
  454. {
  455. return tdesc;
  456. }
  457. int usecount(void)
  458. {
  459. int res;
  460. STANDARD_USECOUNT(res);
  461. return res;
  462. }
  463. char *key()
  464. {
  465. return ASTERISK_GPL_KEY;
  466. }