Merged revisions 29196 via svnmerge from
[asterisk/asterisk.git] / res / res_indications.c
old mode 100755 (executable)
new mode 100644 (file)
index cc744ec..f4777e3
@@ -1,15 +1,25 @@
-/** @file res_indications.c 
- *
- * Asterisk -- A telephony toolkit for Linux.
+/*
+ * Asterisk -- An open source telephony toolkit.
  *
- * Load the indications
- * 
  * Copyright (C) 2002, Pauline Middelink
  *
- * Pauline Middelink <middelink@polyware.nl>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
  *
  * This program is free software, distributed under the terms of
- * the GNU General Public License
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file res_indications.c 
+ *
+ * \brief Load the indications
+ * 
+ * \author Pauline Middelink <middelink@polyware.nl>
  *
  * Load the country specific dialtones into the asterisk PBX.
  */
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <asterisk/lock.h>
-#include <asterisk/file.h>
-#include <asterisk/cli.h>
-#include <asterisk/logger.h>
-#include <asterisk/config.h>
-#include <asterisk/channel.h>
-#include <asterisk/pbx.h>
-#include <asterisk/module.h>
-#include <asterisk/translate.h>
-#include <asterisk/indications.h>
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/lock.h"
+#include "asterisk/file.h"
+#include "asterisk/cli.h"
+#include "asterisk/logger.h"
+#include "asterisk/config.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/module.h"
+#include "asterisk/translate.h"
+#include "asterisk/indications.h"
+#include "asterisk/utils.h"
 
 /* Globals */
-static const char dtext[] = "Indications Configuration";
 static const char config[] = "indications.conf";
 
 /*
@@ -54,12 +69,12 @@ static char help_show_indications[] =
 "       indications for the specified countries.\n";
 
 char *playtones_desc=
-"PlayTone(arg): Plays a tone list. Execution will continue with the next step immediately,\n"
+"PlayTones(arg): Plays a tone list. Execution will continue with the next step immediately,\n"
 "while the tones continue to play.\n"
 "Arg is either the tone name defined in the indications.conf configuration file, or a directly\n"
 "specified list of frequencies and durations.\n"
-"See indications.conf for a description of the specification of a tonelist.\n\n"
-"Use the StopPlaytones application to stop the tones playing. \n";
+"See the sample indications.conf for a description of the specification of a tonelist.\n\n"
+"Use the StopPlayTones application to stop the tones playing. \n";
 
 /*
  * Implementation of functions provided by this module
@@ -78,14 +93,11 @@ static int handle_add_indication(int fd, int argc, char *argv[])
        if (!tz) {
                /* country does not exist, create it */
                ast_log(LOG_NOTICE, "Country '%s' does not exist, creating it.\n",argv[2]);
