Index: lang/ruby/citrus/trunk/plugins/weather_search.rb
===================================================================
--- lang/ruby/citrus/trunk/plugins/weather_search.rb (revision 19598)
+++ lang/ruby/citrus/trunk/plugins/weather_search.rb (revision 19598)
@@ -0,0 +1,107 @@
+# -*- Coding: utf-8 -*-
+require 'net/http'
+require 'rexml/document'
+require 'htmlentities'
+
+class WeatherSearch < Citrus::Plugin
+  def initialize(*args)
+    super
+    @prefix = @config['prefix'] || 'ws '
+    @channel = ''
+    @area = ''
+  end
+  
+  def on_privmsg(prefix, channel, message)
+    case message
+    when /^#{@prefix}([^\s]+)(\s(\++))?$/i
+      @channel = channel
+      @area = $1
+      notice(@channel, weather_search(@area, $3))
+    end
+  end
+  
+  def weather_search(area, after_days)
+    return help() if area == 'help'
+    list = list()
+    
+    if area == 'list'
+      notice(@channel, 'Area list from weather search')
+      str = ""
+      group = []
+      list['city'].each_with_index do |r,i|
+        if i % 20 != 0 || i == 0
+          group << r
+        else
+          notice(@channel, group.join(' /  '))
+          group = [r]
+        end
+      end
+      notice(@channel, group.join(' / ')) if group.size > 0
+      return "ended."
+    end
+    
+    return ws_get(list,@area,after_days)
+  end
+  
+  def help()
+    'usage: ws (list | help | AREANAME) [ "+" 1-6 nums ]. Powered by livedoor.'
+  end
+  
+  def list()
+    data = { 'city' => [], 'source' => [] }
+    Net::HTTP.start('weather.livedoor.com', 80) do |http|
+      begin
+        get = Net::HTTP::Get.new('/forecast/rss/forecastmap.xml')
+        res = http.request(get)
+        log res.code.inspect
+
+        if str = code_message(!res.code.to_i)
+          return str
+        end
+        
+        xml = REXML::Document.new(res.body)
+        
+        REXML::XPath.each(xml,'//city/@title') { |t| data['city'] <<  t.to_s }
+        REXML::XPath.each(xml,'//city/@source') { |t| data['source'] <<  t.to_s }
+      end
+    end
+    data
+  end
+  
+  def ws_get(data,area,after_days)
+    return 'Oops! retry typing!!' unless data["city"].include? area
+    /http:\/\/weather.livedoor.com(.+)/ =~ data["source"][data["city"].index(area)]
+    url = $1
+    
+    Net::HTTP.start('weather.livedoor.com', 80) do |http|
+      begin
+        get = Net::HTTP::Get.new(url)
+        res = http.request(get)
+        log res.code.inspect
+        
+        if str = code_message(!res.code.to_i)
+          return str
+        end
+        
+        xml = REXML::Document.new(res.body)
+        title = []
+        REXML::XPath.each(xml, '//item/title') { |t| title << t.text }
+        after_num = after_days.nil? ? 0 : after_days.split('').size
+        return title[1+after_num] ? title[1+after_num] : "Nothing data!!"
+      end
+    end
+    return "This plugin is testing now. Just a moment, please..."
+  end
+    
+  def code_message(str)
+    case str
+    when 400
+      return "http request error."
+    when 401
+      return "oops,Unauthorized."
+    when 404
+      return "this area is not found."
+    end
+  end
+end
+
