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}" % fact.to_hash)
35 metadata << _("on %{when}" % fact.to_hash)
38 metadata << _("in %{where}" % fact.to_hash)
40 unless metadata.empty?
41 meta << _(" [learnt %{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)
174 factoid = params[:stuff].to_s
175 if @factoids.delete(factoid)
179 m.reply _("I didn't know that %{factoid}" % { :factoid => factoid })
183 def long_fact(fact,index=nil,total=@factoids.length)
184 idx = index || @factoids.index(fact)+1
185 _("fact #%{idx} of %{total}: %{fact}" % {
188 :fact => fact.to_s(:meta => true)
193 total = @factoids.length
194 if params[:words].empty?
195 m.reply _("I know %{total} facts" % { :total => total })
197 rx = Regexp.new(params[:words].to_s, true)
198 known = @factoids.grep(rx)
201 reply << _("I know nothing about %{words}" % params)
207 m.reply _("%{len} out of %{total} facts refer to %{words}, I'll only show %{max}" % {
210 :words => params[:words].to_s,
213 while known.length > max_facts
218 reply << long_fact(f)
221 m.reply reply.join(" -- ")
228 total = @factoids.length
230 idx = params[:index].scan(/\d+/).first.to_i
231 if idx <= 0 or idx > total
232 m.reply _("please select a fact number between 1 and %{total}" % { :total => total })
235 fact = @factoids[idx-1]
238 if params[:words].empty?
240 m.reply _("I know nothing")
245 rx = Regexp.new(params[:words].to_s, true)
246 known = @factoids.grep(rx)
248 m.reply _("I know nothing about %{words}" % params)
252 fact = known.pick_one
253 idx = @factoids.index(fact)+1
255 m.reply long_fact(fact, idx, total)
258 def edit_fact(m, params)
261 total = @factoids.length
262 idx = params[:index].scan(/\d+/).first.to_i
263 if idx <= 0 or idx > total
264 m.reply _("please select a fact number between 1 and %{total}" % { :total => total })
267 fact = @factoids[idx-1]
270 who = params[:who].to_s.sub(/^me$/, m.source.fullform)
275 dstr = params[:when].to_s
277 fact[:when] = Time.parse(dstr, "")
280 raise ArgumentError, _("not a date '%{dstr}'" % { :dstr => dstr })
284 fact[:where] = params[:where].to_s
288 m.reply _("couldn't change learn data for fact %{fact}: %{err}" % {
297 def import(m, params)
298 fname = params[:filename].to_s
299 oldlen = @factoids.length
303 m.reply _("failed to import facts from %{fname}: %{err}" % {
308 m.reply _("%{len} facts loaded from %{fname}" % {
310 :len => @factoids.length - oldlen
317 plugin = FactoidsPlugin.new
319 plugin.default_auth('edit', false)
320 plugin.default_auth('import', false)
322 plugin.map 'learn that *stuff'
323 plugin.map 'forget that *stuff', :auth_path => 'edit'
324 plugin.map 'facts [about *words]'
325 plugin.map 'fact [about *words]'
326 plugin.map 'fact :index', :requirements => { :index => /^#?\d+$/ }
328 plugin.map 'fact :index :learn from *who', :action => :edit_fact, :requirements => { :learn => /^((?:is|was)\s+)?learn(ed|t)$/, :index => /^#?\d+$/ }, :auth_path => 'edit'
329 plugin.map 'fact :index :learn on *when', :action => :edit_fact, :requirements => { :learn => /^((?:is|was)\s+)?learn(ed|t)$/, :index => /^#?\d+$/ }, :auth_path => 'edit'
330 plugin.map 'fact :index :learn in *where', :action => :edit_fact, :requirements => { :learn => /^((?:is|was)\s+)?learn(ed|t)$/, :index => /^#?\d+$/ }, :auth_path => 'edit'
332 plugin.map 'facts import [from] *filename', :action => :import, :auth_path => 'import'