Update copyright year.
[umurmur.git] / src / channel.c
index 942c34847debc64c6589c31aa311ca0e08b2de98..d58d3e3d174b255d93a8afdab13b914543b43798 100644 (file)
@@ -1,5 +1,5 @@
-/* Copyright (C) 2009-2010, Martin Johansson <martin@fatbob.nu>
-   Copyright (C) 2005-2010, Thorvald Natvig <thorvald@natvig.com>
+/* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
+   Copyright (C) 2005-2012, Thorvald Natvig <thorvald@natvig.com>
 
    All rights reserved.
 
@@ -29,6 +29,8 @@
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 #include <limits.h>
+#include <stdlib.h>
+#include <string.h>
 #include "log.h"
 #include "list.h"
 #include "client.h"
@@ -49,8 +51,9 @@ static channel_t *createChannel(int id, const char *name, const char *desc)
                Log_fatal("out of memory");
        memset(ch, 0, sizeof(channel_t));
        ch->id = id;
-       strncpy(ch->name, name, MAX_TEXT);
-       strncpy(ch->desc, desc, MAX_TEXT);
+       ch->name = strdup(name);
+       if (desc)
+               ch->desc = strdup(desc);
        init_list_entry(&ch->subs);
        init_list_entry(&ch->node);
        init_list_entry(&ch->clients);
@@ -139,7 +142,7 @@ void Chan_init()
        conf_channel_link_t chlink;
        const char *defaultChannelName;
 
-       defaultChannelName = getStrConf(DEAFULT_CHANNEL);
+       defaultChannelName = getStrConf(DEFAULT_CHANNEL);
        
        for (i = 0; ; i++) {
                if (Conf_getNextChannel(&chdesc, i) < 0) {
@@ -149,6 +152,7 @@ void Chan_init()
                }
                if (i == 0) {
                        rootChan = createChannel(0, chdesc.name, chdesc.description);
+                       rootChan->noenter = chdesc.noenter;
                        list_add_tail(&rootChan->flatlist_node, &channels);
                        if (strcmp(defaultChannelName, chdesc.name) == 0)
                                defaultChan = rootChan;
@@ -156,9 +160,13 @@ void Chan_init()
                else {
                        channel_t *ch, *ch_itr = NULL;
                        ch = Chan_createChannel(chdesc.name, chdesc.description);
-                       
+                       ch->noenter = chdesc.noenter;
+                       if (chdesc.password) {
+                               Log_info("Setting password on channel '%s'", ch->name); 
+                               ch->password = strdup(chdesc.password);
+                       }
                        if (strcmp(defaultChannelName, chdesc.name) == 0) {
-                               Log_info("Setting default channel %s", ch->name); 
+                               Log_info("Setting default channel '%s'", ch->name); 
                                defaultChan = ch;
                        }
                        
@@ -167,7 +175,7 @@ void Chan_init()
                        } while (ch_itr != NULL && strcmp(ch_itr->name, chdesc.parent) != 0);
                        
                        if (ch_itr == NULL)
-                               Log_fatal("Error in channel configuration: parent not found");
+                               Log_fatal("Error in channel configuration: parent '%s' not found", chdesc.parent);
                        else {
                                Chan_addChannel(ch_itr, ch);
                                Log_info("Adding channel '%s' parent '%s'", ch->name, chdesc.parent);
@@ -176,6 +184,11 @@ void Chan_init()
        }
        if (defaultChan == NULL)
                defaultChan = rootChan;
+       
+       if (defaultChan->noenter)
+               Log_fatal("Error in channel configuration: default channel is marked as noenter");
+       if (defaultChan->password)
+               Log_fatal("Error in channel configuration: default channel has a password");
 
        /* Channel links */
        for (i = 0; ; i++) {
@@ -190,7 +203,8 @@ void Chan_init()
                        Chan_iterate(&ch_itr);
                } while (ch_itr != NULL && strcmp(ch_itr->name, chlink.source) != 0);
                if (ch_itr == NULL)
-                       Log_fatal("Error in channel link configuration: source channel '%s' not found.", chlink.source);
+                       Log_fatal("Error in channel link configuration: source channel '%s' not found.",
+                                         chlink.source);
                else
                        ch_src = ch_itr;
                
@@ -199,22 +213,31 @@ void Chan_init()
                        Chan_iterate(&ch_itr);
                } while (ch_itr != NULL && strcmp(ch_itr->name, chlink.destination) != 0);
                if (ch_itr == NULL)
