1 /** @file res_indications.c
3 * Asterisk -- A telephony toolkit for Linux.
7 * Copyright (C) 2002, Pauline Middelink
9 * Pauline Middelink <middelink@polyware.nl>
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License
14 * Load the country specific dialtones into the asterisk PBX.
22 #include <sys/types.h>
24 #include <asterisk/lock.h>
25 #include <asterisk/file.h>
26 #include <asterisk/cli.h>
27 #include <asterisk/logger.h>
28 #include <asterisk/config.h>
29 #include <asterisk/channel.h>
30 #include <asterisk/pbx.h>
31 #include <asterisk/module.h>
32 #include <asterisk/translate.h>
33 #include <asterisk/indications.h>
37 static const char dtext[] = "Indications Configuration";
38 static const char config[] = "indications.conf";
41 * Help for commands provided by this module ...
43 static char help_add_indication[] =
44 "Usage: add indication <country> <indication> \"<tonelist>\"\n"
45 " Add the given indication to the country.\n";
47 static char help_remove_indication[] =
48 "Usage: remove indication <country> <indication>\n"
49 " Remove the given indication from the country.\n";
51 static char help_show_indications[] =
52 "Usage: show indications [<country> ...]\n"
53 " Show either a condensed for of all country/indications, or the\n"
54 " indications for the specified countries.\n";
57 * Implementation of functions provided by this module
61 * ADD INDICATION command stuff
63 static int handle_add_indication(int fd, int argc, char *argv[])
66 int created_country = 0;
67 if (argc != 5) return RESULT_SHOWUSAGE;
69 tz = ast_get_indication_zone(argv[2]);
71 /* country does not exist, create it */
72 ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n",argv[2]);
74 tz = malloc(sizeof(struct tone_zone));
76 ast_log(LOG_WARNING, "Out of memory\n");
79 memset(tz,0,sizeof(struct tone_zone));
80 strncpy(tz->country,argv[2],sizeof(tz->country)-1);
81 if (ast_register_indication_country(tz)) {
82 ast_log(LOG_WARNING, "Unable to register new country\n");
88 if (ast_register_indication(tz,argv[3],argv[4])) {
89 ast_log(LOG_WARNING, "Unable to register indication %s/%s\n",argv[2],argv[3]);
91 ast_unregister_indication_country(argv[2]);
98 * REMOVE INDICATION command stuff
100 static int handle_remove_indication(int fd, int argc, char *argv[])
102 struct tone_zone *tz;
103 if (argc != 3 && argc != 4) return RESULT_SHOWUSAGE;
106 /* remove entiry country */
107 if (ast_unregister_indication_country(argv[2])) {
108 ast_log(LOG_WARNING, "Unable to unregister indication country %s\n",argv[2]);
114 tz = ast_get_indication_zone(argv[2]);
116 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n",argv[2],argv[3]);
119 if (ast_unregister_indication(tz,argv[3])) {
120 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n",argv[2],argv[3]);
127 * SHOW INDICATIONS command stuff
129 static int handle_show_indications(int fd, int argc, char *argv[])
131 struct tone_zone *tz;
133 int found_country = 0;
135 if (ast_mutex_lock(&tzlock)) {
136 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
140 /* no arguments, show a list of countries */
141 ast_cli(fd,"Country Alias Description\n"
142 "===========================\n");
143 for (tz=tone_zones; tz; tz=tz->next) {
144 ast_cli(fd,"%-7.7s %-7.7s %s\n", tz->country, tz->alias, tz->description);
146 ast_mutex_unlock(&tzlock);
149 /* there was a request for specific country(ies), lets humor them */
150 for (tz=tone_zones; tz; tz=tz->next) {
152 for (i=2; i<argc; i++) {
153 if (strcasecmp(tz->country,argv[i])==0 &&
155 struct tone_zone_sound* ts;
156 if (!found_country) {
158 ast_cli(fd,"Country Indication PlayList\n"
159 "=====================================\n");
161 j = snprintf(buf,sizeof(buf),"%-7.7s %-15.15s ",tz->country,"<ringcadance>");
162 for (i=0; i<tz->nrringcadance; i++) {
163 j += snprintf(buf+j,sizeof(buf)-j,"%d,",tz->ringcadance[i]);
165 if (tz->nrringcadance) j--;
166 strncpy(buf+j,"\n",sizeof(buf)-j);
168 for (ts=tz->tones; ts; ts=ts->next)
169 ast_cli(fd,"%-7.7s %-15.15s %s\n",tz->country,ts->name,ts->data);
175 ast_cli(fd,"No countries matched your criteria.\n");
176 ast_mutex_unlock(&tzlock);
181 * Playtones command stuff
183 static int handle_playtones(struct ast_channel *chan, void *data)
185 struct tone_zone_sound *ts;
188 if (!data || !((char*)data)[0]) {
189 ast_log(LOG_NOTICE,"Nothing to play\n");
192 ts = ast_get_indication_tone(chan->zone, (const char*)data);
193 if (ts && ts->data[0])
194 res = ast_playtones_start(chan, 0, ts->data, 0);
196 res = ast_playtones_start(chan, 0, (const char*)data, 0);
198 ast_log(LOG_NOTICE,"Unable to start playtones\n");
203 * StopPlaylist command stuff
205 static int handle_stopplaytones(struct ast_channel *chan, void *data)
207 ast_playtones_stop(chan);
214 static int ind_load_module(void)
216 struct ast_config *cfg;
217 struct ast_variable *v;
220 struct tone_zone *tones;
221 const char *country = NULL;
223 /* that the following cast is needed, is yuk! */
224 /* yup, checked it out. It is NOT written to. */
225 cfg = ast_load((char *)config);
229 /* Use existing config to populate the Indication table */
230 cxt = ast_category_browse(cfg, NULL);
232 /* All categories but "general" are considered countries */
233 if (!strcasecmp(cxt, "general")) {
234 cxt = ast_category_browse(cfg, cxt);
237 tones = malloc(sizeof(struct tone_zone));
239 ast_log(LOG_WARNING,"Out of memory\n");
243 memset(tones,0,sizeof(struct tone_zone));
244 strncpy(tones->country,cxt,sizeof(tones->country));
246 v = ast_variable_browse(cfg, cxt);
248 if (!strcasecmp(v->name, "description")) {
249 strncpy(tones->description, v->value, sizeof(tones->description)-1);
250 } else if (!strcasecmp(v->name,"ringcadance")) {
251 char *ring,*rings = ast_strdupa(v->value);
253 ring = strsep(&c,",");
256 if (!isdigit(ring[0]) || (val=atoi(ring))==-1) {
257 ast_log(LOG_WARNING,"Invalid ringcadance given '%s' at line %d.\n",ring,v->lineno);
258 ring = strsep(&c,",");
261 tmp = realloc(tones->ringcadance,(tones->nrringcadance+1)*sizeof(int));
263 ast_log(LOG_WARNING, "Out of memory\n");
267 tones->ringcadance = tmp;
268 tmp[tones->nrringcadance] = val;
269 tones->nrringcadance++;
271 ring = strsep(&c,",");
273 } else if (!strcasecmp(v->name,"alias")) {
274 char *countries = ast_strdupa(v->value);
276 country = strsep(&c,",");
278 struct tone_zone* azone = malloc(sizeof(struct tone_zone));
280 ast_log(LOG_WARNING,"Out of memory\n");
284 memset(azone,0,sizeof(struct tone_zone));
285 strncpy(azone->country,country,sizeof(azone->country));
286 strncpy(azone->alias, cxt, sizeof(azone->alias)-1);
287 if (ast_register_indication_country(azone)) {
288 ast_log(LOG_WARNING, "Unable to register indication alias at line %d.\n",v->lineno);
292 country = strsep(&c,",");
295 // add tone to country
296 struct tone_zone_sound *ps,*ts;
297 for (ps=NULL,ts=tones->tones; ts; ps=ts, ts=ts->next) {
298 if (strcasecmp(v->name,ts->name)==0) {
300 ast_log(LOG_NOTICE,"Duplicate entry '%s', skipped.\n",v->name);
304 /* not there, add it to the back */
305 ts = malloc(sizeof(struct tone_zone_sound));
307 ast_log(LOG_WARNING, "Out of memory\n");
312 ts->name = strdup(v->name);
313 ts->data = strdup(v->value);
321 if (tones->description[0] || tones->alias[0] || tones->tones) {
322 if (ast_register_indication_country(tones)) {
323 ast_log(LOG_WARNING, "Unable to register indication at line %d.\n",v->lineno);
328 cxt = ast_category_browse(cfg, cxt);
331 /* determine which country is the default */
332 country = ast_variable_retrieve(cfg,"general","country");
333 if (!country || !*country || ast_set_indication_country(country))
334 ast_log(LOG_WARNING,"Unable to set the default country (for indication tones)\n");
341 * CLI entries for commands provided by this module
343 static struct ast_cli_entry add_indication_cli =
344 { { "add", "indication", NULL }, handle_add_indication,
345 "Add the given indication to the country", help_add_indication,
348 static struct ast_cli_entry remove_indication_cli =
349 { { "remove", "indication", NULL }, handle_remove_indication,
350 "Remove the given indication from the country", help_remove_indication,
353 static struct ast_cli_entry show_indications_cli =
354 { { "show", "indications", NULL }, handle_show_indications,
355 "Show a list of all country/indications", help_show_indications,
359 * Standard module functions ...
361 int unload_module(void)
363 /* remove the registed indications... */
364 ast_unregister_indication_country(NULL);
366 /* and the functions */
367 ast_cli_unregister(&add_indication_cli);
368 ast_cli_unregister(&remove_indication_cli);
369 ast_cli_unregister(&show_indications_cli);
370 ast_unregister_application("Playlist");
371 ast_unregister_application("StopPlaylist");
375 int load_module(void)
377 if (ind_load_module()) return -1;
379 ast_cli_register(&add_indication_cli);
380 ast_cli_register(&remove_indication_cli);
381 ast_cli_register(&show_indications_cli);
382 ast_register_application("Playtones", handle_playtones, "Play a tone list","Play a tone list, either registered (through indications.conf) or a direct list of tones and durations.");
383 ast_register_application("StopPlaytones", handle_stopplaytones, "Stop playing a tone list","Stop playing a tone list");
390 /* remove the registed indications... */
391 ast_unregister_indication_country(NULL);
393 return ind_load_module();
396 char *description(void)
398 /* that the following cast is needed, is yuk! */
409 return ASTERISK_GPL_KEY;