#!/usr/bin/ruby -w require "cgi" require "cgi/session" require "updategooglecalendar" require "tempfile" require "English" # turn this on for debugging RemoveDatebooks = false cgi = CGI.new sess = CGI::Session.new(cgi, "newsession" => true, "prefix" => "googlecalendar", "no_cookies" => true) sess['status'] = 'Parsing palm datebook...' sess.update cgi.out("text/html") { "

#{sess.session_id}

" } $DEFAULT_OUTPUT.flush sessionID = sess.session_id sess.close pid = fork do newCGI = cgi.clone newSess = CGI::Session.new(newCGI, "session_id" => sessionID, "newsession" => false, "prefix" => "googlecalendar", "no_cookies" => true) params = newCGI.params $stdout.close begin # Security check - test PDB file size. If > 10M, exit with error. passedPDBFile = params['pdbFile'].first if (passedPDBFile.length > 10000000) raise "PDB file is too big" elsif (passedPDBFile.length == 0) raise "PDB file is zero-length or nonexistant" end # Save the .pdb file and process it. tempPDBFile = Tempfile.new('datebookpdb') tempPDBFileName = tempPDBFile.path tempPDBFile.close! pdb_file = File.new(tempPDBFileName, 'w+') pdb_file << passedPDBFile.read pdb_file.close params.delete("pdbFile") tempDatebookFile = Tempfile.new('datebookfile') datebookPath = tempDatebookFile.path tempDatebookFile.close! outputCommand = "/usr/bin/pilot-datebook -r pdb -f #{tempPDBFile.path} -w longtxt -f #{datebookPath} -q" success = system(outputCommand) if (success) then newSess['status'] = "Reading existing calendar..." else raise "couldn't read PDB file" end newSess.update if (RemoveDatebooks) File.delete(tempPDBFile.path) end # Only take the first parameter of each. params.each { |key, value| params[key] = value.to_a[0].read } sessionLogger = UpdateGoogleCalendar::SessionLogger.new(newSess) # Update the calendar. Be sure to rescue exceptions UpdateGoogleCalendar::UpdateGoogleCalendar.updateGoogleCalendar(datebookPath, params, sessionLogger) rescue Exception => exc if (exc and exc.message and exc.message != "") newSess['status'] = "ERROR - #{exc.message} (backtrace is #{exc.backtrace.to_s}0" newSess.update end ensure newSess.close if (RemoveDatebooks) File.delete(datebookPath) end end end sleep 1 Process.detach(pid)