summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrekado <rekado@elephly.net>2014-11-27 10:11:11 +0100
committerrekado <rekado@elephly.net>2014-11-27 10:15:35 +0100
commit6360dee27e72ac8609c7d7748a92f7a0d5d09269 (patch)
tree3b4719e13c6a80cd19f04f3c4fbc64649222df47
parentc8128ca3a97015b8811e9ef731d4f2fe35ee5d9b (diff)
add syntactic sugar for XMPP conversations
-rw-r--r--sugar.scm40
1 files changed, 40 insertions, 0 deletions
diff --git a/sugar.scm b/sugar.scm
new file mode 100644
index 0000000..b201844
--- /dev/null
+++ b/sugar.scm
@@ -0,0 +1,40 @@
+(define-module (gnubba sugar)
+ #:use-module ((sxml simple) #:select (sxml->xml))
+ #:export (xmpp->))
+
+;; Simple syntax for XMPP "discussions".
+;; TODO: can this become an actual monad? Would it be worth the
+;; effort?
+(define-syntax xmpp->
+ (lambda (x)
+ (syntax-case x (<-)
+ ;; send and forget stanza
+ ((_ sock (<- stanza) exp ...)
+ #'(begin
+ (sxml->xml stanza sock)
+ (xmpp-> sock exp ...)))
+ ;; send stanza and pass response to next expression
+ ((_ sock (res <- stanza) exp ...)
+ #'(let ((s stanza))
+ (begin
+ (register-temp-stanza-handler-for-id
+ (stanza-id s)
+ (lambda (res)
+ (xmpp-> sock exp ...)))
+ (sxml->xml s sock))))
+ ;; unwrap single expression
+ ((_ sock exp)
+ #'exp)
+ ;; simply evaluate expression and move on to match next expression
+ ((_ sock exp exp* ...)
+ #'(begin
+ exp
+ (xmpp-> sock exp* ...))))))
+
+;; EXAMPLE
+;; (xmpp-> (current-output-port)
+;; (res <- (iq "hello"))
+;; (display res)
+;; (display "something else")
+;; (<- (message "mama@localhost" "hello, there"))
+;; (display "we ignore a response to this message"))