4 # :title: Time Zone Plugin for rbot
6 # Author:: Ian Monroe <ian@monroe.nu>
7 # Copyright:: (C) 2006 Ian Monroe
8 # License:: MIT license
12 class TimePlugin < Plugin
14 def help(plugin, topic="")
15 "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"
20 # this plugin only wants to store strings
32 if zone.length == 2 then #country code
34 zone = 'GB' if zone == 'UK' #country doesn't know its own name
36 nationZones = TZInfo::Country.get(zone).zone_identifiers
37 if nationZones.size == 1 then
40 m.reply "#{zone} has the cities of #{nationZones.join( ', ' )}."
42 rescue TZInfo::InvalidCountryCode
43 m.reply "#{zone} is not a valid country code."
46 ['/', '_'].each { |sp|
48 zone.split(sp).each{ |s|
50 s[1, s.length] = s[1, s.length].downcase if sp == '/'
55 tz = TZInfo::Timezone.get( zone )
56 "#{tz.friendly_identifier} - #{tz.now.strftime( '%a %b %d %H:%M' )} #{tz.current_period.abbreviation}"
59 def showTime(m, params)
60 zone = params[:where].join('_')
61 if params[:where].size > 0 then
63 m.reply getTime( m, zone )
64 rescue TZInfo::InvalidTimezoneIdentifier
65 if @registry.has_key?( zone ) then
66 zone = @registry[ zone ]
67 m.reply getTime( m, zone )
69 m.reply "#{zone} is an unknown time."
73 if @registry.has_key?( m.sourcenick) then
74 zone = @registry[ m.sourcenick ]
75 m.reply "#{m.sourcenick}: #{getTime( m, zone )}"
77 m.reply "#{m.sourcenick}: use time set <Continent/City> to set your timezone."
82 def setUserZone( m, params )
83 if params[:where].size > 0 then
84 s = setZone( m, m.sourcenick, params[:where].join('_') )
86 m.reply "Requires Continent/City or country code"
90 def resetUserZone( m, params )
91 s = resetZone( m, m.sourcenick)
94 def setAdminZone( m, params )
95 if params[:who] and params[:where].size > 0 then
96 s = setZone( m, params[:who], params[:where].join('_') )
98 m.reply "Requires a nick and the Continent/City or country code"
102 def resetAdminZone( m, params )
104 s = resetZone( m, params[:who])
106 m.reply "Requires a nick"
110 def setZone( m, user, zone )
113 rescue TZInfo::InvalidTimezoneIdentifier
114 m.reply "#{zone} is an invalid timezone. Format is Continent/City or a two character country code."
117 @registry[ user ] = zone
118 m.reply "Ok, I'll remember that #{user} is on the #{zone} timezone"
121 def resetZone( m, user )
122 @registry.delete(user)
123 m.reply "Ok, I've forgotten #{user}'s timezone"
127 plugin = TimePlugin.new
129 plugin.default_auth('admin', false)
131 plugin.map 'time set [time][zone] [to] *where', :action=> 'setUserZone', :defaults => {:where => false}
132 plugin.map 'time reset [time][zone]', :action=> 'resetUserZone'
133 plugin.map 'time admin set [time][zone] [for] :who [to] *where', :action=> 'setAdminZone', :defaults => {:who => false, :where => false}
134 plugin.map 'time admin reset [time][zone] [for] :who', :action=> 'resetAdminZone', :defaults => {:who => false}
135 plugin.map 'time *where', :action => 'showTime', :defaults => {:where => false}