]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/plugins/weather.rb
move rbot into lib - still rearranging for packaging/installation
[user/henk/code/ruby/rbot.git] / lib / rbot / plugins / weather.rb
1 class WeatherPlugin < Plugin
2   
3   def help(plugin, topic="")
4     "weather <ICAO> => display the current weather at the location specified by the ICAO code [Lookup your ICAO code at http://www.nws.noaa.gov/oso/siteloc.shtml] - this will also store the ICAO against your nick, so you can later just say \"weather\", weather => display the current weather at the location you last asked for"
5   end
6   
7   def initialize
8     super
9     # this plugin only wants to store strings
10     class << @registry
11       def store(val)
12         val
13       end
14       def restore(val)
15         val
16       end
17     end
18     @metar_cache = Hash.new
19   end
20   
21   def describe(m, where)
22     if @metar_cache.has_key?(where) &&
23        Time.now - @metar_cache[where].date < 3600
24       met = @metar_cache[where]
25     else
26       met = Utils.get_metar(where)
27     end
28     
29     if met
30       m.reply met.pretty_print
31       @metar_cache[where] = met
32     else
33       m.reply "couldn't find weather data for #{where}"
34     end
35   end
36   
37   def privmsg(m)
38     case m.params
39     when nil
40       if @registry.has_key?(m.sourcenick)
41         where = @registry[m.sourcenick]
42         describe(m,where)
43       else
44         m.reply "I don't know where #{m.sourcenick} is!"
45       end
46     when (/^(\S{4})$/)
47       where = $1
48       @registry[m.sourcenick] = where
49       describe(m,where)
50     end
51   end
52   
53 end
54 plugin = WeatherPlugin.new
55 plugin.register("weather")