]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/linkbot.rb
Plugin header boilerplating.
[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 # (TODO make it configurable) 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   BotConfig.register BotConfigArrayValue.new('linkbot.nicks',
26     :default => [],
27     :desc => "Nick(s) of the bots that act as channel links across networks")
28
29   # Initialize the plugin
30   def initialize
31     super
32   end
33
34   # Main method
35   def listen(m)
36     linkbots = @bot.config['linkbot.nicks']
37     return if linkbots.empty?
38     return unless linkbots.include?(m.sourcenick)
39     return unless m.kind_of?(PrivMessage)
40     # Now we know that _m_ is a PRIVMSG from a linkbot. Let's split it
41     # in nick, network, message
42     if m.message.match(/^\((\S+?)@(\S+?)\)\s+(.*)$/)
43       new_nick = $1
44       network = $2
45       message = $3
46
47       debug "#{m.sourcenick} reports that #{new_nick} said #{message.inspect} on #{network}"
48       # One way to pass the new message back to the bot is to create a PrivMessage
49       # and delegate it to the plugins
50       new_m = PrivMessage.new(@bot, m.server, m.server.user(new_nick), m.target, message)
51       @bot.plugins.delegate "listen", new_m
52       @bot.plugins.privmsg(new_m) if new_m.address?
53
54       ## Another way is to create a data Hash with source, target and message keys
55       ## and then letting the bot client :privmsg handler handle it
56       ## Note that this will also create irclog entries for the fake PRIVMSG
57       ## TODO we could probably add a :no_irc_log entry to the data passed to the
58       ## @bot.client handlers, or something like that
59       # data = {
60       #   :source => m.server.user(new_nick)
61       #   :target => m.target
62       #   :message => message
63       # }
64       # @bot.client[:privmsg].call(data)
65     end
66   end
67 end
68
69 plugin = LinkBot.new
70