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))
 
  42         arr[key] = getvalue(s)
 
  44       s.getbyte # skip the 9 'end of object' value
 
  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
 
  59       when 10 then getarray(s)                  # array
 
  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 Numeric, GeoRecord::Coord
 
  90         0.chr + encodedouble(n)
 
  94         0.chr + encodedouble(1)
 
  96         0.chr + encodedouble(0)
 
  98         raise "Unexpected Ruby type for AMF conversion: #{n.class.name}"
 
 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 * 4 + 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.join("config/potlatch/presets.txt")) do |file|
 
 182         file.each_line do |line|
 
 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)
 
 194             kv.split(",").each do |a|
 
 195               presets[pre][Regexp.last_match(1)] = Regexp.last_match(2) if a =~ /^(.+)=(.*)$/
 
 201       # Read colours/styling
 
 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]+)/
 
 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) != "-"
 
 216       # Read relations colours/styling
 
 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]+)/
 
 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) != "-"
 
 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")
 
 238           icon_tags[icon] = Hash[*tags.scan(/([^;=]+)=([^;=]+)/).flatten]
 
 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+(.+)$}
 
 249           tag = Regexp.last_match(1)
 
 250           type = Regexp.last_match(2)
 
 251           values = Regexp.last_match(3)
 
 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]