Commit 6a80c4f0e24ab5fef5b77681cd4652c5e7876d11
- Diff rendering mode:
- inline
- side by side
IRC.rb
(13 / 4)
|   | |||
| 9 | 9 | # Class IRC is a master class that handles connection to the irc | |
| 10 | 10 | # server and pasring of IRC events, through the IRCEvent class. | |
| 11 | 11 | class IRC | |
| 12 | attr_reader :nick, :server, :port | ||
| 13 | |||
| 12 | 14 | @channels = nil | |
| 13 | 15 | # Create a new IRC Object instance | |
| 14 | 16 | def initialize( nick, server, port, realname='RBot') | |
| … | … | ||
| 34 | 34 | ||
| 35 | 35 | IRCEvent.add_handler('332', topic_proc) | |
| 36 | 36 | 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) | ||
| 39 | 48 | end | |
| 40 | |||
| 41 | attr_reader :nick, :server, :port | ||
| 42 | 49 | ||
| 43 | 50 | # Join a channel, adding it to the list of joined channels | |
| 44 | 51 | def add_channel channel |
IRCBot.rb
(18 / 4)
|   | |||
| 2 | 2 | require 'IRC' | |
| 3 | 3 | ||
| 4 | 4 | class IRCBot < IRC | |
| 5 | attr_accessor :operator | ||
| 5 | 6 | def initialize(nick, server, port, realname='RBot') | |
| 6 | 7 | super | |
| 7 | 8 | ||
| 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 | ||
| 12 | 26 | end | |
| 13 | 27 | end |

