raibo 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/Gemfile +0 -5
- data/Rakefile +5 -0
- data/bin/raibo +41 -13
- data/lib/raibo.rb +2 -1
- data/lib/raibo/bot.rb +12 -12
- data/lib/raibo/campfire_connection.rb +73 -0
- data/lib/raibo/{connection.rb → irc_connection.rb} +6 -2
- data/lib/raibo/message.rb +41 -42
- data/lib/raibo/version.rb +1 -1
- data/raibo.gemspec +4 -0
- data/spec/message_spec.rb +5 -5
- data/spec/spec_helper.rb +1 -1
- metadata +39 -5
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/bin/raibo
CHANGED
@@ -1,33 +1,48 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
3
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
4
|
+
|
4
5
|
require 'raibo'
|
5
6
|
require 'optparse'
|
6
7
|
require 'ostruct'
|
7
8
|
|
8
9
|
options = OpenStruct.new
|
9
10
|
options.botfile = 'Botfile'
|
11
|
+
options.verbose = false
|
12
|
+
|
10
13
|
options.nick = 'Raibo'
|
11
14
|
options.channel = '#raibo'
|
12
|
-
|
15
|
+
|
16
|
+
options.room = 'Raibo'
|
17
|
+
options.token = ''
|
13
18
|
|
14
19
|
opts = OptionParser.new do |o|
|
15
|
-
o.banner = "Usage: raibo <server>
|
20
|
+
o.banner = "Usage: raibo (<server>:<port> || campfire:<subdomain>) [options]"
|
16
21
|
|
17
22
|
o.on("-b", "--botfile [FILE]",
|
18
23
|
"specify the location of the bot's configuration (default is \"./Botfile\")") do |file|
|
19
24
|
options.botfile = file
|
20
25
|
end
|
21
26
|
|
27
|
+
o.on("-c", "--channel [CHANNEL]",
|
28
|
+
"specify the IRC channel to use (default is \"#raibo\")") do |channel|
|
29
|
+
options.channel = channel
|
30
|
+
end
|
31
|
+
|
22
32
|
o.on("-n", "--nick [NICK]",
|
23
|
-
"specify the nick to use (default is \"Raibo\")") do |nick|
|
33
|
+
"specify the IRC nick to use (default is \"Raibo\")") do |nick|
|
24
34
|
options.nick = nick
|
25
35
|
end
|
26
36
|
|
27
|
-
o.on("-
|
28
|
-
"specify the
|
29
|
-
options.
|
30
|
-
|
37
|
+
o.on("-r", "--room [ROOM]",
|
38
|
+
"specify the Campfire room to use (default is \"Raibo\")") do |room|
|
39
|
+
options.room = room
|
40
|
+
end
|
41
|
+
|
42
|
+
o.on("-t", "--token [TOKEN]",
|
43
|
+
"specify the Campfire API token to use (required for Campfire)") do |token|
|
44
|
+
options.token = token
|
45
|
+
end
|
31
46
|
|
32
47
|
o.on("-v", "--verbose", "show every incoming and outgoing line") do
|
33
48
|
options.verbose = true
|
@@ -52,10 +67,23 @@ end
|
|
52
67
|
|
53
68
|
server, port = ARGV.shift.split(':')
|
54
69
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
70
|
+
if server == 'campfire'
|
71
|
+
subdomain = port
|
72
|
+
b = Raibo::Bot.new('campfire',
|
73
|
+
subdomain,
|
74
|
+
:token => options.token,
|
75
|
+
:room => options.room,
|
76
|
+
:verbose => options.verbose
|
77
|
+
)
|
78
|
+
else #IRC
|
79
|
+
b = Raibo::Bot.new('irc',
|
80
|
+
server,
|
81
|
+
:port => port,
|
82
|
+
:nick => options.nick,
|
83
|
+
:channel => options.channel,
|
84
|
+
:verbose => options.verbose
|
85
|
+
)
|
86
|
+
end
|
87
|
+
|
60
88
|
b.load_config_file(options.botfile)
|
61
89
|
b.run
|
data/lib/raibo.rb
CHANGED
data/lib/raibo/bot.rb
CHANGED
@@ -3,10 +3,14 @@ module Raibo
|
|
3
3
|
attr_accessor :docs
|
4
4
|
|
5
5
|
def initialize(*args)
|
6
|
-
if args.first.is_a?(Raibo::
|
6
|
+
if args.first.is_a?(Raibo::CampfireConnection) or args.first.is_a?(Raibo::IrcConnection)
|
7
7
|
@connection = args.shift
|
8
|
-
|
9
|
-
|
8
|
+
elsif args.first == 'campfire'
|
9
|
+
args.shift
|
10
|
+
@connection = Raibo::CampfireConnection.new(*args)
|
11
|
+
elsif args.first == 'irc'
|
12
|
+
args.shift
|
13
|
+
@connection = Raibo::IrcConnection.new(*args)
|
10
14
|
end
|
11
15
|
|
12
16
|
reset
|
@@ -61,15 +65,8 @@ module Raibo
|
|
61
65
|
def run_sync
|
62
66
|
@connection.open
|
63
67
|
@connection.handle_lines do |line|
|
64
|
-
|
65
|
-
|
66
|
-
rescue => e
|
67
|
-
if @connection.verbose
|
68
|
-
puts "Error parsing line:"
|
69
|
-
puts " #{line}"
|
70
|
-
puts " #{e.backtrace}"
|
71
|
-
end
|
72
|
-
end
|
68
|
+
message = @connection.construct_message(line)
|
69
|
+
|
73
70
|
@handlers.each do |handler|
|
74
71
|
begin
|
75
72
|
if handler.is_a?(Proc)
|
@@ -78,6 +75,9 @@ module Raibo
|
|
78
75
|
break if handler.call(@connection, message)
|
79
76
|
end
|
80
77
|
rescue Exception => e
|
78
|
+
if @connection.verbose
|
79
|
+
puts "Handler exception:\n #{e.backtrace.join("\n ")}"
|
80
|
+
end
|
81
81
|
@connection.say e.inspect
|
82
82
|
end
|
83
83
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'tinder'
|
2
|
+
|
3
|
+
module Raibo
|
4
|
+
class CampfireConnection
|
5
|
+
attr_accessor :subdomain, :token, :room_name, :verbose, :opened
|
6
|
+
|
7
|
+
def initialize(subdomain, opts={})
|
8
|
+
@subdomain = subdomain
|
9
|
+
@token = opts[:token]
|
10
|
+
@room_name = opts[:room] || 'Raibo'
|
11
|
+
@verbose = !!opts[:verbose]
|
12
|
+
@opened = false
|
13
|
+
end
|
14
|
+
|
15
|
+
def open
|
16
|
+
if @token.nil? or @token == ''
|
17
|
+
raise "token is a required field for Campfire"
|
18
|
+
end
|
19
|
+
|
20
|
+
@campfire = Tinder::Campfire.new @subdomain, :token => @token
|
21
|
+
@room = @campfire.find_room_by_name @room_name
|
22
|
+
@name = @campfire.me['name']
|
23
|
+
|
24
|
+
puts "Connected to room #{@room_name}" if @verbose
|
25
|
+
@opened = true
|
26
|
+
end
|
27
|
+
|
28
|
+
def close
|
29
|
+
@room.leave if @room
|
30
|
+
rescue
|
31
|
+
end
|
32
|
+
|
33
|
+
def handle_lines
|
34
|
+
@room.listen do |m|
|
35
|
+
# Message Format:
|
36
|
+
# :body: the body of the message
|
37
|
+
# :user: Campfire user, which is itself a hash, of:
|
38
|
+
# :id: User id
|
39
|
+
# :name: User name
|
40
|
+
# :email_address: Email address
|
41
|
+
# :admin: Boolean admin flag
|
42
|
+
# :created_at: User creation timestamp
|
43
|
+
# :type: User type (e.g. Member)
|
44
|
+
# :id: Campfire message id
|
45
|
+
# :type: Campfire message type
|
46
|
+
# :room_id: Campfire room id
|
47
|
+
# :created_at: Message creation timestamp
|
48
|
+
|
49
|
+
puts "--> #{m}" if @verbose
|
50
|
+
if m[:type] == 'TextMessage' and m[:user][:name] != @name
|
51
|
+
yield m
|
52
|
+
end
|
53
|
+
end
|
54
|
+
rescue => e
|
55
|
+
puts "Error:\n #{e.backtrace.join(' ')}" if @verbose
|
56
|
+
retry
|
57
|
+
end
|
58
|
+
|
59
|
+
def say(*msgs)
|
60
|
+
m = msgs.join("\n")
|
61
|
+
if msgs.size == 1
|
62
|
+
@room.speak m
|
63
|
+
else
|
64
|
+
@room.paste m
|
65
|
+
end
|
66
|
+
puts "<-- #{m}" if @verbose
|
67
|
+
end
|
68
|
+
|
69
|
+
def construct_message(msg)
|
70
|
+
Raibo::Message.new(:message, msg[:user][:name], @room_name, msg[:body])
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Raibo
|
2
|
-
class
|
3
|
-
attr_accessor :server, :port, :nick, :channel
|
2
|
+
class IrcConnection
|
3
|
+
attr_accessor :server, :port, :nick, :channel, :verbose
|
4
4
|
|
5
5
|
def initialize(server, opts={})
|
6
6
|
@server = server
|
@@ -69,5 +69,9 @@ module Raibo
|
|
69
69
|
puts "<-- #{str}" if @verbose
|
70
70
|
@connection.puts(str)
|
71
71
|
end
|
72
|
+
|
73
|
+
def construct_message(msg)
|
74
|
+
Raibo::IrcMessage.new(msg)
|
75
|
+
end
|
72
76
|
end
|
73
77
|
end
|
data/lib/raibo/message.rb
CHANGED
@@ -1,18 +1,51 @@
|
|
1
1
|
module Raibo
|
2
2
|
class Message
|
3
|
-
# Readers for the components of the actual IRC protocol line.
|
4
|
-
attr_reader :prefix, :type, :middle, :trailing
|
5
|
-
|
6
|
-
# Readers for higher-level attributes of the message.
|
7
3
|
attr_reader :kind, :from, :to, :body
|
8
4
|
|
5
|
+
def initialize(kind, from, to, body)
|
6
|
+
@kind, @from, @to, @body = kind, from, to, body
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class IrcMessage
|
11
|
+
attr_reader :prefix, :type, :middle, :trailing
|
12
|
+
|
9
13
|
def initialize(line)
|
10
14
|
@prefix, @type, @middle, @trailing = parse_line(line)
|
15
|
+
end
|
11
16
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
17
|
+
def kind
|
18
|
+
case type
|
19
|
+
when 'PRIVMSG'
|
20
|
+
if trailing =~ /^\001ACTION/
|
21
|
+
:emote
|
22
|
+
else
|
23
|
+
:message
|
24
|
+
end
|
25
|
+
when 'JOIN'
|
26
|
+
:join
|
27
|
+
when 'PART'
|
28
|
+
:part
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def from
|
33
|
+
prefix[/^([^!@ ]*)/, 1]
|
34
|
+
end
|
35
|
+
|
36
|
+
def to
|
37
|
+
if [:message, :emote].include?(kind)
|
38
|
+
middle.first
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def body
|
43
|
+
case kind
|
44
|
+
when :message
|
45
|
+
trailing
|
46
|
+
when :emote
|
47
|
+
trailing[/\001ACTION ([^\001]*)/, 1]
|
48
|
+
end
|
16
49
|
end
|
17
50
|
|
18
51
|
private
|
@@ -25,39 +58,5 @@ module Raibo
|
|
25
58
|
end
|
26
59
|
[prefix, type, middle, trailing]
|
27
60
|
end
|
28
|
-
|
29
|
-
def get_kind
|
30
|
-
case type
|
31
|
-
when 'PRIVMSG'
|
32
|
-
if trailing =~ /^\001ACTION/
|
33
|
-
:emote
|
34
|
-
else
|
35
|
-
:message
|
36
|
-
end
|
37
|
-
when 'JOIN'
|
38
|
-
:join
|
39
|
-
when 'PART'
|
40
|
-
:part
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def get_from
|
45
|
-
prefix[/^([^!@ ]*)/, 1]
|
46
|
-
end
|
47
|
-
|
48
|
-
def get_to
|
49
|
-
if [:message, :emote].include?(kind)
|
50
|
-
middle.first
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def get_body
|
55
|
-
case kind
|
56
|
-
when :message
|
57
|
-
trailing
|
58
|
-
when :emote
|
59
|
-
trailing[/\001ACTION ([^\001]*)/, 1]
|
60
|
-
end
|
61
|
-
end
|
62
61
|
end
|
63
62
|
end
|
data/lib/raibo/version.rb
CHANGED
data/raibo.gemspec
CHANGED
@@ -18,4 +18,8 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency 'tinder'
|
23
|
+
|
24
|
+
s.add_development_dependency 'rspec'
|
21
25
|
end
|
data/spec/message_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
module Raibo
|
4
4
|
describe Message do
|
5
5
|
it 'parses a message' do
|
6
|
-
m = Raibo::
|
6
|
+
m = Raibo::IrcMessage.new(":someone!someone@Nightstar-251832d1.snfc22.example.com PRIVMSG #raibo :foo bar baz")
|
7
7
|
|
8
8
|
m.kind.should == :message
|
9
9
|
m.from.should == 'someone'
|
@@ -12,7 +12,7 @@ module Raibo
|
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'parses an emote' do
|
15
|
-
m = Raibo::
|
15
|
+
m = Raibo::IrcMessage.new(":someone!someone@Nightstar-251832d1.snfc22.example.com PRIVMSG #raibo :\001ACTION foo bar baz\001")
|
16
16
|
|
17
17
|
m.kind.should == :emote
|
18
18
|
m.from.should == 'someone'
|
@@ -21,7 +21,7 @@ module Raibo
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'parses a join' do
|
24
|
-
m = Raibo::
|
24
|
+
m = Raibo::IrcMessage.new(":someone!someone@Nightstar-251832d1.snfc22.example.com JOIN :#raibo")
|
25
25
|
|
26
26
|
m.kind.should == :join
|
27
27
|
m.from.should == 'someone'
|
@@ -30,7 +30,7 @@ module Raibo
|
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'parses a part' do
|
33
|
-
m = Raibo::
|
33
|
+
m = Raibo::IrcMessage.new(":someone!someone@Nightstar-251832d1.snfc22.example.com PART #raibo")
|
34
34
|
|
35
35
|
m.kind.should == :part
|
36
36
|
m.from.should == 'someone'
|
@@ -39,7 +39,7 @@ module Raibo
|
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'parses an arbitrary line' do
|
42
|
-
m = Raibo::
|
42
|
+
m = Raibo::IrcMessage.new(":Deepthought.NY.US.Nightstar.Net 255 Raibo :I have 83 clients and 1 servers")
|
43
43
|
m.kind.should == nil
|
44
44
|
end
|
45
45
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
1
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
2
2
|
require 'raibo'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: raibo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,40 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
12
|
+
date: 2012-06-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: tinder
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
14
46
|
description: ''
|
15
47
|
email:
|
16
48
|
- rwfitzge@gmail.com
|
@@ -26,8 +58,9 @@ files:
|
|
26
58
|
- example/example_module.rb
|
27
59
|
- lib/raibo.rb
|
28
60
|
- lib/raibo/bot.rb
|
29
|
-
- lib/raibo/
|
61
|
+
- lib/raibo/campfire_connection.rb
|
30
62
|
- lib/raibo/dsl.rb
|
63
|
+
- lib/raibo/irc_connection.rb
|
31
64
|
- lib/raibo/message.rb
|
32
65
|
- lib/raibo/version.rb
|
33
66
|
- raibo.gemspec
|
@@ -53,10 +86,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
86
|
version: '0'
|
54
87
|
requirements: []
|
55
88
|
rubyforge_project: raibo
|
56
|
-
rubygems_version: 1.8.
|
89
|
+
rubygems_version: 1.8.23
|
57
90
|
signing_key:
|
58
91
|
specification_version: 3
|
59
92
|
summary: A simple IRC library
|
60
93
|
test_files:
|
61
94
|
- spec/message_spec.rb
|
62
95
|
- spec/spec_helper.rb
|
96
|
+
has_rdoc:
|