]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/tube.rb
initial import of rbot
[user/henk/code/ruby/rbot.git] / rbot / plugins / tube.rb
1 #Tube Status Enquiry plugin for rbot
2 #Plugin by Colm Linehan
3
4 require 'rexml/document'
5 require 'uri/common'
6
7 class TubePlugin < Plugin
8   include REXML
9   def help(plugin, topic="")
10   "tube [district|circle|metropolitan|central|jubilee|bakerloo|waterloo_city|hammersmith_city|victoria|eastlondon|northern|piccadilly] => display tube service status for the specified line(Docklands Light Railway is not currently supported), tube stations => list tube stations (not lines) with problems"
11   end
12   def privmsg(m)
13   if m.params && m.params =~ /^stations$/
14     check_stations m
15   elsif m.params && m.params =~ /^(.*)$/
16     line = $1.downcase.capitalize
17     check_tube m, line
18   end
19   end
20   
21   def check_tube(m, line)
22   begin
23     tube_page = Utils.http_get("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml")
24   rescue URI::InvalidURIError, URI::BadURIError => e
25     m.reply "Cannot contact Tube Service Status page"
26     return
27   end
28   unless tube_page
29     m.reply "Cannot contact Tube Service Status page"
30     return
31   end
32   tube_page.each_line {|l|
33       if (l =~ /class="#{line}"/i)
34         tube_page.each_line { |l2|
35         if (l2 =~ /^<tr valign=top> <td>\s*(.*#{line}.*)<\/td><\/tr>/i)
36           problem_message = Array.new
37           problem_message = $1.split(/<[^>]+>|&nbsp;/i)
38           m.reply problem_message.join(" ")
39           return
40         end
41         }
42         m.reply "There are problems on the #{line} line, but I didn't understand the page format. You should check out http://www.tfl.gov.uk/tfl/service_rt_tube.shtml for more details."
43         return
44       end
45       }
46   m.reply "No Problems on the #{line} line."
47   end
48
49   def check_stations(m)
50     begin
51       tube_page = Utils.http_get("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml")
52     rescue URI::InvalidURIError, URI::BadURIError => e
53       m.reply "Cannot contact Tube Service Status page"
54       return
55     end
56     unless tube_page
57       m.reply "Cannot contact Tube Service Status page"
58       return
59     end
60     stations_array = Array.new
61     tube_page.each_line {|l|
62         if (l =~ /<tr valign=top> <td valign="middle" class="Station"><b>(.*)<\/b><\/td><\/tr>\s*/i)
63           stations_array.push $1
64         end
65         }
66     if stations_array.empty? 
67       m.reply "There are no station-specific announcements"
68       return
69     else
70       m.reply stations_array.join(", ")
71       return
72     end
73   end
74 end
75 plugin = TubePlugin.new
76 plugin.register("tube")