+/**
+ * Log a message to the local &SERVER channel, if it exists.
+ */
+GLOBAL void
+Channel_LogServer(const char *msg)
+{
+ CHANNEL *sc;
+ CLIENT *c;
+
+ assert(msg != NULL);
+
+ sc = Channel_Search("&SERVER");
+ if (!sc)
+ return;
+
+ c = Client_ThisServer();
+ Channel_Write(sc, c, c, "PRIVMSG", false, msg);
+} /* Channel_LogServer */
+
+
+GLOBAL bool
+Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
+{
+ char *file_name, line[COMMAND_LEN], *nick, *pass;
+ FILE *fd;
+
+ assert(Chan != NULL);
+ assert(Client != NULL);
+ assert(Key != NULL);
+
+ if (!strchr(Chan->modes, 'k'))
+ return true;
+ if (*Key == '\0')
+ return false;
+ if (strcmp(Chan->key, Key) == 0)
+ return true;
+
+ 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;
+ }
+
+ while (fgets(line, (int)sizeof(line), fd) != NULL) {
+ ngt_TrimStr(line);
+ if (! (nick = strchr(line, ':')))
+ continue;
+ *nick++ = '\0';
+ if (!Match(line, Client_User(Client)))
+ continue;
+ if (! (pass = strchr(nick, ':')))
+ continue;
+ *pass++ = '\0';
+ if (!Match(nick, Client_ID(Client)))
+ continue;
+ if (strcmp(Key, pass) != 0)
+ continue;
+
+ fclose(fd);
+ return true;
+ }
+ fclose(fd);
+ return false;
+} /* Channel_CheckKey */
+
+