Skip to content

Commit

Permalink
namespace the connector and Message classes
Browse files Browse the repository at this point in the history
There's a new transitive dependency on the console gem which conflicts
with our Console connector at the root of the module/class namespace.
So I wrapped our connectors in a Linkbot module. Confirmed that Console
and Slack work which are the ones we mostly care about these days.

Wrapping Message and MessageType in the Linkbot module sorted out the
connectors being able to use Message within the namespace. Also,
probably avoids some future naming conflict with a message gem that
sneaks its way into dependencies.
  • Loading branch information
Robb Kidd committed May 1, 2022
1 parent ca8d356 commit c3a66f1
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 402 deletions.
164 changes: 83 additions & 81 deletions connectors/campfire.rb
Original file line number Diff line number Diff line change
@@ -1,105 +1,107 @@
class Campfire < Linkbot::Connector
Linkbot::Connector.register('campfire', self)
module Linkbot
class Campfire < Linkbot::Connector
Linkbot::Connector.register('campfire', self)

def initialize(options)
super(options)
end
def initialize(options)
super(options)
end

def start
request_options = {
:head => {
'authorization' => [@options['username'], @options['password']],
'Content-Type' => 'application/json'
def start
request_options = {
:head => {
'authorization' => [@options['username'], @options['password']],
'Content-Type' => 'application/json'
}
}
}

user_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/users/me.json").get request_options
user_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble logging in." }
user_http.callback {
@user = JSON.parse(user_http.response)["user"]
request_options[:head]['authorization'] = [@user["api_auth_token"],"x"]
request_options[:body] = "_"

join_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/room/#{@options["room"]}/join.xml").post request_options
join_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble entering the room." }
join_http.callback {
listen

user_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/users/me.json").get request_options
user_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble logging in." }
user_http.callback {
@user = JSON.parse(user_http.response)["user"]
request_options[:head]['authorization'] = [@user["api_auth_token"],"x"]
request_options[:body] = "_"

join_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/room/#{@options["room"]}/join.xml").post request_options
join_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble entering the room." }
join_http.callback {
listen
}
}
}
end
end

def listen
options = {
:path => "/room/#{@options["room"]}/live.json",
:host => "streaming.campfirenow.com",
:auth => "#{@user["api_auth_token"]}:x",
:timeout => 6
}
def listen
options = {
:path => "/room/#{@options["room"]}/live.json",
:host => "streaming.campfirenow.com",
:auth => "#{@user["api_auth_token"]}:x",
:timeout => 6
}

stream = Twitter::JSONStream.connect(options)
stream = Twitter::JSONStream.connect(options)

stream.each_item do |item|
process_message(item)
end
stream.each_item do |item|
process_message(item)
end

stream.on_error do |message|
Linkbot.log.error "Campfire connector: #{message.inspect}"
end
stream.on_error do |message|
Linkbot.log.error "Campfire connector: #{message.inspect}"
end

stream.on_max_reconnects do |timeout, retries|
Linkbot.log.fatal "Campire connector: tried #{retries} times to connect."
exit
stream.on_max_reconnects do |timeout, retries|
Linkbot.log.fatal "Campire connector: tried #{retries} times to connect."
exit
end
end
end


def process_message(item)
message = JSON.parse(item)
def process_message(item)
message = JSON.parse(item)

if message['type'] == 'TextMessage' && message['user_id'] != @user["id"]
# Check if the user who is sending this message exists in the DB yet - if not, load the users details before
# processing the message
if Linkbot.user_exists?(message['user_id'])
# Build the message
message = Message.new( message['body'], message['user_id'], Linkbot.user_ids[message['user_id']], self, :message, {} )
invoke_callbacks(message)
else
# Fetch the user data from campfire, then process the callbacks
request_options = {
:head => {
'authorization' => [@user['api_auth_token'], "x"],
'Content-Type' => 'application/json'
}
}

user_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/users/#{message['user_id']}.json").get request_options
user_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble entering the room." }
user_http.callback {
user = JSON.parse(user_http.response)["user"]
Linkbot.add_user(user["name"],user["id"])
if message['type'] == 'TextMessage' && message['user_id'] != @user["id"]
# Check if the user who is sending this message exists in the DB yet - if not, load the users details before
# processing the message
if Linkbot.user_exists?(message['user_id'])
# Build the message
message = Message.new( message['body'], message['user_id'], Linkbot.user_ids[message['user_id']], self, :message, {} )
invoke_callbacks(message)
}
else
# Fetch the user data from campfire, then process the callbacks
request_options = {
:head => {
'authorization' => [@user['api_auth_token'], "x"],
'Content-Type' => 'application/json'
}
}

user_http = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/users/#{message['user_id']}.json").get request_options
user_http.errback { Linkbot.log.error "Campfire connector: Yeah trouble entering the room." }
user_http.callback {
user = JSON.parse(user_http.response)["user"]
Linkbot.add_user(user["name"],user["id"])
message = Message.new( message['body'], message['user_id'], Linkbot.user_ids[message['user_id']], self, :message, {} )
invoke_callbacks(message)
}
end
end
end
end

def send_messages(messages,options = {})
flattened_messages = []
messages.each {|m| flattened_messages = flattened_messages + m.split("\n")}
def send_messages(messages,options = {})
flattened_messages = []
messages.each {|m| flattened_messages = flattened_messages + m.split("\n")}

flattened_messages.each_with_index do |m,i|
next if m.strip.empty?
flattened_messages.each_with_index do |m,i|
next if m.strip.empty?

request_options = {
:head => {
'authorization' => [@user["api_auth_token"],"x"],
'Content-Type' => 'application/json'
},
:body => {'message' => {'body' => m, 'type' => "TextMessage"}}.to_json
}
request_options = {
:head => {
'authorization' => [@user["api_auth_token"],"x"],
'Content-Type' => 'application/json'
},
:body => {'message' => {'body' => m, 'type' => "TextMessage"}}.to_json
}

request = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/room/#{@options['room']}/speak.json").post request_options
request = EventMachine::HttpRequest.new("#{@options["campfire_url"]}/room/#{@options['room']}/speak.json").post request_options
end
end
end
end
56 changes: 29 additions & 27 deletions connectors/console.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
class Console < Linkbot::Connector
Linkbot::Connector.register('console', self)
module Linkbot
class Console < Linkbot::Connector
Linkbot::Connector.register('console', self)

def initialize(options)
super(options)
end
def initialize(options)
super(options)
end

def start
prompt
end
def start
prompt
end

def prompt
print "[Console] >> "
handle_message(gets)
end
def prompt
print "[Console] >> "
handle_message(gets)
end

def handle_message(nick='Terminal User', room='Console', msg)
options = {room: room}
def handle_message(nick='Terminal User', room='Console', msg)
options = {room: room}

if !Linkbot.user_exists?(nick)
Linkbot.add_user(nick, nick)
end
if !Linkbot.user_exists?(nick)
Linkbot.add_user(nick, nick)
end

message = Message.new(msg, Linkbot.user_id(nick), nick, self, :message, options)
message = Message.new(msg, Linkbot.user_id(nick), nick, self, :message, options)

EventMachine::defer(proc {
invoke_callbacks(message, options)
})
EventMachine::defer(proc { prompt})
end
EventMachine::defer(proc {
invoke_callbacks(message, options)
})
EventMachine::defer(proc { prompt})
end

def send_messages(msgs, options={})
msgs.each do |msg|
puts msg
def send_messages(msgs, options={})
msgs.each do |msg|
puts msg
end
EventMachine::defer(proc { prompt})
end
EventMachine::defer(proc { prompt})
end
end
108 changes: 55 additions & 53 deletions connectors/irc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,77 @@
require "em/pure_ruby"
require "em-irc"

class IRCConnector < Linkbot::Connector
Linkbot::Connector.register('irc', self)
module Linkbot
class IRCConnector < Linkbot::Connector
Linkbot::Connector.register('irc', self)

def initialize(options)
super(options)
def initialize(options)
super(options)

@nick = options["nick"]
@fullname = options["fullname"]
@username = options["username"]
@host = options["server"]
@port = options["port"] || "6667"
@rooms = options["rooms"]
end
@nick = options["nick"]
@fullname = options["fullname"]
@username = options["username"]
@host = options["server"]
@port = options["port"] || "6667"
@rooms = options["rooms"]
end

def start
host = @host
port = @port
rooms = @rooms
nick = @nick
rooms = @rooms
parent = self
def start
host = @host
port = @port
rooms = @rooms
nick = @nick
rooms = @rooms
parent = self

@client = EventMachine::IRC::Client.new do
host host
port port
@client = EventMachine::IRC::Client.new do
host host
port port

on :connect do
Linkbot.log.info "IRC connector: connected to #{host}:#{port}, setting nick #{nick}"
nick nick
rooms.each do |room|
Linkbot.log.info "IRC connector: connecting to #{room}"
join room
on :connect do
Linkbot.log.info "IRC connector: connected to #{host}:#{port}, setting nick #{nick}"
nick nick
rooms.each do |room|
Linkbot.log.info "IRC connector: connecting to #{room}"
join room
end
end
end

on :join do |channel|
Linkbot.log.info "IRC connector: joined #{channel}"
end

on :message do |user, room, message|
if !room.start_with? "#"
room = user
on :join do |channel|
Linkbot.log.info "IRC connector: joined #{channel}"
end

parent.handle_message(user, room, message)
Linkbot.log.debug "IRC connector: <#{user}> -> <#{room}>: #{message}"
on :message do |user, room, message|
if !room.start_with? "#"
room = user
end

parent.handle_message(user, room, message)
Linkbot.log.debug "IRC connector: <#{user}> -> <#{room}>: #{message}"
end
end

@client.run! # start EventMachine loop
end

@client.run! # start EventMachine loop
end
def handle_message(nick, room, msg)
options = {room: room}

def handle_message(nick, room, msg)
options = {room: room}
if !Linkbot.user_exists?(nick)
Linkbot.add_user(nick, nick)
end

if !Linkbot.user_exists?(nick)
Linkbot.add_user(nick, nick)
message = Message.new(msg, Linkbot.user_id(nick), nick, self, :message, options)
invoke_callbacks(message, options)
end

message = Message.new(msg, Linkbot.user_id(nick), nick, self, :message, options)
invoke_callbacks(message, options)
end

def send_messages(msgs, options={})
Linkbot.log.debug "IRC connector: got messages #{msgs} <<#{options}>>"
msgs.each do |msg|
if msg && msg.strip.length > 0 && options[:room]
msg.split("\n").each do |line|
@client.message(options[:room], line)
def send_messages(msgs, options={})
Linkbot.log.debug "IRC connector: got messages #{msgs} <<#{options}>>"
msgs.each do |msg|
if msg && msg.strip.length > 0 && options[:room]
msg.split("\n").each do |line|
@client.message(options[:room], line)
end
end
end
end
Expand Down
Loading

0 comments on commit c3a66f1

Please sign in to comment.