Commits

Mike Steder committed 0424225

Implemented PART

Comments (0)

Files changed (3)

     """PART: part #<channel>
 
     """
-    channelName = message.parameters[0]
-    channel = db.getChannel(channelName)
-    channel.part(userId)
-
+    if len(message.parameters) >= 1:
+        channelNames = message.parameters[0].upper().split(",")
+        for channelName in channelNames:
+            try:
+                channel = db.getChannel(channelName)
+                if userId in channel.users:
+                    user = db.getUser(userId)
+                    remainingUsers = list(channel.part(userId))
+                    remainingUsers.sort()
+                    if remainingUsers:
+                        po.send(remainingUsers, ":%s %s %s"%(db.serverName, "PART", user.nick))
+                    else:
+                        db.channelByName[channelName] = None
+                else:
+                    po.send([userId], ":%s %s %s"%(db.serverName, irc.ERR_NOTONCHANNEL, "Not on channel"))
+            except KeyError:
+                po.send([userId], ":%s %s %s"%(db.serverName, irc.ERR_NOSUCHCHANNEL, "No such channel"))
+    else:
+        po.send([userId], ":%s %s %s"%(db.serverName, irc.ERR_NEEDMOREPARAMS, "Part needs a channel name"))
 
 # MODE
 # TOPIC
         self.users.add(userId)
         self.userModes[userId] = set([])
 
+    def part(self, userId):
+        self.users.remove(userId)
+        if self.users == set([]):
+            return []
+        return self.users
 
 class DB(object):
     """Remembers things about the current state of the server"""
     
     def getChannel(self, channelName):
         return self.channelByName[channelName]
+

noir/test/commands/test_channel.py

     ERR_NEEDMOREPARAMS, ERR_NOSUCHCHANNEL
     ERR_NOTONCHANNEL
     """
+    def setUp(self):
+        self.db = models.DB()
+        socket, address = "Fake", "127.0.0.1"
+        self.u1 = self.db.addUser(socket, address)
+        self.u2 = self.db.addUser(socket, address)
+        self.u3 = self.db.addUser(socket, address)
+        self.po = mocks.MockPo()
+        self.db.setNick(self.u1, "alpha")
+        self.db.setNick(self.u2, "bravo")
+        self.db.setNick(self.u3, "charlie")
+        self.command = commands.COMMANDS["PART"]
 
+    def tearDown(self):
+        self.po.send.reset_mock()
 
+    def test_needsMoreParams(self):
+        m = messages.Message(None, "PART", [])
+        self.command(self.db, self.po, self.u1, m)
+        self.po.send.assert_called_with([self.u1],
+            ":%s %s Part needs a channel name"%(self.db.serverName,
+                                                irc.ERR_NEEDMOREPARAMS)
+        )
+
+    def test_noSuchChannel(self):
+        m = messages.Message(None, "PART", ["#fake"])
+        self.command(self.db, self.po, self.u1, m)
+        self.po.send.assert_called_with([self.u1], ":%s %s No such channel"%(
+            self.db.serverName, irc.ERR_NOSUCHCHANNEL))
+
+    def test_notOnChannel(self):
+        self.db.joinChannel("#alphabet")
+        m = messages.Message(None, "PART", ["#alphabet"])
+        self.command(self.db, self.po, self.u1, m)
+        self.po.send.assert_called_with([self.u1], ":%s %s Not on channel"%(
+            self.db.serverName,
+            irc.ERR_NOTONCHANNEL
+        ))
+
+    def test_part_lastPersonInChannel(self):
+        alpha = self.db.joinChannel("#alphabet")
+        alpha.join(self.u1)
+        m = messages.Message(None, "PART", ["#alphabet"])
+        self.command(self.db, self.po, self.u1, m)
+        calls = self.po.send.call_args_list
+        self.assertEqual(calls, [
+            ])
+        self.assertEquals(self.db.channelByName.get("#ALPHABET", None), None)
+
+    def test_part_1userInChannel(self):
+        alpha = self.db.joinChannel("#alphabet")
+        alpha.join(self.u1)
+        alpha.join(self.u2)
+        m = messages.Message(None, "PART", ["#alphabet"])
+        self.command(self.db, self.po, self.u1, m)
+        calls = self.po.send.call_args_list
+        self.assertEqual(calls, [
+            (([self.u2], ":%s PART %s"%(self.db.serverName, "alpha")), {}),
+            ])
+        userIds = [i for i in alpha.users]
+        userIds.sort()
+        self.assertEquals(userIds, [self.u2])
+
+    def test_part_2usersInChannel(self):
+        alpha = self.db.joinChannel("#alphabet")
+        alpha.join(self.u1)
+        alpha.join(self.u2)
+        alpha.join(self.u3)
+        m = messages.Message(None, "PART", ["#alphabet"])
+        self.command(self.db, self.po, self.u1, m)
+        calls = self.po.send.call_args_list
+        self.assertEqual(calls, [
+            (([self.u2, self.u3], ":%s PART %s"%(self.db.serverName, "alpha")), {}),            
+            ])
+        userIds = [i for i in alpha.users]
+        userIds.sort()
+        self.assertEquals(userIds, [self.u2, self.u3])
+
+    def test_part2(self):
+        alpha = self.db.joinChannel("#alphabet")
+        alpha.join(self.u1)
+        alpha.join(self.u2)
+        alpha.join(self.u3)
+        channel = self.db.joinChannel("#channel")
+        channel.join(self.u1)
+        channel.join(self.u2)
+        m = messages.Message(None, "PART", ["#alphabet,#channel"])
+        self.command(self.db, self.po, self.u1, m)
+        calls = self.po.send.call_args_list
+        self.assertEqual(calls, [
+            (([self.u2, self.u3], ":%s PART %s"%(self.db.serverName, "alpha")), {}),
+            (([self.u2], ":%s PART %s"%(self.db.serverName, "alpha")), {}),
+            ])
+
+        
 class TestLIST(unittest2.TestCase):
     """Lists channels and their topics
     LIST