4 ; Dolda Connect - Modular multiuser Direct Connect-style client
5 ; Copyright (C) 2007 Fredrik Tolf <fredrik@dolda2000.com>
7 ; This program is free software; you can redistribute it and/or modify
8 ; it under the terms of the GNU General Public License as published by
9 ; the Free Software Foundation; either version 2 of the License, or
10 ; (at your option) any later version.
12 ; This program is distributed in the hope that it will be useful,
13 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ; GNU General Public License for more details.
17 ; You should have received a copy of the GNU General Public License
18 ; along with this program; if not, write to the Free Software
19 ; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 (use-modules (dolcon ui) (dolcon util))
22 (use-modules (ice-9 format) (ice-9 rdelim) (ice-9 getopt-long))
26 (define hl-file (string-append (getenv "HOME") "/.hublist"))
29 (define statelist '())
34 (let ((fmt (car args)) (args (cdr args)))
36 (apply format (cons* logdest (string-append fmt "~%") args)))))
38 (define (list-delta l1 l2)
39 (let ((r1 '()) (r2 '()))
40 (for-each (lambda (o1)
43 (for-each (lambda (o2)
44 (if (equal? o1 o2) (throw 'found o2)))
46 (set! r2 (cons o1 r2)))
48 (set! r1 (cons (cons o1 ret) r1))
49 (set! l2 (delq ret l2)))))
54 (letrec ((read-lines (lambda (lines p)
55 (let ((line (read-line p)))
56 (if (eof-object? line)
59 (read-lines (let ((lexed (dc-lexsexpr line)))
60 (if (and (> (length lexed) 0)
61 (not (eq? (string-ref (car lexed) 0) #\#)))
62 (append lines (list lexed))
65 (lambda () (read-lines '() (open-input-file hl-file)))
66 (lambda (key . args) '()))))
69 (set! statelist (let ((nl '()) (ct (current-time)))
71 (if (< ct (+ (cadr o) (caddr o)))
72 (set! nl (cons o nl))))
77 (let ((mtime (stat:mtime (stat hl-file))))
78 (if (> mtime hl-mtime)
79 (let* ((delta (list-delta hublist (read-hl)))
84 (let ((el (assq o connlist)))
86 (begin (if (not (eq? (cdr el) 'pend))
87 (dc-qcmd (list "dcnct" (cdr el))))
88 (set! connlist (delq el connlist))))))
90 (set! hublist (append (map (lambda (o) (car o)) same) new))
91 (set! hl-mtime mtime)))))
92 (lambda (key . args) '()))
94 (if (and (not (assq o connlist))
95 (not (assq o statelist)))
96 (begin (logf "connecting to ~a" (cadr o))
97 (set! connlist (cons (cons o 'pend) connlist))
98 (dc-qcmd (cons* "cnct" o)
101 (let ((er (dc-extract resp)) (ir (dc-intresp resp)))
102 (if (= (cdr (assq 'code er)) 200)
103 (begin (set-cdr! (assq hub connlist) (car ir))
104 (logf "~a state syn (~a)" (cadr hub) (car ir)))
105 (begin (set! connlist (delq (assq hub connlist) connlist))
106 (set! statelist (cons (list hub (current-time) 10) statelist))
107 (logf "~a failed (~a)" (cadr hub) (cdr (assq 'code er))))))))))))
110 (define (hubmgr-main args)
111 (let ((opts (getopt-long args '((nodaemon (single-char #\d) (value #f))
112 (server (single-char #\s) (value #t))
113 (quiet (single-char #\q) (value #f))))))
114 (if (option-ref opts 'quiet #f) (set! logdest #f))
115 (set! hublist (read-hl))
116 (set! hl-mtime (stat:mtime (stat hl-file)))
117 (logf "read ~a hubs" (length hublist))
118 (dc-c&l (not (option-ref opts 'quiet #f)) (option-ref opts 'server #f) #t)
119 (dc-ecmd-assert 200 "notify" "fn:act" "on" "msg" "on")
120 (dc-ecmd-assert 200 "register" "hubmgr")
121 (dc-util-handle 'fn 'msg)
122 (dc-fnproc-reg 'state (lambda (fn)
123 (if (and (eq? (cdr (assq 'state fn)) 'dead)
124 (assq (cdr (assq 'id fn)) (map (lambda (o) (cons (cdr o) (car o))) connlist)))
125 (begin (logf "~a died" (cdr (assq 'id fn)))
126 (dc-qcmd (list "dcnct" (cdr (assq 'id fn))))))
128 (dc-fnproc-reg 'dstr (lambda (fn)
129 (let ((clf (assq (cdr (assq 'id fn)) (map (lambda (o) (cons (cdr o) (car o))) connlist))))
131 (let ((hlf (cdr clf)))
132 (logf "~a disappeared" (cadr hlf))
133 (set! connlist (delq (assq hlf connlist) connlist))
134 (set! statelist (cons (list hlf (current-time) 10) statelist)))))
136 (dc-msgproc-reg (lambda (sender msg)
137 (if (equal? (car msg) "exit")
139 (dc-loop-reg ".periodic" #f cklist)
141 (if (not (option-ref opts 'nodaemon #f))
142 (begin (logf "daemonizing...")
143 (if (= (primitive-fork) 0)
145 (primitive-exit 0))))
147 (for-each (lambda (sig) (sigaction sig (lambda (sig) (throw 'quit 0)))) (list SIGINT SIGTERM SIGHUP))
153 (for-each (lambda (o)
154 (if (not (eq? (cdr o) 'pend))
155 (dc-ecmd "dcnct" (cdr o))))
158 (lambda (sig ret) ret))
161 (setlocale LC_ALL "")
162 (hubmgr-main (command-line))