# Brett Bates dir = File.dirname(__FILE__) $LOAD_PATH.unshift(dir) Dir[File.join(dir, "*.rb")].each {|file| require File.basename(file) } require 'P4' class P4GTD attr_accessor :vars, :conf, :p4, :note_db def initialize(to_read) # Read in the configuration file into a P4 object @conf = Hash.new read_conf @p4 = P4.new connect_to_p4 # Read in STDIN to a hash @vars = Hash.new to_read.each_line do |line| if( line =~ /(\w+):\s+(.*)/ ) @vars[$1] = $2.strip end end read_db end # Connect to the server defined in the config def connect_to_p4 @p4.port = @conf['port'] @p4.user = @conf['user'] @p4.client = @conf['client'] @p4.connect @conf['root'] = @p4.fetch_client(@conf['client'])['Root'] end # Read in the config file and save to hash @conf def read_conf conf_file = File.open(File.join(File.dirname(__FILE__), "gtd.conf"), "r") conf_file.each_line do |line| key, value = line.split('=') @conf[key.strip] = value.strip end end def read_db @p4.run_sync(File.join(@conf['depot'], @vars['user'], "note_db")) File.open(File.join(@conf['root'], @vars['user'], "note_db"), "r") { |i| @note_db = Marshal.load(i) } end def dump_db @p4.run_edit(File.join(@conf['depot'], @vars['user'], "note_db")) File.open(File.join(@conf['root'], @vars['user'], "note_db"), "w") { |o| Marshal.dump(@note_db, o) } end # Run the command # Send the ultimate answer def run bool, msg = command_switch send_answer(bool, msg) end # Find the correct module to run def command_switch case @vars['Arg0'].downcase when "alarm" obj = GTDAlarm.new(arg_hash_to_list, self) comm = obj.run when "note" obj = GTDNote.new(arg_hash_to_list, self) comm = obj.run else comm = [false, "Command #{@vars['Arg0']} not recogniz(s)ed"] end return comm end # Take the @vars hash and turn the arguments at the end into a list def arg_hash_to_list arg_list = [] for x in 0..(@vars['argCount'].to_i - 1) arg_list << @vars['Arg' + x.to_s] end return arg_list end # Send the finishing message to the broker # # bool: If true send PASS else REJECT # msg: the message to accompany the action def send_answer(bool, msg = "null") if !bool puts "action: REJECT" puts "message: #{msg}" else puts "action: RESPOND" puts "message: #{msg}" end exit(0) end # For P4Exception's def send_fail send_answer(false, "Warnings: #{@p4.warnings.join(" | ")} Errors: #{@p4.errors.join(" | ")}") end # Send the outputs to file "dbg.txt" def send_debug(bool, msg) f = File.open("dbg.txt", "w") #If is Windows broker... bleh if ENV['OS'] unless bool print "action: REJECT\r\n" print "message: #{msg}\r\n" else print "action: RESPOND\r\n" print "message: #{msg}\r\n" end else unless bool puts "action: REJECT" puts "message: #{msg}" else puts "action: RESPOND" puts "message: #{msg}" end end f.close end end to_be_read = "command: gtd \n \ clientProg: 1 \n \ clientVersion: 2 \n \ clientProtocol: 3 \n \ apiLevel: 4 \n \ workspace: 5 \n \ user: 6 \n \ clientIp: 7 \n \ proxyIp: 8 \n \ cwd: 9 \n \ argCount: 3 \n \ Arg0: note \n \ Arg1: add\n \ Arg2: title\n \ Arg3: \"A really long arg\"" gtd = P4GTD.new(to_be_read) gtd.run gtd.dump_db