-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bartosz Kopiński
committed
Apr 15, 2015
1 parent
8dc75a3
commit f59f3f7
Showing
15 changed files
with
180 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
coverage/* | ||
.bundle | ||
Gemfile.lock | ||
.ruby-version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ end | |
platforms :mri do | ||
gem 'coveralls', require: false | ||
end | ||
|
||
gem 'pry' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
module Hipbot | ||
module Adapters | ||
class XMPP | ||
class Initializer | ||
attr_accessor :client | ||
|
||
KEEP_ALIVE_INTERVAL = 60 | ||
|
||
def initialize | ||
Jabber.debug = true | ||
Jabber.logger = Hipbot.logger | ||
initialize_client do | ||
initialize_rooms | ||
initialize_users | ||
initialize_bot_user | ||
initialize_callbacks | ||
initialize_keep_alive | ||
end | ||
end | ||
|
||
protected | ||
|
||
def initialize_client | ||
self.client = ::Jabber::MUC::HipchatClient.new( | ||
Hipbot.jid, | ||
Hipbot.conference_host, | ||
) | ||
yield if client.connect(Hipbot.password) | ||
end | ||
|
||
def initialize_rooms | ||
room_ids = client.get_rooms.map do |room_data| | ||
room = Room.find_or_create_by(id: room_data.jid) | ||
room.update_attributes(room_data.attributes) | ||
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) | ||
|
||
if user.attributes['email'].nil? | ||
user.update_attributes(client.get_user_details(user.id).attributes) | ||
end | ||
user.id | ||
end | ||
clean_other_objects(User, user_ids) if user_ids.any? | ||
end | ||
|
||
def clean_other_objects klass, object_ids | ||
klass.all.select{ |r| !object_ids.include?(r.id) }.each(&:destroy) | ||
end | ||
|
||
def initialize_bot_user | ||
Hipbot.configuration.user = User.find_or_create_by(id: Hipbot.jid) | ||
client.name = Hipbot.user.name | ||
end | ||
|
||
def initialize_callbacks | ||
client.on_room_message{ |*args| | ||
Callbacks::RoomMessage.new(*args) | ||
} | ||
client.on_private_message{ |*args| | ||
Callbacks::PrivateMessage.new(*args) | ||
} | ||
client.on_invite{ |*args| | ||
Callbacks::Invite.new(*args) | ||
} | ||
client.on_lobby_presence{ |*args| | ||
Callbacks::LobbyPresence.new(*args) | ||
} | ||
client.on_room_presence{ |*args| | ||
Callbacks::RoomPresence.new(*args) | ||
} | ||
client.activate_callbacks | ||
end | ||
|
||
def initialize_keep_alive | ||
::EM::add_periodic_timer(KEEP_ALIVE_INTERVAL) do | ||
client.keep_alive(Hipbot.password) unless client.nil? | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,9 @@ def first_name | |
def myself? | ||
self == Hipbot.user | ||
end | ||
|
||
def guest? | ||
attributes[:role] == 'visitor' | ||
end | ||
end | ||
end |