-
-               tz = malloc(sizeof(struct tone_zone));
-               if (!tz) {
-                       ast_log(LOG_WARNING, "Out of memory\n");
+               
+               if (!(tz = ast_calloc(1, sizeof(*tz)))) {
                        return -1;
                }
-               memset(tz,0,sizeof(struct tone_zone));
-               strncpy(tz->country,argv[2],sizeof(tz->country)-1);
+               ast_copy_string(tz->country,argv[2],sizeof(tz->country));
                if (ast_register_indication_country(tz)) {
                        ast_log(LOG_WARNING, "Unable to register new country\n");
                        free(tz);
@@ -136,26 +148,20 @@ static int handle_remove_indication(int fd, int argc, char *argv[])
  */
 static int handle_show_indications(int fd, int argc, char *argv[])
 {
-       struct tone_zone *tz;
+       struct tone_zone *tz = NULL;
        char buf[256];
        int found_country = 0;
 
-       if (ast_mutex_lock(&tzlock)) {
-               ast_log(LOG_WARNING, "Unable to lock tone_zones list\n");
-               return 0;
-       }
        if (argc == 2) {
                /* no arguments, show a list of countries */
                ast_cli(fd,"Country Alias   Description\n"
                           "===========================\n");
-               for (tz=tone_zones; tz; tz=tz->next) {
+               while ( (tz = ast_walk_indications(tz) ) )
                        ast_cli(fd,"%-7.7s %-7.7s %s\n", tz->country, tz->alias, tz->description);
-               }
-               ast_mutex_unlock(&tzlock);
                return 0;
        }
        /* there was a request for specific country(ies), lets humor them */
-       for (tz=tone_zones; tz; tz=tz->next) {
+       while ( (tz = ast_walk_indications(tz) ) ) {
                int i,j;
                for (i=2; i<argc; i++) {
                        if (strcasecmp(tz->country,argv[i])==0 &&
@@ -166,12 +172,13 @@ static int handle_show_indications(int fd, int argc, char *argv[])
                                        ast_cli(fd,"Country Indication      PlayList\n"
                                                   "=====================================\n");
                                }
-                               j = snprintf(buf,sizeof(buf),"%-7.7s %-15.15s ",tz->country,"<ringcadance>");
-                               for (i=0; i<tz->nrringcadance; i++) {
-                                       j += snprintf(buf+j,sizeof(buf)-j,"%d,",tz->ringcadance[i]);
+                               j = snprintf(buf,sizeof(buf),"%-7.7s %-15.15s ",tz->country,"<ringcadence>");
+                               for (i=0; i<tz->nrringcadence; i++) {
+                                       j += snprintf(buf+j,sizeof(buf)-j,"%d,",tz->ringcadence[i]);
                                }
-                               if (tz->nrringcadance) j--;
-                               strncpy(buf+j,"\n",sizeof(buf)-j-1);
+                               if (tz->nrringcadence)
+                                       j--;
+                               ast_copy_string(buf+j,"\n",sizeof(buf)-j);
                                ast_cli(fd,buf);
                                for (ts=tz->tones; ts; ts=ts->next)
                                        ast_cli(fd,"%-7.7s %-15.15s %s\n",tz->country,ts->name,ts->data);
@@ -181,7 +188,6 @@ static int handle_show_indications(int fd, int argc, char *argv[])
        }
        if (!found_country)
                ast_cli(fd,"No countries matched your criteria.\n");
-       ast_mutex_unlock(&tzlock);
        return -1;
 }
 
@@ -241,40 +247,35 @@ static int ind_load_module(void)
                if (!strcasecmp(cxt, "general")) {
                        cxt = ast_category_browse(cfg, cxt);
                        continue;
-               }
-               tones = malloc(sizeof(struct tone_zone));
-               if (!tones) {
-                       ast_log(LOG_WARNING,"Out of memory\n");
+               }               
+               if (!(tones = ast_calloc(1, sizeof(*tones)))) {
                        ast_config_destroy(cfg);
                        return -1;
                }
-               memset(tones,0,sizeof(struct tone_zone));
-               strncpy(tones->country,cxt,sizeof(tones->country) - 1);
+               ast_copy_string(tones->country,cxt,sizeof(tones->country));
 
                v = ast_variable_browse(cfg, cxt);
                while(v) {
                        if (!strcasecmp(v->name, "description")) {
-                               strncpy(tones->description, v->value, sizeof(tones->description)-1);
-                       } else if (!strcasecmp(v->name,"ringcadance")) {
+                               ast_copy_string(tones->description, v->value, sizeof(tones->description));
+                       } else if ((!strcasecmp(v->name,"ringcadence"))||(!strcasecmp(v->name,"ringcadance"))) {
                                char *ring,*rings = ast_strdupa(v->value);
                                c = rings;
                                ring = strsep(&c,",");
                                while (ring) {
                                        int *tmp, val;
                                        if (!isdigit(ring[0]) || (val=atoi(ring))==-1) {
-                                               ast_log(LOG_WARNING,"Invalid ringcadance given '%s' at line %d.\n",ring,v->lineno);
+                                               ast_log(LOG_WARNING,"Invalid ringcadence given '%s' at line %d.\n",ring,v->lineno);
                                                ring = strsep(&c,",");
                                                continue;
-                                       }
-                                       tmp = realloc(tones->ringcadance,(tones->nrringcadance+1)*sizeof(int));
-                                       if (!tmp) {
-                                               ast_log(LOG_WARNING, "Out of memory\n");
+                                       }                                       
+                                       if (!(tmp = ast_realloc(tones->ringcadence, (tones->nrringcadence + 1) * sizeof(int)))) {
                                                ast_config_destroy(cfg);
                                                return -1;
                                        }
-                                       tones->ringcadance = tmp;
-                                       tmp[tones->nrringcadance] = val;
-                                       tones->nrringcadance++;
+                                       tones->ringcadence = tmp;
+                                       tmp[tones->nrringcadence] = val;
+                                       tones->nrringcadence++;
                                        /* next item */
                                        ring = strsep(&c,",");
                                }
@@ -283,15 +284,13 @@ static int ind_load_module(void)
                                c = countries;
                                country = strsep(&c,",");
                                while (country) {
-                                       struct tone_zone* azone = malloc(sizeof(struct tone_zone));
-                                       if (!azone) {
-                                               ast_log(LOG_WARNING,"Out of memory\n");
+                                       struct tone_zone* azone;
+                                       if (!(azone = ast_calloc(1, sizeof(*azone)))) {
                                                ast_config_destroy(cfg);
                                                return -1;
                                        }
-                                       memset(azone,0,sizeof(struct tone_zone));
-                                       strncpy(azone->country, country, sizeof(azone->country) - 1);
-                                       strncpy(azone->alias, cxt, sizeof(azone->alias)-1);
+                                       ast_copy_string(azone->country, country, sizeof(azone->country));
+                                       ast_copy_string(azone->alias, cxt, sizeof(azone->alias));
                                        if (ast_register_indication_country(azone)) {
                                                ast_log(LOG_WARNING, "Unable to register indication alias at line %d.\n",v->lineno);
                                                free(tones);
@@ -309,10 +308,8 @@ static int ind_load_module(void)
                                                goto out;
                                        }
                                }
-                               /* not there, add it to the back */
-                               ts = malloc(sizeof(struct tone_zone_sound));
-                               if (!ts) {
-                                       ast_log(LOG_WARNING, "Out of memory\n");
+                               /* not there, add it to the back */                             
+                               if (!(ts = ast_malloc(sizeof(*ts)))) {
                                        ast_config_destroy(cfg);
                                        return -1;
                                }
@@ -366,7 +363,7 @@ static struct ast_cli_entry show_indications_cli =
 /*
  * Standard module functions ...
  */
-int unload_module(void)
+static int unload_module(void *mod)
 {
        /* remove the registed indications... */
        ast_unregister_indication_country(NULL);
@@ -375,26 +372,27 @@ int unload_module(void)
        ast_cli_unregister(&add_indication_cli);
        ast_cli_unregister(&remove_indication_cli);
        ast_cli_unregister(&show_indications_cli);
-       ast_unregister_application("Playtones");
-       ast_unregister_application("StopPlaytones");
+       ast_unregister_application("PlayTones");
+       ast_unregister_application("StopPlayTones");
        return 0;
 }
 
 
-int load_module(void)
+static int load_module(void *mod)
 {
-       if (ind_load_module()) return -1;
+       if (ind_load_module())
+               return -1;
  
        ast_cli_register(&add_indication_cli);
        ast_cli_register(&remove_indication_cli);
        ast_cli_register(&show_indications_cli);
-       ast_register_application("Playtones", handle_playtones, "Play a tone list", playtones_desc);
-       ast_register_application("StopPlaytones", handle_stopplaytones, "Stop playing a tone list","Stop playing a tone list");
+       ast_register_application("PlayTones", handle_playtones, "Play a tone list", playtones_desc);
+       ast_register_application("StopPlayTones", handle_stopplaytones, "Stop playing a tone list","Stop playing a tone list");
 
        return 0;
 }
 
-int reload(void)
+static int reload(void *mod)
 {
        /* remove the registed indications... */
        ast_unregister_indication_country(NULL);
@@ -402,18 +400,14 @@ int reload(void)
        return ind_load_module();
 }
 
-char *description(void)
+static const char *description(void)
 {
-       /* that the following cast is needed, is yuk! */
-       return (char*)dtext;
-}
-
-int usecount(void)
-{
-       return 0;
+       return "Indications Configuration";
 }
 
-char *key()
+static const char *key(void)
 {
        return ASTERISK_GPL_KEY;
 }
+
+STD_MOD(MOD_0 | NO_USECOUNT, reload, NULL, NULL);