Simplified.
[riece] / lisp / rubyserv.rb
index d3a82ff..032adfc 100644 (file)
@@ -1,5 +1,6 @@
 # A simple IPC server for executing arbitrary Ruby program.
 # The protocol is based on Assuan protocol of GnuPG.
+# http://www.gnupg.org/(en)/related_software/libassuan/index.html
 
 require 'thread'
 require 'stringio'
@@ -13,7 +14,7 @@ class RubyServ
   def dispatch(line)
     case line.chomp
     when /\AD /
-      return @buf << unescape($')
+      @buf << unescape($')
     when /\A(\S+)\s*/
       c = $1
       r = $'
@@ -85,13 +86,11 @@ class RubyServ
   end
 
   def send_data(d)
+    d = escape(d)
     begin
-      r = [d.length, 998].min   # 998 = 1000 - CRLF
-      (0 ... r).each do |i|
-        r -= 2 if d[i] =~ /[%\r\n]/
-      end
-      puts("D #{escape(d[0 ... r])}\r\n")
-      d = d[r .. -1]
+      len = [d.length, 998].min   # 998 = 1000 - "D "
+      puts("D #{d[0 ... len]}\r\n")
+      d = d[len .. -1]
     end until d.empty?
   end