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. time [admin] reset [nick] to let the bot forget about the tzinfo about someone"
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 resetUserZone( m, params )
85 s = resetZone( m, m.sourcenick)
88 def setAdminZone( m, params )
89 if params[:who] and params[:where].size > 0 then
90 s = setZone( m, params[:who], params[:where].join('_') )
92 m.reply "Requires a nick and the Continent/City or country code"
96 def resetAdminZone( m, params )
98 s = resetZone( m, params[:who])
100 m.reply "Requires a nick"
104 def setZone( m, user, zone )
107 rescue TZInfo::InvalidTimezoneIdentifier
108 m.reply "#{zone} is an invalid timezone. Format is Continent/City or a two character country code."
111 @registry[ user ] = zone
112 m.reply "Ok, I'll remember that #{user} is on the #{zone} timezone"
115 def resetZone( m, user )
116 @registry.delete(user)
117 m.reply "Ok, I've forgotten #{user}'s timezone"
121 plugin = TimePlugin.new
123 plugin.default_auth('admin', false)
125 plugin.map 'time set [time][zone] [to] *where', :action=> 'setUserZone', :defaults => {:where => false}
126 plugin.map 'time reset [time][zone]', :action=> 'resetUserZone'
127 plugin.map 'time admin set [time][zone] [for] :who [to] *where', :action=> 'setAdminZone', :defaults => {:who => false, :where => false}
128 plugin.map 'time admin reset [time][zone] [for] :who', :action=> 'resetAdminZone', :defaults => {:who => false}
129 plugin.map 'time *where', :action => 'showTime', :defaults => {:where => false}