summaryrefslogtreecommitdiff
path: root/miniircd
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2011-09-22 21:38:15 +0200
committerJoel Rosdahl <joel@rosdahl.net>2011-11-17 21:49:43 +0100
commit8ef97d05eb2e6b0090c9eb0c8f3799884ac3a2dd (patch)
treed62cbeb58d2628b512573741e649a4b5e52b9853 /miniircd
parent37c80c181a21dec4278a565a3069272c064a71ed (diff)
downloadminiircd-8ef97d05eb2e6b0090c9eb0c8f3799884ac3a2dd.tar.gz
miniircd-8ef97d05eb2e6b0090c9eb0c8f3799884ac3a2dd.zip
Add support for channel keys
Diffstat (limited to 'miniircd')
-rwxr-xr-xminiircd117
1 files changed, 79 insertions, 38 deletions
diff --git a/miniircd b/miniircd
index 36953ea..d2ddc83 100755
--- a/miniircd
+++ b/miniircd
@@ -39,6 +39,7 @@ class Channel(object):
self.name = name
self.members = set()
self.topic = ""
+ self.key = None
def add_member(self, client):
self.members.add(client)
@@ -181,33 +182,43 @@ class Client(object):
self.channel_log(channel, "left", meta=True)
server.remove_member_from_channel(self, channelname)
self.channels = {}
+ return
+ channelnames = arguments[0].split(",")
+ if len(arguments) > 1:
+ keys = arguments[1].split(",")
else:
- for channelname in arguments[0].split(","):
- if irc_lower(arguments[0]) in self.channels:
- pass
- elif not valid_channel_re.match(channelname):
- self.reply_403(channelname)
- else:
- server.add_member_to_channel(self, channelname)
- channel = server.get_channel(channelname)
- self.channels[irc_lower(channelname)] = channel
- self.message_channel(
- channel, "JOIN", channelname, True)
- self.channel_log(channel, "joined", meta=True)
- if channel.topic:
- self.reply(
- "332 %s %s :%s"
- % (self.nickname, channel.name, channel.topic))
- else:
- self.reply("331 %s %s :No topic is set"
- % (self.nickname, channel.name))
- self.reply("353 %s = %s :%s"
- % (self.nickname,
- channelname,
- " ".join([x.nickname
- for x in channel.members])))
- self.reply("366 %s %s :End of NAMES list"
- % (self.nickname, channelname))
+ keys = []
+ keys.extend((len(channelnames) - len(keys)) * [None])
+ for (i, channelname) in enumerate(channelnames):
+ if channelname in self.channels:
+ continue
+ if not valid_channel_re.match(channelname):
+ self.reply_403(channelname)
+ continue
+ channel = server.get_channel(channelname)
+ if channel and channel.key != keys[i]:
+ self.reply(
+ "475 %s %s :Cannot join channel (+k) - bad key"
+ % (self.nickname, channelname))
+ continue
+ server.add_member_to_channel(self, channelname)
+ channel = server.get_channel(channelname)
+ self.channels[irc_lower(channelname)] = channel
+ self.message_channel(channel, "JOIN", channelname, True)
+ self.channel_log(channel, "joined", meta=True)
+ if channel.topic:
+ self.reply("332 %s %s :%s"
+ % (self.nickname, channel.name, channel.topic))
+ else:
+ self.reply("331 %s %s :No topic is set"
+ % (self.nickname, channel.name))
+ self.reply("353 %s = %s :%s"
+ % (self.nickname,
+ channelname,
+ " ".join([x.nickname
+ for x in channel.members])))
+ self.reply("366 %s %s :End of NAMES list"
+ % (self.nickname, channelname))
def list_handler():
if len(arguments) < 1:
@@ -231,21 +242,51 @@ class Client(object):
targetname = arguments[0]
channel = server.get_channel(targetname)
if channel:
- if len(arguments) > 1:
- modes = arguments[1]
+ if len(arguments) < 2:
+ if channel.key:
+ modes = "+k"
+ if irc_lower(channel.name) in self.channels:
+ modes += " %s" % channel.key
+ else:
+ modes = "+"
+ self.reply("324 %s %s %s"
+ % (self.nickname, targetname, modes))
+ return
+ flag = arguments[1]
+ if flag == "+k":
+ if len(arguments) < 3:
+ self.reply_461("JOIN")
+ return
+ key = arguments[2]
+ if irc_lower(channel.name) in self.channels:
+ channel.key = key
+ self.message_channel(
+ channel, "MODE", "%s +k %s" % (channel.name, key),
+ True)
+ self.channel_log(
+ channel, "set channel key to %s" % key, meta=True)
+ else:
+ self.reply("442 %s: You're not on that channel")
+ elif flag == "-k":
+ if irc_lower(channel.name) in self.channels:
+ channel.key = None
+ self.message_channel(
+ channel, "MODE", "%s -k" % channel.name,
+ True)
+ self.channel_log(
+ channel, "removed channel key", meta=True)
+ else:
+ self.reply("442 %s: You're not on that channel")
+ else:
self.reply("472 %s %s :Unknown MODE flag"
- % (self.nickname, modes))
+ % (self.nickname, flag))
+ elif targetname == self.nickname:
+ if len(arguments) == 1:
+ self.reply("221 %s +" % self.nickname)
else:
- self.reply("324 %s %s +"
- % (self.nickname, targetname))
+ self.reply("501 %s :Unknown MODE flag" % self.nickname)
else:
- if targetname == self.nickname:
- if len(arguments) == 1:
- self.reply("221 %s +" % self.nickname)
- else:
- self.reply("501 %s :Unknown MODE flag" % self.nickname)
- else:
- self.reply_403(targetname)
+ self.reply_403(targetname)
def motd_handler():
self.send_motd()