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]
34 alias :to_hash :to_hsh
37 class FactoidList < ArrayOf
45 self.map { |f| f[:fact] }.index(fact)
65 @dir = File.join(@bot.botclass,"factoids")
66 @filename = "factoids.rbot"
67 @factoids = FactoidList.new
72 def read_factfile(name=@filename,dir=@dir)
73 fname = File.join(dir,name)
75 factoids = File.readlines(fname)
76 return if factoids.empty?
77 firstline = factoids.shift
78 pattern = firstline.chomp.split(" | ")
79 if pattern.length == 1 and pattern.first != "fact"
80 factoids.unshift(firstline)
82 @factoids << Factoid.new( :fact => f.chomp )
85 pattern.map! { |p| p.intern }
86 raise ArgumentError, "fact must be the last field" unless pattern.last == :fact
88 ar = f.chomp.split(" | ", pattern.length)
89 @factoids << Factoid.new(Hash[*([pattern, ar].transpose.flatten)])
96 return unless @changed
97 Dir.mkdir(@dir) unless FileTest.directory?(@dir)
98 fname = File.join(@dir,@filename)
99 ar = ["when | who | where | fact"]
101 ar << "%s | %s | %s | %s" % [ f[:when], f[:who], f[:where], f[:fact]]
103 Utils.safe_save(fname) do |file|
109 def help(plugin, topic="")
110 _("factoids plugin: learn that <factoid>, forget that <factoids>, facts about <words>")
114 factoid = Factoid.new(
115 :fact => params[:stuff].to_s,
117 :who => m.source.fullform,
118 :where => m.channel.to_s
120 if @factoids.index(factoid)
121 m.reply _("I already know that %{factoid}" % { :factoid => factoid })
129 def forget(m, params)
130 factoid = params[:stuff].to_s
131 if @factoids.delete(factoid)
135 m.reply _("I didn't know that %{factoid}" % { :factoid => factoid })
140 if params[:words].empty?
141 m.reply _("I know %{count} facts" % { :count => @factoids.length })
143 rx = Regexp.new(params[:words].to_s, true)
144 known = @factoids.grep(rx)
146 m.reply _("I know nothing about %{words}" % params)
148 m.reply known.join(" | "), :split_at => /\s+\|\s+/
156 total = @factoids.length
158 idx = params[:index].scan(/\d+/).first.to_i
159 if idx <= 0 or idx > total
160 m.reply _("please select a fact number between 1 and %{total}" % { :total => total })
163 fact = @factoids[idx-1]
166 if params[:words].empty?
168 m.reply _("I know nothing")
173 rx = Regexp.new(params[:words].to_s, true)
174 known = @factoids.grep(rx)
176 m.reply _("I know nothing about %{words}" % params)
180 fact = known.pick_one
181 idx = @factoids.index(fact)+1
186 metadata << _("from %{who}" % fact.to_hash)
189 metadata << _("on %{when}" % fact.to_hash)
192 metadata << _("in %{where}" % fact.to_hash)
194 unless metadata.empty?
195 meta = _(" [learnt %{data}]" % {:data => metadata.join(" ")})
197 m.reply _("fact #%{idx} of %{total}: %{fact}%{meta}" % {
207 plugin = FactoidsPlugin.new
209 plugin.default_auth('edit', false)
211 plugin.map 'learn that *stuff'
212 plugin.map 'forget that *stuff', :auth_path => 'edit'
213 plugin.map 'facts [about *words]'
214 plugin.map 'fact [about *words]'
215 plugin.map 'fact :index', :requirements => { :index => /#?\d+/ }