1 # Time Zone Plugin for rbot
2 # (c) 2006 Ian Monroe <ian@monroe.nu>
3 # Licensed under MIT License.
7 class TimePlugin < Plugin
9 def help(plugin, topic="")
10 "time <time zone> to get the local time of a certain location. <time zone> can be <Continent/City> or <two character country code>. time <nick> to see the local time of that person if their time zone is set. time admin set <nick> <time zone> to set the time zone for another user."
15 # this plugin only wants to store strings
27 if zone.length == 2 then #country code
29 zone = 'GB' if zone == 'UK' #country doesn't know its own name
31 nationZones = TZInfo::Country.get(zone).zone_identifiers
32 if nationZones.size == 1 then
35 m.reply "#{zone} has the cities of #{nationZones.join( ', ' )}."
37 rescue TZInfo::InvalidCountryCode
38 m.reply "#{zone} is not a valid country code."
41 ['/', '_'].each { |sp|
43 zone.split(sp).each{ |s|
45 s[1, s.length] = s[1, s.length].downcase if sp == '/'
50 TZInfo::Timezone.get( zone ).now
53 def showTime(m, params)
54 zone = params[:where].join('_')
55 if params[:where].size > 0 then
57 m.reply "#{zone} - #{getTime( m, zone )}"
58 rescue TZInfo::InvalidTimezoneIdentifier
59 if @registry.has_key?( zone ) then
60 zone = @registry[ zone ]
61 m.reply "#{zone} - #{getTime( m, zone )}"
63 m.reply "#{zone} is an unknown time."
67 if @registry.has_key?( m.sourcenick) then
68 zone = @registry[ m.sourcenick ]
69 m.reply "#{m.sourcenick}: #{zone} - #{getTime( m, zone )}"
71 m.reply "#{m.sourcenick}: use time set <Continent/City> to set your timezone."
76 def setUserZone( m, params )
77 if params[:where].size > 0 then
78 s = setZone( m, m.sourcenick, params[:where].join('_') )
80 m.reply "Requires Continent/City or country code"
84 def setAdminZone( m, params )
85 if params[:who] and params[:where].size > 0 then
86 s = setZone( m, params[:who], params[:where].join('_') )
88 m.reply 'Requires a nick and the Continent/City or country code.'
92 def setZone( m, user, zone )
95 rescue TZInfo::InvalidTimezoneIdentifier
96 m.reply "#{zone} is an invalid timezone. Format is Continent/City or a two character country code."
99 @registry[ user ] = zone
103 plugin = TimePlugin.new
104 plugin.map 'time set *where', :action=> 'setUserZone', :defaults => {:where => false}
105 plugin.map 'time admin set :who *where', :action=> 'setAdminZone', :defaults => {:where => false, :who=>false}, :auth=> 'timeadmin'
106 plugin.map 'time *where', :action => 'showTime', :defaults => {:where => false}