]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/forecast.rb
plugins/keywords: add method that was missing from commit 7cac523563de6473d2f93fd2d05 ...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / forecast.rb
index 8079072853084b5ed2d43747f6fad788dd8646dd..1095886f3b46ac28036f5d922b4131485137b83f 100644 (file)
@@ -107,41 +107,40 @@ class ForecastPlugin < Plugin
     end
     
     def get_forecast(m,loc)
-      Thread.new {
-        begin
-          @cache_mutex.synchronize do
-            if @forecast_cache.has_key?(loc) and
-                Time.new - @forecast_cache[loc][:date] < 3600
-                forecast = @forecast_cache[loc][:forecast]
-                if forecast
-                  m.reply forecast
-                  Thread.exit
-                end
+      begin
+        @cache_mutex.synchronize do
+          if @forecast_cache.has_key?(loc) and
+            Time.new - @forecast_cache[loc][:date] < 3600
+            forecast = @forecast_cache[loc][:forecast]
+            if forecast
+              m.reply forecast
+              return
             end
           end
-          begin
-            l = LatLong.new
-            f = Forecast.new(*l.get_lat_long(loc))
-            forecast,forecast_date = f.forecast
-          rescue => err
-            m.reply err
-          end
-          if forecast
-            m.reply forecast
-            @cache_mutex.synchronize do
-              @forecast_cache[loc] = {
-                :forecast => forecast,
-                :date => forecast_date
-              }
-            end
-          else
-            m.reply "Couldn't find forecast for #{loc}"
+        end
+        begin
+          l = LatLong.new
+          f = Forecast.new(*l.get_lat_long(loc))
+          forecast,forecast_date = f.forecast
+        rescue => err
+          m.reply err
+        end
+        if forecast
+          m.reply forecast
+          @cache_mutex.synchronize do
+            @forecast_cache[loc] = {
+              :forecast => forecast,
+              :date => forecast_date
+            }
           end
-        rescue => e
-          m.reply "ERROR: #{e}"
+        else
+          m.reply "Couldn't find forecast for #{loc}"
         end
-      }
+      rescue => e
+        m.reply "ERROR: #{e}"
+      end
     end
 end
 plugin = ForecastPlugin.new
-plugin.map 'forecast *location', :defaults => {:location => false}
+plugin.map 'forecast *location',
+  :defaults => {:location => false}, :thread => true