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 idx = @factoids.index(factoid)
165 m.reply _("I already know that %{factoid} [#%{idx}]" % {
173 fact(m, :index => @factoids.length.to_s)
177 def forget(m, params)
179 idx = params[:index].scan(/\d+/).first.to_i
180 total = @factoids.length
181 if idx <= 0 or idx > total
182 m.reply _("please select a fact number between 1 and %{total}" % { :total => total })
185 if factoid = @factoids.delete_at(idx-1)
186 m.reply _("I forgot that %{factoid}" % { :factoid => factoid })
189 m.reply _("I couldn't delete factoid %{idx}" % { :idx => idx })
192 factoid = params[:stuff].to_s
193 if @factoids.delete(factoid)
197 m.reply _("I didn't know that %{factoid}" % { :factoid => factoid })
202 def long_fact(fact,index=nil,total=@factoids.length)
203 idx = index || @factoids.index(fact)+1
204 _("fact #%{idx} of %{total}: %{fact}" % {
207 :fact => fact.to_s(:meta => true)
212 total = @factoids.length
213 if params[:words].empty?
214 m.reply _("I know %{total} facts" % { :total => total })
216 rx = Regexp.new(params[:words].to_s, true)
217 known = @factoids.grep(rx)
220 reply << _("I know nothing about %{words}" % params)
226 m.reply _("%{len} out of %{total} facts refer to %{words}, I'll only show %{max}" % {
229 :words => params[:words].to_s,
232 while known.length > max_facts
237 reply << long_fact(f)
240 m.reply reply.join(" -- ")
247 total = @factoids.length
249 idx = params[:index].scan(/\d+/).first.to_i
250 if idx <= 0 or idx > total
251 m.reply _("please select a fact number between 1 and %{total}" % { :total => total })
254 fact = @factoids[idx-1]
257 if params[:words].empty?
259 m.reply _("I know nothing")
264 rx = Regexp.new(params[:words].to_s, true)
265 known = @factoids.grep(rx)
267 m.reply _("I know nothing about %{words}" % params)
271 fact = known.pick_one
272 idx = @factoids.index(fact)+1
274 m.reply long_fact(fact, idx, total)
277 def edit_fact(m, params)
280 total = @factoids.length
281 idx = params[:index].scan(/\d+/).first.to_i
282 if idx <= 0 or idx > total
283 m.reply _("please select a fact number between 1 and %{total}" % { :total => total })
286 fact = @factoids[idx-1]
289 who = params[:who].to_s.sub(/^me$/, m.source.fullform)
294 dstr = params[:when].to_s
296 fact[:when] = Time.parse(dstr, "")
299 raise ArgumentError, _("not a date '%{dstr}'" % { :dstr => dstr })
303 fact[:where] = params[:where].to_s
307 m.reply _("couldn't change learn data for fact %{fact}: %{err}" % {
316 def import(m, params)
317 fname = params[:filename].to_s
318 oldlen = @factoids.length
322 m.reply _("failed to import facts from %{fname}: %{err}" % {
327 m.reply _("%{len} facts loaded from %{fname}" % {
329 :len => @factoids.length - oldlen
336 plugin = FactoidsPlugin.new
338 plugin.default_auth('edit', false)
339 plugin.default_auth('import', false)
341 plugin.map 'learn that *stuff'
342 plugin.map 'forget that *stuff', :auth_path => 'edit'
343 plugin.map 'forget fact :index', :requirements => { :index => /^#?\d+$/ }, :auth_path => 'edit'
344 plugin.map 'facts [about *words]'
345 plugin.map 'fact [about *words]'
346 plugin.map 'fact :index', :requirements => { :index => /^#?\d+$/ }
348 plugin.map 'fact :index :learn from *who', :action => :edit_fact, :requirements => { :learn => /^((?:is|was)\s+)?learn(ed|t)$/, :index => /^#?\d+$/ }, :auth_path => 'edit'
349 plugin.map 'fact :index :learn on *when', :action => :edit_fact, :requirements => { :learn => /^((?:is|was)\s+)?learn(ed|t)$/, :index => /^#?\d+$/ }, :auth_path => 'edit'
350 plugin.map 'fact :index :learn in *where', :action => :edit_fact, :requirements => { :learn => /^((?:is|was)\s+)?learn(ed|t)$/, :index => /^#?\d+$/ }, :auth_path => 'edit'
352 plugin.map 'facts import [from] *filename', :action => :import, :auth_path => 'import'