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.
23 #include <sys/types.h>
25 #include <asterisk/lock.h>
26 #include <asterisk/file.h>
27 #include <asterisk/cli.h>
28 #include <asterisk/logger.h>
29 #include <asterisk/config.h>
30 #include <asterisk/channel.h>
31 #include <asterisk/pbx.h>
32 #include <asterisk/module.h>
33 #include <asterisk/translate.h>
34 #include <asterisk/indications.h>
38 static const char dtext[] = "Indications Configuration";
39 static const char config[] = "indications.conf";
42 * Help for commands provided by this module ...
44 static char help_add_indication[] =
45 "Usage: add indication <country> <indication> \"<tonelist>\"\n"
46 " Add the given indication to the country.\n";
48 static char help_remove_indication[] =
49 "Usage: remove indication <country> <indication>\n"
50 " Remove the given indication from the country.\n";
52 static char help_show_indications[] =
53 "Usage: show indications [<country> ...]\n"
54 " Show either a condensed for of all country/indications, or the\n"
55 " indications for the specified countries.\n";
58 * Implementation of functions provided by this module
62 * ADD INDICATION command stuff
64 static int handle_add_indication(int fd, int argc, char *argv[])
67 int created_country = 0;
68 if (argc != 5) return RESULT_SHOWUSAGE;
70 tz = ast_get_indication_zone(argv[2]);
72 /* country does not exist, create it */
73 ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n",argv[2]);
75 tz = malloc(sizeof(struct tone_zone));
77 ast_log(LOG_WARNING, "Out of memory\n");
80 memset(tz,0,sizeof(struct tone_zone));
81 strncpy(tz->country,argv[2],sizeof(tz->country)-1);
82 if (ast_register_indication_country(tz)) {
83 ast_log(LOG_WARNING, "Unable to register new country\n");
89 if (ast_register_indication(tz,argv[3],argv[4])) {
90 ast_log(LOG_WARNING, "Unable to register indication %s/%s\n",argv[2],argv[3]);
92 ast_unregister_indication_country(argv[2]);
99 * REMOVE INDICATION command stuff
101 static int handle_remove_indication(int fd, int argc, char *argv[])
103 struct tone_zone *tz;
104 if (argc != 3 && argc != 4) return RESULT_SHOWUSAGE;
107 /* remove entiry country */
108 if (ast_unregister_indication_country(argv[2])) {
109 ast_log(LOG_WARNING, "Unable to unregister indication country %s\n",argv[2]);
115 tz = ast_get_indication_zone(argv[2]);
117 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n",argv[2],argv[3]);
120 if (ast_unregister_indication(tz,argv[3])) {
121 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n",argv[2],argv[3]);
128 * SHOW INDICATIONS command stuff
130 static int handle_show_indications(int fd, int argc, char *argv[])
132 struct tone_zone *tz;
134 int found_country = 0;
136 if (ast_mutex_lock(&tzlock)) {
137 ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
141 /* no arguments, show a list of countries */
142 ast_cli(fd,"Country Alias Description\n"
143 "===========================\n");
144 for (tz=tone_zones; tz; tz=tz->next) {
145 ast_cli(fd,"%-7.7s %-7.7s %s\n", tz->country, tz->alias, tz->description);
147 ast_mutex_unlock(&tzlock);
150 /* there was a request for specific country(ies), lets humor them */
151 for (tz=tone_zones; tz; tz=tz->next) {
153 for (i=2; i<argc; i++) {
154 if (strcasecmp(tz->country,argv[i])==0 &&
156 struct tone_zone_sound* ts;
157 if (!found_country) {
159 ast_cli(fd,"Country Indication PlayList\n"
160 "=====================================\n");
162 j = snprintf(buf,sizeof(buf),"%-7.7s %-15.15s ",tz->country,"<ringcadance>");
163 for (i=0; i<tz->nrringcadance; i++) {
164 j += snprintf(buf+j,sizeof(buf)-j,"%d,",tz->ringcadance[i]);
166 if (tz->nrringcadance) j--;
167 strncpy(buf+j,"\n",sizeof(buf)-j);
169 for (ts=tz->tones; ts; ts=ts->next)
170 ast_cli(fd,"%-7.7s %-15.15s %s\n",tz->country,ts->name,ts->data);
176 ast_cli(fd,"No countries matched your criteria.\n");
177 ast_mutex_unlock(&tzlock);
182 * Playtones command stuff
184 static int handle_playtones(struct ast_channel *chan, void *data)
186 struct tone_zone_sound *ts;
189 if (!data || !((char*)data)[0]) {
190 ast_log(LOG_NOTICE,"Nothing to play\n");
193 ts = ast_get_indication_tone(chan->zone, (const char*)data);
194 if (ts && ts->data[0])
195 res = ast_playtones_start(chan, 0, ts->data, 0);
197 res = ast_playtones_start(chan, 0, (const char*)data, 0);
199 ast_log(LOG_NOTICE,"Unable to start playtones\n");
204 * StopPlaylist command stuff
206 static int handle_stopplaytones(struct ast_channel *chan, void *data)
208 ast_playtones_stop(chan);
215 static int ind_load_module(void)
217 struct ast_config *cfg;
218 struct ast_variable *v;
221 struct tone_zone *tones;
222 const char *country = NULL;
224 /* that the following cast is needed, is yuk! */
225 /* yup, checked it out. It is NOT written to. */
226 cfg = ast_load((char *)config);
230 /* Use existing config to populate the Indication table */
231 cxt = ast_category_browse(cfg, NULL);
233 /* All categories but "general" are considered countries */
234 if (!strcasecmp(cxt, "general")) {
235 cxt = ast_category_browse(cfg, cxt);
238 tones = malloc(sizeof(struct tone_zone));
240 ast_log(LOG_WARNING,"Out of memory\n");
244 memset(tones,0,sizeof(struct tone_zone));
245 strncpy(tones->country,cxt,sizeof(tones->country));
247 v = ast_variable_browse(cfg, cxt);
249 if (!strcasecmp(v->name, "description")) {
250 strncpy(tones->description, v->value, sizeof(tones->description)-1);
251 } else if (!strcasecmp(v->name,"ringcadance")) {
252 char *ring,*rings = ast_strdupa(v->value);
254 ring = strsep(&c,",");
257 if (!isdigit(ring[0]) || (val=atoi(ring))==-1) {
258 ast_log(LOG_WARNING,"Invalid ringcadance given '%s' at line %d.\n",ring,v->lineno);
259 ring = strsep(&c,",");
262 tmp = realloc(tones->ringcadance,(tones->nrringcadance+1)*sizeof(int));
264 ast_log(LOG_WARNING, "Out of memory\n");
268 tones->ringcadance = tmp;
269 tmp[tones->nrringcadance] = val;
270 tones->nrringcadance++;
272 ring = strsep(&c,",");
274 } else if (!strcasecmp(v->name,"alias")) {
275 char *countries = ast_strdupa(v->value);
277 country = strsep(&c,",");
279 struct tone_zone* azone = malloc(sizeof(struct tone_zone));
281 ast_log(LOG_WARNING,"Out of memory\n");
285 memset(azone,0,sizeof(struct tone_zone));
286 strncpy(azone->country,country,sizeof(azone->country));
287 strncpy(azone->alias, cxt, sizeof(azone->alias)-1);
288 if (ast_register_indication_country(azone)) {
289 ast_log(LOG_WARNING, "Unable to register indication alias at line %d.\n",v->lineno);
293 country = strsep(&c,",");
296 // add tone to country
297 struct tone_zone_sound *ps,*ts;
298 for (ps=NULL,ts=tones->tones; ts; ps=ts, ts=ts->next) {
299 if (strcasecmp(v->name,ts->name)==0) {
301 ast_log(LOG_NOTICE,"Duplicate entry '%s', skipped.\n",v->name);
305 /* not there, add it to the back */
306 ts = malloc(sizeof(struct tone_zone_sound));
308 ast_log(LOG_WARNING, "Out of memory\n");
313 ts->name = strdup(v->name);
314 ts->data = strdup(v->value);
322 if (tones->description[0] || tones->alias[0] || tones->tones) {
323 if (ast_register_indication_country(tones)) {
324 ast_log(LOG_WARNING, "Unable to register indication at line %d.\n",v->lineno);
329 cxt = ast_category_browse(cfg, cxt);
332 /* determine which country is the default */
333 country = ast_variable_retrieve(cfg,"general","country");
334 if (!country || !*country || ast_set_indication_country(country))
335 ast_log(LOG_WARNING,"Unable to set the default country (for indication tones)\n");
342 * CLI entries for commands provided by this module
344 static struct ast_cli_entry add_indication_cli =
345 { { "add", "indication", NULL }, handle_add_indication,
346 "Add the given indication to the country", help_add_indication,
349 static struct ast_cli_entry remove_indication_cli =
350 { { "remove", "indication", NULL }, handle_remove_indication,
351 "Remove the given indication from the country", help_remove_indication,
354 static struct ast_cli_entry show_indications_cli =
355 { { "show", "indications", NULL }, handle_show_indications,
356 "Show a list of all country/indications", help_show_indications,
360 * Standard module functions ...
362 int unload_module(void)
364 /* remove the registed indications... */
365 ast_unregister_indication_country(NULL);
367 /* and the functions */
368 ast_cli_unregister(&add_indication_cli);
369 ast_cli_unregister(&remove_indication_cli);
370 ast_cli_unregister(&show_indications_cli);
371 ast_unregister_application("Playlist");
372 ast_unregister_application("StopPlaylist");
376 int load_module(void)
378 if (ind_load_module()) return -1;
380 ast_cli_register(&add_indication_cli);
381 ast_cli_register(&remove_indication_cli);
382 ast_cli_register(&show_indications_cli);
383 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.");
384 ast_register_application("StopPlaytones", handle_stopplaytones, "Stop playing a tone list","Stop playing a tone list");
391 /* remove the registed indications... */
392 ast_unregister_indication_country(NULL);
394 return ind_load_module();
397 char *description(void)
399 /* that the following cast is needed, is yuk! */
410 return ASTERISK_GPL_KEY;