4 # :title: Factoids pluing
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2007 Giuseppe Bilotta
10 # Store (and retrieve) unstructured one-sentence factoids
12 class FactoidsPlugin < Plugin
16 @hash = hash.reject { |k, val| val.nil? or val.empty? rescue false }
17 raise ArgumentError, "no fact!" unless @hash[:fact]
18 if String === @hash[:when]
19 @hash[:when] = Time.parse @hash[:when]
24 show_meta = opts[:meta]
32 metadata << _("from %{who}" % @hash)
35 metadata << _("on %{when}" % @hash)
38 metadata << _("in %{where}" % @hash)
40 unless metadata.empty?
41 meta << _(" [%{data}]" % {:data => metadata.join(" ")})
51 @hash.send(:[]=,*args)
57 alias :to_hash :to_hsh
60 class FactoidList < ArrayOf
68 self.map { |f| f[:fact] }.index(fact)
88 @dir = File.join(@bot.botclass,"factoids")
89 @filename = "factoids.rbot"
90 @factoids = FactoidList.new
99 def read_factfile(name=@filename,dir=@dir)
100 fname = File.join(dir,name)
102 expf = File.expand_path(fname)
103 expd = File.expand_path(dir)
104 raise ArgumentError, _("%{name} (%{fname}) must be under %{dir}" % {
108 }) unless expf.index(expd) == 0
110 if File.exist?(fname)
111 raise ArgumentError, _("%{name} is not a file" % {
113 }) unless File.file?(fname)
114 factoids = File.readlines(fname)
115 return if factoids.empty?
116 firstline = factoids.shift
117 pattern = firstline.chomp.split(" | ")
118 if pattern.length == 1 and pattern.first != "fact"
119 factoids.unshift(firstline)
121 @factoids << Factoid.new( :fact => f.chomp )
124 pattern.map! { |p| p.intern }
125 raise ArgumentError, _("fact must be the last field") unless pattern.last == :fact
127 ar = f.chomp.split(" | ", pattern.length)
128 @factoids << Factoid.new(Hash[*([pattern, ar].transpose.flatten)])
132 raise ArgumentError, _("%{name} (%{fname}) doesn't exist" % {
140 return unless @changed
141 Dir.mkdir(@dir) unless FileTest.directory?(@dir)
142 fname = File.join(@dir,@filename)
143 ar = ["when | who | where | fact"]
145 ar << "%s | %s | %s | %s" % [ f[:when], f[:who], f[:where], f[:fact]]
147 Utils.safe_save(fname) do |file|
153 def help(plugin, topic="")
154 _("factoids plugin: learn that <factoid>, forget that <factoids>, facts about <words>")
158 factoid = Factoid.new(
159 :fact => params[:stuff].to_s,
161 :who => m.source.fullform,
162 :where => m.channel.to_s
164 if @factoids.index(factoid)
165 m.reply _("I already know that %{factoid}" % { :factoid => factoid })
173 def forget(m, params)
175 idx = params[:index].scan(/\d+/).first.to_i
176 total = @factoids.length
177 if idx <= 0 or idx > total
178 m.reply _("please select a fact number between 1 and %{total}" % { :total => total })
181 if factoid = @factoids.delete_at(idx-1)
182 m.reply _("I forgot that %{factoid}" % { :factoid => factoid })
185 m.reply _("I couldn't delete factoid %{idx}" % { :idx => idx })
188 factoid = params[:stuff].to_s
189 if @factoids.delete(factoid)
193 m.reply _("I didn't know that %{factoid}" % { :factoid => factoid })
198 def long_fact(fact,index=nil,total=@factoids.length)
199 idx = index || @factoids.index(fact)+1
200 _("fact #%{idx} of %{total}: %{fact}" % {
203 :fact => fact.to_s(:meta => true)
208 total = @factoids.length
209 if params[:words].empty?
210 m.reply _("I know %{total} facts" % { :total => total })
212 rx = Regexp.new(params[:words].to_s, true)
213 known = @factoids.grep(rx)
216 reply << _("I know nothing about %{words}" % params)
222 m.reply _("%{len} out of %{total} facts refer to %{words}, I'll only show %{max}" % {
225 :words => params[:words].to_s,
228 while known.length > max_facts
233 reply << long_fact(f)
236 m.reply reply.join(" -- ")
243 total = @factoids.length
245 idx = params[:index].scan(/\d+/).first.to_i
246 if idx <= 0 or idx > total
247 m.reply _("please select a fact number between 1 and %{total}" % { :total => total })
250 fact = @factoids[idx-1]
253 if params[:words].empty?
255 m.reply _("I know nothing")
260 rx = Regexp.new(params[:words].to_s, true)
261 known = @factoids.grep(rx)
263 m.reply _("I know nothing about %{words}" % params)
267 fact = known.pick_one
268 idx = @factoids.index(fact)+1
270 m.reply long_fact(fact, idx, total)
273 def edit_fact(m, params)
276 total = @factoids.length
277 idx = params[:index].scan(/\d+/).first.to_i
278 if idx <= 0 or idx > total
279 m.reply _("please select a fact number between 1 and %{total}" % { :total => total })
282 fact = @factoids[idx-1]
285 who = params[:who].to_s.sub(/^me$/, m.source.fullform)
290 dstr = params[:when].to_s
292 fact[:when] = Time.parse(dstr, "")
295 raise ArgumentError, _("not a date '%{dstr}'" % { :dstr => dstr })
299 fact[:where] = params[:where].to_s
303 m.reply _("couldn't change learn data for fact %{fact}: %{err}" % {
312 def import(m, params)
313 fname = params[:filename].to_s
314 oldlen = @factoids.length
318 m.reply _("failed to import facts from %{fname}: %{err}" % {
323 m.reply _("%{len} facts loaded from %{fname}" % {
325 :len => @factoids.length - oldlen
332 plugin = FactoidsPlugin.new
334 plugin.default_auth('edit', false)
335 plugin.default_auth('import', false)
337 plugin.map 'learn that *stuff'
338 plugin.map 'forget that *stuff', :auth_path => 'edit'
339 plugin.map 'forget fact :index', :requirements => { :index => /^#?\d+$/ }, :auth_path => 'edit'
340 plugin.map 'facts [about *words]'
341 plugin.map 'fact [about *words]'
342 plugin.map 'fact :index', :requirements => { :index => /^#?\d+$/ }
344 plugin.map 'fact :index :learn from *who', :action => :edit_fact, :requirements => { :learn => /^((?:is|was)\s+)?learn(ed|t)$/, :index => /^#?\d+$/ }, :auth_path => 'edit'
345 plugin.map 'fact :index :learn on *when', :action => :edit_fact, :requirements => { :learn => /^((?:is|was)\s+)?learn(ed|t)$/, :index => /^#?\d+$/ }, :auth_path => 'edit'
346 plugin.map 'fact :index :learn in *where', :action => :edit_fact, :requirements => { :learn => /^((?:is|was)\s+)?learn(ed|t)$/, :index => /^#?\d+$/ }, :auth_path => 'edit'
348 plugin.map 'facts import [from] *filename', :action => :import, :auth_path => 'import'