summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/topic.rb
blob: 93058a01e11ddd428ea8b3a7f03b915f04dd72ff (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
# Add a bunch of topic manipulation features

class TopicPlugin < Plugin
  def initialize
    super
    @separator = "|" # default separator
  end

  def help(plugin, topic="")
    case plugin
    when "topic"
      case topic
      when "add"
        return "topic add <text> => add <text> at the end the topic"
      when "prepend"
        return "topic prepend <text> => add <text> at the beginning of the topic"
      when "addat"
        return "topic addat <num> <text> => add <text> at position <num> of the topic"
      when "del", "delete"
        return "topic del <num> => remove section <num> from the topic"
      when "replace"
        return "topic replace <num> <text> => Replaces section <num> with <text>"
      when "sep", "separator"
        return "topic sep(arator) [<text>] => get or set the topic section separator"
      when "learn"
        return "topic learn => remembers the topic for later"
      when "restore"
        return "topic restore => resets the topic to the latest remembered one"
      when "clear"
        return "topic clear => clears the topic"
      when "set"
        return "topic set <text> => sets the topic to <text>"
      when "undo"
        return "topic undo => undoes the latest change to the topic"
      else
        return "topic add(at)|prepend|del(ete)|replace|sep(arator)|learn|restore|clear|set|undo: " + \
               "manipulate the topic of the current channel; use topic <#channel> <command> " + \
               "for private addressing"
      end
    end
  end

  def handletopic(m, param)
    return unless m.kind_of?(PrivMessage)
    if m.public?
      ch = m.channel
    else
      ch = m.server.get_channel(param[:channel])
      unless ch
        m.reply("I am not in channel #{param[:channel]}")
        return
      end
    end
    cmd = param[:command]
    txt = param[:text].to_s

    case cmd
    when /^a(dd|ppend)$/
      topicappend(m, ch, txt)
    when 'prepend'
      topicprepend(m, ch, txt)
    when 'addat'
      if txt =~ /\s*(-?\d+)\s+(.*)\s*/
        num = $1.to_i - 1
        num += 1 if num < 0
        txt = $2
        topicaddat(m, ch, num, txt)
      end
    when /^del(ete)?$/
      if txt =~ /\s*(-?\d+)\s*/
        num=$1.to_i - 1
        num += 1 if num < 0
        topicdel(m, ch, num)
      end
    when 'set'
      topicset(m, ch, txt)
    when 'clear'
      topicset(m, ch, '')
    when /^sep(arator)?$/
      topicsep(m, ch, txt)
    when 'learn'
      learntopic(m, ch)
    when 'replace'
      if txt =~ /\s*(-?\d+)\s+(.*)\s*/
        num = $1.to_i - 1
        num += 1 if num < 0
        txt = $2
        replacetopic(m, ch, num, txt)
      end
    when 'restore'
      restoretopic(m, ch)
    when 'undo'
      undotopic(m, ch)
    else
      m.reply 'unknown command'
    end
  end

  def topicsep(m, ch, txt)
    return if !@bot.auth.allow?("topic::edit::separator", m.source, m.replyto)
    if txt
      sep = txt.strip
      if sep != ""
        setsep(ch, sep)
      end
    end
    m.reply "Topic separator set to #{getsep(ch)}"
  end

  def setsep(ch, sep)
    raise unless ch.class <= Irc::Channel
    # TODO multiserver
    k = ch.downcase

    if @registry.has_key?(k)
      data = @registry[k]
    else
      data = Hash.new
    end

    oldsep = getsep(ch)
    topic = ch.topic.text
    topicarray = topic.split(/\s+#{Regexp.escape(oldsep)}\s*/)

    if sep != oldsep and topicarray.length > 0
      newtopic = topicarray.join(" #{sep} ")
      @bot.topic ch, newtopic if newtopic != topic
    end

    data[:separator] = sep
    @registry[k] = data
  end

  def getsep(ch)
    raise unless ch.class <= Irc::Channel
    # TODO multiserver
    k = ch.downcase

    if @registry.has_key?(k)
      if @registry[k].has_key?(:separator)
        return @registry[k][:separator]
      end
    end
    return @separator
  end

  def topicaddat(m, ch, num, txt)
    return if !@bot.auth.allow?("topic::edit::add", m.source, m.replyto)
    sep = getsep(ch)
    topic = ch.topic.text
    topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
    topicarray.insert(num, txt)
    newtopic = topicarray.join(" #{sep} ")
    changetopic(m, ch, newtopic)
  end

  def topicappend(m, ch, txt)
    topicaddat(m, ch, -1, txt)
  end

  def topicprepend(m, ch, txt)
    topicaddat(m, ch, 0, txt)
  end

  def topicdel(m, ch, num)
    return if !@bot.auth.allow?("topic::edit::del", m.source, m.replyto)
    sep = getsep(ch)
    topic = ch.topic.text
    topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
    topicarray.delete_at(num)
    newtopic = topicarray.join(" #{sep} ")
    changetopic(m, ch, newtopic)
  end

  def learntopic(m, ch)
    return if !@bot.auth.allow?("topic::store::store", m.source, m.replyto)
    topic = ch.topic.text
    k = ch.downcase
    if @registry.has_key?(k)
      data = @registry[k]
    else
      data = Hash.new
    end
    data[:topic] = topic
    @registry[k] = data
    m.okay
  end

  def replacetopic(m, ch, num, txt)
    return if !@bot.auth.allow?("topic::edit::replace", m.source, m.replyto)
    sep = getsep(ch)
    topic = ch.topic.text
    topicarray = topic.split(/\s+#{Regexp.escape(sep)}\s*/)
    topicarray[num] = txt
    newtopic = topicarray.join(" #{sep} ")
    changetopic(m, ch, newtopic)
  end

  def restoretopic(m, ch)
    return if !@bot.auth.allow?("topic::store::restore", m.source, m.replyto)
    k = ch.downcase
    if @registry.has_key?(k) && @registry[k].has_key?(:topic)
      topic = @registry[k][:topic]
      topicset(m, ch, topic)
    else
      m.reply "I don't remember any topic for #{ch}"
    end
  end

  def topicset(m, ch, text)
    return if !@bot.auth.allow?("topic::edit::replace", m.source, m.replyto)
    changetopic(m, ch, text)
  end

  # This method changes the topic on channel +ch+ to +text+, storing
  # the previous topic for undo
  def changetopic(m, ch, text)
    k = ch.downcase
    if @registry.has_key?(k)
      data = @registry[k]
    else
      data = Hash.new
    end

    data[:oldtopic] = ch.topic.text
    @registry[k] = data

    @bot.topic ch, text
  end

  def undotopic(m, ch)
    k = ch.downcase
    if @registry.has_key?(k)
      data = @registry[k]
      if data.has_key?(:oldtopic)
        changetopic(m, ch, data[:oldtopic].dup)
        return
      end
    end

    m.reply "No recent changes were recorded for #{ch}"
  end

end
plugin = TopicPlugin.new

plugin.map 'topic :command [*text]', :action => 'handletopic', :public => true, :private => false
plugin.map 'topic :channel :command [*text]', :action => 'handletopic', :public => false, :private => true

plugin.default_auth('*', false)