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 TZInfo::Timezone.get( zone ).now
58 def showTime(m, params)
59 zone = params[:where].join('_')
60 if params[:where].size > 0 then
62 m.reply "#{zone} - #{getTime( m, zone )}"
63 rescue TZInfo::InvalidTimezoneIdentifier
64 if @registry.has_key?( zone ) then
65 zone = @registry[ zone ]
66 m.reply "#{zone} - #{getTime( m, zone )}"
68 m.reply "#{zone} is an unknown time."
72 if @registry.has_key?( m.sourcenick) then
73 zone = @registry[ m.sourcenick ]
74 m.reply "#{m.sourcenick}: #{zone} - #{getTime( m, zone )}"
76 m.reply "#{m.sourcenick}: use time set <Continent/City> to set your timezone."
81 def setUserZone( m, params )
82 if params[:where].size > 0 then
83 s = setZone( m, m.sourcenick, params[:where].join('_') )
85 m.reply "Requires Continent/City or country code"
89 def resetUserZone( m, params )
90 s = resetZone( m, m.sourcenick)
93 def setAdminZone( m, params )
94 if params[:who] and params[:where].size > 0 then
95 s = setZone( m, params[:who], params[:where].join('_') )
97 m.reply "Requires a nick and the Continent/City or country code"
101 def resetAdminZone( m, params )
103 s = resetZone( m, params[:who])
105 m.reply "Requires a nick"
109 def setZone( m, user, zone )
112 rescue TZInfo::InvalidTimezoneIdentifier
113 m.reply "#{zone} is an invalid timezone. Format is Continent/City or a two character country code."
116 @registry[ user ] = zone
117 m.reply "Ok, I'll remember that #{user} is on the #{zone} timezone"
120 def resetZone( m, user )
121 @registry.delete(user)
122 m.reply "Ok, I've forgotten #{user}'s timezone"
126 plugin = TimePlugin.new
128 plugin.default_auth('admin', false)
130 plugin.map 'time set [time][zone] [to] *where', :action=> 'setUserZone', :defaults => {:where => false}
131 plugin.map 'time reset [time][zone]', :action=> 'resetUserZone'
132 plugin.map 'time admin set [time][zone] [for] :who [to] *where', :action=> 'setAdminZone', :defaults => {:who => false, :where => false}
133 plugin.map 'time admin reset [time][zone] [for] :who', :action=> 'resetAdminZone', :defaults => {:who => false}
134 plugin.map 'time *where', :action => 'showTime', :defaults => {:where => false}