Commit 33da7f9

mo <mo.khan@gmail.com>
2018-04-27 22:51:33
convince del to join rooms at startup.
1 parent a6be0cc
lib/del/connection.rb
@@ -27,6 +27,7 @@ module Del
         robot.receive(message)
       end
       client.send(Jabber::Presence.new(:chat))
+      configuration[:default_rooms].each { |room| join(room, robot) }
       list_rooms(configuration[:muc_domain]).each do |room|
         rooms.upsert(room)
       end
@@ -50,9 +51,7 @@ module Del
     end
 
     def jid
-      jid = Jabber::JID.new(configuration[:jid])
-      jid.resource = "bot"
-      jid
+      @jid ||= normalize_jid(configuration[:jid], "chat.hipchat.com", "bot")
     end
 
     def list_rooms(muc_domain)
@@ -61,8 +60,23 @@ 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)
+      jid = Jabber::JID.new(jid)
+      jid.resource = resource
+      unless jid.node
+        jid.node = jid.domain
+        jid.domain = domain
+      end
+      jid
+    end
   end
 end
lib/del/repository.rb
@@ -2,15 +2,22 @@ module Del
   class Repository
     def initialize(storage = {})
       @storage = storage
+      @lock = Mutex.new
+    end
+
+    def [](id)
+      find_by(id)
     end
 
     def find_by(id)
-      @storage[id]
+      @lock.synchronize { @storage[id] }
     end
 
     def upsert(id, attributes = {})
       Del.logger.debug([id, attributes].inspect)
-      @storage[id] = attributes
+      @lock.synchronize do
+        @storage[id] = attributes
+      end
     end
   end
 end
lib/del/robot.rb
@@ -2,9 +2,11 @@ module Del
   class Robot
     attr_reader :connection, :router
     attr_reader :users, :rooms
+    attr_reader :name
 
     def initialize(configuration:)
       @connection = Connection.new(configuration: configuration)
+      @name = configuration[:name]
       @router = configuration[:router]
       @users = configuration[:users]
       @rooms = configuration[:rooms]
lib/del.rb
@@ -26,10 +26,12 @@ module Del
 
   def self.configuration
     @configuration ||= {
+      default_rooms: ENV.fetch("DEL_ROOMS", '').split(','),
       host: ENV.fetch("DEL_HOST"),
       jid: ENV.fetch("DEL_JID"),
       logger: Logger.new(STDOUT),
       muc_domain: ENV.fetch("DEL_MUC_DOMAIN"),
+      name: ENV.fetch("DEL_FULL_NAME"),
       password: ENV.fetch("DEL_PASSWORD"),
       rooms: Repository.new,
       router: DefaultRouter.new,