summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/topic.rb
blob: 075b69426953e14d09c4115fd78e0a731e411bee (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
# Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
# Add a bunch of topic manipulation features
# NOTE: topic separator is defined by the global symbol SEPARATOR

SEPARATOR=" | "

class TopicPlugin < Plugin
  def initialize
    super
    @addtopic = /(?:topic(?:append|add)|(?:append|add)topic)/
    @prependtopic = /(?:topicprepend|prependtopic)/
    @addtopicat = /(?:(?:addtopic|topicadd)at)/
    @deltopic = /(?:deltopic|topicdel)/
  end

  def help(plugin, topic="")
    case plugin
    when "topic"
      case topic
      when @addtopic
	return "#{topic} <text> => add <text> at the end the topic"
      when @prependtopic
	return "#{topic} <text> => add <text> at the beginning of the topic"
      when @addtopicat
	return "#{topic} <num> <text> => add <text> at position <num> of the topic"
      when @deltopic
	return "#{topic} <num> => remove section <num> from the topic"
      when "learntopic"
	return "learntopic => remembers the topic for later"
      when "resumetopic"
	return "resumetopic => resets the topic to the latest remembered one"
      when "settopic"
	return "settopic <text> => sets the topic to <text>"
      else
	return "topic commands: addtopic, prependtopic, addtopicat, deltopic, learntopic, resumetopic, settopic"
      end
    when "learntopic"
      return "learntopic => remembers the topic for later"
    when "resumetopic"
      return "resumetopic => resets the topic to the latest remembered one"
    when "settopic"
      return "settopic <text> => sets the topic to <text>"
    end
  end

  def listen(m)
    return unless m.kind_of?(PrivMessage) && m.public?
    command = m.message.dup
    debug command
    if m.address? || command.gsub!(/^!/, "")
      case command
      when /^#{@addtopic}\s+(.*)\s*/
	txt=$1
	debug txt
	if @bot.auth.allow?("topic", m.source, m.replyto)
	  topicappend(m, txt)
	end
      when /^#{@prependtopic}\s+(.*)\s*/
	txt=$1
	debug txt
	if @bot.auth.allow?("topic", m.source, m.replyto)
	  topicaddat(m, 0, txt)
	end
      when /^#{@addtopicat}\s+(-?\d+)\s+(.*)\s*/
	num=$1.to_i - 1
	num += 1 if num < 0
	txt=$2
	debug txt
	if @bot.auth.allow?("topic", m.source, m.replyto)
	  topicaddat(m, num, txt)
	end
      when /^#{@deltopic}\s+(-?\d+)\s*/
	num=$1.to_i - 1
	num += 1 if num < 0
	debug num
	if @bot.auth.allow?("topic", m.source, m.replyto)
	  topicdel(m, num)
	end
      end
    end
  end

  def topicaddat(m, num, txt)
    channel = m.channel.downcase
    topic = @bot.channels[m.channel].topic.to_s
    topicarray = topic.split(SEPARATOR)
    topicarray.insert(num, txt)
    newtopic = topicarray.join(SEPARATOR)
    @bot.topic channel, newtopic
  end

  def topicappend(m, txt)
    channel = m.channel.downcase
    topic = @bot.channels[m.channel].topic.to_s
    topicarray = topic.split(SEPARATOR)
    topicarray << txt
    newtopic = topicarray.join(SEPARATOR)
    @bot.topic channel, newtopic
  end

  def topicdel(m, num)
    channel = m.channel.downcase
    topic = @bot.channels[m.channel].topic.to_s
    topicarray = topic.split(SEPARATOR)
    topicarray.delete_at(num)
    newtopic = topicarray.join(SEPARATOR)
    @bot.topic channel, newtopic
  end

  def learntopic(m, param)
    return if !@bot.auth.allow?("learntopic", m.source, m.replyto)
    channel = m.channel.downcase
    debug channel
    topic = @bot.channels[m.channel].topic.to_s
    @registry[channel] = topic
    @bot.say channel, "Ok"
  end

  def resumetopic(m, param)
    return if !@bot.auth.allow?("resumetopic", m.source, m.replyto)
    channel = m.channel.downcase
    debug "Channel: #{channel}"
    if @registry.has_key?(channel)
      topic = @registry[channel]
      debug "Channel: #{channel}, topic: #{topic}"
      @bot.topic channel, topic
    else
      @bot.say channel, "Non ricordo nulla"
    end
  end

  def settopic(m, param)
    return if !@bot.auth.allow?("topic", m.source, m.replyto)
    channel = m.channel.downcase
    debug "Channel: #{channel}"
    @bot.topic channel, param[:text].to_s
  end

end
plugin = TopicPlugin.new
plugin.register 'topic'
plugin.map 'settopic *text', :action => 'settopic'
plugin.map 'resumetopic'
plugin.map 'learntopic'