]> git.openstreetmap.org Git - rails.git/blob - lib/potlatch.rb
Localisation updates from http://translatewiki.net.
[rails.git] / lib / potlatch.rb
1 require 'stringio'
2
3 # The Potlatch module provides helper functions for potlatch and its communication with the server
4 module Potlatch
5
6   # The AMF class is a set of helper functions for encoding and decoding AMF.
7   class AMF
8     
9     # Return two-byte integer
10     def self.getint(s) 
11       s.getbyte*256+s.getbyte
12     end
13
14     # Return four-byte long
15     def self.getlong(s) 
16       ((s.getbyte*256+s.getbyte)*256+s.getbyte)*256+s.getbyte
17     end
18
19     # Return string with two-byte length 
20     def self.getstring(s) 
21       len=s.getbyte*256+s.getbyte
22       str=s.read(len)
23       str.force_encoding("UTF-8") if str.respond_to?("force_encoding")
24       str
25     end
26
27     # Return eight-byte double-precision float 
28     def self.getdouble(s) 
29       a=s.read(8).unpack('G')                   # G big-endian, E little-endian
30       a[0]
31     end
32
33     # Return numeric array
34     def self.getarray(s) 
35       len=getlong(s)
36       arr=[]
37       for i in (0..len-1)
38         arr[i]=getvalue(s)
39       end
40       arr
41     end
42
43     # Return object/hash 
44     def self.getobject(s) 
45       arr={}
46       while (key=getstring(s))
47         if (key=='') then break end
48         arr[key]=getvalue(s)
49       end
50       s.getbyte         # skip the 9 'end of object' value
51       arr
52     end
53
54     # Parse and get value
55     def self.getvalue(s) 
56       case s.getbyte
57       when 0;   return getdouble(s)                     # number
58       when 1;   return s.getbyte                        # boolean
59       when 2;   return getstring(s)                     # string
60       when 3;   return getobject(s)                     # object/hash
61       when 5;   return nil                              # null
62       when 6;   return nil                              # undefined
63       when 8;   s.read(4)                               # mixedArray
64                 return getobject(s)                     #  |
65       when 10;  return getarray(s)                      # array
66       else;     return nil                              # error
67       end
68     end
69
70     # Envelope data into AMF writeable form
71     def self.putdata(index,n) 
72       d =encodestring(index+"/onResult")
73       d+=encodestring("null")
74       d+=[-1].pack("N")
75       d+=encodevalue(n)
76     end
77
78     # Pack variables as AMF
79     def self.encodevalue(n) 
80       case n.class.to_s
81       when 'Array'
82         a=10.chr+encodelong(n.length)
83         n.each do |b|
84           a+=encodevalue(b)
85         end
86         a
87       when 'Hash'
88         a=3.chr
89         n.each do |k,v|
90           a+=encodestring(k.to_s)+encodevalue(v)
91         end
92         a+0.chr+0.chr+9.chr
93       when 'String'
94         2.chr+encodestring(n)
95       when 'Bignum','Fixnum','Float'
96         0.chr+encodedouble(n)
97       when 'NilClass'
98         5.chr
99           when 'TrueClass'
100         0.chr+encodedouble(1)
101           when 'FalseClass'
102         0.chr+encodedouble(0)
103       else
104         Rails.logger.error("Unexpected Ruby type for AMF conversion: "+n.class.to_s)
105       end
106     end
107
108     # Encode string with two-byte length
109     def self.encodestring(n)
110       n=n.dup.force_encoding("ASCII-8BIT") if n.respond_to?("force_encoding")
111       a,b=n.size.divmod(256)
112       a.chr+b.chr+n
113     end
114
115     # Encode number as eight-byte double precision float 
116     def self.encodedouble(n) 
117       [n].pack('G')
118     end
119
120     # Encode number as four-byte long
121     def self.encodelong(n) 
122       [n].pack('N')
123     end
124
125   end
126
127   # The Dispatcher class handles decoding a series of RPC calls
128   # from the request, dispatching them, and encoding the response
129   class Dispatcher
130     def initialize(request, &block)
131       # Get stream for request data
132       @request = StringIO.new(request + 0.chr)
133
134       # Skip version indicator and client ID
135       @request.read(2)
136
137       # Skip headers
138       AMF.getint(@request).times do     # Read number of headers and loop
139         AMF.getstring(@request)         #  | skip name
140         req.getbyte                     #  | skip boolean
141         AMF.getvalue(@request)          #  | skip value
142       end
143
144       # Capture the dispatch routine
145       @dispatch = Proc.new
146     end
147
148     def each(&block)
149       # Read number of message bodies
150       bodies = AMF.getint(@request)
151
152       # Output response header
153       a,b = bodies.divmod(256)
154       yield 0.chr + 0.chr + 0.chr + 0.chr + a.chr + b.chr
155
156       # Process the bodies
157       bodies.times do                     # Read each body
158         name = AMF.getstring(@request)    #  | get message name
159         index = AMF.getstring(@request)   #  | get index in response sequence
160         bytes = AMF.getlong(@request)     #  | get total size in bytes
161         args = AMF.getvalue(@request)     #  | get response (probably an array)
162
163         result = @dispatch.call(name, *args)
164
165         yield AMF.putdata(index, result)
166       end
167     end
168   end
169
170   # The Potlatch class is a helper for Potlatch
171   class Potlatch
172
173     # ----- getpresets
174     #             in:   none
175     #             does: reads tag preset menus, colours, and autocomplete config files
176     #         out:  [0] presets, [1] presetmenus, [2] presetnames,
177     #                           [3] colours, [4] casing, [5] areas, [6] autotags
178     #                           (all hashes)
179     def self.get_presets
180       Rails.logger.info("  Message: getpresets")
181
182       # Read preset menus
183       presets={}
184       presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]; presetmenus['POI']=[]
185       presetnames={}; presetnames['point']={}; presetnames['way']={}; presetnames['POI']={}
186       presettype=''
187       presetcategory=''
188       # StringIO.open(txt) do |file|
189       File.open("#{Rails.root}/config/potlatch/presets.txt") do |file|
190         file.each_line {|line|
191           t=line.chomp
192           if (t=~/(\w+)\/(\w+)/) then
193             presettype=$1
194             presetcategory=$2
195             presetmenus[presettype].push(presetcategory)
196             presetnames[presettype][presetcategory]=["(no preset)"]
197           elsif (t=~/^([\w\s]+):\s?(.+)$/) then
198             pre=$1; kv=$2
199             presetnames[presettype][presetcategory].push(pre)
200             presets[pre]={}
201             kv.split(',').each {|a|
202               if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end
203             }
204           end
205         }
206       end
207
208       # Read colours/styling
209       colours={}; casing={}; areas={}
210       File.open("#{Rails.root}/config/potlatch/colours.txt") do |file|
211         file.each_line {|line|
212           t=line.chomp
213           if (t=~/(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/) then
214             tag=$1
215             if ($2!='-') then colours[tag]=$2.hex end
216             if ($3!='-') then casing[tag]=$3.hex end
217             if ($4!='-') then areas[tag]=$4.hex end
218           end
219         }
220       end
221
222       # Read relations colours/styling
223       relcolours={}; relalphas={}; relwidths={}
224       File.open("#{Rails.root}/config/potlatch/relation_colours.txt") do |file|
225         file.each_line {|line|
226           t=line.chomp
227           if (t=~/(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/) then
228             tag=$1
229             if ($2!='-') then relcolours[tag]=$2.hex end
230             if ($3!='-') then relalphas[tag]=$3.to_i end
231             if ($4!='-') then relwidths[tag]=$4.to_i end
232           end
233         }
234       end
235
236       # Read POI presets
237       icon_list=[]; icon_tags={};
238       File.open("#{Rails.root}/config/potlatch/icon_presets.txt") do |file|
239         file.each_line {|line|
240           (icon,tags)=line.chomp.split("\t")
241           icon_list.push(icon)
242           icon_tags[icon]=Hash[*tags.scan(/([^;=]+)=([^;=]+)/).flatten]
243         }
244       end
245       icon_list.reverse!
246       
247       # Read auto-complete
248       autotags={}; autotags['point']={}; autotags['way']={}; autotags['POI']={};
249       File.open("#{Rails.root}/config/potlatch/autocomplete.txt") do |file|
250         file.each_line {|line|
251           t=line.chomp
252           if (t=~/^([\w:]+)\/(\w+)\s+(.+)$/) then
253             tag=$1; type=$2; values=$3
254             if values=='-' then autotags[type][tag]=[]
255             else autotags[type][tag]=values.split(',').sort.reverse end
256           end
257         }
258       end
259
260       [presets,presetmenus,presetnames,colours,casing,areas,autotags,relcolours,relalphas,relwidths,icon_list,{},icon_tags]
261     end
262   end
263
264 end
265