rubot 0.1.5 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/rubot CHANGED
@@ -6,7 +6,7 @@ require "template"
6
6
 
7
7
  options = OpenStruct.new
8
8
  parser = OptionParser.new do |parser|
9
- parser.banner = "Usage: #{__FILE__} <options>"
9
+ parser.banner = "Usage: #{File.basename(__FILE__)} <options>"
10
10
 
11
11
  parser.separator ""
12
12
  parser.separator "Initialization"
@@ -19,17 +19,22 @@ parser = OptionParser.new do |parser|
19
19
  parser.separator ""
20
20
  parser.separator "Generators"
21
21
 
22
- parser.on("-c", "--command Name", "Generates a new command named Name") do |name|
22
+ parser.on("-c", "--command Name", "Generates a new command") do |name|
23
23
  generate_command(name)
24
24
  exit
25
25
  end
26
26
 
27
- parser.on("-l", "--listener Name", "Generates a new listener named Name") do |name|
27
+ parser.on("-e", "--exiter Name", "Generates a new exiter") do |name|
28
+ generate_exiter(name)
29
+ exit
30
+ end
31
+
32
+ parser.on("-l", "--listener Name", "Generates a new listener") do |name|
28
33
  generate_listener(name)
29
34
  exit
30
35
  end
31
36
 
32
- parser.on("-r", "--runner Name", "Generates a new listener named Name") do |name|
37
+ parser.on("-r", "--runner Name", "Generates a new listener") do |name|
33
38
  generate_runner(name)
34
39
  exit
35
40
  end
@@ -3,6 +3,7 @@ module Rubot
3
3
  require "core/command"
4
4
  require "core/listener"
5
5
  require "core/runner"
6
+ require "core/exiter"
6
7
  require "core/dispatcher"
7
8
  end
8
9
  end
@@ -42,6 +42,7 @@ module Rubot
42
42
  reload
43
43
  # runners are only run on server connection, so there's no need them to be in reload
44
44
  load_dir "runners", @runners = {}
45
+ load_dir "exiters", @exiters = {}
45
46
  end
46
47
 
47
48
  # Called when successful connection is made to a server. This is when the runners are
@@ -116,6 +117,15 @@ module Rubot
116
117
  def remove_auth(nick)
117
118
  @auth_list.delete nick
118
119
  end
120
+
121
+ # Handles the on quit event, which is triggered when the server recieves the
122
+ # quit message or an interrupt is triggered.
123
+ #
124
+ # ==== Parameters
125
+ # server<Rubot::Irc::Server>:: The server that is quiting.
126
+ def on_quit(server)
127
+ execute_exiters(server)
128
+ end
119
129
 
120
130
  private
121
131
 
@@ -126,6 +136,14 @@ module Rubot
126
136
  def run_runners(server)
127
137
  @runners.each_value {|runner| runner.run(server)}
128
138
  end
139
+
140
+ # Executes all exiters with the given server.
141
+ #
142
+ # ==== Parameters
143
+ # server<Rubot::Irc::Server>:: Server instance
144
+ def execute_exiters(server)
145
+ @exiters.each_value {|exiter| exiter.execute(server)}
146
+ end
129
147
 
130
148
  # Loads all files in the given directory and stores the class instances
131
149
  # in the given set. This is used to load commands, listeners, and runners.
@@ -0,0 +1,36 @@
1
+ module Rubot
2
+ module Core
3
+ # Base class for all exiters. An exiter cannot be called directly, but is executed
4
+ # when the server receives the quit message or an interrupt is triggered
5
+ #
6
+ # ==== Example
7
+ # This exiter says goodbye to all channels the bot is in.
8
+ # class Bye < Rubot::Core::Exiter
9
+ # def execute(server)
10
+ # server.channels.each do |channel|
11
+ # server.msg(channel, "bye everybody!")
12
+ # end
13
+ # end
14
+ # end
15
+ class Exiter
16
+ # Takes an instance of Rubot::Core::Dispatcher. Any
17
+ # child class that needs a constructor should override
18
+ # this method.
19
+ #
20
+ # ==== Parameters
21
+ # dispatcher<Rubot::Core::Dispatcher>:: The dispatcher that was used to create
22
+ # the instance of the exiter.
23
+ def initialize(dispatcher)
24
+ @dispatcher = dispatcher
25
+ end
26
+
27
+ # Runs the exiter with the given server.
28
+ #
29
+ # ==== Paramters
30
+ # server<Rubot::Irc::Server>:: Server instance the exiter should use for
31
+ # messaging and information.
32
+ def execute(server)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ class --NAME-- < Rubot::Core::Exiter
2
+ def execute(server)
3
+ server.channels.each do |channel|
4
+ server.msg(channel, "goodbye cruel world")
5
+ end
6
+ end
7
+ end
@@ -101,6 +101,7 @@ module Rubot
101
101
  @lock.synchronize do
