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