Commit 6a80c4f0e24ab5fef5b77681cd4652c5e7876d11

Moved unhandled proc to IRC from IRCBot and implemented actions for IRCBot
IRC.rb
(13 / 4)
  
99# Class IRC is a master class that handles connection to the irc
1010# server and pasring of IRC events, through the IRCEvent class.
1111class IRC
12 attr_reader :nick, :server, :port
13
1214 @channels = nil
1315 # Create a new IRC Object instance
1416 def initialize( nick, server, port, realname='RBot')
3434
3535 IRCEvent.add_handler('332', topic_proc)
3636 IRCEvent.add_handler('topic', topic_proc)
37
38
37
38 unhandle_proc = Proc.new { |event|
39 method_id = "on_#{event.type}".to_sym
40 if respond_to?(method_id)
41 __send__(method_id, event)
42 elsif $DEBUG
43 $stderr.puts "=> Undefined method #{method_id}"
44 end
45 }
46
47 IRCEvent.add_handler("unhandled", unhandle_proc)
3948 end
40
41 attr_reader :nick, :server, :port
4249
4350 # Join a channel, adding it to the list of joined channels
4451 def add_channel channel
IRCBot.rb
(18 / 4)
  
22require 'IRC'
33
44class IRCBot < IRC
5 attr_accessor :operator
56 def initialize(nick, server, port, realname='RBot')
67 super
78
8 IRCEvent.add_handler("unhandled", lambda { |event|
9 method_id = "on_#{event.type}".to_sym
10 __send__(method_id, event) if respond_to?(method_id)
11 })
9 @operator = "%"
10 end
11
12 def on_privmsg(event)
13 if event.message =~ /^#{@operator}(\w+)(\s|.+)*/
14 command = $1
15 args = $2.to_s.split(/\s+/)
16 method = "do_#{command}".to_sym
17
18 if respond_to?(method)
19 if respond_to?(method)
20 __send__(method, event, args)
21 end
22 else
23 $stderr.puts "Invalid action: #{command} from #{event.from} in #{event.channel}"
24 end
25 end
1226 end
1327end