res_indications.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /** @file res_indications.c
  2. *
  3. * Asterisk -- A telephony toolkit for Linux.
  4. *
  5. * Load the indications
  6. *
  7. * Copyright (C) 2002, Pauline Middelink
  8. *
  9. * Pauline Middelink <middelink@polyware.nl>
  10. *
  11. * This program is free software, distributed under the terms of
  12. * the GNU General Public License
  13. *
  14. * Load the country specific dialtones into the asterisk PBX.
  15. */
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19. #include <stdlib.h>
  20. #include <errno.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <asterisk/lock.h>
  24. #include <asterisk/file.h>
  25. #include <asterisk/cli.h>
  26. #include <asterisk/logger.h>
  27. #include <asterisk/config.h>
  28. #include <asterisk/channel.h>
  29. #include <asterisk/pbx.h>
  30. #include <asterisk/module.h>
  31. #include <asterisk/translate.h>
  32. #include <asterisk/indications.h>
  33. // Globals
  34. static const char dtext[] = "Indications Configuration";
  35. static const char config[] = "indications.conf";
  36. /*
  37. * Help for commands provided by this module ...
  38. */
  39. static char help_add_indication[] =
  40. "Usage: add indication <country> <indication> \"<tonelist>\"\n"
  41. " Add the given indication to the country.\n";
  42. static char help_remove_indication[] =
  43. "Usage: remove indication <country> <indication>\n"
  44. " Remove the given indication from the country.\n";
  45. static char help_show_indications[] =
  46. "Usage: show indications [<country> ...]\n"
  47. " Show either a condensed for of all country/indications, or the\n"
  48. " indications for the specified countries.\n";
  49. char *playtones_desc=
  50. "PlayTones(<arg>): Plays a tone list. Execution will continue with the next step immediately,\n"
  51. "while the tones continue to play.\n"
  52. "Arg is either the tone name defined in the indications.conf configuration file, or a directly\n"
  53. "specified list of frequencies and durations.\n"
  54. "See the sample indications.conf for a description of the specification of a tonelist.\n\n"
  55. "Use the StopPlayTones application to stop the tones playing. \n";
  56. /*
  57. * Implementation of functions provided by this module
  58. */
  59. /*
  60. * ADD INDICATION command stuff
  61. */
  62. static int handle_add_indication(int fd, int argc, char *argv[])
  63. {
  64. struct tone_zone *tz;
  65. int created_country = 0;
  66. if (argc != 5) return RESULT_SHOWUSAGE;
  67. tz = ast_get_indication_zone(argv[2]);
  68. if (!tz) {
  69. /* country does not exist, create it */
  70. ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n",argv[2]);
  71. tz = malloc(sizeof(struct tone_zone));
  72. if (!tz) {
  73. ast_log(LOG_WARNING, "Out of memory\n");
  74. return -1;
  75. }
  76. memset(tz,0,sizeof(struct tone_zone));
  77. strncpy(tz->country,argv[2],sizeof(tz->country)-1);
  78. if (ast_register_indication_country(tz)) {
  79. ast_log(LOG_WARNING, "Unable to register new country\n");
  80. free(tz);
  81. return -1;
  82. }
  83. created_country = 1;
  84. }
  85. if (ast_register_indication(tz,argv[3],argv[4])) {
  86. ast_log(LOG_WARNING, "Unable to register indication %s/%s\n",argv[2],argv[3]);
  87. if (created_country)
  88. ast_unregister_indication_country(argv[2]);
  89. return -1;
  90. }
  91. return 0;
  92. }
  93. /*
  94. * REMOVE INDICATION command stuff
  95. */
  96. static int handle_remove_indication(int fd, int argc, char *argv[])
  97. {
  98. struct tone_zone *tz;
  99. if (argc != 3 && argc != 4) return RESULT_SHOWUSAGE;
  100. if (argc == 3) {
  101. /* remove entiry country */
  102. if (ast_unregister_indication_country(argv[2])) {
  103. ast_log(LOG_WARNING, "Unable to unregister indication country %s\n",argv[2]);
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. tz = ast_get_indication_zone(argv[2]);
  109. if (!tz) {
  110. ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n",argv[2],argv[3]);
  111. return -1;
  112. }
  113. if (ast_unregister_indication(tz,argv[3])) {
  114. ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n",argv[2],argv[3]);
  115. return -1;
  116. }
  117. return 0;
  118. }
  119. /*
  120. * SHOW INDICATIONS command stuff
  121. */
  122. static int handle_show_indications(int fd, int argc, char *argv[])
  123. {
  124. struct tone_zone *tz;
  125. char buf[256];
  126. int found_country = 0;
  127. if (ast_mutex_lock(&tzlock)) {
  128. ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
  129. return 0;
  130. }
  131. if (argc == 2) {
  132. /* no arguments, show a list of countries */
  133. ast_cli(fd,"Country Alias Description\n"
  134. "===========================\n");
  135. for (tz=tone_zones; tz; tz=tz->next) {
  136. ast_cli(fd,"%-7.7s %-7.7s %s\n", tz->country, tz->alias, tz->description);
  137. }
  138. ast_mutex_unlock(&tzlock);
  139. return 0;
  140. }
  141. /* there was a request for specific country(ies), lets humor them */
  142. for (tz=tone_zones; tz; tz=tz->next) {
  143. int i,j;
  144. for (i=2; i<argc; i++) {
  145. if (strcasecmp(tz->country,argv[i])==0 &&
  146. !tz->alias[0]) {
  147. struct tone_zone_sound* ts;
  148. if (!found_country) {
  149. found_country = 1;
  150. ast_cli(fd,"Country Indication PlayList\n"
  151. "=====================================\n");
  152. }
  153. j = snprintf(buf,sizeof(buf),"%-7.7s %-15.15s ",tz->country,"<ringcadance>");
  154. for (i=0; i<tz->nrringcadance; i++) {
  155. j += snprintf(buf+j,sizeof(buf)-j,"%d,",tz->ringcadance[i]);
  156. }
  157. if (tz->nrringcadance) j--;
  158. strncpy(buf+j,"\n",sizeof(buf)-j-1);
  159. ast_cli(fd,buf);
  160. for (ts=tz->tones; ts; ts=ts->next)
  161. ast_cli(fd,"%-7.7s %-15.15s %s\n",tz->country,ts->name,ts->data);
  162. break;
  163. }
  164. }
  165. }
  166. if (!found_country)
  167. ast_cli(fd,"No countries matched your criteria.\n");
  168. ast_mutex_unlock(&tzlock);
  169. return -1;
  170. }
  171. /*
  172. * Playtones command stuff
  173. */
  174. static int handle_playtones(struct ast_channel *chan, void *data)
  175. {
  176. struct tone_zone_sound *ts;
  177. int res;
  178. if (!data || !((char*)data)[0]) {
  179. ast_log(LOG_NOTICE,"Nothing to play\n");
  180. return -1;
  181. }
  182. ts = ast_get_indication_tone(chan->zone, (const char*)data);
  183. if (ts && ts->data[0])
  184. res = ast_playtones_start(chan, 0, ts->data, 0);
  185. else
  186. res = ast_playtones_start(chan, 0, (const char*)data, 0);
  187. if (res)
  188. ast_log(LOG_NOTICE,"Unable to start playtones\n");
  189. return res;
  190. }
  191. /*
  192. * StopPlaylist command stuff
  193. */
  194. static int handle_stopplaytones(struct ast_channel *chan, void *data)
  195. {
  196. ast_playtones_stop(chan);
  197. return 0;
  198. }
  199. /*
  200. * Load module stuff
  201. */
  202. static int ind_load_module(void)
  203. {
  204. struct ast_config *cfg;
  205. struct ast_variable *v;
  206. char *cxt;
  207. char *c;
  208. struct tone_zone *tones;
  209. const char *country = NULL;
  210. /* that the following cast is needed, is yuk! */
  211. /* yup, checked it out. It is NOT written to. */
  212. cfg = ast_load((char *)config);
  213. if (!cfg)
  214. return 0;
  215. /* Use existing config to populate the Indication table */
  216. cxt = ast_category_browse(cfg, NULL);
  217. while(cxt) {
  218. /* All categories but "general" are considered countries */
  219. if (!strcasecmp(cxt, "general")) {
  220. cxt = ast_category_browse(cfg, cxt);
  221. continue;
  222. }
  223. tones = malloc(sizeof(struct tone_zone));
  224. if (!tones) {
  225. ast_log(LOG_WARNING,"Out of memory\n");
  226. ast_destroy(cfg);
  227. return -1;
  228. }
  229. memset(tones,0,sizeof(struct tone_zone));
  230. strncpy(tones->country,cxt,sizeof(tones->country) - 1);
  231. v = ast_variable_browse(cfg, cxt);
  232. while(v) {
  233. if (!strcasecmp(v->name, "description")) {
  234. strncpy(tones->description, v->value, sizeof(tones->description)-1);
  235. } else if (!strcasecmp(v->name,"ringcadance")) {
  236. char *ring,*rings = ast_strdupa(v->value);
  237. c = rings;
  238. ring = strsep(&c,",");
  239. while (ring) {
  240. int *tmp, val;
  241. if (!isdigit(ring[0]) || (val=atoi(ring))==-1) {
  242. ast_log(LOG_WARNING,"Invalid ringcadance given '%s' at line %d.\n",ring,v->lineno);
  243. ring = strsep(&c,",");
  244. continue;
  245. }
  246. tmp = realloc(tones->ringcadance,(tones->nrringcadance+1)*sizeof(int));
  247. if (!tmp) {
  248. ast_log(LOG_WARNING, "Out of memory\n");
  249. ast_destroy(cfg);
  250. return -1;
  251. }
  252. tones->ringcadance = tmp;
  253. tmp[tones->nrringcadance] = val;
  254. tones->nrringcadance++;
  255. /* next item */
  256. ring = strsep(&c,",");
  257. }
  258. } else if (!strcasecmp(v->name,"alias")) {
  259. char *countries = ast_strdupa(v->value);
  260. c = countries;
  261. country = strsep(&c,",");
  262. while (country) {
  263. struct tone_zone* azone = malloc(sizeof(struct tone_zone));
  264. if (!azone) {
  265. ast_log(LOG_WARNING,"Out of memory\n");
  266. ast_destroy(cfg);
  267. return -1;
  268. }
  269. memset(azone,0,sizeof(struct tone_zone));
  270. strncpy(azone->country, country, sizeof(azone->country) - 1);
  271. strncpy(azone->alias, cxt, sizeof(azone->alias)-1);
  272. if (ast_register_indication_country(azone)) {
  273. ast_log(LOG_WARNING, "Unable to register indication alias at line %d.\n",v->lineno);
  274. free(tones);
  275. }
  276. /* next item */
  277. country = strsep(&c,",");
  278. }
  279. } else {
  280. // add tone to country
  281. struct tone_zone_sound *ps,*ts;
  282. for (ps=NULL,ts=tones->tones; ts; ps=ts, ts=ts->next) {
  283. if (strcasecmp(v->name,ts->name)==0) {
  284. /* already there */
  285. ast_log(LOG_NOTICE,"Duplicate entry '%s', skipped.\n",v->name);
  286. goto out;
  287. }
  288. }
  289. /* not there, add it to the back */
  290. ts = malloc(sizeof(struct tone_zone_sound));
  291. if (!ts) {
  292. ast_log(LOG_WARNING, "Out of memory\n");
  293. ast_destroy(cfg);
  294. return -1;
  295. }
  296. ts->next = NULL;
  297. ts->name = strdup(v->name);
  298. ts->data = strdup(v->value);
  299. if (ps)
  300. ps->next = ts;
  301. else
  302. tones->tones = ts;
  303. }
  304. out: v = v->next;
  305. }
  306. if (tones->description[0] || tones->alias[0] || tones->tones) {
  307. if (ast_register_indication_country(tones)) {
  308. ast_log(LOG_WARNING, "Unable to register indication at line %d.\n",v->lineno);
  309. free(tones);
  310. }
  311. } else free(tones);
  312. cxt = ast_category_browse(cfg, cxt);
  313. }
  314. /* determine which country is the default */
  315. country = ast_variable_retrieve(cfg,"general","country");
  316. if (!country || !*country || ast_set_indication_country(country))
  317. ast_log(LOG_WARNING,"Unable to set the default country (for indication tones)\n");
  318. ast_destroy(cfg);
  319. return 0;
  320. }
  321. /*
  322. * CLI entries for commands provided by this module
  323. */
  324. static struct ast_cli_entry add_indication_cli =
  325. { { "add", "indication", NULL }, handle_add_indication,
  326. "Add the given indication to the country", help_add_indication,
  327. NULL };
  328. static struct ast_cli_entry remove_indication_cli =
  329. { { "remove", "indication", NULL }, handle_remove_indication,
  330. "Remove the given indication from the country", help_remove_indication,
  331. NULL };
  332. static struct ast_cli_entry show_indications_cli =
  333. { { "show", "indications", NULL }, handle_show_indications,
  334. "Show a list of all country/indications", help_show_indications,
  335. NULL };
  336. /*
  337. * Standard module functions ...
  338. */
  339. int unload_module(void)
  340. {
  341. /* remove the registed indications... */
  342. ast_unregister_indication_country(NULL);
  343. /* and the functions */
  344. ast_cli_unregister(&add_indication_cli);
  345. ast_cli_unregister(&remove_indication_cli);
  346. ast_cli_unregister(&show_indications_cli);
  347. ast_unregister_application("PlayTones");
  348. ast_unregister_application("StopPlayTones");
  349. return 0;
  350. }
  351. int load_module(void)
  352. {
  353. if (ind_load_module()) return -1;
  354. ast_cli_register(&add_indication_cli);
  355. ast_cli_register(&remove_indication_cli);
  356. ast_cli_register(&show_indications_cli);
  357. ast_register_application("PlayTones", handle_playtones, "Play a tone list", playtones_desc);
  358. ast_register_application("StopPlayTones", handle_stopplaytones, "Stop playing a tone list","Stop playing a tone list");
  359. return 0;
  360. }
  361. int reload(void)
  362. {
  363. /* remove the registed indications... */
  364. ast_unregister_indication_country(NULL);
  365. return ind_load_module();
  366. }
  367. char *description(void)
  368. {
  369. /* that the following cast is needed, is yuk! */
  370. return (char*)dtext;
  371. }
  372. int usecount(void)
  373. {
  374. return 0;
  375. }
  376. char *key()
  377. {
  378. return ASTERISK_GPL_KEY;
  379. }