diff options
author | Dmitry Kim <dmitry point kim at gmail point com> | 2007-09-11 17:37:26 +0000 |
---|---|---|
committer | Dmitry Kim <dmitry point kim at gmail point com> | 2007-09-11 17:37:26 +0000 |
commit | a6738b25dbe4f8eee9223a60622abac5ac6108e5 (patch) | |
tree | 9e0129631b5455c4e9d0f41a4832794f9c595718 /data/rbot | |
parent | 38db12d7482f62275ecf9e67819ae148b3514f9a (diff) |
+ (plugins) :thread option for plugin.map makes an action automatically threaded
* (plugins/*) converted to the new threading model
Diffstat (limited to 'data/rbot')
-rw-r--r-- | data/rbot/plugins/forecast.rb | 61 | ||||
-rw-r--r-- | data/rbot/plugins/grouphug.rb | 30 | ||||
-rw-r--r-- | data/rbot/plugins/hl2.rb | 14 | ||||
-rw-r--r-- | data/rbot/plugins/lastfm.rb | 140 | ||||
-rw-r--r-- | data/rbot/plugins/nslookup.rb | 34 | ||||
-rw-r--r-- | data/rbot/plugins/wow.rb | 11 |
6 files changed, 138 insertions, 152 deletions
diff --git a/data/rbot/plugins/forecast.rb b/data/rbot/plugins/forecast.rb index 80790728..1095886f 100644 --- a/data/rbot/plugins/forecast.rb +++ b/data/rbot/plugins/forecast.rb @@ -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 diff --git a/data/rbot/plugins/grouphug.rb b/data/rbot/plugins/grouphug.rb index 32a6e55e..bf246b4c 100644 --- a/data/rbot/plugins/grouphug.rb +++ b/data/rbot/plugins/grouphug.rb @@ -20,19 +20,17 @@ class GrouphugPlugin < Plugin opts.delete(:cache) end - Thread.start do - begin - data = @bot.httputil.get("http://grouphug.us/#{path}", opts) - - reg = Regexp.new('(<td class="conf-text")(.*?)(<p>)(.*?)(</p>)', - Regexp::MULTILINE) - confession = reg.match( data )[4].ircify_html - confession = "no confession ##{params[:num]} found" if confession.empty? and params[:num] - - m.reply confession - rescue - m.reply "failed to connect to grouphug.us" - end + begin + data = @bot.httputil.get("http://grouphug.us/#{path}", opts) + + reg = Regexp.new('(<td class="conf-text")(.*?)(<p>)(.*?)(</p>)', + Regexp::MULTILINE) + confession = reg.match( data )[4].ircify_html + confession = "no confession ##{params[:num]} found" if confession.empty? and params[:num] + + m.reply confession + rescue + m.reply "failed to connect to grouphug.us" end end end @@ -40,6 +38,8 @@ end plugin = GrouphugPlugin.new -plugin.map "grouphug [:num]", :action => :confess, :requirements => { :num => /\d+/ } -plugin.map "confess [:num]", :action => :confess, :requirements => { :num => /\d+/ } +plugin.map "grouphug [:num]", + :thread => true, :action => :confess, :requirements => { :num => /\d+/ } +plugin.map "confess [:num]", + :thread => true, :action => :confess, :requirements => { :num => /\d+/ } diff --git a/data/rbot/plugins/hl2.rb b/data/rbot/plugins/hl2.rb index 152afdb9..d6444143 100644 --- a/data/rbot/plugins/hl2.rb +++ b/data/rbot/plugins/hl2.rb @@ -44,18 +44,16 @@ class HL2Plugin < Plugin def hl2(m, params)
addr, port = params[:conn_str].split(':')
- Thread.start do
- info = a2s_info(addr, port)
- if info != nil
- m.reply "#{info[3]} is online with #{info[8]}/#{info[9]} players."
- else
- m.reply "Couldn't connect to #{params[:conn_str]}"
- end
+ info = a2s_info(addr, port)
+ if info != nil
+ m.reply "#{info[3]} is online with #{info[8]}/#{info[9]} players."
+ else
+ m.reply "Couldn't connect to #{params[:conn_str]}"
end
end
end
plugin = HL2Plugin.new
-plugin.map 'hl2 :conn_str'
+plugin.map 'hl2 :conn_str', :thread => true
diff --git a/data/rbot/plugins/lastfm.rb b/data/rbot/plugins/lastfm.rb index 4ec79510..8c562055 100644 --- a/data/rbot/plugins/lastfm.rb +++ b/data/rbot/plugins/lastfm.rb @@ -70,67 +70,65 @@ class LastFmPlugin < Plugin page = nil spec = location ? "in #{location}" : "by #{artist}" query = location ? "?findloc=#{CGI.escape(location)}" : "?s=#{CGI.escape(artist)}&findloc=" - Thread.new { - begin - page = @bot.httputil.get LASTFM + "/events/" + query - if page - events = Array.new - disp_events = Array.new - - pre_events = page.scan(LastFmEvent::REGEXP) - # debug pre_events.inspect + begin + page = @bot.httputil.get LASTFM + "/events/" + query + if page + events = Array.new + disp_events = Array.new + + pre_events = page.scan(LastFmEvent::REGEXP) + # debug pre_events.inspect + if pre_events.empty? + # We may not find any even because the page gives a list + # of locations instead. In this case, retry with the first of + # these location + if page.match(/<a href="(\/events\/\?l=[^"]+)">/) + debug "Rechecking with #{$1}" + page = @bot.httputil.get(LASTFM+$1) + pre_events = page.scan(LastFmEvent::REGEXP) if page + end if pre_events.empty? - # We may not find any even because the page gives a list - # of locations instead. In this case, retry with the first of - # these location - if page.match(/<a href="(\/events\/\?l=[^"]+)">/) - debug "Rechecking with #{$1}" - page = @bot.httputil.get(LASTFM+$1) - pre_events = page.scan(LastFmEvent::REGEXP) if page - end - if pre_events.empty? - m.reply "No events found #{spec}, sorry" - Thread.exit - end + m.reply "No events found #{spec}, sorry" + return end - pre_events.each { |day, month, year, url_who, who, url_where, where, how_many| - date = Time.utc(year.to_i, month.to_i, day.to_i) - url = LASTFM + url_who - if who.match(/<strong>(.*?)<\/strong>(.+)?/) - artist = Bold + $1.ircify_html + Bold - artist << ", " << $2.ircify_html if $2 - else - debug "who: #{who.inspect}" - artist = who.ircify_html - end - if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/) - loc = Bold + $1.ircify_html + Bold - loc << ", " << $2.ircify_html if $2 - else - debug where.inspect - loc = where.ircify_html - end - attendance = how_many.ircify_html - events << LastFmEvent.new(url, date, artist, loc, attendance) - } - # debug events.inspect - - events[0...num].each { |event| - disp_events << event.to_s - } - m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/ - else - m.reply "No events found #{spec}" - Thread.exit end - rescue Exception => e - m.reply "I had problems looking for events #{spec}" - error e.inspect - debug e.backtrace.join("\n") - debug page[0...10*1024] if page - Thread.exit + pre_events.each { |day, month, year, url_who, who, url_where, where, how_many| + date = Time.utc(year.to_i, month.to_i, day.to_i) + url = LASTFM + url_who + if who.match(/<strong>(.*?)<\/strong>(.+)?/) + artist = Bold + $1.ircify_html + Bold + artist << ", " << $2.ircify_html if $2 + else + debug "who: #{who.inspect}" + artist = who.ircify_html + end + if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/) + loc = Bold + $1.ircify_html + Bold + loc << ", " << $2.ircify_html if $2 + else + debug where.inspect + loc = where.ircify_html + end + attendance = how_many.ircify_html + events << LastFmEvent.new(url, date, artist, loc, attendance) + } + # debug events.inspect + + events[0...num].each { |event| + disp_events << event.to_s + } + m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/ + else + m.reply "No events found #{spec}" + return end - } + rescue Exception => e + m.reply "I had problems looking for events #{spec}" + error e.inspect + debug e.backtrace.join("\n") + debug page[0...10*1024] if page + return + end end def find_artist(m, params) @@ -178,25 +176,23 @@ class LastFmPlugin < Plugin action = params[:action].intern action = :neighbours if action == :neighbors user = params[:user] - Thread.new { - begin - data = @bot.httputil.get("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt") - m.reply "#{action} for #{user}:" - m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ") - rescue - m.reply "could not find #{action} for #{user} (is #{user} a user?)" - end - } + begin + data = @bot.httputil.get("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt") + m.reply "#{action} for #{user}:" + m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ") + rescue + m.reply "could not find #{action} for #{user} (is #{user} a user?)" + end end end plugin = LastFmPlugin.new -plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ } -plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ } -plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ } -plugin.map 'lastfm artist *who', :action => :find_artist -plugin.map 'lastfm group *who', :action => :find_artist +plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true +plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true +plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true +plugin.map 'lastfm artist *who', :action => :find_artist, :thread => true +plugin.map 'lastfm group *who', :action => :find_artist, :thread => true plugin.map 'lastfm track *dunno', :action => :find_track plugin.map 'lastfm song *dunno', :action => :find_track plugin.map 'lastfm album *dunno', :action => :find_album -plugin.map 'lastfm :action *user' +plugin.map 'lastfm :action *user', :thread => true diff --git a/data/rbot/plugins/nslookup.rb b/data/rbot/plugins/nslookup.rb index 160fee85..0f901017 100644 --- a/data/rbot/plugins/nslookup.rb +++ b/data/rbot/plugins/nslookup.rb @@ -12,32 +12,28 @@ class DnsPlugin < Plugin end def name_to_ip(m, params) - Thread.new do - begin - a = getaddresses(params[:host]) - if a.length > 0 - m.reply m.params + ": " + a.join(", ") - else - m.reply "#{params[:host]}: not found" - end - rescue StandardError => err + begin + a = getaddresses(params[:host]) + if a.length > 0 + m.reply m.params + ": " + a.join(", ") + else m.reply "#{params[:host]}: not found" end + rescue StandardError => err + m.reply "#{params[:host]}: not found" end end def ip_to_name(m, params) - Thread.new do - begin - a = gethostname(params[:ip]) - m.reply m.params + ": " + a if a - rescue StandardError => err - m.reply "#{params[:ip]}: not found (does not reverse resolve)" - end - end + begin + a = gethostname(params[:ip]) + m.reply m.params + ": " + a if a + rescue StandardError => err + m.reply "#{params[:ip]}: not found (does not reverse resolve)" + end end end plugin = DnsPlugin.new -plugin.map 'dns :ip', :action => 'ip_to_name', +plugin.map 'dns :ip', :action => 'ip_to_name', :thread => true, :requirements => {:ip => /^\d+\.\d+\.\d+\.\d+$/} -plugin.map 'dns :host', :action => 'name_to_ip' +plugin.map 'dns :host', :action => 'name_to_ip', :thread => true diff --git a/data/rbot/plugins/wow.rb b/data/rbot/plugins/wow.rb index 3b73414c..ec4a19d7 100644 --- a/data/rbot/plugins/wow.rb +++ b/data/rbot/plugins/wow.rb @@ -111,15 +111,11 @@ class RealmPlugin < Plugin tok.capitalize }.join(' ') @registry[m.sourcenick] = realm_name - Thread.new do - m.reply Realm.get_realm_status(realm_name) - end + m.reply Realm.get_realm_status(realm_name) else if @registry.has_key?(m.sourcenick) realm_name = @registry[m.sourcenick] - Thread.new do - m.reply Realm.get_realm_status(realm_name) - end + m.reply Realm.get_realm_status(realm_name) else m.reply "I don't know which realm you want.\n#{USAGE}" end @@ -127,4 +123,5 @@ class RealmPlugin < Plugin end end plugin = RealmPlugin.new -plugin.map 'realm *realm_name', :defaults => {:realm_name => false} +plugin.map 'realm *realm_name', + :defaults => {:realm_name => false}, :thread => true |