Skip to content

Commit

Permalink
Fix irc server example (#265)
Browse files Browse the repository at this point in the history
* Fix and enhance IRC-Server example

* Implement LIST command
  • Loading branch information
spaceone authored and prologic committed Jul 1, 2019
1 parent fcc82e6 commit 5f3a327
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
4 changes: 4 additions & 0 deletions circuits/protocols/irc/replies.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ def RPL_WHOISCHANNELS(nick, channels):
return _M(u("319"), nick, u(":{0}".format(u(" ").join(channels))))


def RPL_LISTSTART(header=None):
return _M(u("321"), header or u("Channels :Users Name"))


def RPL_LIST(channel, nvisible, topic):
return _M(u("322"), channel, u("{0}").format(nvisible), topic)

Expand Down
33 changes: 28 additions & 5 deletions examples/ircd.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from circuits.protocols.irc.replies import (
ERR_NICKNAMEINUSE, ERR_NOMOTD, ERR_NOSUCHCHANNEL, ERR_NOSUCHNICK,
ERR_UNKNOWNCOMMAND, RPL_ENDOFNAMES, RPL_ENDOFWHO, RPL_NAMEREPLY,
RPL_NOTOPIC, RPL_WELCOME, RPL_WHOREPLY, RPL_YOURHOST,
RPL_NOTOPIC, RPL_TOPIC, RPL_WELCOME, RPL_WHOREPLY, RPL_YOURHOST,
RPL_CHANNELMODEIS, RPL_LISTSTART, RPL_LIST, RPL_LISTEND,
)

__version__ = "0.0.1"
Expand Down Expand Up @@ -62,6 +63,8 @@ class Channel(object):

def __init__(self, name):
self.name = name
self.topic = None
self.mode = '+n'

self.users = []

Expand All @@ -74,6 +77,7 @@ def __init__(self, sock, host, port):
self.port = port

self.nick = None
self.mode = ''
self.away = False
self.channels = []
self.signon = None
Expand Down Expand Up @@ -179,7 +183,7 @@ def disconnect(self, sock):
user, host = user.userinfo.user, user.userinfo.host

yield self.call(
response.create("quit", sock, (nick, user, host), "Leavling")
response.create("quit", sock, (nick, user, host), "Leaving")
)

del self.users[sock]
Expand Down Expand Up @@ -262,9 +266,12 @@ def join(self, sock, source, name):
Message("JOIN", name, prefix=user.prefix)
)

self.fire(reply(sock, RPL_NOTOPIC(name)))
self.fire(reply(sock, RPL_NAMEREPLY(channel)))
self.fire(reply(sock, RPL_ENDOFNAMES()))
if channel.topic:
self.fire(reply(sock, RPL_TOPIC(channel.topic)))
else:
self.fire(reply(sock, RPL_NOTOPIC(channel.name)))
self.fire(reply(sock, RPL_NAMEREPLY(channel.name, [u.prefix for u in channel.users])))
self.fire(reply(sock, RPL_ENDOFNAMES(channel.name)))

def part(self, sock, source, name, reason="Leaving"):
user = self.users[sock]
Expand Down Expand Up @@ -341,6 +348,22 @@ def reply(self, target, message):

self.fire(write(target, bytes(message)))

def mode(self, sock, source, mask, mode=None, params=None):
if mask.startswith('#'):
if mask not in self.channels:
return self.fire(reply(sock, ERR_NOSUCHCHANNEL(mask)))
channel = self.channels[mask]
if not params:
self.fire(reply(sock, RPL_CHANNELMODEIS(channel.name, channel.mode)))
elif mask not in self.users:
return self.fire(reply(sock, ERR_NOSUCHNICK(mask)))

def list(self, sock, source):
self.fire(reply(sock, RPL_LISTSTART()))
for channel in self.channels.values():
self.fire(reply(sock, RPL_LIST(channel, str(len(channel.users)), channel.topic or '')))
self.fire(reply(sock, RPL_LISTEND()))

@property
def commands(self):
exclude = {"ready", "connect", "disconnect", "read", "write"}
Expand Down

0 comments on commit 5f3a327

Please sign in to comment.