]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/time.rb
echo must default to false
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / time.rb
1 # Time Zone Plugin for rbot
2 # (c) 2006 Ian Monroe <ian@monroe.nu>
3 # Licensed under MIT License.
4
5 require 'tzinfo'
6
7 class TimePlugin < Plugin
8
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."
11   end
12
13   def initialize
14     super
15     # this plugin only wants to store strings
16     class << @registry
17       def store(val)
18         val
19       end
20       def restore(val)
21         val
22       end
23     end
24   end
25
26   def getTime(m, zone )
27     if zone.length == 2 then #country code
28       zone.upcase!
29       zone = 'GB' if zone == 'UK' #country doesn't know its own name
30       begin
31         nationZones = TZInfo::Country.get(zone).zone_identifiers
32         if nationZones.size == 1 then
33           zone = nationZones[0]
34         else
35           m.reply "#{zone} has the cities of #{nationZones.join( ', ' )}."
36         end
37       rescue TZInfo::InvalidCountryCode
38         m.reply "#{zone} is not a valid country code."
39       end
40     end
41     ['/', '_'].each { |sp|
42         arr = Array.new
43         zone.split(sp).each{ |s| 
44             s[0] = s[0,1].upcase
45             s[1, s.length] = s[1, s.length].downcase if sp == '/'
46             arr.push(s) }
47             zone = arr.join( sp )
48         }
49     
50     TZInfo::Timezone.get( zone ).now
51   end
52
53   def showTime(m, params)
54     zone = params[:where].join('_')
55     if params[:where].size > 0 then
56       begin
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 )}"
62         else
63           m.reply "#{zone} is an unknown time."
64         end
65       end
66     else
67       if @registry.has_key?( m.sourcenick) then
68         zone = @registry[ m.sourcenick ]
69         m.reply "#{m.sourcenick}: #{zone} - #{getTime( m,  zone )}"
70       else
71         m.reply "#{m.sourcenick}: use time set <Continent/City> to set your timezone."
72       end
73     end
74   end
75
76   def setUserZone( m, params )
77     if params[:where].size > 0 then
78       s = setZone( m, m.sourcenick, params[:where].join('_') )
79     else
80       m.reply "Requires Continent/City or country code"
81     end
82   end
83
84   def setAdminZone( m, params )
85     if params[:who] and params[:where].size > 0 then
86       s = setZone( m, params[:who], params[:where].join('_') )
87     else
88       m.reply 'Requires a nick and the Continent/City or country code.'
89     end
90   end
91
92   def setZone( m, user, zone )
93     begin
94       getTime( m,  zone )
95     rescue TZInfo::InvalidTimezoneIdentifier
96       m.reply "#{zone} is an invalid timezone. Format is Continent/City or a two character country code."
97       return
98     end
99     @registry[ user ] = zone
100   end
101 end
102
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}