4 # :title: linkbot management for rbot
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2006 Giuseppe Bilotta
10 # Based on an idea by hagabaka (Yaohan Chen <yaohan.chen@gmail.com>)
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.
18 # This plugin also shows how to create 'fake' messages from a plugin, letting
21 # TODO a possible enhancement to the Irc framework could be to create 'fake'
22 # servers to make this even easier.
24 class LinkBot < Plugin
25 Config.register Config::ArrayValue.new('linkbot.nicks',
27 :desc => "Nick(s) of the bots that act as channel links across networks")
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 # TODO use template strings instead of regexp for user friendliness
34 # Initialize the plugin
38 @message_patterns = @bot.config['linkbot.message_patterns'].map {|p|
45 linkbots = @bot.config['linkbot.nicks']
46 return if linkbots.empty?
47 return unless linkbots.include?(m.sourcenick)
48 return unless m.kind_of?(PrivMessage)
49 # Now we know that _m_ is a PRIVMSG from a linkbot. Let's split it
50 # in nick, network, message
51 message = BasicUserMessage.stripcolour m.message
52 if @message_patterns.any? {|p| message =~ p}
53 # if the regexp doesn't contain all parts, the default values get used
54 new_nick = $1 || 'unknown_nick'
55 network = $2 || 'unknown_network'
56 message = $3 || 'unknown_message'
58 debug "#{m.sourcenick} reports that #{new_nick} said #{message.inspect} on #{network}"
59 # One way to pass the new message back to the bot is to create a PrivMessage
60 # and delegate it to the plugins
61 new_m = PrivMessage.new(@bot, m.server, m.server.user(new_nick), m.target, message)
62 @bot.plugins.delegate "listen", new_m
63 @bot.plugins.privmsg(new_m) if new_m.address?
65 ## Another way is to create a data Hash with source, target and message keys
66 ## and then letting the bot client :privmsg handler handle it
67 ## Note that this will also create irclog entries for the fake PRIVMSG
68 ## TODO we could probably add a :no_irc_log entry to the data passed to the
69 ## @bot.client handlers, or something like that
71 # :source => m.server.user(new_nick)
75 # @bot.client[:privmsg].call(data)