]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/sed.rb
plugin(points): new message parser, see #34
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / sed.rb
1 # -*- coding: utf-8 -*-
2 #
3 # :title: Sed plugin
4 #
5 # Author:: MelmothX <melmothx@gmail.com>
6 # Copyright:: No copyright
7 # License:: Public Domain
8 #
9
10 class Sed < Plugin
11   def initialize
12     super
13     # create the multidimensional hash 
14     @amendlog = Hash.new
15 #    @answers = [
16 #                "voleva dire",
17 #                ", nella sua sbataggine, intendeva",
18 #                "si รจ sbagliato. Voleva dire",
19 #               ]
20   end
21
22   def help(plugin, topic="")
23     "Fix the previous sentence using regexp and a sed-like syntax. Supported delimiters are /|,! and the modifier \"g\". Grouping is supported via parens, and backreferencing is done via \\1 \\2 and so on. You don't have to directly address the bot. Examples: <nick>hello <nick>s/e/u/"
24   end
25   
26   def message(m)
27     return unless m.public? 
28     # log 
29     source = m.source
30     channel = m.channel
31     stuff = m.message
32     if not @amendlog.has_key?(channel)
33       @amendlog[channel] = Hash.new
34     end
35     oldstring = @amendlog[channel][source] 
36     newstring = oldstring
37     if m.message.match(/^s([\/|,!])(.*?)\1(.*?)\1(g?)/) then
38       target = Regexp.new($2)
39       replace_with = $3
40       global = $4
41       if (global == "")
42         newstring = oldstring.sub(target, replace_with)
43       else
44         newstring = oldstring.gsub(target, replace_with)
45       end
46 #      sentence = @answers[rand(@answers.length)]
47       sentence = _("meant")
48       if (oldstring == newstring)
49         failreply = _("You did something wrong... Try s/you/me/ or tell me \"help sed\"")
50         m.reply("#{source}: #{failreply}")
51         return
52       end
53       m.reply("#{source} #{sentence}: \"#{newstring}\"", :nick => false)
54       return
55     end
56     @amendlog[channel][source] = stuff
57   end
58 end
59 plugin = Sed.new
60