raibo 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  .bundle
3
+ .project
3
4
  Gemfile.lock
4
5
  pkg/*
5
6
  Botfile
data/Gemfile CHANGED
@@ -1,7 +1,2 @@
1
1
  source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in raibo.gemspec
4
2
  gemspec
5
-
6
- # Development dependencies
7
- gem 'rspec'
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc 'Run specs'
7
+ RSpec::Core::RakeTask.new
data/bin/raibo CHANGED
@@ -1,33 +1,48 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'rubygems'
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
- options.verbose = false
15
+
16
+ options.room = 'Raibo'
17
+ options.token = ''
13
18
 
14
19
  opts = OptionParser.new do |o|
15
- o.banner = "Usage: raibo <server>[:<port>] [options]"
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("-c", "--channel [CHANNEL]",
28
- "specify the channel to use (default is \"#raibo\")") do |channel|
29
- options.channel = channel
30
- end
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
- b = Raibo::Bot.new(server,
56
- :port => port,
57
- :nick => options.nick,
58
- :channel => options.channel,
59
- :verbose => options.verbose)
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
@@ -4,7 +4,8 @@ end
4
4
  require 'socket'
5
5
 
6
6
  require 'raibo/version'
7
- require 'raibo/connection'
7
+ require 'raibo/campfire_connection'
8
+ require 'raibo/irc_connection'
8
9
  require 'raibo/bot'
9
10
  require 'raibo/message'
10
11
  require 'raibo/dsl'
@@ -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::Connection)
6
+ if args.first.is_a?(Raibo::CampfireConnection) or args.first.is_a?(Raibo::IrcConnection)
7
7
  @connection = args.shift
8
- else
9
- @connection = Raibo::Connection.new(*args)
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
- begin
65
- message = Raibo::Message.new(line)
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 Connection
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
@@ -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
- @kind = get_kind
13
- @from = get_from
14
- @to = get_to
15
- @body = get_body
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
@@ -1,3 +1,3 @@
1
1
  module Raibo
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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
@@ -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::Message.new(":someone!someone@Nightstar-251832d1.snfc22.example.com PRIVMSG #raibo :foo bar baz")
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::Message.new(":someone!someone@Nightstar-251832d1.snfc22.example.com PRIVMSG #raibo :\001ACTION foo bar baz\001")
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::Message.new(":someone!someone@Nightstar-251832d1.snfc22.example.com JOIN :#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::Message.new(":someone!someone@Nightstar-251832d1.snfc22.example.com PART #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::Message.new(":Deepthought.NY.US.Nightstar.Net 255 Raibo :I have 83 clients and 1 servers")
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
@@ -1,2 +1,2 @@
1
- require 'rubygems'
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
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: 2011-08-30 00:00:00.000000000Z
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/connection.rb
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.6
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: