]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/tube.rb
77ca5227e9391b2c73fdc6893a5acdb9d0df1e5e
[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 = @bot.httputil.get(URI.parse("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml"), 1, 1)
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   next_line = false
33   tube_page.each_line {|l|
34     next if l == "\r\n"
35     next if l == "\n"
36     if (next_line)
37       if (l =~ /^<tr valign=top> <td>\s*(.*)<\/td><\/tr>/i)
38         m.reply $1.split(/<[^>]+>|&nbsp;/i).join(" ")
39         return
40       else
41         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."
42         return
43       end
44     end
45     next_line = true if (l =~ /class="#{line}"/i)
46     }
47   m.reply "No Problems on the #{line} line."
48   end
49
50   def check_stations(m)
51     begin
52       tube_page = @bot.httputil.get(URI.parse("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml"))
53     rescue URI::InvalidURIError, URI::BadURIError => e
54       m.reply "Cannot contact Tube Service Status page"
55       return
56     end
57     unless tube_page
58       m.reply "Cannot contact Tube Service Status page"
59       return
60     end
61     stations_array = Array.new
62     tube_page.each_line {|l|
63         if (l =~ /<tr valign=top> <td valign="middle" class="Station"><b>(.*)<\/b><\/td><\/tr>\s*/i)
64           stations_array.push $1
65         end
66         }
67     if stations_array.empty? 
68       m.reply "There are no station-specific announcements"
69       return
70     else
71       m.reply stations_array.join(", ")
72       return
73     end
74   end
75 end
76 plugin = TubePlugin.new
77 plugin.register("tube")