#!/usr/bin/ruby require 'socket' require "rexml/document" if ARGV.length == 0 || ARGV.length > 1 then puts "Usage: addMember " puts " Where == [/]*" puts puts " Example: addMember CU" puts " Example: addMember CU/CS" puts " Example: addMember CU/CS/Software" puts puts " NOTE: Command will ask for member information" exit end groupName = ARGV[0] # if groupName does not end with a "/" character, add one if !(groupName =~ /\/$/) then groupName = groupName + "/" end print "Enter User Name: " userName = $stdin.gets.chop print "Enter User Phone: " userPhone = $stdin.gets.chop print "Enter User Email: " userEmail = $stdin.gets.chop doc = REXML::Document.new doc.add_element "member" doc << REXML::XMLDecl.new name = REXML::Element.new "name" name.text = userName phone = REXML::Element.new "phone" phone.text = userPhone email = REXML::Element.new "email" email.text = userEmail doc.root.add_element name doc.root.add_element phone doc.root.add_element email length = doc.to_s.length id = userEmail.dup id.gsub!(/[@\.]/, "_") s = TCPSocket.new( 'infinite.local' , 80 ) s.puts("PUT /~kena/addressBook/data/#{groupName}/#{id}.xml HTTP/1.0") s.puts("Content-type: text/xml") s.puts("Content-length: #{length}") s.puts s.puts doc.to_s doc = REXML::Document.new s status = doc.root.elements["//status"] reason = doc.root.elements["//reason"] if !(defined?(status) && defined?(reason)) then puts "Received Invalid Response from Server" exit end if status.text == "success" then print("Succeeded: ") else print("Failed: ") end puts "#{reason.text}"