]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/linkbot.rb
factoids plugin: an empty trigger_pattern list means any word is a keyword
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / linkbot.rb
index 624d241d6f9f8f5a9d61d86cf2bd8e0c2618d1ea..6a5cafbce728967292d854125ce84ccae90e0b33 100644 (file)
@@ -1,8 +1,11 @@
 #-- vim:sw=2:et
 #++
 #
-# Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
-# Copyright (C) 2006 Giuseppe Bilotta
+# :title: linkbot management for rbot
+#
+# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
+# Copyright:: (C) 2006 Giuseppe Bilotta
+# License:: GPL v2
 #
 # Based on an idea by hagabaka (Yaohan Chen <yaohan.chen@gmail.com>)
 #
 # channels from different networks. For the time being, a PRIVMSG echoed by an
 # eggdrop is assumed to be in the form:
 #    <eggdrop> (nick@network) text of the message
-# (TODO make it configurable) and it's fed back to the message delegators.
+# and it's fed back to the message delegators.
 #
 # This plugin also shows how to create 'fake' messages from a plugin, letting
 # the bot parse them.
+#
 # TODO a possible enhancement to the Irc framework could be to create 'fake'
 # servers to make this even easier.
+
 class LinkBot < Plugin
-  BotConfig.register BotConfigArrayValue.new('linkbot.nicks',
+  Config.register Config::ArrayValue.new('linkbot.nicks',
     :default => [],
     :desc => "Nick(s) of the bots that act as channel links across networks")
 
+  Config.register Config::ArrayValue.new('linkbot.message_patterns',
+    :default => ['^<(\S+?)@(\S+?)>\s+(.*)$', '^\((\S+?)@(\S+?)\)\s+(.*)$'],
+    :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")
+  # TODO use template strings instead of regexp for user friendliness
+  
   # Initialize the plugin
   def initialize
     super
+    
+    @message_patterns = @bot.config['linkbot.message_patterns'].map {|p|
+      Regexp.new(p)
+    }
   end
 
   # Main method
@@ -34,10 +48,12 @@ class LinkBot < Plugin
     return unless m.kind_of?(PrivMessage)
     # Now we know that _m_ is a PRIVMSG from a linkbot. Let's split it
     # in nick, network, message
-    if m.message.match(/^\((\w+?)@(\w+?)\)\s+(.*)$/)
-      new_nick = $1
-      network = $2
-      message = $3
+    message = BasicUserMessage.stripcolour m.message
+    if @message_patterns.any? {|p| message =~ p}
+      # if the regexp doesn't contain all parts, the default values get used
+      new_nick = $1 || 'unknown_nick'
+      network = $2 || 'unknown_network'
+      message = $3 || 'unknown_message'
 
       debug "#{m.sourcenick} reports that #{new_nick} said #{message.inspect} on #{network}"
       # One way to pass the new message back to the bot is to create a PrivMessage