102
102
  @thread = Thread.new { flush } unless @thread && @thread.alive?
103
103
  end
104
+ @thread
104
105
  end
105
106
  end
106
107
  nil
@@ -38,31 +38,37 @@ module Rubot
38
38
  @message_queue.action do |destination, action|
39
39
  raw "PRIVMSG #{destination} :\001ACTION #{action}\001"
40
40
  end
41
+
42
+ @message_queue.quit do |blah, more_blah|
43
+ raw "QUIT :#{@quit_message}"
44
+ @conn.close
45
+ end
41
46
  end
42
47
 
43
48
  # Attempts to make a connection to the server
44
49
  def connect
45
50
  return if @is_connected
46
-
51
+
47
52
  @conn = TCPSocket.open(@host, @port, @vhost)
48
53
  raw "USER #{@nick} #{@nick} #{@nick} :#{@real_name}"
49
54
  change_nick @nick
50
55
  join @channels
51
-
56
+
52
57
  begin
53
- main_loop()
58
+ main_loop()
54
59
  rescue Interrupt
60
+ quit
55
61
  rescue Exception => detail
56
- puts detail.message()
57
- print detail.backtrace.join("\n")
58
- retry
62
+ puts detail.message()
63
+ print detail.backtrace.join("\n")
64
+ retry
59
65
  end
60
66
  end
61
67
 
62
68
  # Sends the quit command to the IRC server, then closes the connection.
63
69
  def quit
64
- raw "QUIT :#{@quit_message}"
65
- @conn.close
70
+ @dispatcher.on_quit(self)
71
+ @message_queue.quit("Hades", "ice water plz").join
66
72
  end
67
73
 
68
74
  # Changes our nick.
@@ -32,7 +32,12 @@ def generate_runner(name)
32
32
  generate("runners", name)
33
33
  end
34
34
 
35
+ def generate_exiter(name)
36
+ generate("exiters", name)
37
+ end
38
+
35
39
  def generate(template, name)
40
+ FileUtils.mkdir(template) unless Dir.exist?(template)
36
41
  filename = File.join(template, "#{name.underscore}.rb")
37
42
  puts "file '#{filename}' already exists" and return if File.exist?(filename)
38
43
 
metadata CHANGED
@@ -4,9 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 5
9
- version: 0.1.5
7
+ - 2
8
+ version: "0.2"
10
9
  platform: ruby
11
10
  authors:
12
11
  - Chris Thorn
@@ -14,7 +13,7 @@ autorequire:
14
13
  bindir: bin
15
14
  cert_chain: []
16
15
 
17
- date: 2010-04-10 00:00:00 -08:00
16
+ date: 2010-04-13 00:00:00 -08:00
18
17
  default_executable:
19
18
  dependencies: []
20
19
 
@@ -30,6 +29,7 @@ files:
30
29
  - bin/rubot
31
30
  - lib/core/command.rb
32
31
  - lib/core/dispatcher.rb
32
+ - lib/core/exiter.rb
33
33
  - lib/core/listener.rb
34
34
  - lib/core/runner.rb
35
35
  - lib/core.rb
@@ -37,6 +37,7 @@ files:
37
37
  - lib/extensions/string.rb
38
38
  - lib/extensions.rb
39
39
  - lib/generators/commands.template
40
+ - lib/generators/exiters.template
40
41
  - lib/generators/listeners.template
41
42
  - lib/generators/runners.template
42
43
  - lib/irc/constants.rb