-                       Log_fatal("Error in channel link configuration: destination channel '%s' not found", chlink.destination);
+                       Log_fatal("Error in channel link configuration: destination channel '%s' not found",
+                                         chlink.destination);
                else
                        ch_dst = ch_itr;
                
                list_add_tail(&ch_dst->link_node, &ch_src->channel_links);
-               Log_info("Adding channel link %s -> %s", ch_src->name, ch_dst->name);
+               ch_src->linkcount++;
+               Log_info("Adding channel link '%s' -> '%s'", ch_src->name, ch_dst->name);
        }
 }
 
 void Chan_free()
 {
        struct dlist *itr, *save;
+       channel_t *ch;
        
        list_iterate_safe(itr, save, &channels) {
-               Log_debug("Free channel %s", list_get_entry(itr, channel_t, flatlist_node)->name);
-               free(list_get_entry(itr, channel_t, flatlist_node));
+               ch = list_get_entry(itr, channel_t, flatlist_node);
+               Log_debug("Free channel '%s'", ch->name);
+               free(ch->name);
+               if (ch->desc)
+                       free(ch->desc);
+               if (ch->password)
+                       free(ch->password);
+               free(ch);
        }
 }
 
@@ -241,14 +264,11 @@ void Chan_addChannel(channel_t *parent, channel_t *ch)
 }
 
 
-int Chan_playerJoin(channel_t *ch, client_t *client)
+int Chan_userLeave(client_t *client)
 {
        channel_t *leaving = NULL;
        int leaving_id = -1;
        
-       /* Only allowed in one channel at a time */
-       Log_debug("Add player %s to channel %s", client->playerName, ch->name); 
-
        if (client->channel) {
                list_del(&client->chan_node);
                leaving = (channel_t *)client->channel;
@@ -257,12 +277,26 @@ int Chan_playerJoin(channel_t *ch, client_t *client)
                        Chan_freeChannel(leaving);
                }
        }
+       return leaving_id;
+}
+
+int Chan_userJoin(channel_t *ch, client_t *client)
+{
+       int leaving_id;
+
+       /* Do nothing if user already is in this channel */
+       if ((channel_t *)client->channel == ch)
+               return 0;
+       
+       Log_debug("Add user %s to channel %s", client->username, ch->name); 
+       /* Only allowed in one channel at a time */
+       leaving_id = Chan_userLeave(client);
        list_add_tail(&client->chan_node, &ch->clients);
        client->channel = (void *)ch;
        return leaving_id;
 }
 
-int Chan_playerJoin_id(int channelid, client_t *client)
+int Chan_userJoin_id(int channelid, client_t *client)
 {
        channel_t *ch_itr = NULL;
        do {
@@ -273,7 +307,24 @@ int Chan_playerJoin_id(int channelid, client_t *client)
                return -1;
        }
        else
-               return Chan_playerJoin(ch_itr, client); 
+               return Chan_userJoin(ch_itr, client);   
+}
+
+channelJoinResult_t Chan_userJoin_id_test(int channelid, client_t *client)
+{
+       channel_t *ch_itr = NULL;
+       do {
+               Chan_iterate(&ch_itr);
+       } while (ch_itr != NULL && ch_itr->id != channelid);
+       if (ch_itr == NULL) {
+               Log_warn("Channel id %d not found - ignoring.", channelid);
+               return CHJOIN_NOTFOUND;
+       }
+       else if (ch_itr->noenter)
+               return CHJOIN_NOENTER;
+       else if (ch_itr->password && !Client_token_match(client, ch_itr->password))
+               return CHJOIN_WRONGPW;
+       else return CHJOIN_OK;
 }
 
 #if 0
@@ -305,3 +356,28 @@ void Chan_removeChannel(channel_t *ch)
 {
        list_del(&ch->node);
 }
+
+void Chan_buildTreeList(channel_t *ch, struct dlist *head)
+{
+       channellist_t *chl;
+       struct dlist *itr;
+       channel_t *sub;
+       
+       chl = malloc(sizeof(channellist_t));
+       chl->chan = ch;
+       init_list_entry(&chl->node);
+       list_add_tail(&chl->node, head);
+
+       list_iterate(itr, &ch->subs) {
+               sub = list_get_entry(itr, channel_t, node);
+               Chan_buildTreeList(sub, head);
+       }
+}
+
+void Chan_freeTreeList(struct dlist *head)
+{
+       struct dlist *itr, *save;
+       list_iterate_safe(itr, save, head) {
+               free(list_get_entry(itr, channellist_t, node));
+       }
+}