Skip to content

Commit

Permalink
Configurable adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz Kopiński committed Apr 17, 2015
1 parent 2ef9dc8 commit f300862
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 203 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source 'https://rubygems.org'

gemspec
gem 'xmpp4r-hipchat', github: 'bartoszkopinski/xmpp4r-hipchat', branch: 'master'
gem 'slack-api'

platforms :rbx do
gem 'racc'
Expand Down
17 changes: 10 additions & 7 deletions lib/hipbot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@
require 'hipbot/callbacks/lobby_presence'
require 'hipbot/callbacks/room_presence'
require 'hipbot/callbacks/invite'
require 'hipbot/adapter'
require 'hipbot/adapters/xmpp/initializer'
require 'hipbot/adapters/slack'
require 'hipbot/adapters/hipchat/initializer'
require 'hipbot/adapters/hipchat'
require 'hipbot/adapters/telnet'
require 'hipbot/adapters/shell'
require 'hipbot/reaction_factory'
require 'hipbot/reactable'
require 'hipbot/matchable'
require 'hipbot/configuration'
require 'hipbot/configurable'
require 'hipbot/adapter'
require 'hipbot/bot'
require 'hipbot/adaptable'
require 'hipbot/adapters/xmpp'
require 'hipbot/adapters/xmpp/client'
require 'hipbot/adapters/slack'
require 'hipbot/adapters/slack/client'
require 'hipbot/adapters/hipchat'
require 'hipbot/adapters/hipchat/client'
require 'hipbot/adapters/telnet'
require 'hipbot/adapters/shell'
require 'hipbot/plugin'
require 'hipbot/storages/base'
require 'hipbot/storages/hash'
Expand Down
24 changes: 24 additions & 0 deletions lib/hipbot/adaptable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Hipbot
module Adaptable
extend ActiveSupport::Concern

included do
extend ClassMethods

Hipbot.adapters.unshift(self)
end

module ClassMethods
attr_reader :options

def inherited child
Hipbot.adapters.unshift(child)
end

def add_config_options *keys
@options ||= []
@options |= keys
end
end
end
end
50 changes: 2 additions & 48 deletions lib/hipbot/adapters/hipchat.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
module Hipbot
module Adapters
class Hipchat
attr_accessor :client

def start!
self.client = Initializer.new.client
end

def restart!
start!
end

def invite_to_room(room, users)
client.invite(user_ids(users), room.id)
end

def kick_from_room(room, users)
client.kick(user_ids(users), room.id)
end

def send_to_room(room, message)
client.send_message(:groupchat, room.id, message)
end

def send_to_user(user, message)
client.send_message(:chat, user.id, message)
end

def set_topic(room, topic)
client.send_message(:groupchat, room.id, nil, topic)
end

def set_presence(status, type)
client.set_presence(status, type)
end

def join_room(room)
client.join(room.id)
end

def leave_room(room, reason = '')
client.exit(room.id, reason)
end

protected

def user_ids users
Array(users).map(&:id)
end
class Hipchat < XMPP
include Hipbot::Adaptable
end
end
end
8 changes: 8 additions & 0 deletions lib/hipbot/adapters/hipchat/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Hipbot
module Adapters
class Hipchat < XMPP
class Client < XMPP::Client
end
end
end
end
2 changes: 2 additions & 0 deletions lib/hipbot/adapters/shell.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Hipbot
module Adapters
class Shell
include Hipbot::Adaptable

attr_accessor :connection

def start!
Expand Down
50 changes: 2 additions & 48 deletions lib/hipbot/adapters/slack.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
module Hipbot
module Adapters
class Slack
attr_accessor :client

def start!
self.client = XMPP::Initializer.new.client
end

def restart!
start!
end

def invite_to_room(room, users)
client.invite(user_ids(users), room.id)
end

def kick_from_room(room, users)
client.kick(user_ids(users), room.id)
end

def send_to_room(room, message)
client.send_message(:groupchat, room.id, message)
end

def send_to_user(user, message)
client.send_message(:chat, user.id, message)
end

def set_topic(room, topic)
client.send_message(:groupchat, room.id, nil, topic)
end

def set_presence(status, type)
client.set_presence(status, type)
end

def join_room(room)
client.join(room.id)
end

def leave_room(room, reason = '')
client.exit(room.id, reason)
end

protected

def user_ids users
Array(users).map(&:id)
end
class Slack < XMPP
add_config_options :slack_api_token, :conference_host
end
end
end
42 changes: 42 additions & 0 deletions lib/hipbot/adapters/slack/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "slack"

module Hipbot
module Adapters
class Slack < XMPP
class Client < XMPP::Client
protected

def initialize_bot_user
Hipbot.configuration.user = User.find(Hipbot.configuration.jid)
client.name = Hipbot.user.name.delete(" ").downcase
Hipbot.configuration.user.update(mention: client.name)
end

def initialize_rooms
::Slack.configure do |config|
config.token = Hipbot.configuration.slack_api_token
end

room_ids = ::Slack.client.channels_list["channels"].map do |channel|
room = Room.find_or_create_by(
name: channel["name"],
id: Jabber::JID.new(channel["name"], Hipbot.configuration.conference_host, "hipbot").to_s
)
room.id
end

clean_other_objects(Room, room_ids) if room_ids.any?
end

def initialize_users
user_ids = client.get_users.map do |user_data|
user = User.find_or_create_by(id: user_data.jid)
user.update_attributes(user_data.attributes)
user.id
end
clean_other_objects(User, user_ids) if user_ids.any?
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/hipbot/adapters/telnet.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Hipbot
module Adapters
class Telnet
include Hipbot::Adaptable

attr_accessor :connection

def start!
Expand Down
57 changes: 57 additions & 0 deletions lib/hipbot/adapters/xmpp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module Hipbot
module Adapters
class XMPP
include Hipbot::Adaptable

add_config_options :jid

attr_accessor :client

def start!
self.client = self.class::Client.new.client
end

def restart!
start!
end

def invite_to_room(room, users)
client.invite(user_ids(users), room.id)
end

def kick_from_room(room, users)
client.kick(user_ids(users), room.id)
end

def send_to_room(room, message)
client.send_message(:groupchat, room.id, message)
end

def send_to_user(user, message)
client.send_message(:chat, user.id, message)
end

def set_topic(room, topic)
client.send_message(:groupchat, room.id, nil, topic)
end

def set_presence(status, type)
client.set_presence(status, type)
end

def join_room(room)
client.join(room.id)
end

def leave_room(room, reason = '')
client.exit(room.id, reason)
end

protected

def user_ids users
Array(users).map(&:id)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Hipbot
module Adapters
class Hipchat
class Initializer
class XMPP
class Client
attr_accessor :client

KEEP_ALIVE_INTERVAL = 60
Expand All @@ -21,8 +21,8 @@ def initialize
protected

def initialize_client
self.client = ::Jabber::MUC::HipchatClient.new(Hipbot.jid)
yield if client.connect(Hipbot.password)
self.client = ::Jabber::MUC::HipchatClient.new(Hipbot.configuration.jid)
yield if client.connect(Hipbot.configuration.password)
end

def initialize_rooms
Expand Down Expand Up @@ -52,7 +52,7 @@ def clean_other_objects klass, object_ids
end

def initialize_bot_user
Hipbot.configuration.user = User.find(Hipbot.jid)
Hipbot.configuration.user = User.find(Hipbot.configuration.jid)
client.name = Hipbot.user.name
end

Expand Down
Loading

0 comments on commit f300862

Please sign in to comment.