2 # Salutations plugin: respond to salutations
\r
3 # TODO allow online editing of salutations
\r
4 # TODO *REMEMBER* to set @changed to true after edit
\r
5 # TODO or changes won't be saved
\r
7 # TODO reply in the language the salutation was in
\r
9 unless Array.respond_to?(:pick_one)
\r
10 debug "Defining the pick_one method for Array"
\r
13 return nil if self.empty?
\r
14 self[rand(self.length)]
\r
20 class SalutPlugin < Plugin
\r
21 BotConfig.register BotConfigBooleanValue.new('salut.all_languages',
\r
23 :desc => "Check for a salutation in all languages and not just in the one defined by core.language",
\r
24 :on_change => Proc.new {|bot, v| bot.plugins['salut'].reload}
\r
26 BotConfig.register BotConfigBooleanValue.new('salut.address_only',
\r
28 :desc => "When set to true, the bot will only reply to salutations directed at him",
\r
29 :on_change => Proc.new {|bot, v| bot.plugins['salut'].reload}
\r
34 @salutations = Hash.new
\r
36 @main_lang_str = nil
\r
44 def set_language(what)
\r
54 @salutations.each { |lang, hash|
\r
55 hash.each { |situation, array|
\r
57 when /^generic-dest$/
\r
68 @match[:in] = Regexp.new("\\b(?:" + ar_in.uniq.map { |txt|
\r
70 }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_in.empty?
\r
71 @match[:out] = Regexp.new("\\b(?:" + ar_out.uniq.map { |txt|
\r
73 }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_out.empty?
\r
74 @match[:both] = Regexp.new("\\b(?:" + ar_both.uniq.map { |txt|
\r
76 }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_both.empty?
\r
77 debug "Matches: #{@match.inspect}"
\r
78 @match[:dest] = Regexp.new("\\b(?:" + ar_dest.uniq.map { |txt|
\r
80 }.join('|') + ")\\b", Regexp::IGNORECASE) unless ar_dest.empty?
\r
81 @punct = /\s*[.,:!;?]?\s*/ # Punctuation
\r
85 return unless @match
\r
86 return unless m.kind_of?(PrivMessage)
\r
87 return if m.address? and m.plugin == 'config'
\r
88 to_me = m.address? || m.message =~ /#{Regexp.escape(@bot.nick)}/i
\r
89 if @bot.config['salut.address_only']
\r
93 [:both, :in, :out].each { |k|
\r
94 next unless @match[k]
\r
95 # debug "Checking salutations #{k} (#{@match[k].inspect})"
\r
96 if m.message =~ @match[k]
\r
101 return unless salut
\r
102 # If the bot wasn't addressed, we continue only if the match was exact
\r
103 # (apart from space and punctuation) or if @match[:dest] matches too
\r
104 return unless to_me or m.message =~ @match[:dest] or m.message =~ /^#{@punct}#{@match[salut]}#{@punct}$/
\r
108 salut_reply(m, salut, :morning)
\r
110 salut_reply(m, salut, :afternoon)
\r
112 salut_reply(m, salut, :evening)
\r
116 def salut_reply(m, k, time)
\r
117 debug "Replying to #{k} in the #{time}"
\r
124 debug "Building array ..."
\r
126 rep_ar += @salutations[@main_lang].fetch("#{time}#{sfx}".to_sym, [])
\r
127 rep_ar += @salutations[@main_lang].fetch("#{time}".to_sym, []) unless sfx.empty?
\r
128 rep_ar += @salutations[@main_lang].fetch("generic#{sfx}".to_sym, [])
\r
129 rep_ar += @salutations[@main_lang].fetch("generic".to_sym, []) unless sfx.empty?
\r
130 debug "Choosing reply in #{rep_ar.inspect} ..."
\r
132 if m.public? # and (m.address? or m =~ /#{Regexp.escape(@bot.nick)}/)
\r
133 choice = @bot.lang.get("hello_X") % m.sourcenick
\r
135 choice = @bot.lang.get("hello") % m.sourcenick
\r
138 choice = rep_ar.pick_one
\r
139 if m.public? and (m.address? or m.message =~ /#{Regexp.escape(@bot.nick)}/)
\r
140 choice += "#{[',',''].pick_one} #{m.sourcenick}"
\r
141 choice += [" :)", " :D", "!", "", "", ""].pick_one
\r
144 debug "Replying #{choice}"
\r
145 m.plainreply choice
\r
150 @main_lang_str = @bot.config['core.language']
\r
151 @main_lang = @main_lang_str.to_sym
\r
152 @all_langs = @bot.config['salut.all_languages']
\r
154 # Get all available languages
\r
155 langs = Dir.new("#{@bot.botclass}/salut").collect {|f|
\r
156 f =~ /salut-([^.]+)/ ? $1 : nil
\r
158 langs.each { |lang|
\r
159 @salutations[lang.to_sym] = load_lang(lang)
\r
163 @salutations[@main_lang] = load_lang(@main_lang_str)
\r
169 def load_lang(lang)
\r
170 dir = "#{@bot.botclass}/salut"
\r
171 if not File.exist?(dir)
\r
174 file = "#{@bot.botclass}/salut/salut-#{lang}"
\r
175 if File.exist?(file)
\r
177 salutations = Hash.new
\r
178 content = YAML::load_file(file)
\r
179 content.each { |key, val|
\r
180 salutations[key.to_sym] = val
\r
184 error "failed to read salutations in #{lang}: #{$!}"
\r
191 return if @salutations.empty?
\r
192 return unless @changed
\r
193 @salutations.each { |lang, val|
\r
195 save_lang(lang, val)
\r
200 def save_lang(lang, val)
\r
201 fn = "#{@bot.botclass}/salut/salut-#{lang}"
\r
202 Utils.safe_save(fn) { |file|
\r
203 file.puts val.to_yaml
\r
209 plugin = SalutPlugin.new
\r