]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/linkbot.rb
plugin(points): new message parser, see #34
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / linkbot.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: linkbot management for rbot
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2006 Giuseppe Bilotta
8 # License:: GPL v2
9 #
10 # Based on an idea by hagabaka (Yaohan Chen <yaohan.chen@gmail.com>)
11 #
12 # This plugin is used to grab messages from eggdrops (or other bots) that link
13 # channels from different networks. For the time being, a PRIVMSG echoed by an
14 # eggdrop is assumed to be in the form:
15 #    <eggdrop> (nick@network) text of the message
16 # and it's fed back to the message delegators.
17 #
18 # This plugin also shows how to create 'fake' messages from a plugin, letting
19 # the bot parse them.
20 #
21 # TODO a possible enhancement to the Irc framework could be to create 'fake'
22 # servers to make this even easier.
23
24 class LinkBot < Plugin
25   Config.register Config::ArrayValue.new('linkbot.nicks',
26     :default => [],
27     :desc => "Nick(s) of the bots that act as channel links across networks")
28
29   Config.register Config::ArrayValue.new('linkbot.message_patterns',
30     :default => ['^<(\S+?)@(\S+?)>\s+(.*)$', '^\((\S+?)@(\S+?)\)\s+(.*)$'],
31     :desc => "List of regexp which match linkbot messages; each regexp needs to have three captures, which in order are the nickname of the original speaker, network, and original message",
32     :on_change => proc {|bot, v| bot.plugins['linkbot'].update_patterns})
33   # TODO use template strings instead of regexp for user friendliness
34
35   # Initialize the plugin
36   def initialize
37     super
38     update_patterns
39   end
40
41   def update_patterns
42     @message_patterns = @bot.config['linkbot.message_patterns'].map {|p|
43       Regexp.new(p)
44     }
45   end
46
47   # Main method
48   def message(m)
49     linkbots = @bot.config['linkbot.nicks']
50     return if linkbots.empty?
51     return unless linkbots.include?(m.sourcenick)
52     # Now we know that _m_ is a PRIVMSG from a linkbot. Let's split it
53     # in nick, network, message
54     if @message_patterns.any? {|p| m.message =~ p}
55       # if the regexp doesn't contain all parts, the default values get used
56       new_nick = $1 || 'unknown_nick'
57       network = $2 || 'unknown_network'
58       message = $3 || 'unknown_message'
59       # strip any formatting codes in the new_nick. some people configure their linkbots
60       # to embed these codes in nicknames (such as to\B\Bm), to avoid triggering the
61       # person's highlight
62       new_nick.gsub!(/[#{Bold}#{Underline}#{Reverse}#{Italic}#{NormalText}]/, '')
63       debug "#{m.sourcenick} reports that #{new_nick} said #{message.inspect} on #{network}"
64       begin
65         # Pass the new message back to the bot
66         fake_message(message, :from => m, :source => m.server.user(new_nick))
67       rescue RecurseTooDeep => e
68         error e
69       end
70     end
71   end
72 end
73
74 plugin = LinkBot.new
75