summaryrefslogtreecommitdiff
path: root/rbot/plugins/tube.rb
blob: 0244df64f5c5a7b17c8934d133486718d240b3ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#Tube Status Enquiry plugin for rbot
#Plugin by Colm Linehan

require 'rexml/document'
require 'uri/common'

class TubePlugin < Plugin
  include REXML
  def help(plugin, topic="")
  "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"
  end
  def privmsg(m)
  if m.params && m.params =~ /^stations$/
    check_stations m
  elsif m.params && m.params =~ /^(.*)$/
    line = $1.downcase.capitalize
    check_tube m, line
  end
  end
  
  def check_tube(m, line)
  begin
    tube_page = Utils.http_get("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml")
  rescue URI::InvalidURIError, URI::BadURIError => e
    m.reply "Cannot contact Tube Service Status page"
    return
  end
  unless tube_page
    m.reply "Cannot contact Tube Service Status page"
    return
  end
  tube_page.each_line {|l|
      if (l =~ /class="#{line}"/i)
        tube_page.each_line { |l2|
        if (l2 =~ /^<tr valign=top> <td>\s*(.*#{line}.*)<\/td><\/tr>/i)
          problem_message = Array.new
          problem_message = $1.split(/<[^>]+>|&nbsp;/i)
          m.reply problem_message.join(" ")
          return
        end
        }
        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."
        return
      end
      }
  m.reply "No Problems on the #{line} line."
  end

  def check_stations(m)
    begin
      tube_page = Utils.http_get("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml")
    rescue URI::InvalidURIError, URI::BadURIError => e
      m.reply "Cannot contact Tube Service Status page"
      return
    end
    unless tube_page
      m.reply "Cannot contact Tube Service Status page"
      return
    end
    stations_array = Array.new
    tube_page.each_line {|l|
        if (l =~ /<tr valign=top> <td valign="middle" class="Station"><b>(.*)<\/b><\/td><\/tr>\s*/i)
          stations_array.push $1
        end
        }
    if stations_array.empty? 
      m.reply "There are no station-specific announcements"
      return
    else
      m.reply stations_array.join(", ")
      return
    end
  end
end
plugin = TubePlugin.new
plugin.register("tube")