Permalink
Browse files

add INVITE command

  • Loading branch information...
1 parent fb23056 commit b6bad0057fcd3ffa61ba6f119aada016d9fb785f @danlarkin danlarkin committed Mar 14, 2011
Showing with 88 additions and 0 deletions.
  1. +1 −0 HISTORY
  2. +36 −0 src/subrosa/commands.clj
  3. +51 −0 test/subrosa/test/rooms.clj
View
@@ -1,4 +1,5 @@
0.8 ??
+* Add INVITE command.
0.7 2011-03-13
* End all messages to clients with \r\n.
View
@@ -366,3 +366,39 @@
(raise {:type :client-error
:code 461
:msg "ISON :Not enough parameters"})))
+
+(defcommand invite [channel args]
+ (let [sender-nick (nick-for-channel channel)
+ [target-nick room-name] (.split args " " 2)
+ do-invite (fn []
+ (send-to-client* (channel-for-nick target-nick)
+ (format ":%s INVITE %s %s"
+ (format-client channel)
+ target-nick
+ room-name))
+ (send-to-client (channel-for-nick sender-nick)
+ 341
+ (format "%s %s" room-name target-nick)))]
+ (if (not (or (empty? target-nick)
+ (empty? room-name)))
+ (if (user-for-nick target-nick)
+ (if (not (nick-in-room? target-nick room-name))
+ (if (room-for-name room-name)
+ (if (nick-in-room? sender-nick room-name)
+ (do-invite)
+ (raise {:type :client-error
+ :code 442
+ :msg (format "%s :You're not on that channel"
+ room-name)}))
+ (do-invite))
+ (raise {:type :client-error
+ :code 443
+ :msg (format "%s %s :is already on channel"
+ target-nick
+ room-name)}))
+ (raise {:type :client-error
+ :code 401
+ :msg (format "%s :No such nick/channel" target-nick)}))
+ (raise {:type :client-error
+ :code 461
+ :msg "INVITE :Not enough parameters"}))))
@@ -186,3 +186,54 @@
(transmit s "USER dan 0 * :Dan Larkin")
(transmit s "LIST #foo")
(is (not-received? s #"322"))))
+
+(deftest invite-error-conditions
+ (with-connection s0
+ (transmit s0 "NICK dan0")
+ (transmit s0 "USER dan 0 * :Dan Larkin")
+ (transmit s0 "JOIN #dan0")
+ (with-connection s
+ (transmit s "NICK dan")
+ (transmit s "USER dan 0 * :Dan Larkin")
+ (transmit s "JOIN #foo")
+ (with-connection s2
+ (transmit s2 "NICK dan2")
+ (transmit s2 "USER dan 0 * :Dan Larkin")
+ (transmit s2 "JOIN #foo")
+ (Thread/sleep 1000)
+ (transmit s "INVITE")
+ (is (received? s #"461 dan INVITE :Not enough parameters"))
+ (reset! (:received s) [])
+ (transmit s "INVITE foo")
+ (is (received? s #"461 dan INVITE :Not enough parameters"))
+ (transmit s "INVITE notanick #foo")
+ (is (received? s #"401 dan notanick :No such nick/channel"))
+ (transmit s "INVITE dan2 #foo")
+ (is (received? s #"443 dan dan2 #foo :is already on channel"))
+ (transmit s "INVITE dan2 #dan0")
+ (is (received? s #"442 dan #dan0 :You're not on that channel"))))))
+
+(deftest successful-invite-to-channel-im-on
+ (with-connection s
+ (transmit s "NICK dan")
+ (transmit s "USER dan 0 * :Dan Larkin")
+ (transmit s "JOIN #foo")
+ (with-connection s2
+ (transmit s2 "NICK dan2")
+ (transmit s2 "USER dan 0 * :Dan Larkin")
+ (Thread/sleep 1000)
+ (transmit s "INVITE dan2 #foo")
+ (is (received? s2 #"dan!dan@.* INVITE dan2 #foo"))
+ (is (received? s #"341 dan #foo dan2")))))
+
+(deftest successful-invite-to-channel-im-not-on
+ (with-connection s
+ (transmit s "NICK dan")
+ (transmit s "USER dan 0 * :Dan Larkin")
+ (with-connection s2
+ (transmit s2 "NICK dan2")
+ (transmit s2 "USER dan 0 * :Dan Larkin")
+ (Thread/sleep 1000)
+ (transmit s "INVITE dan2 #foo")
+ (is (received? s2 #"dan!dan@.* INVITE dan2 #foo"))
+ (is (received? s #"341 dan #foo dan2")))))

0 comments on commit b6bad00

Please sign in to comment.