3 # The Potlatch module provides helper functions for potlatch and its communication with the server
5 # The AMF class is a set of helper functions for encoding and decoding AMF.
7 # Return two-byte integer
9 s.getbyte * 256 + s.getbyte
12 # Return four-byte long
14 ((s.getbyte * 256 + s.getbyte) * 256 + s.getbyte) * 256 + s.getbyte
17 # Return string with two-byte length
19 len = s.getbyte * 256 + s.getbyte
21 str.force_encoding("UTF-8") if str.respond_to?("force_encoding")
25 # Return eight-byte double-precision float
27 a = s.read(8).unpack("G") # G big-endian, E little-endian
31 # Return numeric array
33 Array.new(getlong(s)) { getvalue(s) }
39 while (key = getstring(s))
41 arr[key] = getvalue(s)
43 s.getbyte # skip the 9 'end of object' value
50 when 0 then return getdouble(s) # number
51 when 1 then return s.getbyte # boolean
52 when 2 then return getstring(s) # string
53 when 3 then return getobject(s) # object/hash
54 when 5 then return nil # null
55 when 6 then return nil # undefined
56 when 8 then s.read(4) # mixedArray
57 return getobject(s) # |
58 when 10 then return getarray(s) # array
59 else return nil # error
63 # Envelope data into AMF writeable form
64 def self.putdata(index, n)
65 d = encodestring(index + "/onResult")
66 d += encodestring("null")
72 # Pack variables as AMF
73 def self.encodevalue(n)
76 a = 10.chr + encodelong(n.length)
84 a += encodestring(k.to_s) + encodevalue(v)
86 a + 0.chr + 0.chr + 9.chr
88 2.chr + encodestring(n)
89 when "Bignum", "Fixnum", "Float"
90 0.chr + encodedouble(n)
94 0.chr + encodedouble(1)
96 0.chr + encodedouble(0)
98 Rails.logger.error("Unexpected Ruby type for AMF conversion: " + n.class.to_s)
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)
109 # Encode number as eight-byte double precision float
110 def self.encodedouble(n)
114 # Encode number as four-byte long
115 def self.encodelong(n)
120 # The Dispatcher class handles decoding a series of RPC calls
121 # from the request, dispatching them, and encoding the response
123 def initialize(request, &_block)
124 # Get stream for request data
125 @request = StringIO.new(request + 0.chr)
127 # Skip version indicator and client ID
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
137 # Capture the dispatch routine
142 # Read number of message bodies
143 bodies = AMF.getint(@request)
145 # Output response header
146 a, b = bodies.divmod(256)
147 yield 0.chr + 0.chr + 0.chr + 0.chr + a.chr + b.chr
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)
156 result = @dispatch.call(name, *args)
158 yield AMF.putdata(index, result)
163 # The Potlatch class is a helper for Potlatch
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
172 Rails.logger.info(" Message: getpresets")
176 presetmenus = { "point" => [], "way" => [], "POI" => [] }
177 presetnames = { "point" => {}, "way" => {}, "POI" => {} }
180 # StringIO.open(txt) do |file|
181 File.open("#{Rails.root}/config/potlatch/presets.txt") do |file|
182 file.each_line do |line|
184 if t =~ %r{(\w+)/(\w+)}
187 presetmenus[presettype].push(presetcategory)
188 presetnames[presettype][presetcategory] = ["(no preset)"]
189 elsif t =~ /^([\w\s]+):\s?(.+)$/
192 presetnames[presettype][presetcategory].push(pre)
194 kv.split(",").each do |a|
195 presets[pre][$1] = $2 if a =~ /^(.+)=(.*)$/
201 # Read colours/styling
205 File.open("#{Rails.root}/config/potlatch/colours.txt") do |file|
206 file.each_line do |line|
207 next unless line.chomp =~ /(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/
210 colours[tag] = $2.hex if $2 != "-"
211 casing[tag] = $3.hex if $3 != "-"
212 areas[tag] = $4.hex if $4 != "-"
216 # Read relations colours/styling
220 File.open("#{Rails.root}/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]+)/
225 relcolours[tag] = $2.hex if $2 != "-"
226 relalphas[tag] = $3.to_i if $3 != "-"
227 relwidths[tag] = $4.to_i if $4 != "-"
234 File.open("#{Rails.root}/config/potlatch/icon_presets.txt") do |file|
235 file.each_line do |line|
236 (icon, tags) = line.chomp.split("\t")
238 icon_tags[icon] = Hash[*tags.scan(/([^;=]+)=([^;=]+)/).flatten]
244 autotags = { "point" => {}, "way" => {}, "POI" => {} }
245 File.open("#{Rails.root}/config/potlatch/autocomplete.txt") do |file|
246 file.each_line do |line|
247 next unless line.chomp =~ %r{^([\w:]+)/(\w+)\s+(.+)$}
252 autotags[type][tag] = if values == "-"
255 values.split(",").sort.reverse
260 [presets, presetmenus, presetnames, colours, casing, areas, autotags, relcolours, relalphas, relwidths, icon_list, {}, icon_tags]