time: be consistent with <Continent>/<City> notation
[rbot] / data / rbot / plugins / time.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Time Zone Plugin for rbot
5 #
6 # Author:: Ian Monroe <ian@monroe.nu>
7 # Author:: Raine Virta <raine.virta@gmail.com>
8 # Copyright:: (C) 2006 Ian Monroe
9 # Copyright:: (C) 2010 Raine Virta
10 # License:: MIT license
11
12 require 'tzinfo'
13
14 class TimePlugin < Plugin
15   def help(plugin, topic="")
16     case topic
17     when "set"
18       _("usage: time set <Continent>/<City> -- setting your location allows the bot to calibrate time replies into your time zone, and other people to figure out what time it is for you")
19     else
20       _("usage: time <timestamp|time zone|nick> -- %{b}timestamp%{b}: get info about a specific time, relative to your own time zone | %{b}time zone%{b}: get local time of a certain location, <time zone> can be '<Continent>/<City>' or a two character country code | %{b}nick%{b}: get local time of another person, given they have set their location | see `%{prefix}help time set` on how to set your location") % {
21         :b => Bold,
22         :prefix => @bot.config['core.address_prefix'].first
23       }
24     end
25   end
26
27   def initialize
28     super
29     # this plugin only wants to store strings
30     class << @registry
31       def store(val)
32         val
33       end
34       def restore(val)
35         val
36       end
37     end
38   end
39
40   def getTime(m, zone )
41     if zone.length == 2 then #country code
42       zone.upcase!
43       zone = 'GB' if zone == 'UK' #country doesn't know its own name
44       begin
45         nationZones = TZInfo::Country.get(zone).zone_identifiers
46         if nationZones.size == 1 then
47           zone = nationZones[0]
48         else
49           m.reply "#{zone} has the cities of #{nationZones.join( ', ' )}."
50         end
51       rescue TZInfo::InvalidCountryCode
52         m.reply "#{zone} is not a valid country code."
53       end
54     end
55     ['/', '_'].each { |sp|
56         arr = Array.new
57         zone.split(sp).each{ |s|
58             s[0] = s[0,1].upcase
59             s[1, s.length] = s[1, s.length].downcase if sp == '/'
60             arr.push(s) }
61             zone = arr.join( sp )
62         }
63
64     tz = TZInfo::Timezone.get( zone )
65     "#{tz.friendly_identifier} - #{tz.now.strftime( '%a %b %d %H:%M' )} #{tz.current_period.abbreviation}"
66   end
67
68   def showTime(m, params)
69     zone = params[:where].join('_')
70     if params[:where].size > 0 then
71       begin
72         m.reply getTime( m,  zone )
73       rescue TZInfo::InvalidTimezoneIdentifier
74         if @registry.has_key?( zone ) then
75           zone =  @registry[ zone ]
76           m.reply getTime( m,  zone )
77         else
78           parse(m, params)
79         end
80       end
81     else
82       if @registry.has_key?( m.sourcenick) then
83         zone = @registry[ m.sourcenick ]
84         m.reply "#{m.sourcenick}: #{getTime( m,  zone )}"
85       else
86         m.reply "#{m.sourcenick}: use time set <Continent>/<City> to set your time zone."
87       end
88     end
89   end
90
91   def setUserZone( m, params )
92     if params[:where].size > 0 then
93       s = setZone( m, m.sourcenick, params[:where].join('_') )
94     else
95       m.reply "Requires <Continent>/<City> or country code"
96     end
97   end
98
99   def resetUserZone( m, params )
100     s = resetZone( m, m.sourcenick)
101   end
102
103   def setAdminZone( m, params )
104     if params[:who] and params[:where].size > 0 then
105       s = setZone( m, params[:who], params[:where].join('_') )
106     else
107       m.reply "Requires a nick and the <Continent>/<City> or country code"
108     end
109   end
110
111   def resetAdminZone( m, params )
112     if params[:who]
113       s = resetZone( m, params[:who])
114     else
115       m.reply "Requires a nick"
116     end
117   end
118
119   def setZone( m, user, zone )
120     begin
121       getTime( m,  zone )
122     rescue TZInfo::InvalidTimezoneIdentifier
123       m.reply "#{zone} is an invalid time zone. Format is <Continent>/<City> or a two character country code."
124       return
125     end
126     @registry[ user ] = zone
127     m.reply "Ok, I'll remember that #{user} is on the #{zone} time zone"
128   end
129
130   def resetZone( m, user )
131     @registry.delete(user)
132     m.reply "Ok, I've forgotten #{user}'s time zone"
133   end
134
135   def parse(m, params)
136     require 'time'
137     str = params[:where].to_s
138     now = Time.now
139
140     begin
141       time = begin
142         if zone = @registry[m.sourcenick]
143           on_timezone(zone) {
144             Time.parse str
145           }
146         else
147           Time.parse str
148         end
149       rescue ArgumentError => e
150         # Handle 28/9/1978, which is a valid date representation at least in Italy
151         if e.message == 'argument out of range'
152           str.tr!('/', '-')
153           Time.parse str
154         else
155           raise
156         end
157       end
158
159       offset = (time - now).abs
160       raise if offset < 0.1
161     rescue => e
162       m.reply _("unintelligible time")
163       return
164     end
165
166     if zone = @registry[m.sourcenick]
167       time = time.convert_zone(zone)
168     end
169
170     m.reply _("%{time} %{w} %{str}") % {
171       :time => time.strftime(_("%a, %d %b %Y %H:%M:%S %Z %z")),
172       :str  => Utils.timeago(time),
173       :w    => time >= now ? _("is") : _("was")
174     }
175   end
176
177   def on_timezone(to_zone)
178     original_zone = ENV["TZ"]
179     ENV["TZ"] = to_zone
180     return yield
181     ENV["TZ"] = original_zone
182   end
183 end
184
185 class ::Time
186   def convert_zone(to_zone)
187     original_zone = ENV["TZ"]
188     utc_time = dup.gmtime
189     ENV["TZ"] = to_zone
190     to_zone_time = utc_time.localtime
191     ENV["TZ"] = original_zone
192     return to_zone_time
193   end
194 end
195
196 plugin = TimePlugin.new
197
198 plugin.default_auth('admin', false)
199
200 plugin.map 'time set [time][zone] [to] *where', :action=> 'setUserZone', :defaults => {:where => false}
201 plugin.map 'time reset [time][zone]', :action=> 'resetUserZone'
202 plugin.map 'time admin set [time][zone] [for] :who [to] *where', :action=> 'setAdminZone', :defaults => {:who => false, :where => false}
203 plugin.map 'time admin reset [time][zone] [for] :who', :action=> 'resetAdminZone', :defaults => {:who => false}
204 plugin.map 'time *where', :action => 'showTime', :defaults => {:where => false}