Commit 3f09f3e

mo <mo.khan@gmail.com>
2018-04-28 03:57:50
join hipchat room.
1 parent a3d0329
lib/del/connection.rb
@@ -6,6 +6,7 @@ module Del
       @configuration = configuration
       @rooms = configuration[:rooms]
       @users = configuration[:users]
+      @mucs = {}
     end
 
     def connect(robot)
@@ -18,16 +19,31 @@ module Del
       client.auth(configuration[:password])
       roster = Jabber::Roster::Helper.new(client, false)
       roster.add_update_callback do |old_item, item|
-        users.upsert(item['jid'], item.attributes.to_h) if item
+        users.upsert(item['jid'], User.new(item['jid'], item)) if item
       end
       roster.get_roster
       roster.wait_for_roster
       client.add_message_callback do |message|
         next if message.type == :error || message.body.nil?
-        robot.receive(message)
+        user = configuration[:users].find_by(message.from.strip)
+        robot.receive(message.body, source: Source.new(user: user))
       end
       client.send(Jabber::Presence.new(:chat))
-      configuration[:default_rooms].each { |room| join(room, robot) }
+      configuration[:default_rooms].each do |room|
+        Del.logger.debug("Joining #{room} as #{robot.name}")
+        room_jid = jid_for(room, configuration[:muc_domain].dup, robot.name)
+        stripped_jid = room_jid.strip.to_s
+        next if @mucs[stripped_jid]
+
+        muc = Jabber::MUC::SimpleMUCClient.new(client)
+        @mucs[stripped_jid] = muc
+        muc.on_message do |_, nickname, message|
+          Del.logger.debug([nickname, message].inspect)
+          other_jid = roster.items.find { |jid, item| item.iname == nickname }
+          robot.receive(message, source: Source.new(user: User.new(other_jid[0], other_jid[1]), room: stripped_jid))
+        end
+        muc.join(room_jid)
+      end
       list_rooms(configuration[:muc_domain]).each do |room|
         rooms.upsert(room)
       end
@@ -51,7 +67,7 @@ module Del
     end
 
     def jid
-      @jid ||= normalize_jid(configuration[:jid], "chat.hipchat.com", "bot")
+      @jid ||= jid_for(configuration[:jid], "chat.hipchat.com", "bot")
     end
 
     def list_rooms(muc_domain)
@@ -60,16 +76,11 @@ module Del
       end
     end
 
-    def join(room, robot)
-      Del.logger.debug("Joining #{room} as #{robot.name}")
-      normalize_jid(room, configuration[:muc_domain], robot.name)
-    end
-
     def encode_string(s)
       s.encode("UTF-8", invalid: :replace, undef: :replace)
     end
 
-    def normalize_jid(jid, domain, resource)
+    def jid_for(jid, domain, resource)
       jid = Jabber::JID.new(jid)
       jid.resource = resource
       unless jid.node
lib/del/default_router.rb
@@ -1,7 +1,7 @@
 module Del
   class DefaultRouter
     def route(message)
-      Del.logger.debug(message)
+      Del.logger.info(message.to_s)
     end
   end
 end
lib/del/message.rb
@@ -0,0 +1,15 @@
+module Del
+  class Message
+    attr_reader :text, :robot, :source
+
+    def initialize(text, robot:, source:)
+      @text = text
+      @robot = robot
+      @source = source
+    end
+
+    def to_s
+      "#{source}: #{text}"
+    end
+  end
+end
lib/del/repository.rb
@@ -10,13 +10,17 @@ module Del
     end
 
     def find_by(id)
-      @lock.synchronize { @storage[id] }
+      @lock.synchronize { @storage[id.to_s] }
+    end
+
+    def find_all
+      @lock.synchronize { @storage.keys }
     end
 
     def upsert(id, attributes = {})
       Del.logger.debug([id, attributes].inspect)
       @lock.synchronize do
-        @storage[id] = attributes
+        @storage[id.to_s] = attributes
       end
     end
   end
lib/del/robot.rb
@@ -19,8 +19,8 @@ module Del
       connection.disconnect
     end
 
-    def receive(message)
-      router.route(message)
+    def receive(message, source:)
+      router.route(Message.new(message, robot: self, source: source))
     end
 
     def send_message(jid, message)
lib/del/source.rb
@@ -0,0 +1,14 @@
+module Del
+  class Source
+    attr_reader :user, :room
+
+    def initialize(user:, room: nil)
+      @user = user
+      @room = room
+    end
+
+    def to_s
+      "#{user.mention_name}:#{room}:"
+    end
+  end
+end
lib/del/user.rb
@@ -0,0 +1,14 @@
+module Del
+  class User
+    attr_reader :jid, :attributes
+
+    def initialize(jid, attributes)
+      @jid = jid
+      @attributes = attributes || {}
+    end
+
+    def mention_name
+      attributes[:mention_name]
+    end
+  end
+end
lib/del.rb
@@ -7,8 +7,11 @@ require "xmpp4r/roster/helper/roster"
 
 require "del/connection"
 require "del/default_router"
-require "del/robot"
+require "del/message"
 require "del/repository"
+require "del/robot"
+require "del/source"
+require "del/user"
 require "del/version"
 
 module Del
@@ -16,6 +19,7 @@ module Del
     puts "Loading... #{dotenv_file}"
     Dotenv.load(dotenv_file.to_s)
     puts "It's fire! 🔥"
+    Del.logger.level = Logger::INFO
     del = Robot.new(configuration: configuration)
     del.get_funky!
   end