summaryrefslogtreecommitdiff
path: root/efax.scm
blob: 0d1b115de7da8dc47a2f6cc737d6f6260915f866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
(module efax
  (efax-password efax-username efax-account efax-send efax-status)

(import chicken scheme)

(use srfi-1 srfi-13 data-structures
     http-client base64 sxml-serializer ssax sxpath)

(define efax-service-uri "https://secure.efaxdeveloper.com/EFax_WebFax.serv")

(define efax-account  (make-parameter #f))
(define efax-username (make-parameter #f))
(define efax-password (make-parameter #f))

(define-syntax let-auth
  (syntax-rules ()
    ((_ ?procname ?account ?username ?password ?body ...)
     (let ((?username (or (and ?password ?username) (efax-username)))
           (?password (or (and ?username ?password) (efax-password)))
           ;; Account can be provided separately via the global param,
           ;; but username and password must be provided at the same place.
           (?account  (or ?account (efax-account))))
       (unless (and ?username ?password ?account)
         (error '?procname
                (conc "You must provide an eFax account, username & password, "
                      "either via the efax-account, efax-username and "
                      "efax-password global parameters or via the account, "
                      "username and password keys for this procedure")))
       ?body ...))))

(define (read-document-or-raise-error port location)
  (let* ((result (ssax:xml->sxml port '()))
         (tid ((if-car-sxpath
                '(// TransmissionControl TransmissionID *text*)) result))
         (did ((if-car-sxpath
                '(// TransmissionControl DOCID *text*)) result))
         (code ((if-car-sxpath '(// Response StatusCode *text*)) result)))
    (if (or (not code) (string=? code "1"))
        result
        (signal (make-composite-condition
                 (make-property-condition
                  'exn
                  'location location
                  'message
                  ((if-car-sxpath '(// Response ErrorMessage *text*))
                   result))
                 (make-property-condition
                  'efax
                  'error-code code
                  'error-description
                  ((sxpath '(// Response StatusDescription)) result)
                  'error-level
                  (string->symbol
                   (string-downcase
                    ((if-car-sxpath '(// Response ErrorLevel *text*)) result)))
                  'document-id did 'transmission-id tid))))))

;; TODO: This should probably allow files to be ports so that we can
;; stream large amounts of data. That would mean we can't use SXML anymore,
;; though!  Instead, we would need some kind of chunked layer-upon-layer
;; procedure that encodes base64 inside XML inside uri-encoded POST data.
;; It's probably not worth it since it isn't likely people are going to
;; send gigabytes worth of data; it has to come out a fax machine, which
;; would translate to many pages of dead tree!
(define (efax-send recipients files
                   #!key account username password transmission-id
                   unique-id customer-id high-resolution high-priority)
  (let-auth efax-send account username password
    (call-with-input-request
     efax-service-uri
     `((id . ,account)
       (respond . "XML")
       (xml . ,(serialize-sxml
                `(*TOP*
                  (*PI* xml "version=\"1.0\"")
                  (OutboundRequest
                   (AccessControl (UserName ,username) (Password ,password))
                   (Transmission
                    (TransmissionControl
                     ;; TODO: SelfBusy, FaxHeader
                     ,@(if transmission-id `((TransmissionID ,transmission-id)) '())
                     (NoDuplicates ,(if unique-id "ENABLE" "DISABLE"))
                     ,@(if customer-id `((CustomerID ,customer-id)) '())
                     (Resolution ,(if high-resolution "FINE" "STANDARD"))
                     (Priority ,(if high-priority "HIGH" "NORMAL")))
                    (DispositionControl
                     ;; TODO: DispositionURL, DispositionLevel, DispositionMethod, DispositionEmail(s)?
                     (DispositionLevel "NONE"))
                    (Recipients
                     . ,(if (string? recipients)
                            `((Recipient (RecipientFax ,recipients)))
                            (map
                             (lambda (recipient)
                               `(if (string? recipient)
                                    `(Recipient (RecipientFax ,recipient))
                                    `(Recipient
                                      ,@(map
                                         (lambda (entry)
                                           (let ((key (car entry))
                                                 (value (cdr entry)))
                                             (case key
                                               ((name) `(RecipientName ,value))
                                               ((company) `(RecipientName ,value))
                                               ((fax) `(RecipientFax ,value))
                                               (else (error "Unknown recipient key" key)))))
                                         recipient))))
                             recipients)))
                    (Files . ,(map (lambda (file)
                                     `(File (FileContents ,(base64-encode (cdr file)))
                                            (FileType ,(->string (car file)))))
                                   files))))))))
     (lambda (port)
       (let* ((doc (read-document-or-raise-error port 'efax-send))
              (tid ((if-car-sxpath
                     '(// TransmissionControl TransmissionID *text*)) doc))
              (did ((if-car-sxpath
                     '(// TransmissionControl DOCID *text*)) doc)))
         `((doc-id . ,did) (transmission-id . ,tid)))))))

(define (efax-status #!key account username password doc-id transmission-id)
  (let-auth efax-status account username password
    (call-with-input-request
     efax-service-uri
     `((id . ,account)
       (respond . "XML")
       (xml . ,(serialize-sxml
                `(*TOP*
                  (*PI* xml "version=\"1.0\"")
                  (OutboundStatus
                   (AccessControl (UserName ,username) (Password ,password))
                   (Transmission
                    (TransmissionControl
                     ,@(if transmission-id `((TransmissionID ,transmission-id)) '())
                     ,@(if doc-id `((DOCID ,doc-id)) '()))))))))
     (lambda (port)
       (let* ((doc (read-document-or-raise-error port 'efax-status))
              (tid ((if-car-sxpath
                     '(// TransmissionControl TransmissionID *text*)) doc)))
         `((transmission-id . ,tid)
           (recipients . ,(map (lambda (r)
                                 (cons
                                  ((if-car-sxpath '(Fax *text*)) r)
                                  (fold-right
                                   (lambda (name expr result)
                                     (let ((value ((if-car-sxpath expr) r)))
                                       (if value
                                           (cons (cons name value) result)
                                           result)))
                                   '()
                                   '(name company fax status-message
                                     status-classification status-outcome
                                     last-attempt-date last-attempt-time
                                     next-attempt-date next-attempt-time
                                     pages-scheduled pages-sent
                                     baud-rate retries remote-csid)
                                   '((Name *text*) (Company *text*) (Fax *text*)
                                     (Status Message *text*)
                                     (Status Outcome *text*)
                                     (Status Classification *text*)
                                     (LastAttempt LastDate *text*)
                                     (LastAttempt LastTime *text*)
                                     (NextAttempt NextDate *text*)
                                     (NextAttempt NextTime *text*)
                                     (Pages Scheduled *text*)
                                     (Pages Sent *text*)
                                     (BaudRate *text*)
                                     (Retries *text*)
                                     (RemoteCSID *text*)))))
                           ((sxpath '(// Recipient)) doc)))))))))

)