Add Estonian tone zone (bug #3589)
[asterisk/asterisk.git] / muted.c
diff --git a/muted.c b/muted.c
index c4e03dd..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;
@@ -36,11 +36,16 @@ static int debug = 0;
 static int stepsize = 3;
 static int mixchan = SOUND_MIXER_VOLUME;
 
+struct subchannel {
+       char *name;
+       struct subchannel *next;
+};
+
 static struct channel {
        char *tech;
        char *location;
        struct channel *next;
-       int offhook;
+       struct subchannel *subs;
 } *channels;
 
 static void add_channel(char *tech, char *location)
@@ -93,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")) {
@@ -259,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';
@@ -370,7 +375,7 @@ static void check_mute(void)
        struct channel *chan;
        chan = channels;
        while(chan) {
-               if (chan->offhook) {
+               if (chan->subs) {
                        offhook++;
                        break;
                }
@@ -382,40 +387,88 @@ static void check_mute(void)
                unmute();
 }
 
+static void delete_sub(struct channel *chan, char *name)
+{
+       struct subchannel *sub, *prev;
+       prev = NULL;
+       sub = chan->subs;
+       while(sub) {
+               if (!strcasecmp(sub->name, name)) {
+                       if (prev)
+                               prev->next = sub->next;
+                       else
+                               chan->subs = sub->next;
+                       free(sub->name);
+                       free(sub);
+                       return;
+               }
+               prev = sub;
+               sub = sub->next;
+       }
+}
+
+static void append_sub(struct channel *chan, char *name)
+{
+       struct subchannel *sub;
+       sub = chan->subs;
+       while(sub) {
+               if (!strcasecmp(sub->name, name)) 
+                       return;
+               sub = sub->next;
+       }
+       sub = malloc(sizeof(struct subchannel));
+       if (sub) {
+               memset(sub, 0, sizeof(struct subchannel));
+               sub->name = strdup(name);
+               sub->next = chan->subs;
+               chan->subs = sub;
+       }
+}
+
 static void hangup_chan(char *channel)
 {
        struct channel *chan;
+       if (debug)
+               printf("Hangup '%s'\n", channel);
        chan = find_channel(channel);
        if (chan)
-               chan->offhook = 0;
+               delete_sub(chan, channel);
        check_mute();
 }
 
 static void offhook_chan(char *channel)
 {
        struct channel *chan;
+       if (debug)
+               printf("Offhook '%s'\n", channel);
        chan = find_channel(channel);
        if (chan)
-               chan->offhook = 1;
+               append_sub(chan, channel);
        check_mute();
 }
 
 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")) 
@@ -423,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));
@@ -476,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);
 }