]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/linkbot.rb
linkbot plugins to properly delegate messages from linkbots
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / linkbot.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
5 # Copyright (C) 2006 Giuseppe Bilotta
6 #
7 # Based on an idea by hagabaka (Yaohan Chen <yaohan.chen@gmail.com>)
8 #
9 # This plugin is used to grab messages from eggdrops (or other bots) that link
10 # channels from different networks. For the time being, a PRIVMSG echoed by an
11 # eggdrop is assumed to be in the form:
12 #    <eggdrop> (nick@network) text of the message
13 # (TODO make it configurable) and it's fed back to the message delegators.
14 #
15 # This plugin also shows how to create 'fake' messages from a plugin, letting
16 # the bot parse them.
17 # TODO a possible enhancement to the Irc framework could be to create 'fake'
18 # servers to make this even easier.
19 class LinkBot < Plugin
20   BotConfig.register BotConfigArrayValue.new('linkbot.nicks',
21     :default => [],
22     :desc => "Nick(s) of the bots that act as channel links across networks")
23
24   # Initialize the plugin
25   def initialize
26     super
27   end
28
29   # Main method
30   def listen(m)
31     linkbots = @bot.config['linkbot.nicks']
32     return if linkbots.empty?
33     return unless linkbots.include?(m.sourcenick)
34     return unless m.kind_of?(PrivMessage)
35     # Now we know that _m_ is a PRIVMSG from a linkbot. Let's split it
36     # in nick, network, message
37     if m.message.match(/^\((\w+?)@(\w+?)\)\s+(.*)$/)
38       new_nick = $1
39       network = $2
40       message = $3
41
42       debug "#{m.sourcenick} reports that #{new_nick} said #{message.inspect} on #{network}"
43       # One way to pass the new message back to the bot is to create a PrivMessage
44       # and delegate it to the plugins
45       new_m = PrivMessage.new(@bot, m.server, m.server.user(new_nick), m.target, message)
46       @bot.plugins.delegate "listen", new_m
47       @bot.plugins.privmsg(new_m) if new_m.address?
48
49       ## Another way is to create a data Hash with source, target and message keys
50       ## and then letting the bot client :privmsg handler handle it
51       ## Note that this will also create irclog entries for the fake PRIVMSG
52       ## TODO we could probably add a :no_irc_log entry to the data passed to the
53       ## @bot.client handlers, or something like that
54       # data = {
55       #   :source => m.server.user(new_nick)
56       #   :target => m.target
57       #   :message => message
58       # }
59       # @bot.client[:privmsg].call(data)
60     end
61   end
62 end
63
64 plugin = LinkBot.new
65