]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/time.rb
4838a469586cb496bfdf345d7e0be879a3c3ac2d
[user/henk/code/ruby/rbot.git] / 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       if str.match(/^\d+$/)
163         time = Time.at(str.to_i)
164       else
165         m.reply _("unintelligible time")
166         return
167       end
168     end
169
170     if zone = @registry[m.sourcenick]
171       time = time.convert_zone(zone)
172     end
173
174     m.reply _("%{time} %{w} %{str}") % {
175       :time => time.strftime(_("%a, %d %b %Y %H:%M:%S %Z %z")),
176       :str  => Utils.timeago(time),
177       :w    => time >= now ? _("is") : _("was")
178     }
179   end
180
181   def on_timezone(to_zone)
182     original_zone = ENV["TZ"]
183     ENV["TZ"] = to_zone
184     ret = yield
185     ENV["TZ"] = original_zone
186     return ret
187   end
188 end
189
190 class ::Time
191   def convert_zone(to_zone)
192     original_zone = ENV["TZ"]
193     utc_time = dup.gmtime
194     ENV["TZ"] = to_zone
195     to_zone_time = utc_time.localtime
196     ENV["TZ"] = original_zone
197     return to_zone_time
198   end
199 end
200
201 plugin = TimePlugin.new
202
203 plugin.default_auth('admin', false)
204
205 plugin.map 'time set [time][zone] [to] *where', :action=> 'setUserZone', :defaults => {:where => false}
206 plugin.map 'time reset [time][zone]', :action=> 'resetUserZone'
207 plugin.map 'time admin set [time][zone] [for] :who [to] *where', :action=> 'setAdminZone', :defaults => {:who => false, :where => false}
208 plugin.map 'time admin reset [time][zone] [for] :who', :action=> 'resetAdminZone', :defaults => {:who => false}
209 plugin.map 'time *where', :action => 'showTime', :defaults => {:where => false}