add syntactic sugar for XMPP conversations
[software/gnubba.git] / sugar.scm
1 (define-module (gnubba sugar)
2 #:use-module ((sxml simple) #:select (sxml->xml))
3 #:export (xmpp->))
4
5 ;; Simple syntax for XMPP "discussions".
6 ;; TODO: can this become an actual monad? Would it be worth the
7 ;; effort?
8 (define-syntax xmpp->
9 (lambda (x)
10 (syntax-case x (<-)
11 ;; send and forget stanza
12 ((_ sock (<- stanza) exp ...)
13 #'(begin
14 (sxml->xml stanza sock)
15 (xmpp-> sock exp ...)))
16 ;; send stanza and pass response to next expression
17 ((_ sock (res <- stanza) exp ...)
18 #'(let ((s stanza))
19 (begin
20 (register-temp-stanza-handler-for-id
21 (stanza-id s)
22 (lambda (res)
23 (xmpp-> sock exp ...)))
24 (sxml->xml s sock))))
25 ;; unwrap single expression
26 ((_ sock exp)
27 #'exp)
28 ;; simply evaluate expression and move on to match next expression
29 ((_ sock exp exp* ...)
30 #'(begin
31 exp
32 (xmpp-> sock exp* ...))))))
33
34 ;; EXAMPLE
35 ;; (xmpp-> (current-output-port)
36 ;; (res <- (iq "hello"))
37 ;; (display res)
38 ;; (display "something else")
39 ;; (<- (message "mama@localhost" "hello, there"))
40 ;; (display "we ignore a response to this message"))