stringfields conversion for struct sip_peer, as requested :-)
[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                                 for (ts = tz->tones; ts; ts = ts->next)
234                                         ast_cli(a->fd, "%-7.7s %-15.15s %s\n", tz->country, ts->name, ts->data);
235                                 break;
236                         }
237                 }
238         }
239         if (!found_country)
240                 ast_cli(a->fd, "No countries matched your criteria.\n");
241         return CLI_SUCCESS;
242 }
243
244 /*!
245  * \brief play tone for indication country
246  * \param chan ast_channel to play the sounds back to
247  * \param data contains tone to play
248  */
249 static int handle_playtones(struct ast_channel *chan, void *data)
250 {
251         struct ind_tone_zone_sound *ts;
252         int res;
253
254         if (!data || !((char*)data)[0]) {
255                 ast_log(LOG_NOTICE,"Nothing to play\n");
256                 return -1;
257         }
258         ts = ast_get_indication_tone(chan->zone, (const char*)data);
259         if (ts && ts->data[0])
260                 res = ast_playtones_start(chan, 0, ts->data, 0);
261         else
262                 res = ast_playtones_start(chan, 0, (const char*)data, 0);
263         if (res)
264                 ast_log(LOG_NOTICE,"Unable to start playtones\n");
265         return res;
266 }
267
268 /*!
269  * \brief Stop tones playing
270  * \param chan 
271  * \param data 
272  */
273 static int handle_stopplaytones(struct ast_channel *chan, void *data)
274 {
275         ast_playtones_stop(chan);
276         return 0;
277 }
278
279 /* helper function to delete a tone_zone in its entirety */
280 static inline void free_zone(struct ind_tone_zone* zone)
281 {
282         while (zone->tones) {
283                 struct ind_tone_zone_sound *tmp = zone->tones->next;
284                 ast_free((void *)zone->tones->name);
285                 ast_free((void *)zone->tones->data);
286                 ast_free(zone->tones);
287                 zone->tones = tmp;
288         }
289
290         if (zone->ringcadence)
291                 ast_free(zone->ringcadence);
292
293         ast_free(zone);
294 }
295
296 /*! \brief load indications module */
297 static int ind_load_module(int reload)
298 {
299         struct ast_config *cfg;
300         struct ast_variable *v;
301         char *cxt;
302         char *c;
303         struct ind_tone_zone *tones;
304         const char *country = NULL;
305         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
306
307         /* that the following cast is needed, is yuk! */
308         /* yup, checked it out. It is NOT written to. */
309         cfg = ast_config_load((char *)config, config_flags);
310         if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
311                 return -1;
312         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
313                 return 0;
314         }
315
316         if (reload)
317                 ast_unregister_indication_country(NULL);
318
319         /* Use existing config to populate the Indication table */
320         cxt = ast_category_browse(cfg, NULL);
321         while(cxt) {
322                 /* All categories but "general" are considered countries */
323                 if (!strcasecmp(cxt, "general")) {
324                         cxt = ast_category_browse(cfg, cxt);
325                         continue;
326                 }               
327                 if (!(tones = ast_calloc(1, sizeof(*tones)))) {
328                         ast_config_destroy(cfg);
329                         return -1;
330                 }
331                 ast_copy_string(tones->country,cxt,sizeof(tones->country));
332
333                 v = ast_variable_browse(cfg, cxt);
334                 while(v) {
335                         if (!strcasecmp(v->name, "description")) {
336                                 ast_copy_string(tones->description, v->value, sizeof(tones->description));
337                         } else if ((!strcasecmp(v->name,"ringcadence"))||(!strcasecmp(v->name,"ringcadance"))) {
338                                 char *ring,*rings = ast_strdupa(v->value);
339                                 c = rings;
340                                 ring = strsep(&c,",");
341                                 while (ring) {
342                                         int *tmp, val;
343                                         if (!isdigit(ring[0]) || (val=atoi(ring))==-1) {
344                                                 ast_log(LOG_WARNING,"Invalid ringcadence given '%s' at line %d.\n",ring,v->lineno);
345                                                 ring = strsep(&c,",");
346                                                 continue;
347                                         }                                       
348                                         if (!(tmp = ast_realloc(tones->ringcadence, (tones->nrringcadence + 1) * sizeof(int)))) {
349                                                 ast_config_destroy(cfg);
350                                                 free_zone(tones);
351                                                 return -1;
352                                         }
353                                         tones->ringcadence = tmp;
354                                         tmp[tones->nrringcadence] = val;
355                                         tones->nrringcadence++;
356                                         /* next item */
357                                         ring = strsep(&c,",");
358                                 }
359                         } else if (!strcasecmp(v->name,"alias")) {
360                                 char *countries = ast_strdupa(v->value);
361                                 c = countries;
362                                 country = strsep(&c,",");
363                                 while (country) {
364                                         struct ind_tone_zone* azone;
365                                         if (!(azone = ast_calloc(1, sizeof(*azone)))) {
366                                                 ast_config_destroy(cfg);
367                                                 free_zone(tones);
368                                                 return -1;
369                                         }
370                                         ast_copy_string(azone->country, country, sizeof(azone->country));
371                                         ast_copy_string(azone->alias, cxt, sizeof(azone->alias));
372                                         if (ast_register_indication_country(azone)) {
373                                                 ast_log(LOG_WARNING, "Unable to register indication alias at line %d.\n",v->lineno);
374                                                 free_zone(tones);
375                                         }
376                                         /* next item */
377                                         country = strsep(&c,",");
378                                 }
379                         } else {
380                                 /* add tone to country */
381                                 struct ind_tone_zone_sound *ps,*ts;
382                                 for (ps=NULL,ts=tones->tones; ts; ps=ts, ts=ts->next) {
383                                         if (strcasecmp(v->name,ts->name)==0) {
384                                                 /* already there */
385                                                 ast_log(LOG_NOTICE,"Duplicate entry '%s', skipped.\n",v->name);
386                                                 goto out;
387                                         }
388                                 }
389                                 /* not there, add it to the back */                             
390                                 if (!(ts = ast_malloc(sizeof(*ts)))) {
391                                         ast_config_destroy(cfg);
392                                         return -1;
393                                 }
394                                 ts->next = NULL;
395                                 ts->name = ast_strdup(v->name);
396                                 ts->data = ast_strdup(v->value);
397                                 if (ps)
398                                         ps->next = ts;
399                                 else
400                                         tones->tones = ts;
401                         }
402 out:                    v = v->next;
403                 }
404                 if (tones->description[0] || tones->alias[0] || tones->tones) {
405                         if (ast_register_indication_country(tones)) {
406                                 ast_log(LOG_WARNING, "Unable to register indication at line %d.\n",v->lineno);
407                                 free_zone(tones);
408                         }
409                 } else {
410                         free_zone(tones);
411                 }
412
413                 cxt = ast_category_browse(cfg, cxt);
414         }
415
416         /* determine which country is the default */
417         country = ast_variable_retrieve(cfg,"general","country");
418         if (ast_strlen_zero(country) || ast_set_indication_country(country)) {
419                 ast_log(LOG_WARNING,"Unable to set the default country (for indication tones)\n");
420         }
421
422         ast_config_destroy(cfg);
423         return 0;
424 }
425
426 /*! \brief CLI entries for commands provided by this module */
427 static struct ast_cli_entry cli_indications[] = {
428         AST_CLI_DEFINE(handle_cli_indication_add,    "Add the given indication to the country"),
429         AST_CLI_DEFINE(handle_cli_indication_remove, "Remove the given indication from the country"),
430         AST_CLI_DEFINE(handle_cli_indication_show,   "Display a list of all countries/indications")
431 };
432
433 /*! \brief Unload indicators module */
434 static int unload_module(void)
435 {
436         /* remove the registed indications... */
437         ast_unregister_indication_country(NULL);
438
439         /* and the functions */
440         ast_cli_unregister_multiple(cli_indications, sizeof(cli_indications) / sizeof(struct ast_cli_entry));
441         ast_unregister_application("PlayTones");
442         ast_unregister_application("StopPlayTones");
443         return 0;
444 }
445
446
447 /*! \brief Load indications module */
448 static int load_module(void)
449 {
450         if (ind_load_module(0))
451                 return AST_MODULE_LOAD_DECLINE; 
452         ast_cli_register_multiple(cli_indications, sizeof(cli_indications) / sizeof(struct ast_cli_entry));
453         ast_register_application_xml("PlayTones", handle_playtones);
454         ast_register_application_xml("StopPlayTones", handle_stopplaytones);
455
456         return AST_MODULE_LOAD_SUCCESS;
457 }
458
459 /*! \brief Reload indications module */
460 static int reload(void)
461 {
462         return ind_load_module(1);
463 }
464
465 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Region-specific tones",
466                 .load = load_module,
467                 .unload = unload_module,
468                 .reload = reload,
469                );