app_directory.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include <ctype.h>
  32. #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
  33. #include "asterisk/file.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/say.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/utils.h"
  39. /*** DOCUMENTATION
  40. <application name="Directory" language="en_US">
  41. <synopsis>
  42. Provide directory of voicemail extensions.
  43. </synopsis>
  44. <syntax>
  45. <parameter name="vm-context">
  46. <para>This is the context within voicemail.conf to use for the Directory. If not
  47. specified and <literal>searchcontexts=no</literal> in
  48. <filename>voicemail.conf</filename>, then <literal>default</literal>
  49. will be assumed.</para>
  50. </parameter>
  51. <parameter name="dial-context" required="false">
  52. <para>This is the dialplan context to use when looking for an
  53. extension that the user has selected, or when jumping to the
  54. <literal>o</literal> or <literal>a</literal> extension. If not
  55. specified, the current context will be used.</para>
  56. </parameter>
  57. <parameter name="options" required="false">
  58. <optionlist>
  59. <option name="e">
  60. <para>In addition to the name, also read the extension number to the
  61. caller before presenting dialing options.</para>
  62. </option>
  63. <option name="f">
  64. <para>Allow the caller to enter the first name of a user in the
  65. directory instead of using the last name. If specified, the
  66. optional number argument will be used for the number of
  67. characters the user should enter.</para>
  68. <argument name="n" required="true" />
  69. </option>
  70. <option name="l">
  71. <para>Allow the caller to enter the last name of a user in the
  72. directory. This is the default. If specified, the
  73. optional number argument will be used for the number of
  74. characters the user should enter.</para>
  75. <argument name="n" required="true" />
  76. </option>
  77. <option name="b">
  78. <para> Allow the caller to enter either the first or the last name
  79. of a user in the directory. If specified, the optional number
  80. argument will be used for the number of characters the user should enter.</para>
  81. <argument name="n" required="true" />
  82. </option>
  83. <option name="a">
  84. <para>Allow the caller to additionally enter an alias for a user in the
  85. directory. This option must be specified in addition to the
  86. <literal>f</literal>, <literal>l</literal>, or <literal>b</literal>
  87. option.</para>
  88. </option>
  89. <option name="m">
  90. <para>Instead of reading each name sequentially and asking for
  91. confirmation, create a menu of up to 8 names.</para>
  92. </option>
  93. <option name="n">
  94. <para>Read digits even if the channel is not answered.</para>
  95. </option>
  96. <option name="p">
  97. <para>Pause for n milliseconds after the digits are typed. This is
  98. helpful for people with cellphones, who are not holding the
  99. receiver to their ear while entering DTMF.</para>
  100. <argument name="n" required="true" />
  101. </option>
  102. </optionlist>
  103. <note><para>Only one of the <replaceable>f</replaceable>, <replaceable>l</replaceable>, or <replaceable>b</replaceable>
  104. options may be specified. <emphasis>If more than one is specified</emphasis>, then Directory will act as
  105. if <replaceable>b</replaceable> was specified. The number
  106. of characters for the user to type defaults to <literal>3</literal>.</para></note>
  107. </parameter>
  108. </syntax>
  109. <description>
  110. <para>This application will present the calling channel with a directory of extensions from which they can search
  111. by name. The list of names and corresponding extensions is retrieved from the
  112. voicemail configuration file, <filename>voicemail.conf</filename>.</para>
  113. <para>This application will immediately exit if one of the following DTMF digits are
  114. received and the extension to jump to exists:</para>
  115. <para><literal>0</literal> - Jump to the 'o' extension, if it exists.</para>
  116. <para><literal>*</literal> - Jump to the 'a' extension, if it exists.</para>
  117. <para>This application will set the following channel variable before completion:</para>
  118. <variablelist>
  119. <variable name="DIRECTORY_RESULT">
  120. <para>Reason Directory application exited.</para>
  121. <value name="OPERATOR">User requested operator</value>
  122. <value name="ASSISTANT">User requested assistant</value>
  123. <value name="TIMEOUT">User allowed DTMF wait duration to pass without sending DTMF</value>
  124. <value name="HANGUP">The channel hung up before the application finished</value>
  125. <value name="SELECTED">User selected a user to call from the directory</value>
  126. <value name="USEREXIT">User exited with '#' during selection</value>
  127. <value name="FAILED">The application failed</value>
  128. </variable>
  129. </variablelist>
  130. </description>
  131. </application>
  132. ***/
  133. static const char app[] = "Directory";
  134. /* For simplicity, I'm keeping the format compatible with the voicemail config,
  135. but i'm open to suggestions for isolating it */
  136. #define VOICEMAIL_CONFIG "voicemail.conf"
  137. enum {
  138. OPT_LISTBYFIRSTNAME = (1 << 0),
  139. OPT_SAYEXTENSION = (1 << 1),
  140. OPT_FROMVOICEMAIL = (1 << 2),
  141. OPT_SELECTFROMMENU = (1 << 3),
  142. OPT_LISTBYLASTNAME = (1 << 4),
  143. OPT_LISTBYEITHER = OPT_LISTBYFIRSTNAME | OPT_LISTBYLASTNAME,
  144. OPT_PAUSE = (1 << 5),
  145. OPT_NOANSWER = (1 << 6),
  146. OPT_ALIAS = (1 << 7),
  147. };
  148. enum {
  149. OPT_ARG_FIRSTNAME = 0,
  150. OPT_ARG_LASTNAME = 1,
  151. OPT_ARG_EITHER = 2,
  152. OPT_ARG_PAUSE = 3,
  153. /* This *must* be the last value in this enum! */
  154. OPT_ARG_ARRAY_SIZE = 4,
  155. };
  156. struct directory_item {
  157. char exten[AST_MAX_EXTENSION + 1];
  158. char name[AST_MAX_EXTENSION + 1];
  159. char context[AST_MAX_CONTEXT + 1];
  160. char key[50]; /* Text to order items. Either lastname+firstname or firstname+lastname */
  161. AST_LIST_ENTRY(directory_item) entry;
  162. };
  163. AST_APP_OPTIONS(directory_app_options, {
  164. AST_APP_OPTION_ARG('f', OPT_LISTBYFIRSTNAME, OPT_ARG_FIRSTNAME),
  165. AST_APP_OPTION_ARG('l', OPT_LISTBYLASTNAME, OPT_ARG_LASTNAME),
  166. AST_APP_OPTION_ARG('b', OPT_LISTBYEITHER, OPT_ARG_EITHER),
  167. AST_APP_OPTION_ARG('p', OPT_PAUSE, OPT_ARG_PAUSE),
  168. AST_APP_OPTION('e', OPT_SAYEXTENSION),
  169. AST_APP_OPTION('v', OPT_FROMVOICEMAIL),
  170. AST_APP_OPTION('m', OPT_SELECTFROMMENU),
  171. AST_APP_OPTION('n', OPT_NOANSWER),
  172. AST_APP_OPTION('a', OPT_ALIAS),
  173. });
  174. static int compare(const char *text, const char *template)
  175. {
  176. char digit;
  177. if (ast_strlen_zero(text)) {
  178. return -1;
  179. }
  180. while (*template) {
  181. digit = toupper(*text++);
  182. switch (digit) {
  183. case 0:
  184. return -1;
  185. case '1':
  186. digit = '1';
  187. break;
  188. case '2':
  189. case 'A':
  190. case 'B':
  191. case 'C':
  192. digit = '2';
  193. break;
  194. case '3':
  195. case 'D':
  196. case 'E':
  197. case 'F':
  198. digit = '3';
  199. break;
  200. case '4':
  201. case 'G':
  202. case 'H':
  203. case 'I':
  204. digit = '4';
  205. break;
  206. case '5':
  207. case 'J':
  208. case 'K':
  209. case 'L':
  210. digit = '5';
  211. break;
  212. case '6':
  213. case 'M':
  214. case 'N':
  215. case 'O':
  216. digit = '6';
  217. break;
  218. case '7':
  219. case 'P':
  220. case 'Q':
  221. case 'R':
  222. case 'S':
  223. digit = '7';
  224. break;
  225. case '8':
  226. case 'T':
  227. case 'U':
  228. case 'V':
  229. digit = '8';
  230. break;
  231. case '9':
  232. case 'W':
  233. case 'X':
  234. case 'Y':
  235. case 'Z':
  236. digit = '9';
  237. break;
  238. default:
  239. if (digit > ' ')
  240. return -1;
  241. continue;
  242. }
  243. if (*template++ != digit)
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. static int goto_exten(struct ast_channel *chan, const char *dialcontext, char *ext)
  249. {
  250. if (!ast_goto_if_exists(chan, S_OR(dialcontext, ast_channel_context(chan)), ext, 1) ||
  251. (!ast_strlen_zero(ast_channel_macrocontext(chan)) &&
  252. !ast_goto_if_exists(chan, ast_channel_macrocontext(chan), ext, 1))) {
  253. return 0;
  254. } else {
  255. ast_log(LOG_WARNING, "Can't find extension '%s' in current context. "
  256. "Not Exiting the Directory!\n", ext);
  257. return -1;
  258. }
  259. }
  260. /* play name of mailbox owner.
  261. * returns: -1 for bad or missing extension
  262. * '1' for selected entry from directory
  263. * '*' for skipped entry from directory
  264. */
  265. static int play_mailbox_owner(struct ast_channel *chan, const char *context,
  266. const char *ext, const char *name, struct ast_flags *flags)
  267. {
  268. int res = 0;
  269. char *mailbox_id;
  270. mailbox_id = ast_alloca(strlen(ext) + strlen(context) + 2);
  271. sprintf(mailbox_id, "%s@%s", ext, context); /* Safe */
  272. res = ast_app_sayname(chan, mailbox_id);
  273. if (res >= 0) {
  274. ast_stopstream(chan);
  275. /* If Option 'e' was specified, also read the extension number with the name */
  276. if (ast_test_flag(flags, OPT_SAYEXTENSION)) {
  277. ast_stream_and_wait(chan, "vm-extension", AST_DIGIT_ANY);
  278. res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, ast_channel_language(chan), AST_SAY_CASE_NONE);
  279. }
  280. } else {
  281. res = ast_say_character_str(chan, S_OR(name, ext), AST_DIGIT_ANY, ast_channel_language(chan), AST_SAY_CASE_NONE);
  282. if (!ast_strlen_zero(name) && ast_test_flag(flags, OPT_SAYEXTENSION)) {
  283. ast_stream_and_wait(chan, "vm-extension", AST_DIGIT_ANY);
  284. res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, ast_channel_language(chan), AST_SAY_CASE_NONE);
  285. }
  286. }
  287. return res;
  288. }
  289. static int select_entry(struct ast_channel *chan, const char *dialcontext, const struct directory_item *item, struct ast_flags *flags)
  290. {
  291. ast_debug(1, "Selecting '%s' - %s@%s\n", item->name, item->exten, S_OR(dialcontext, item->context));
  292. if (ast_test_flag(flags, OPT_FROMVOICEMAIL)) {
  293. /* We still want to set the exten though */
  294. ast_channel_exten_set(chan, item->exten);
  295. } else if (ast_goto_if_exists(chan, S_OR(dialcontext, item->context), item->exten, 1)) {
  296. ast_log(LOG_WARNING,
  297. "Can't find extension '%s' in context '%s'. "
  298. "Did you pass the wrong context to Directory?\n",
  299. item->exten, S_OR(dialcontext, item->context));
  300. return -1;
  301. }
  302. pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "SELECTED");
  303. return 0;
  304. }
  305. static int select_item_pause(struct ast_channel *chan, struct ast_flags *flags, char *opts[])
  306. {
  307. int res = 0, opt_pause = 0;
  308. if (ast_test_flag(flags, OPT_PAUSE) && !ast_strlen_zero(opts[OPT_ARG_PAUSE])) {
  309. opt_pause = atoi(opts[OPT_ARG_PAUSE]);
  310. if (opt_pause > 3000) {
  311. opt_pause = 3000;
  312. }
  313. res = ast_waitfordigit(chan, opt_pause);
  314. }
  315. return res;
  316. }
  317. static int select_item_seq(struct ast_channel *chan, struct directory_item **items, int count, const char *dialcontext, struct ast_flags *flags, char *opts[])
  318. {
  319. struct directory_item *item, **ptr;
  320. int i, res, loop;
  321. /* option p(n): cellphone pause option */
  322. /* allow early press of selection key */
  323. res = select_item_pause(chan, flags, opts);
  324. for (ptr = items, i = 0; i < count; i++, ptr++) {
  325. item = *ptr;
  326. for (loop = 3 ; loop > 0; loop--) {
  327. if (!res)
  328. res = play_mailbox_owner(chan, item->context, item->exten, item->name, flags);
  329. if (!res)
  330. res = ast_stream_and_wait(chan, "dir-instr", AST_DIGIT_ANY);
  331. if (!res)
  332. res = ast_waitfordigit(chan, 3000);
  333. ast_stopstream(chan);
  334. if (res == '0') { /* operator selected */
  335. goto_exten(chan, dialcontext, "o");
  336. pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "OPERATOR");
  337. return '0';
  338. } else if (res == '1') { /* Name selected */
  339. return select_entry(chan, dialcontext, item, flags) ? -1 : 1;
  340. } else if (res == '*') {
  341. /* Skip to next match in list */
  342. break;
  343. } else if (res == '#') {
  344. /* Exit reading, continue in dialplan */
  345. pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "USEREXIT");
  346. return res;
  347. }
  348. if (res < 0)
  349. return -1;
  350. res = 0;
  351. }
  352. res = 0;
  353. }
  354. /* Nothing was selected */
  355. return 0;
  356. }
  357. static int select_item_menu(struct ast_channel *chan, struct directory_item **items, int count, const char *dialcontext, struct ast_flags *flags, char *opts[])
  358. {
  359. struct directory_item **block, *item;
  360. int i, limit, res = 0;
  361. char buf[9];
  362. /* option p(n): cellphone pause option */
  363. select_item_pause(chan, flags, opts);
  364. for (block = items; count; block += limit, count -= limit) {
  365. limit = count;
  366. if (limit > 8)
  367. limit = 8;
  368. for (i = 0; i < limit && !res; i++) {
  369. item = block[i];
  370. snprintf(buf, sizeof(buf), "digits/%d", i + 1);
  371. /* Press <num> for <name>, [ extension <ext> ] */
  372. res = ast_streamfile(chan, "dir-multi1", ast_channel_language(chan));
  373. if (!res)
  374. res = ast_waitstream(chan, AST_DIGIT_ANY);
  375. if (!res)
  376. res = ast_streamfile(chan, buf, ast_channel_language(chan));
  377. if (!res)
  378. res = ast_waitstream(chan, AST_DIGIT_ANY);
  379. if (!res)
  380. res = ast_streamfile(chan, "dir-multi2", ast_channel_language(chan));
  381. if (!res)
  382. res = ast_waitstream(chan, AST_DIGIT_ANY);
  383. if (!res)
  384. res = play_mailbox_owner(chan, item->context, item->exten, item->name, flags);
  385. if (!res)
  386. res = ast_waitstream(chan, AST_DIGIT_ANY);
  387. if (!res)
  388. res = ast_waitfordigit(chan, 800);
  389. }
  390. /* Press "9" for more names. */
  391. if (!res && count > limit) {
  392. res = ast_streamfile(chan, "dir-multi9", ast_channel_language(chan));
  393. if (!res)
  394. res = ast_waitstream(chan, AST_DIGIT_ANY);
  395. }
  396. if (!res) {
  397. res = ast_waitfordigit(chan, 3000);
  398. }
  399. if (res && res > '0' && res < '1' + limit) {
  400. pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "SELECTED");
  401. return select_entry(chan, dialcontext, block[res - '1'], flags) ? -1 : 1;
  402. }
  403. if (res < 0)
  404. return -1;
  405. res = 0;
  406. }
  407. /* Nothing was selected */
  408. return 0;
  409. }
  410. AST_THREADSTORAGE(commonbuf);
  411. static struct ast_config *realtime_directory(char *context)
  412. {
  413. struct ast_config *cfg;
  414. struct ast_config *rtdata = NULL;
  415. struct ast_category *cat;
  416. struct ast_variable *var;
  417. char *category = NULL;
  418. const char *fullname;
  419. const char *hidefromdir, *searchcontexts = NULL;
  420. struct ast_flags config_flags = { 0 };
  421. struct ast_str *tmp = ast_str_thread_get(&commonbuf, 100);
  422. if (!tmp) {
  423. return NULL;
  424. }
  425. /* Load flat file config. */
  426. cfg = ast_config_load(VOICEMAIL_CONFIG, config_flags);
  427. if (!cfg) {
  428. /* Loading config failed. */
  429. ast_log(LOG_WARNING, "Loading config failed.\n");
  430. return NULL;
  431. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  432. ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", VOICEMAIL_CONFIG);
  433. return NULL;
  434. }
  435. /* Get realtime entries, categorized by their mailbox number
  436. and present in the requested context */
  437. if (ast_strlen_zero(context) && (searchcontexts = ast_variable_retrieve(cfg, "general", "searchcontexts"))) {
  438. if (ast_true(searchcontexts)) {
  439. rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", SENTINEL);
  440. context = NULL;
  441. } else {
  442. rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", "context", "default", SENTINEL);
  443. context = "default";
  444. }
  445. } else if (!ast_strlen_zero(context)) {
  446. rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", "context", context, SENTINEL);
  447. }
  448. /* if there are no results, just return the entries from the config file */
  449. if (!rtdata) {
  450. return cfg;
  451. }
  452. while ((category = ast_category_browse(rtdata, category))) {
  453. const char *mailbox = ast_variable_retrieve(rtdata, category, "mailbox");
  454. const char *ctx = ast_variable_retrieve(rtdata, category, "context");
  455. if (ast_strlen_zero(mailbox)) {
  456. ast_debug(3, "Skipping result with missing or empty mailbox\n");
  457. continue;
  458. }
  459. fullname = ast_variable_retrieve(rtdata, category, "fullname");
  460. hidefromdir = ast_variable_retrieve(rtdata, category, "hidefromdir");
  461. if (ast_true(hidefromdir)) {
  462. /* Skip hidden */
  463. continue;
  464. }
  465. /* password,Full Name,email,pager,options */
  466. ast_str_set(&tmp, 0, "no-password,%s,,,", S_OR(fullname, ""));
  467. if (ast_variable_retrieve(rtdata, category, "alias")) {
  468. struct ast_variable *alias;
  469. for (alias = ast_variable_browse(rtdata, category); alias; alias = alias->next) {
  470. if (!strcasecmp(alias->name, "alias")) {
  471. ast_str_append(&tmp, 0, "|alias=%s", alias->value);
  472. }
  473. }
  474. }
  475. /* Does the context exist within the config file? If not, make one */
  476. if (!(cat = ast_category_get(cfg, ctx, NULL))) {
  477. if (!(cat = ast_category_new_dynamic(ctx))) {
  478. ast_log(LOG_WARNING, "Out of memory\n");
  479. ast_config_destroy(cfg);
  480. if (rtdata) {
  481. ast_config_destroy(rtdata);
  482. }
  483. return NULL;
  484. }
  485. ast_category_append(cfg, cat);
  486. }
  487. if ((var = ast_variable_new(mailbox, ast_str_buffer(tmp), ""))) {
  488. ast_variable_append(cat, var);
  489. } else {
  490. ast_log(LOG_WARNING, "Out of memory adding mailbox '%s'\n", mailbox);
  491. }
  492. }
  493. ast_config_destroy(rtdata);
  494. return cfg;
  495. }
  496. static int check_match(struct directory_item **result, const char *item_context, const char *item_fullname, const char *item_ext, const char *pattern_ext, int use_first_name)
  497. {
  498. struct directory_item *item;
  499. const char *key = NULL;
  500. int namelen;
  501. if (ast_strlen_zero(item_fullname)) {
  502. return 0;
  503. }
  504. /* Set key to last name or first name depending on search mode */
  505. if (!use_first_name)
  506. key = strchr(item_fullname, ' ');
  507. if (key)
  508. key++;
  509. else
  510. key = item_fullname;
  511. if (compare(key, pattern_ext))
  512. return 0;
  513. ast_debug(1, "Found match %s@%s\n", item_ext, item_context);
  514. /* Match */
  515. item = ast_calloc(1, sizeof(*item));
  516. if (!item)
  517. return -1;
  518. ast_copy_string(item->context, item_context, sizeof(item->context));
  519. ast_copy_string(item->name, item_fullname, sizeof(item->name));
  520. ast_copy_string(item->exten, item_ext, sizeof(item->exten));
  521. ast_copy_string(item->key, key, sizeof(item->key));
  522. if (key != item_fullname) {
  523. /* Key is the last name. Append first name to key in order to sort Last,First */
  524. namelen = key - item_fullname - 1;
  525. if (namelen > sizeof(item->key) - strlen(item->key) - 1)
  526. namelen = sizeof(item->key) - strlen(item->key) - 1;
  527. strncat(item->key, item_fullname, namelen);
  528. }
  529. *result = item;
  530. return 1;
  531. }
  532. typedef AST_LIST_HEAD_NOLOCK(, directory_item) itemlist;
  533. static int search_directory_sub(const char *context, struct ast_config *vmcfg, struct ast_config *ucfg, const char *ext, struct ast_flags flags, itemlist *alist)
  534. {
  535. struct ast_variable *v;
  536. struct ast_str *buf = ast_str_thread_get(&commonbuf, 100);
  537. char *name;
  538. char *options;
  539. char *alias;
  540. char *cat;
  541. struct directory_item *item;
  542. int res;
  543. if (!buf) {
  544. return -1;
  545. }
  546. ast_debug(2, "Pattern: %s\n", ext);
  547. for (v = ast_variable_browse(vmcfg, context); v; v = v->next) {
  548. ast_str_set(&buf, 0, "%s", v->value);
  549. options = ast_str_buffer(buf);
  550. /* password,Full Name,email,pager,options */
  551. strsep(&options, ","); /* Skip password */
  552. name = strsep(&options, ","); /* Save full name */
  553. strsep(&options, ","); /* Skip email */
  554. strsep(&options, ","); /* Skip pager */
  555. /* options is now the options field if it exists. */
  556. if (options && strcasestr(options, "hidefromdir=yes")) {
  557. /* Ignore hidden */
  558. continue;
  559. }
  560. if (ast_strlen_zero(name)) {
  561. /* No name to compare against */
  562. continue;
  563. }
  564. res = 0;
  565. if (ast_test_flag(&flags, OPT_LISTBYLASTNAME)) {
  566. res = check_match(&item, context, name, v->name, ext, 0 /* use_first_name */);
  567. }
  568. if (!res && ast_test_flag(&flags, OPT_LISTBYFIRSTNAME)) {
  569. res = check_match(&item, context, name, v->name, ext, 1 /* use_first_name */);
  570. }
  571. if (!res && ast_test_flag(&flags, OPT_ALIAS)
  572. && options && (alias = strcasestr(options, "alias="))) {
  573. char *a;
  574. ast_debug(1, "Found alias: %s\n", alias);
  575. while ((a = strsep(&alias, "|"))) {
  576. if (!strncasecmp(a, "alias=", 6)) {
  577. if ((res = check_match(&item, context, a + 6, v->name, ext, 1))) {
  578. break;
  579. }
  580. }
  581. }
  582. }
  583. if (!res) {
  584. continue;
  585. } else if (res < 0) {
  586. return -1;
  587. }
  588. AST_LIST_INSERT_TAIL(alist, item, entry);
  589. }
  590. if (ucfg) {
  591. for (cat = ast_category_browse(ucfg, NULL); cat ; cat = ast_category_browse(ucfg, cat)) {
  592. const char *position;
  593. if (!strcasecmp(cat, "general")) {
  594. continue;
  595. }
  596. if (!ast_true(ast_config_option(ucfg, cat, "hasdirectory"))) {
  597. continue;
  598. }
  599. /* Find all candidate extensions */
  600. if (!(position = ast_variable_retrieve(ucfg, cat, "fullname"))) {
  601. continue;
  602. }
  603. res = 0;
  604. if (ast_test_flag(&flags, OPT_LISTBYLASTNAME)) {
  605. res = check_match(&item, context, position, cat, ext, 0 /* use_first_name */);
  606. }
  607. if (!res && ast_test_flag(&flags, OPT_LISTBYFIRSTNAME)) {
  608. res = check_match(&item, context, position, cat, ext, 1 /* use_first_name */);
  609. }
  610. if (!res && ast_test_flag(&flags, OPT_ALIAS)) {
  611. for (v = ast_variable_browse(ucfg, cat); v; v = v->next) {
  612. if (!strcasecmp(v->name, "alias")
  613. && (res = check_match(&item, context, v->value, cat, ext, 1))) {
  614. break;
  615. }
  616. }
  617. }
  618. if (!res) {
  619. continue;
  620. } else if (res < 0) {
  621. return -1;
  622. }
  623. AST_LIST_INSERT_TAIL(alist, item, entry);
  624. }
  625. }
  626. return 0;
  627. }
  628. static int search_directory(const char *context, struct ast_config *vmcfg, struct ast_config *ucfg, const char *ext, struct ast_flags flags, itemlist *alist)
  629. {
  630. const char *searchcontexts = ast_variable_retrieve(vmcfg, "general", "searchcontexts");
  631. if (ast_strlen_zero(context)) {
  632. if (!ast_strlen_zero(searchcontexts) && ast_true(searchcontexts)) {
  633. /* Browse each context for a match */
  634. int res;
  635. const char *catg;
  636. for (catg = ast_category_browse(vmcfg, NULL); catg; catg = ast_category_browse(vmcfg, catg)) {
  637. if (!strcmp(catg, "general") || !strcmp(catg, "zonemessages")) {
  638. continue;
  639. }
  640. if ((res = search_directory_sub(catg, vmcfg, ucfg, ext, flags, alist))) {
  641. return res;
  642. }
  643. }
  644. return 0;
  645. } else {
  646. ast_debug(1, "Searching by category default\n");
  647. return search_directory_sub("default", vmcfg, ucfg, ext, flags, alist);
  648. }
  649. } else {
  650. /* Browse only the listed context for a match */
  651. ast_debug(1, "Searching by category %s\n", context);
  652. return search_directory_sub(context, vmcfg, ucfg, ext, flags, alist);
  653. }
  654. }
  655. static void sort_items(struct directory_item **sorted, int count)
  656. {
  657. int reordered, i;
  658. struct directory_item **ptr, *tmp;
  659. if (count < 2)
  660. return;
  661. /* Bubble-sort items by the key */
  662. do {
  663. reordered = 0;
  664. for (ptr = sorted, i = 0; i < count - 1; i++, ptr++) {
  665. if (strcasecmp(ptr[0]->key, ptr[1]->key) > 0) {
  666. tmp = ptr[0];
  667. ptr[0] = ptr[1];
  668. ptr[1] = tmp;
  669. reordered++;
  670. }
  671. }
  672. } while (reordered);
  673. }
  674. static int do_directory(struct ast_channel *chan, struct ast_config *vmcfg, struct ast_config *ucfg, char *context, char *dialcontext, char digit, int digits, struct ast_flags *flags, char *opts[])
  675. {
  676. /* Read in the first three digits.. "digit" is the first digit, already read */
  677. int res = 0;
  678. itemlist alist = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  679. struct directory_item *item, **ptr, **sorted = NULL;
  680. int count, i;
  681. char ext[10] = "";
  682. if (digit == '0' && !goto_exten(chan, dialcontext, "o")) {
  683. pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "OPERATOR");
  684. return digit;
  685. }
  686. if (digit == '*' && !goto_exten(chan, dialcontext, "a")) {
  687. pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "ASSISTANT");
  688. return digit;
  689. }
  690. ext[0] = digit;
  691. if (ast_readstring(chan, ext + 1, digits - 1, 3000, 3000, "#") < 0)
  692. return -1;
  693. res = search_directory(context, vmcfg, ucfg, ext, *flags, &alist);
  694. if (res)
  695. goto exit;
  696. /* Count items in the list */
  697. count = 0;
  698. AST_LIST_TRAVERSE(&alist, item, entry) {
  699. count++;
  700. }
  701. if (count < 1) {
  702. res = ast_streamfile(chan, "dir-nomatch", ast_channel_language(chan));
  703. goto exit;
  704. }
  705. /* Create plain array of pointers to items (for sorting) */
  706. sorted = ast_calloc(count, sizeof(*sorted));
  707. ptr = sorted;
  708. AST_LIST_TRAVERSE(&alist, item, entry) {
  709. *ptr++ = item;
  710. }
  711. /* Sort items */
  712. sort_items(sorted, count);
  713. if (DEBUG_ATLEAST(2)) {
  714. ast_log(LOG_DEBUG, "Listing matching entries:\n");
  715. for (ptr = sorted, i = 0; i < count; i++, ptr++) {
  716. ast_log(LOG_DEBUG, "%s: %s\n", ptr[0]->exten, ptr[0]->name);
  717. }
  718. }
  719. if (ast_test_flag(flags, OPT_SELECTFROMMENU)) {
  720. /* Offer multiple entries at the same time */
  721. res = select_item_menu(chan, sorted, count, dialcontext, flags, opts);
  722. } else {
  723. /* Offer entries one by one */
  724. res = select_item_seq(chan, sorted, count, dialcontext, flags, opts);
  725. }
  726. if (!res) {
  727. res = ast_streamfile(chan, "dir-nomore", ast_channel_language(chan));
  728. }
  729. exit:
  730. if (sorted)
  731. ast_free(sorted);
  732. while ((item = AST_LIST_REMOVE_HEAD(&alist, entry)))
  733. ast_free(item);
  734. return res;
  735. }
  736. static int directory_exec(struct ast_channel *chan, const char *data)
  737. {
  738. int res = 0, digit = 3;
  739. struct ast_config *cfg, *ucfg;
  740. const char *dirintro;
  741. char *parse, *opts[OPT_ARG_ARRAY_SIZE] = { 0, };
  742. struct ast_flags flags = { 0 };
  743. struct ast_flags config_flags = { 0 };
  744. enum { FIRST, LAST, BOTH } which = LAST;
  745. char digits[9] = "digits/3";
  746. AST_DECLARE_APP_ARGS(args,
  747. AST_APP_ARG(vmcontext);
  748. AST_APP_ARG(dialcontext);
  749. AST_APP_ARG(options);
  750. );
  751. parse = ast_strdupa(data);
  752. AST_STANDARD_APP_ARGS(args, parse);
  753. if (args.options && ast_app_parse_options(directory_app_options, &flags, opts, args.options))
  754. return -1;
  755. if (!(cfg = realtime_directory(args.vmcontext))) {
  756. ast_log(LOG_ERROR, "Unable to read the configuration data!\n");
  757. return -1;
  758. }
  759. if ((ucfg = ast_config_load("users.conf", config_flags)) == CONFIG_STATUS_FILEINVALID) {
  760. ast_log(LOG_ERROR, "Config file users.conf is in an invalid format. Aborting.\n");
  761. ucfg = NULL;
  762. }
  763. dirintro = ast_variable_retrieve(cfg, args.vmcontext, "directoryintro");
  764. if (ast_strlen_zero(dirintro))
  765. dirintro = ast_variable_retrieve(cfg, "general", "directoryintro");
  766. /* the above prompts probably should be modified to include 0 for dialing operator
  767. and # for exiting (continues in dialplan) */
  768. if (ast_test_flag(&flags, OPT_LISTBYFIRSTNAME) && ast_test_flag(&flags, OPT_LISTBYLASTNAME)) {
  769. if (!ast_strlen_zero(opts[OPT_ARG_EITHER])) {
  770. digit = atoi(opts[OPT_ARG_EITHER]);
  771. }
  772. which = BOTH;
  773. } else if (ast_test_flag(&flags, OPT_LISTBYFIRSTNAME)) {
  774. if (!ast_strlen_zero(opts[OPT_ARG_FIRSTNAME])) {
  775. digit = atoi(opts[OPT_ARG_FIRSTNAME]);
  776. }
  777. which = FIRST;
  778. } else {
  779. if (!ast_strlen_zero(opts[OPT_ARG_LASTNAME])) {
  780. digit = atoi(opts[OPT_ARG_LASTNAME]);
  781. }
  782. which = LAST;
  783. }
  784. /* If no options specified, search by last name */
  785. if (!ast_test_flag(&flags, OPT_LISTBYFIRSTNAME) && !ast_test_flag(&flags, OPT_LISTBYLASTNAME)) {
  786. ast_set_flag(&flags, OPT_LISTBYLASTNAME);
  787. which = LAST;
  788. }
  789. if (digit > 9) {
  790. digit = 9;
  791. } else if (digit < 1) {
  792. digit = 3;
  793. }
  794. digits[7] = digit + '0';
  795. if (ast_channel_state(chan) != AST_STATE_UP) {
  796. if (!ast_test_flag(&flags, OPT_NOANSWER)) {
  797. /* Otherwise answer unless we're supposed to read while on-hook */
  798. res = ast_answer(chan);
  799. }
  800. }
  801. for (;;) {
  802. if (!ast_strlen_zero(dirintro) && !res) {
  803. res = ast_stream_and_wait(chan, dirintro, AST_DIGIT_ANY);
  804. } else if (!res) {
  805. /* Stop playing sounds as soon as we have a digit. */
  806. res = ast_stream_and_wait(chan, "dir-welcome", AST_DIGIT_ANY);
  807. if (!res) {
  808. res = ast_stream_and_wait(chan, "dir-pls-enter", AST_DIGIT_ANY);
  809. }
  810. if (!res) {
  811. res = ast_stream_and_wait(chan, digits, AST_DIGIT_ANY);
  812. }
  813. if (!res) {
  814. res = ast_stream_and_wait(chan,
  815. which == FIRST ? "dir-first" :
  816. which == LAST ? "dir-last" :
  817. "dir-firstlast", AST_DIGIT_ANY);
  818. }
  819. if (!res) {
  820. res = ast_stream_and_wait(chan, "dir-usingkeypad", AST_DIGIT_ANY);
  821. }
  822. }
  823. ast_stopstream(chan);
  824. if (!res)
  825. res = ast_waitfordigit(chan, 5000);
  826. if (res <= 0) {
  827. if (res == 0) {
  828. pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "TIMEOUT");
  829. }
  830. break;
  831. }
  832. res = do_directory(chan, cfg, ucfg, args.vmcontext, args.dialcontext, res, digit, &flags, opts);
  833. if (res)
  834. break;
  835. res = ast_waitstream(chan, AST_DIGIT_ANY);
  836. ast_stopstream(chan);
  837. if (res < 0) {
  838. break;
  839. }
  840. }
  841. if (ucfg)
  842. ast_config_destroy(ucfg);
  843. ast_config_destroy(cfg);
  844. if (ast_check_hangup(chan)) {
  845. pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "HANGUP");
  846. } else if (res < 0) {
  847. /* If the res < 0 and we didn't hangup, an unaccounted for error must have happened. */
  848. pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "FAILED");
  849. }
  850. return res < 0 ? -1 : 0;
  851. }
  852. static int unload_module(void)
  853. {
  854. int res;
  855. res = ast_unregister_application(app);
  856. return res;
  857. }
  858. static int load_module(void)
  859. {
  860. return ast_register_application_xml(app, directory_exec);
  861. }
  862. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Extension Directory");