Add message sending mechanism.
[doldaconnect.git] / lib / guile / autodl
index fe454e8..62ccbf7 100755 (executable)
        (if tag (set! args (append args (list "tag" (cdr tag))))))
       (let ((uarg (assoc 'uarg session)))
        (if uarg (set! args (append args (list "user" (cdr uarg))))))
        (if tag (set! args (append args (list "tag" (cdr tag))))))
       (let ((uarg (assoc 'uarg session)))
        (if uarg (set! args (append args (list "user" (cdr uarg))))))
+      (let ((xargs (assoc 'xargs session)))
+       (if xargs (for-each (lambda (o)
+                             (set! args (append args (list (car o) (cdr o)))))
+                          (cdr xargs))))
       (set! resp (apply dc-ecmd-assert 200 args)))
     (let ((id (car (dc-intresp resp))))
       (set! trans
       (set! resp (apply dc-ecmd-assert 200 args)))
     (let ((id (car (dc-intresp resp))))
       (set! trans
 (define (write-info-file)
   (if (assoc 'info-file session)
       (let ((op (open-output-file (cdr (assoc 'info-file session)))))
 (define (write-info-file)
   (if (assoc 'info-file session)
       (let ((op (open-output-file (cdr (assoc 'info-file session)))))
-       (write (list (cons 'numdl (length trans))
-                    (cons 'lastdl lastdl)
-                    (cons 'availsr info-numavail)
-                    (cons 'realsr info-numreal)
-                    (cons 'totalsr info-numtotal)
-                    (cons 'lastsrch lastsearch)
-                    (cons 'srcheta info-searcheta)
-                    (cons 'srchmode (cdr (assoc 'search-mode session))))
-              op)
-       (newline op)
+       (pretty-print (list (cons 'numdl (length trans))
+                           (cons 'lastdl lastdl)
+                           (cons 'availsr info-numavail)
+                           (cons 'realsr info-numreal)
+                           (cons 'totalsr info-numtotal)
+                           (cons 'lastsrch lastsearch)
+                           (cons 'srcheta info-searcheta)
+                           (cons 'srchmode (cdr (assoc 'search-mode session))))
+                     op)
        (close-port op))))
 
 (define (parseresults)
        (close-port op))))
 
 (define (parseresults)
   )
 
 (define (handlesr filename fnet peer size slots resptime hash)
   )
 
 (define (handlesr filename fnet peer size slots resptime hash)
+  (if (eq? (cdr (assoc 'search-mode session)) 'wait)
+      (begin (set-cdr! (assoc 'search-mode session) 'normal)
+            (logf "reverting to normal mode")))
   (let ((cl (or (assoc size sr)
                (let ((newp (cons size '()))) (set! sr (append sr (list newp))) newp)))
        (newsr (list
   (let ((cl (or (assoc size sr)
                (let ((newp (cons size '()))) (set! sr (append sr (list newp))) newp)))
        (newsr (list
   )
 
 (define (autodl-main args)
   )
 
 (define (autodl-main args)
-  (let ((dc-server #f) (done #f) (retval 0))
-    (let ((getopt (make-getopt (cdr args) "whs:S:e:p:t:a:I:")) (arg #f))
+  (let ((dc-server #f) (done #f) (retval 0) (filterexit ""))
+    (let ((getopt (make-getopt (cdr args) "whs:S:e:p:t:a:I:E:x:")) (arg #f))
       (do ((arg (getopt) (getopt))) ((not (and (pair? arg) (char? (car arg)))) (set! args arg))
        (cond ((eq? (car arg) #\h)
               (begin (display "usage: autodl [-s server] -S sessfile\n" (current-error-port))
       (do ((arg (getopt) (getopt))) ((not (and (pair? arg) (char? (car arg)))) (set! args arg))
        (cond ((eq? (car arg) #\h)
               (begin (display "usage: autodl [-s server] -S sessfile\n" (current-error-port))
               (let ((c (assoc 'info-file session)))
                 (if c (set-cdr! c (cdr arg))
                     (set! session (cons (cons 'info-file (cdr arg)) session)))))
               (let ((c (assoc 'info-file session)))
                 (if c (set-cdr! c (cdr arg))
                     (set! session (cons (cons 'info-file (cdr arg)) session)))))
+             ((eq? (car arg) #\E)
+              (let ((c (assoc 'estat-file session)))
+                (if c (set-cdr! c (cdr arg))
+                    (set! session (cons (cons 'estat-file (cdr arg)) session)))))
              ((eq? (car arg) #\e)
               (set! session (cons (cons 'sexpr (dc-lexsexpr (cdr arg))) session)))
              ((eq? (car arg) #\w)
               (set! session (cons '(search-mode . wait) session)))
              ((eq? (car arg) #\e)
               (set! session (cons (cons 'sexpr (dc-lexsexpr (cdr arg))) session)))
              ((eq? (car arg) #\w)
               (set! session (cons '(search-mode . wait) session)))
+             ((eq? (car arg) #\x)
+              (let* ((c (assoc 'xargs session)) (p (string-index (cdr arg) #\=))
+                     (recons (cons (substring (cdr arg) 0 p) (substring (cdr arg) (1+ p)))))
+                (if c (set-cdr! c (cons recons (cdr c)))
+                    (set! session (cons (cons 'xargs (list recons)) session)))))
              )
        )
       )
              )
        )
       )
     (if (not (assoc 'maxtrans session))
        (set! session (cons '(maxtrans . 1) session)))
     (if (not (assoc 'search-mode session))
     (if (not (assoc 'maxtrans session))
        (set! session (cons '(maxtrans . 1) session)))
     (if (not (assoc 'search-mode session))
-       (set! session (cons '(search-mode . wait) session)))
+       (set! session (cons '(search-mode . normal) session)))
     (if (not (validate-session session)) (begin (display "Invalid session!\n" (current-error-port)) (exit 1)))
     (if (not dc-server) (set! dc-server (getenv "DCSERVER")))
     (if (not dc-server) (set! dc-server "localhost"))
     (if (not (validate-session session)) (begin (display "Invalid session!\n" (current-error-port)) (exit 1)))
     (if (not dc-server) (set! dc-server (getenv "DCSERVER")))
     (if (not dc-server) (set! dc-server "localhost"))
           (lambda (key . args)
             (logf (string-append "could not connect to server: " (apply format #f (cadr args) (caddr args))))
             (exit 2)))
           (lambda (key . args)
             (logf (string-append "could not connect to server: " (apply format #f (cadr args) (caddr args))))
             (exit 2)))
-    (dc-ecmd-assert 200 "notify" "all" "on")
+    (dc-ecmd-assert 200 "notify" "trans:act" "on" "trans:prog" "on" "srch:act" "on")
     (for-each (lambda (sig) (sigaction sig (lambda (sig) (throw 'sig sig)))) (list SIGINT SIGTERM SIGHUP))
     (catch 'sig
           (lambda ()
     (for-each (lambda (sig) (sigaction sig (lambda (sig) (throw 'sig sig)))) (list SIGINT SIGTERM SIGHUP))
     (catch 'sig
           (lambda ()
                                           (let* ((ires (dc-intresp resp)) (tr (and ires (assoc (car ires) trans))))
                                             (if tr
                                                 (begin (if (eq? (cdr (assoc 'state (cdr tr))) 'done)
                                           (let* ((ires (dc-intresp resp)) (tr (and ires (assoc (car ires) trans))))
                                             (if tr
                                                 (begin (if (eq? (cdr (assoc 'state (cdr tr))) 'done)
-                                                           (begin (logf (string-append "transfer " (number->string (car ires)) " done"))
+                                                           (begin (logf (string-append "transfer " (number->string (car ires)) " done (" (cadr ires) ")"))
                                                                   (set! trans (assq-remove! trans (car ires)))
                                                                   (set! done #t)
                                                                   (set! trans (assq-remove! trans (car ires)))
                                                                   (set! done #t)
+                                                                  (set! filterexit (cadr ires))
                                                                   (throw 'sig 0))
                                                            (begin (logf (string-append "transfer " (number->string (car ires)) " disappeared"))
                                                                   (set! trans (assq-remove! trans (car ires)))))))))
                                                                   (throw 'sig 0))
                                                            (begin (logf (string-append "transfer " (number->string (car ires)) " disappeared"))
                                                                   (set! trans (assq-remove! trans (car ires)))))))))
                                                        (write-info-file)))))
                                          ((622) ; Search result
                                           (let ((ires (list->vector (dc-intresp resp))))
                                                        (write-info-file)))))
                                          ((622) ; Search result
                                           (let ((ires (list->vector (dc-intresp resp))))
-                                            (if (eq? (cdr (assoc 'search-mode session)) 'wait)
-                                                (begin (set-cdr! (assoc 'search-mode session) 'normal)
-                                                       (logf "reverting to normal mode")))
                                             (if (and ires (= (vector-ref ires 0) srchid)) (apply handlesr (map (lambda (n) (vector-ref ires n)) '(1 2 3 4 5 7 8))))))
                                          
                                          )
                                             (if (and ires (= (vector-ref ires 0) srchid)) (apply handlesr (map (lambda (n) (vector-ref ires n)) '(1 2 3 4 5 7 8))))))
                                          
                                          )
                        )))
           (lambda (key sig)
             (logf "forcing quit")))
                        )))
           (lambda (key sig)
             (logf "forcing quit")))
+    (if (assoc 'estat-file session)
+       (let ((op (open-output-file (cdr (assoc 'estat-file session)))))
+         (display filterexit op)
+         (newline op)
+         (close-port op)))
     (exit retval)
     )
   )
     (exit retval)
     )
   )