summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/lart.rb
blob: 435ed04dab3613f71a02ec4ef5b1ea163c767489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#  Original Author:
#               Michael Brailsford  <brailsmt@yahoo.com>
#               aka brailsmt
#  Author:      Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
#  Purpose:     Provide for humorous larts and praises
#  Original Copyright:
#               2002 Michael Brailsford.  All rights reserved.
#  Copyright:   2006 Giuseppe Bilotta.  All rights reserved.
#  License:     This plugin is licensed under the BSD license.  The terms of
#               which follow.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#  1. Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.

class LartPlugin < Plugin

  # Keep a 1:1 relation between commands and handlers
  @@handlers = {
    "lart" => "handle_lart",
    "praise" => "handle_praise",
    "addlart" => "handle_addlart",
    "rmlart" => "handle_rmlart",
    "addpraise" => "handle_addpraise",
    "rmpraise" => "handle_rmpraise"
  }

  def name
    "lart"
  end

  def initialize
    @larts = Array.new
    @praises = Array.new
    @lartfile = nil
    @praisefile = nil
    super
  end

  def set_language(lang)
    save
    @lartfile = "#{@bot.botclass}/lart/larts-#{lang}"
    @praisefile = "#{@bot.botclass}/lart/praises-#{lang}"
    @larts.clear
    @praises.clear
    if File.exists? @lartfile
      IO.foreach(@lartfile) { |line|
        @larts << line.chomp
      }
    end
    if File.exists? @praisefile
      IO.foreach(@praisefile) { |line|
        @praises << line.chomp
      }
    end
  end

  def cleanup
  end

  def save
    return if @lartfile.nil? and @praisefile.nil?
    Dir.mkdir("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart"
    # TODO implement safe saving here too
    Utils.safe_save(@lartfile) { |file|
      file.puts @larts
    }
    Utils.safe_save(@praisefile) { |file|
      file.puts @praises
    }
  end

  def privmsg(m)
    if not m.params
      m.reply "What a crazy fool!  Did you mean |help stats?"
      return
    end

    meth = self.method(@@handlers[m.plugin])
    meth.call(m) if(@bot.auth.allow?(m.plugin, m.source, m.replyto))
  end

  def help(plugin, topic="")
    "Lart: The lart plugin allows you to lart/praise someone in the channel. You can also add new larts and new praises as well as delete them. For the curious, LART is an acronym for Luser Attitude Readjustment Tool. Usage: lart <who> [<reason>] -- larts <who> for <reason>. praise <who> [<reason>] -- praises <who> for <reason>. [add|rm][lart|praise] -- Add or remove a lart or praise."
  end

  # The following are command handler

  def handle_lart(m)
    for_idx = m.params =~ /\s+\bfor\b/
    if for_idx
      nick = m.params[0, for_idx]
    else
      nick = m.params
    end
    lart = @larts[get_msg_idx(@larts.length)]
    if lart == nil
      m.reply "I dunno any larts"
      return
    end
    if nick == @bot.nick
      lart = replace_who lart, m.sourcenick
      lart << " for trying to make me lart myself"
    else
      lart = replace_who lart, nick
      lart << m.params[for_idx, m.params.length] if for_idx
    end

    @bot.action m.replyto, lart
  end

  def handle_praise(m)
    for_idx = m.params =~ /\s+\bfor\b/
    if for_idx
      nick = m.params[0, for_idx]
    else
      nick = m.params
    end
    praise = @praises[get_msg_idx(@praises.length)]
    if not praise
      m.reply "I dunno any praises"
      return
    end

    if nick == m.sourcenick
      praise = @larts[get_msg_idx(@larts.length)]
      praise = replace_who praise, nick
    else
      praise = replace_who praise, nick
      praise << m.params.gsub("#{nick}", "")
    end

    @bot.action m.replyto, praise
  end

  def handle_addlart(m)
    @larts << m.params
    m.okay
  end

  def handle_rmlart(m)
    @larts.delete m.params
    m.okay
  end

  def handle_addpraise(m)
    @praises << m.params
    m.okay
  end

  def handle_rmpraise(m)
    @praises.delete m.params
    m.okay
  end

  #  The following are utils for larts/praises
  def replace_who(msg, nick)
    msg.gsub(/<who>/i, "#{nick}")
  end

  def get_msg_idx(max)
    idx = rand(max)
  end

end
plugin = LartPlugin.new
plugin.register("lart")
plugin.register("praise")

plugin.register("addlart")
plugin.register("addpraise")

plugin.register("rmlart")
plugin.register("rmpraise")