]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/channel.c
Refactoring: Rename CONNECTION.res_stat to .proc_stat
[ngircd-alex.git] / src / ngircd / channel.c
index 6d23b249e1773fc51924f5e1672567fb38f4c5c7..0fa38debc2c1d56e60f9477798076a2ed7ffc10d 100644 (file)
@@ -34,7 +34,6 @@
 
 #include "imp.h"
 #include "irc-write.h"
-#include "resolve.h"
 #include "conf.h"
 #include "hash.h"
 #include "lists.h"
@@ -61,9 +60,7 @@ static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
 static void Delete_Channel PARAMS(( CHANNEL *Chan ));
 static void Free_Channel PARAMS(( CHANNEL *Chan ));
-static void Update_Predefined PARAMS((CHANNEL *Chan,
-                                     const struct Conf_Channel *Conf_Chan));
-static void Set_Key_File PARAMS((CHANNEL *Chan, FILE *KeyFile));
+static void Set_KeyFile PARAMS((CHANNEL *Chan, const char *KeyFile));
 
 
 GLOBAL void
@@ -124,7 +121,7 @@ Channel_InitPredefined( void )
                        Log(LOG_INFO,
                            "Can't create pre-defined channel \"%s\": name already in use.",
                            conf_chan->name);
-                       Update_Predefined(new_chan, conf_chan);
+                       Set_KeyFile(new_chan, conf_chan->keyfile);
                        continue;
                }
 
@@ -148,7 +145,7 @@ Channel_InitPredefined( void )
 
                Channel_SetKey(new_chan, conf_chan->key);
                Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
-               Update_Predefined(new_chan, conf_chan);
+               Set_KeyFile(new_chan, conf_chan->keyfile);
        }
        if (channel_count)
                array_free(&Conf_Channels);
@@ -159,10 +156,9 @@ static void
 Free_Channel(CHANNEL *chan)
 {
        array_free(&chan->topic);
+       array_free(&chan->keyfile);
        Lists_Free(&chan->list_bans);
        Lists_Free(&chan->list_invites);
-       if (Chan->keyfile)
-               fclose(Chan->keyfile);
 
        free(chan);
 }
@@ -201,7 +197,7 @@ Channel_Exit( void )
  * Add_Client().
  */
 GLOBAL bool
-Channel_Join( CLIENT *Client, char *Name )
+Channel_Join( CLIENT *Client, const char *Name )
 {
        CHANNEL *chan;
 
@@ -328,7 +324,7 @@ Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
 
 
 GLOBAL void
-Channel_Quit( CLIENT *Client, char *Reason )
+Channel_Quit( CLIENT *Client, const char *Reason )
 {
        CHANNEL *c, *next_c;
 
@@ -734,7 +730,7 @@ Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
 
 
 GLOBAL void
-Channel_SetModes( CHANNEL *Chan, char *Modes )
+Channel_SetModes( CHANNEL *Chan, const char *Modes )
 {
        assert( Chan != NULL );
        assert( Modes != NULL );
@@ -1045,7 +1041,7 @@ Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
  * Log a message to the local &SERVER channel, if it exists.
  */
 GLOBAL void
-Channel_LogServer(char *msg)
+Channel_LogServer(const char *msg)
 {
        CHANNEL *sc;
        CLIENT *c;
@@ -1064,7 +1060,8 @@ Channel_LogServer(char *msg)
 GLOBAL bool
 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
 {
-       char line[COMMAND_LEN], *nick, *pass;
+       char *file_name, line[COMMAND_LEN], *nick, *pass;
+       FILE *fd;
 
        assert(Chan != NULL);
        assert(Client != NULL);
@@ -1074,11 +1071,20 @@ Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
                return true;
        if (strcmp(Chan->key, Key) == 0)
                return true;
-       if (!Chan->keyfile)
+       if (*Key == '\0')
+               return false;
+
+       file_name = array_start(&Chan->keyfile);
+       if (!file_name)
                return false;
+       fd = fopen(file_name, "r");
+       if (!fd) {
+               Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
+                   file_name, Chan->name, strerror(errno));
+               return false;
+       }
 
-       Chan->keyfile = freopen(NULL, "r", Chan->keyfile);
-       while (fgets(line, sizeof(line), Chan->keyfile) != NULL) {
+       while (fgets(line, (int)sizeof(line), fd) != NULL) {
                ngt_TrimStr(line);
                if (! (nick = strchr(line, ':')))
                        continue;
@@ -1093,8 +1099,10 @@ Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
                if (strcmp(Key, pass) != 0)
                        continue;
 
+               fclose(fd);
                return true;
        }
+       fclose(fd);
        return false;
 } /* Channel_CheckKey */
 
@@ -1157,35 +1165,31 @@ Delete_Channel(CHANNEL *Chan)
 
 
 static void
-Update_Predefined(CHANNEL *Chan, const struct Conf_Channel *Conf_Chan)
+Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
 {
-       FILE *fd;
-
-       if (! Conf_Chan->keyfile || ! *Conf_Chan->keyfile)
-               return;
+       size_t len;
 
-       fd = fopen(Conf_Chan->keyfile, "r");
-       if (! fd)
-               Log(LOG_ERR,
-                   "Can't open channel key file for \"%s\", \"%s\": %s",
-                   Conf_Chan->name, Conf_Chan->keyfile,
-                   strerror(errno));
-       else
-               Set_Key_File(Chan, fd);
-} /* Update_Predefined */
+       assert(Chan != NULL);
+       assert(KeyFile != NULL);
 
+       len = strlen(KeyFile);
+       if (len < array_bytes(&Chan->keyfile)) {
+               Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
+               array_free(&Chan->keyfile);
+       }
 
-static void
-Set_Key_File(CHANNEL *Chan, FILE *KeyFile)
-{
-       assert(Chan != NULL);
+       if (len < 1)
+               return;
 
-       if (Chan->keyfile)
-               fclose(Chan->keyfile);
-       Chan->keyfile = KeyFile;
-       Log(LOG_INFO|LOG_snotice,
-           "New local channel key file for \"%s\" activated.", Chan->name);
-} /* Set_Key_File */
+       if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
+               Log(LOG_WARNING,
+                   "Could not set new channel key file \"%s\" for %s: %s",
+                   KeyFile, Chan->name, strerror(errno));
+       else
+               Log(LOG_INFO|LOG_snotice,
+                   "New local channel key file \"%s\" for %s activated.",
+                   KeyFile, Chan->name);
+} /* Set_KeyFile */
 
 
 /* -eof- */