Add Estonian tone zone (bug #3589)
[asterisk/asterisk.git] / muted.c
diff --git a/muted.c b/muted.c
index 135a416..28f3e04 100755 (executable)
--- a/muted.c
+++ b/muted.c
@@ -3,7 +3,7 @@
  *
  * Specially written for Malcolm Davenport, but I think I'll use it too
  *
- * Copyright (C) 2004, Digium Inc.
+ * Copyright (C)  2004 - 2005, Digium Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
@@ -25,9 +25,9 @@
 
 static char *config = "/etc/muted.conf";
 
-static char host[256];
-static char user[256];
-static char pass[256];
+static char host[256] = "";
+static char user[256] = "";
+static char pass[256] = "";
 static int smoothfade = 0;
 static int mutelevel = 20;
 static int muted = 0;
@@ -98,17 +98,17 @@ static int load_config(void)
                        }
                        if (!strcasecmp(buf, "host")) {
                                if (val && strlen(val))
-                                       strncpy(host, val, sizeof(host));
+                                       strncpy(host, val, sizeof(host) - 1);
                                else
                                        fprintf(stderr, "host needs an argument (the host) at line %d\n", lineno);
                        } else if (!strcasecmp(buf, "user")) {
                                if (val && strlen(val))
-                                       strncpy(user, val, sizeof(user));
+                                       strncpy(user, val, sizeof(user) - 1);
                                else
                                        fprintf(stderr, "user needs an argument (the user) at line %d\n", lineno);
                        } else if (!strcasecmp(buf, "pass")) {
                                if (val && strlen(val))
-                                       strncpy(pass, val, sizeof(pass));
+                                       strncpy(pass, val, sizeof(pass) - 1);
                                else
                                        fprintf(stderr, "pass needs an argument (the password) at line %d\n", lineno);
                        } else if (!strcasecmp(buf, "smoothfade")) {
@@ -264,7 +264,7 @@ static struct channel *find_channel(char *channel)
        char tmp[256] = "";
        char *s, *t;
        struct channel *chan;
-       strncpy(tmp, channel, sizeof(tmp));
+       strncpy(tmp, channel, sizeof(tmp) - 1);
        s = strchr(tmp, '/');
        if (s) {
                *s = '\0';
@@ -428,6 +428,8 @@ static void append_sub(struct channel *chan, char *name)
 static void hangup_chan(char *channel)
 {
        struct channel *chan;
+       if (debug)
+               printf("Hangup '%s'\n", channel);
        chan = find_channel(channel);
        if (chan)
                delete_sub(chan, channel);
@@ -437,6 +439,8 @@ static void hangup_chan(char *channel)
 static void offhook_chan(char *channel)
 {
        struct channel *chan;
+       if (debug)
+               printf("Offhook '%s'\n", channel);
        chan = find_channel(channel);
        if (chan)
                append_sub(chan, channel);
@@ -446,19 +450,25 @@ static void offhook_chan(char *channel)
 static int wait_event(void)
 {
        char *resp;
-       char event[80]="";
-       char channel[80]="";
+       char event[120]="";
+       char channel[120]="";
+       char oldname[120]="";
+       char newname[120]="";
        resp = get_line();
        if (!resp) {
                fprintf(stderr, "disconnected (6)\n");
                return -1;
        }
        if (!strncasecmp(resp, "Event: ", strlen("Event: "))) {
-               strncpy(event, resp + strlen("Event: "), sizeof(event));
+               strncpy(event, resp + strlen("Event: "), sizeof(event) - 1);
                /* Consume the rest of the non-event */
                while((resp = get_line()) && strlen(resp)) {
                        if (!strncasecmp(resp, "Channel: ", strlen("Channel: ")))
-                               strncpy(channel, resp + strlen("Channel: "), sizeof(channel));
+                               strncpy(channel, resp + strlen("Channel: "), sizeof(channel) - 1);
+                       if (!strncasecmp(resp, "Newname: ", strlen("Newname: ")))
+                               strncpy(newname, resp + strlen("Newname: "), sizeof(newname) - 1);
+                       if (!strncasecmp(resp, "Oldname: ", strlen("Oldname: ")))
+                               strncpy(oldname, resp + strlen("Oldname: "), sizeof(oldname) - 1);
                }
                if (strlen(channel)) {
                        if (!strcasecmp(event, "Hangup")) 
@@ -466,6 +476,12 @@ static int wait_event(void)
                        else
                                offhook_chan(channel);
                }
+               if (strlen(newname) && strlen(oldname)) {
+                       if (!strcasecmp(event, "Rename")) {
+                               hangup_chan(oldname);
+                               offhook_chan(newname);
+                       }
+               }
        } else {
                /* Consume the rest of the non-event */
                while((resp = get_line()) && strlen(resp));
@@ -519,8 +535,16 @@ int main(int argc, char *argv[])
        if (needfork)
                daemon(0,0);
        for(;;) {
-               if (wait_event())
-                       exit(1);
+               if (wait_event()) {
+                       fclose(astf);
+                       while(connect_asterisk()) {
+                               sleep(5);
+                       }
+                       if (login_asterisk()) {
+                               fclose(astf);
+                               exit(1);
+                       }
+               }
        }
        exit(0);
 }