/*
getstatus.p
Program to check on the status of a fax previously submitted to faxage.com.
Written in Progress 4GL, version 10.1A.  Earlier versions of Progress may
   require a different method of base-64 encoding (see Progress web site) or
   socket handling.
*/

/* replace "xxxxx" with your faxage-assigned username */
define variable username     as character  no-undo initial "xxxxx".

/* replace "xxxxx" with your faxage-assigned company credential - the one
   you use during the login process at www.faxage.com */
define variable company      as character  no-undo initial "xxxxx".

/* replace "xxxxx" with your faxage-assigned password */
define variable password     as character  no-undo initial "xxxxx".

define variable operation    as character  no-undo initial "status".

/* Replace NNNNNN with the Faxage-assigned Job ID for which you wish to check 
   the status.  Leave blank for all jobs for your company */
define variable jobid        as character  no-undo initial "NNNNNN".

/* This somewhat inconsistent tag will force the page count for your jobid(s)
   to be displayed.  Set the entire variable to "" if you don't want this */
define variable pageCountTag as character  no-undo initial
   "&pagecount=1". 


define variable hSocket   as handle     no-undo.
/* remove the "-debug" when done testing */
define variable postURL   as character  no-undo initial
   "https://api.faxage.com/httpsfax.php". 


/* create a socket and connect */
create socket hSocket.

/* if you are having problems, and want to eliminate SSL, etc., as a source
   of difficulty, try hSocket:connect("-H api.faxage.com -S 80").  This will NOT
   be a secure connection - for that you need the line of code below */
hSocket:connect("-ssl -H api.faxage.com -S 443").


/* first message exists just to verify while testing */
if hSocket:connected() then message "Socket connected."
   view-as alert-box info buttons ok.
else do:
   message "Problem connecting to socket."
      view-as alert-box info buttons OK.
   delete object hSocket.
   return.
end. /* problem: clean up and abort */

/* set the socket object to use the internal procedure (defined below) 
   named "getResponse" to read the host's response */
hSocket:set-read-response-procedure('getResponse').

/* post our request */
run postRequest (
   input postURL,
   input "username="        + username  + "&" +
         "company="         + company   + "&" +
         "password="        + password  + "&" +
         "operation="       + operation + "&" +
         "jobid="           + jobid     + 
         pageCountTag).

/* see what comes back from the host, using getResponse */
wait-for read-response of hSocket.

/* done */
hSocket:disconnect() no-error.
delete object hSocket.
quit.

/* ------------------------ internal procedures ------------------------ */

PROCEDURE getResponse:
   
   /* holds response from host */
   define variable mResponse     as memptr           no-undo.
   /* for screen display */
   define variable responseText  as character        no-undo.
   
   if hSocket:connected() = false then do:
       message 'In getResponse: socket not connected.' view-as alert-box.
       return.
   end.
      
   do while hSocket:get-bytes-available() > 0:
      set-size(mResponse) = hSocket:get-bytes-available() + 1.
      set-byte-order(mResponse) = big-endian.
      hSocket:read(mResponse,1,1,hSocket:get-bytes-available()).
      responseText = responseText + get-string(mResponse,1).
   end.

   /* for testing */
   message responseText
      view-as alert-box info buttons OK.

   /*
    *
    *
    * put code here to manipulate the response in responseText
    *
    */
                      
END. /* getResponse */

PROCEDURE PostRequest:
   /* URL that will send the data. It must be all the path after the server.
      IE: /scripts/cgiip.exe/WService=wsbroker1/myApp.htm */
   define input  parameter postUrl  as character  no-undo.
   /* Parameters to be sent in the format
      paramName=value&paramName=value&paramName=value */
   define input  parameter postData as longchar   no-undo.

   define variable postRequest as longchar   no-undo.
   define variable mRequest  as memptr     no-undo.


   /* formulate the post request:  this is sensitive down to the last space:
      don't mess with it unless you know what you're doing */
   postRequest = 
      'POST ' + postUrl + ' HTTP/1.0~r~n' +
      'Content-Type: application/x-www-form-urlencoded~r~n' +
      'Content-Length:' + string(length(postData)) + '~r~n' +
      '~r~n' + 
      postData + '~r~n'.
   
   /* do the actual post */
   set-size(mRequest)            = 0.
   set-size(mRequest)            = length(postRequest) + 1.
   set-byte-order(mRequest)      = big-endian.
   put-string(mRequest,1)        = postRequest.
   hSocket:WRITE(mRequest, 1, LENGTH(postRequest)).
END PROCEDURE.

