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