Janitor, use ARRAY_LEN() when possible.
[asterisk/asterisk.git] / res / res_indications.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2002, Pauline Middelink
5  *
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17
18 /*! \file res_indications.c 
19  *
20  * \brief Load the indications
21  * 
22  * \author Pauline Middelink <middelink@polyware.nl>
23  *
24  * Load the country specific dialtones into the asterisk PBX.
25  */
26  
27 #include "asterisk.h"
28
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30
31 #include <ctype.h>
32 #include <sys/stat.h>
33
34 #include "asterisk/lock.h"
35 #include "asterisk/file.h"
36 #include "asterisk/cli.h"
37 #include "asterisk/config.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/module.h"
41 #include "asterisk/translate.h"
42 #include "asterisk/indications.h"
43 #include "asterisk/utils.h"
44
45 /*** DOCUMENTATION
46         <application name="PlayTones" language="en_US">
47                 <synopsis>
48                         Play a tone list.
49                 </synopsis>
50                 <syntax>
51                         <parameter name="arg" required="true">
52                                 <para>Arg is either the tone name defined in the <filename>indications.conf</filename>
53                                 configuration file, or a directly specified list of frequencies and durations.</para>
54                         </parameter>
55                 </syntax>
56                 <description>
57                         <para>Plays a tone list. Execution will continue with the next step immediately,
58                         while the tones continue to play.</para>
59                         <para>See the sample <filename>indications.conf</filename> for a description of the
60                         specification of a tonelist.</para>
61                 </description>
62                 <see-also>
63                         <ref type="application">StopPlayTones</ref>
64                 </see-also>
65         </application>
66         <application name="StopPlayTones" language="en_US">
67                 <synopsis>
68                         Stop playing a tone list.
69                 </synopsis>
70                 <syntax />
71                 <description>
72                         <para>Stop playing a tone list, initiated by PlayTones().</para>
73                 </description>
74                 <see-also>
75                         <ref type="application">PlayTones</ref>
76                 </see-also>
77         </application>
78  ***/
79
80 /* Globals */
81 static const char config[] = "indications.conf";
82
83 /*
84  * Implementation of functions provided by this module
85  */
86
87 /*!
88  * \brief Add a country to indication
89  * \param e the ast_cli_entry for this CLI command
90  * \param cmd the reason we are being called
91  * \param a the arguments being passed to us
92  */
93 static char *handle_cli_indication_add(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
94 {
95         struct ind_tone_zone *tz;
96         int created_country = 0;
97
98         switch (cmd) {
99         case CLI_INIT:
100                 e->command = "indication add";
101                 e->usage =
102                         "Usage: indication add <country> <indication> \"<tonelist>\"\n"
103                         "       Add the given indication to the country.\n";
104                 return NULL;
105         case CLI_GENERATE:
106                 return NULL;
107         }
108
109         if (a->argc != 5)
110                 return CLI_SHOWUSAGE;
111
112         tz = ast_get_indication_zone(a->argv[2]);
113         if (!tz) {
114                 /* country does not exist, create it */
115                 ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n", a->argv[2]);
116                 
117                 if (!(tz = ast_calloc(1, sizeof(*tz)))) {
118                         return CLI_FAILURE;
119                 }
120                 ast_copy_string(tz->country, a->argv[2], sizeof(tz->country));
121                 if (ast_register_indication_country(tz)) {
122                         ast_log(LOG_WARNING, "Unable to register new country\n");
123                         ast_free(tz);
124                         return CLI_FAILURE;
125                 }
126                 created_country = 1;
127         }
128         if (ast_register_indication(tz, a->argv[3], a->argv[4])) {
129                 ast_log(LOG_WARNING, "Unable to register indication %s/%s\n", a->argv[2], a->argv[3]);
130                 if (created_country)
131                         ast_unregister_indication_country(a->argv[2]);
132                 return CLI_FAILURE;
133         }
134         return CLI_SUCCESS;
135 }
136
137 /*!
138  * \brief Remove a country from indication
139  * \param e the ast_cli_entry for this CLI command
140  * \param cmd the reason we are being called
141  * \param a the arguments being passed to us
142  */
143 static char *handle_cli_indication_remove(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
144 {
145         struct ind_tone_zone *tz;
146
147         switch (cmd) {
148         case CLI_INIT:
149                 e->command = "indication remove";
150                 e->usage =
151                         "Usage: indication remove <country> <indication>\n"
152                         "       Remove the given indication from the country.\n";
153                 return NULL;
154         case CLI_GENERATE:
155                 return NULL;
156         }
157
158         if (a->argc != 3 && a->argc != 4)
159                 return CLI_SHOWUSAGE;
160
161         if (a->argc == 3) {
162                 /* remove entiry country */
163                 if (ast_unregister_indication_country(a->argv[2])) {
164                         ast_log(LOG_WARNING, "Unable to unregister indication country %s\n", a->argv[2]);
165                         return CLI_FAILURE;
166                 }
167                 return CLI_SUCCESS;
168         }
169
170         tz = ast_get_indication_zone(a->argv[2]);
171         if (!tz) {
172                 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s, country does not exists\n", a->argv[2], a->argv[3]);
173                 return CLI_FAILURE;
174         }
175         if (ast_unregister_indication(tz, a->argv[3])) {
176                 ast_log(LOG_WARNING, "Unable to unregister indication %s/%s\n", a->argv[2], a->argv[3]);
177                 return CLI_FAILURE;
178         }
179         return CLI_SUCCESS;
180 }
181
182 /*!
183  * \brief Show the current indications
184  * \param e the ast_cli_entry for this CLI command
185  * \param cmd the reason we are being called
186  * \param a the arguments being passed to us
187  */
188 static char *handle_cli_indication_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
189 {
190         struct ind_tone_zone *tz = NULL;
191         char buf[256];
192         int found_country = 0;
193
194         switch (cmd) {
195         case CLI_INIT:
196                 e->command = "indication show";
197                 e->usage =
198                         "Usage: indication show [<country> ...]\n"
199                         "       Display either a condensed for of all country/indications, or the\n"
200                         "       indications for the specified countries.\n";
201                 return NULL;
202         case CLI_GENERATE:
203                 return NULL;
204         }
205
206         if (a->argc == 2) {
207                 /* no arguments, show a list of countries */
208                 ast_cli(a->fd, "Country Alias   Description\n");
209                 ast_cli(a->fd, "===========================\n");
210                 while ((tz = ast_walk_indications(tz)))
211                         ast_cli(a->fd, "%-7.7s %-7.7s %s\n", tz->country, tz->alias, tz->description);
212                 return CLI_SUCCESS;
213         }
214         /* there was a request for specific country(ies), lets humor them */
215         while ((tz = ast_walk_indications(tz))) {
216                 int i, j;
217                 for (i = 2; i < a->argc; i++) {
218                         if (strcasecmp(tz->country, a->argv[i]) == 0 && !tz->alias[0]) {
219                                 struct ind_tone_zone_sound *ts;
220                                 if (!found_country) {
221                                         found_country = 1;
222                                         ast_cli(a->fd, "Country Indication      PlayList\n");
223                                         ast_cli(a->fd, "=====================================\n");
224                                 }
225                                 j = snprintf(buf, sizeof(buf), "%-7.7s %-15.15s ", tz->country, "<ringcadence>");
226                                 for (i = 0; i < tz->nrringcadence; i++) {
227                                         j += snprintf(buf + j, sizeof(buf) - j, "%d,", tz->ringcadence[i]);
228                                 }
229                                 if (tz->nrringcadence)
230                                         j--;
231                                 ast_copy_string(buf + j, "\n", sizeof(buf) - j);
232                                 ast_cli(a->fd, "%s", buf);
233                                 AST_LIST_TRAVERSE(&tz->tones, ts, list) {
234                                         ast_cli(a->fd, "%-7.7s %-15.15s %s\n", tz->country, ts->name, ts->data);
235                                 }
236                                 break;
237                         }
238                 }
239         }
240         if (!found_country)
241                 ast_cli(a->fd, "No countries matched your criteria.\n");
242         return CLI_SUCCESS;
243 }
244
245 /*!
246  * \brief play tone for indication country
247  * \param chan ast_channel to play the sounds back to
248  * \param data contains tone to play
249  */
250 static int handle_playtones(struct ast_channel *chan, void *data)
251 {
252         struct ind_tone_zone_sound *ts;
253         int res;
254
255         if (!data || !((char*)data)[0]) {
256                 ast_log(LOG_NOTICE,"Nothing to play\n");
257                 return -1;
258         }
259         ts = ast_get_indication_tone(chan->zone, (const char*)data);
260         if (ts && ts->data[0])
261                 res = ast_playtones_start(chan, 0, ts->data, 0);
262         else
263                 res = ast_playtones_start(chan, 0, (const char*)data, 0);
264         if (res)
265                 ast_log(LOG_NOTICE,"Unable to start playtones\n");
266         return res;
267 }
268
269 /*!
270  * \brief Stop tones playing
271  * \param chan 
272  * \param data 
273  */
274 static int handle_stopplaytones(struct ast_channel *chan, void *data)
275 {
276         ast_playtones_stop(chan);
277         return 0;
278 }
279
280 /*! \brief load indications module */
281 static int ind_load_module(int reload)
282 {
283         struct ast_config *cfg;
284         struct ast_variable *v;
285         char *cxt;
286         char *c;
287         struct ind_tone_zone *tones;
288         const char *country = NULL;
289         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
290
291         /* that the following cast is needed, is yuk! */
292         /* yup, checked it out. It is NOT written to. */
293         cfg = ast_config_load((char *)config, config_flags);
294         if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
295                 return -1;
296         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
297                 return 0;
298         }
299
300         if (reload)
301                 ast_unregister_indication_country(NULL);
302
303         /* Use existing config to populate the Indication table */
304         cxt = ast_category_browse(cfg, NULL);
305         while(cxt) {
306                 /* All categories but "general" are considered countries */
307                 if (!strcasecmp(cxt, "general")) {
308                         cxt = ast_category_browse(cfg, cxt);
309                         continue;
310                 }               
311                 if (!(tones = ast_calloc(1, sizeof(*tones)))) {
312                         ast_config_destroy(cfg);
313                         return -1;
314                 }
315                 ast_copy_string(tones->country,cxt,sizeof(tones->country));
316
317                 v = ast_variable_browse(cfg, cxt);
318                 while(v) {
319                         if (!strcasecmp(v->name, "description")) {
320                                 ast_copy_string(tones->description, v->value, sizeof(tones->description));
321                         } else if ((!strcasecmp(v->name,"ringcadence"))||(!strcasecmp(v->name,"ringcadance"))) {
322                                 char *ring,*rings = ast_strdupa(v->value);
323                                 c = rings;
324                                 ring = strsep(&c,",");
325                                 while (ring) {
326                                         int *tmp, val;
327                                         if (!isdigit(ring[0]) || (val=atoi(ring))==-1) {
328                                                 ast_log(LOG_WARNING,"Invalid ringcadence given '%s' at line %d.\n",ring,v->lineno);
329                                                 ring = strsep(&c,",");
330                                                 continue;
331                                         }                                       
332                                         if (!(tmp = ast_realloc(tones->ringcadence, (tones->nrringcadence + 1) * sizeof(int)))) {
333                                                 ast_config_destroy(cfg);
334                                                 ast_destroy_indication_zone(tones);
335                                                 return -1;
336                                         }
337                                         tones->ringcadence = tmp;
338                                         tmp[tones->nrringcadence] = val;
339                                         tones->nrringcadence++;
340                                         /* next item */
341                                         ring = strsep(&c,",");
342                                 }
343                         } else if (!strcasecmp(v->name,"alias")) {
344                                 char *countries = ast_strdupa(v->value);
345                                 c = countries;
346                                 country = strsep(&c,",");
347                                 while (country) {
348                                         struct ind_tone_zone* azone;
349                                         if (!(azone = ast_calloc(1, sizeof(*azone)))) {
350                                                 ast_config_destroy(cfg);
351                                                 ast_destroy_indication_zone(tones);
352                                                 return -1;
353                                         }
354                                         ast_copy_string(azone->country, country, sizeof(azone->country));
355                                         ast_copy_string(azone->alias, cxt, sizeof(azone->alias));
356                                         if (ast_register_indication_country(azone)) {
357                                                 ast_log(LOG_WARNING, "Unable to register indication alias at line %d.\n",v->lineno);
358                                                 ast_destroy_indication_zone(tones);
359                                         }
360                                         /* next item */
361                                         country = strsep(&c,",");
362                                 }
363                         } else {
364                                 struct ind_tone_zone_sound *ts;
365
366                                 /* add tone to country */
367                                 AST_LIST_TRAVERSE(&tones->tones, ts, list) {
368                                         if (!strcasecmp(v->name, ts->name)) {
369                                                 /* already there */
370                                                 ast_log(LOG_NOTICE, "Duplicate entry '%s' skipped.\n", v->name);
371                                                 goto out;
372                                         }
373                                 }
374
375                                 /* not there, add it to the back */
376                                 if (!(ts = ast_calloc(1, sizeof(*ts)))) {
377                                         ast_config_destroy(cfg);
378                                         return -1;
379                                 }
380                                 ts->name = ast_strdup(v->name);
381                                 ts->data = ast_strdup(v->value);
382
383                                 AST_LIST_INSERT_TAIL(&tones->tones, ts, list);
384                         }
385 out:                    v = v->next;
386                 }
387                 if (tones->description[0] || tones->alias[0] || !AST_LIST_EMPTY(&tones->tones)) {
388                         if (ast_register_indication_country(tones)) {
389                                 ast_log(LOG_WARNING, "Unable to register indication at line %d.\n",v->lineno);
390                                 ast_destroy_indication_zone(tones);
391                         }
392                 } else {
393                         ast_destroy_indication_zone(tones);
394                 }
395
396                 cxt = ast_category_browse(cfg, cxt);
397         }
398
399         /* determine which country is the default */
400         country = ast_variable_retrieve(cfg,"general","country");
401         if (ast_strlen_zero(country) || ast_set_indication_country(country)) {
402                 ast_log(LOG_WARNING,"Unable to set the default country (for indication tones)\n");
403         }
404
405         ast_config_destroy(cfg);
406         return 0;
407 }
408
409 /*! \brief CLI entries for commands provided by this module */
410 static struct ast_cli_entry cli_indications[] = {
411         AST_CLI_DEFINE(handle_cli_indication_add,    "Add the given indication to the country"),
412         AST_CLI_DEFINE(handle_cli_indication_remove, "Remove the given indication from the country"),
413         AST_CLI_DEFINE(handle_cli_indication_show,   "Display a list of all countries/indications")
414 };
415
416 /*! \brief Unload indicators module */
417 static int unload_module(void)
418 {
419         /* remove the registed indications... */
420         ast_unregister_indication_country(NULL);
421
422         /* and the functions */
423         ast_cli_unregister_multiple(cli_indications, ARRAY_LEN(cli_indications));
424         ast_unregister_application("PlayTones");
425         ast_unregister_application("StopPlayTones");
426         return 0;
427 }
428
429
430 /*! \brief Load indications module */
431 static int load_module(void)
432 {
433         if (ind_load_module(0))
434                 return AST_MODULE_LOAD_DECLINE; 
435         ast_cli_register_multiple(cli_indications, ARRAY_LEN(cli_indications));
436         ast_register_application_xml("PlayTones", handle_playtones);
437         ast_register_application_xml("StopPlayTones", handle_stopplaytones);
438
439         return AST_MODULE_LOAD_SUCCESS;
440 }
441
442 /*! \brief Reload indications module */
443 static int reload(void)
444 {
445         return ind_load_module(1);
446 }
447
448 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Region-specific tones",
449                 .load = load_module,
450                 .unload = unload_module,
451                 .reload = reload,
452                );