app_enumlookup.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Time of day - Report the time of day
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/options.h>
  19. #include <asterisk/config.h>
  20. #include <asterisk/module.h>
  21. #include <asterisk/enum.h>
  22. #include <asterisk/utils.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <ctype.h>
  28. static char *tdesc = "ENUM Lookup";
  29. static char *app = "EnumLookup";
  30. static char *synopsis = "Lookup number in ENUM";
  31. static char *descrip =
  32. " EnumLookup(exten): Looks up an extension via ENUM and sets\n"
  33. "the variable 'ENUM'. For VoIP URIs this variable will \n"
  34. "look like 'TECHNOLOGY/URI' with the appropriate technology.\n"
  35. "Returns -1 on hangup, or 0 on completion regardless of whether the \n"
  36. "lookup was successful. \n"
  37. "Currently, the enumservices SIP, H323, IAX, IAX2 and TEL are recognized. \n"
  38. "A good SIP, H323, IAX or IAX2 entry will result in normal priority handling, \n"
  39. "whereas a good TEL entry will increase the priority by 51 (if existing).\n"
  40. "If the lookup was *not* successful and there exists a priority n + 101,\n"
  41. "then that priority will be taken next.\n" ;
  42. #define ENUM_CONFIG "enum.conf"
  43. static char h323driver[80] = "";
  44. #define H323DRIVERDEFAULT "H323"
  45. STANDARD_LOCAL_USER;
  46. LOCAL_USER_DECL;
  47. static int enumlookup_exec(struct ast_channel *chan, void *data)
  48. {
  49. int res=0;
  50. char tech[80];
  51. char dest[80];
  52. char tmp[256];
  53. char *c,*t;
  54. struct localuser *u;
  55. if (!data || ast_strlen_zero(data)) {
  56. ast_log(LOG_WARNING, "EnumLookup requires an argument (extension)\n");
  57. res = 1;
  58. }
  59. LOCAL_USER_ADD(u);
  60. if (!res) {
  61. res = ast_get_enum(chan, data, dest, sizeof(dest), tech, sizeof(tech));
  62. printf("ENUM got '%d'\n", res);
  63. }
  64. LOCAL_USER_REMOVE(u);
  65. /* Parse it out */
  66. if (res > 0) {
  67. if (!strcasecmp(tech, "SIP")) {
  68. c = dest;
  69. if (!strncmp(c, "sip:", 4))
  70. c += 4;
  71. snprintf(tmp, sizeof(tmp), "SIP/%s", c);
  72. pbx_builtin_setvar_helper(chan, "ENUM", tmp);
  73. } else if (!strcasecmp(tech, "h323")) {
  74. c = dest;
  75. if (!strncmp(c, "h323:", 5))
  76. c += 5;
  77. snprintf(tmp, sizeof(tmp), "%s/%s", h323driver, c);
  78. /* do a s!;.*!! on the H323 URI */
  79. t = strchr(c,';');
  80. if (t)
  81. *t = 0;
  82. pbx_builtin_setvar_helper(chan, "ENUM", tmp);
  83. } else if (!strcasecmp(tech, "iax")) {
  84. c = dest;
  85. if (!strncmp(c, "iax:", 4))
  86. c += 4;
  87. snprintf(tmp, sizeof(tmp), "IAX/%s", c);
  88. pbx_builtin_setvar_helper(chan, "ENUM", tmp);
  89. } else if (!strcasecmp(tech, "iax2")) {
  90. c = dest;
  91. if (!strncmp(c, "iax2:", 5))
  92. c += 5;
  93. snprintf(tmp, sizeof(tmp), "IAX2/%s", c);
  94. pbx_builtin_setvar_helper(chan, "ENUM", tmp);
  95. } else if (!strcasecmp(tech, "tel")) {
  96. c = dest;
  97. if (!strncmp(c, "tel:", 4))
  98. c += 4;
  99. if (c[0] != '+') {
  100. ast_log(LOG_NOTICE, "tel: uri must start with a \"+\" (got '%s')\n", c);
  101. res = 0;
  102. } else {
  103. /* now copy over the number, skipping all non-digits and stop at ; or NULL */
  104. t = tmp;
  105. while( *c && (*c != ';') && (t - tmp < (sizeof(tmp) - 1))) {
  106. if (isdigit(*c))
  107. *t++ = *c;
  108. c++;
  109. }
  110. *t = 0;
  111. pbx_builtin_setvar_helper(chan, "ENUM", tmp);
  112. ast_log(LOG_NOTICE, "tel: ENUM set to \"%s\"\n", tmp);
  113. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 51, chan->callerid))
  114. chan->priority += 50;
  115. else
  116. res = 0;
  117. }
  118. } else if (!ast_strlen_zero(tech)) {
  119. ast_log(LOG_NOTICE, "Don't know how to handle technology '%s'\n", tech);
  120. res = 0;
  121. }
  122. }
  123. if (!res) {
  124. /* Look for a "busy" place */
  125. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
  126. chan->priority += 100;
  127. } else if (res > 0)
  128. res = 0;
  129. return res;
  130. }
  131. static int load_config(void)
  132. {
  133. struct ast_config *cfg;
  134. char *s;
  135. cfg = ast_load(ENUM_CONFIG);
  136. if (cfg) {
  137. if (!(s=ast_variable_retrieve(cfg, "general", "h323driver"))) {
  138. strncpy(h323driver, H323DRIVERDEFAULT, sizeof(h323driver) - 1);
  139. } else {
  140. strncpy(h323driver, s, sizeof(h323driver) - 1);
  141. }
  142. ast_destroy(cfg);
  143. return 0;
  144. }
  145. ast_log(LOG_NOTICE, "No ENUM Config file, using defaults\n");
  146. return 0;
  147. }
  148. int unload_module(void)
  149. {
  150. STANDARD_HANGUP_LOCALUSERS;
  151. return ast_unregister_application(app);
  152. }
  153. int load_module(void)
  154. {
  155. int res;
  156. res = ast_register_application(app, enumlookup_exec, synopsis, descrip);
  157. if (res)
  158. return(res);
  159. if ((res=load_config())) {
  160. return(res);
  161. }
  162. return(0);
  163. }
  164. int reload(void)
  165. {
  166. return(load_config());
  167. }
  168. char *description(void)
  169. {
  170. return tdesc;
  171. }
  172. int usecount(void)
  173. {
  174. int res;
  175. STANDARD_USECOUNT(res);
  176. return res;
  177. }
  178. char *key()
  179. {
  180. return ASTERISK_GPL_KEY;
  181. }