hackerbase/src/mailman3.scm

120 lines
3.2 KiB
Scheme

;;
;; mailman3.scm
;;
;; Mailman management interface - Mailman version 3.x support
;;
;; ISC License
;;
;; Copyright 2023 Brmlab, z.s.
;; Dominik Pantůček <dominik.pantucek@trustica.cz>
;;
;; Permission to use, copy, modify, and/or distribute this software
;; for any purpose with or without fee is hereby granted, provided
;; that the above copyright notice and this permission notice appear
;; in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
;; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
;; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
;; CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
;; OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
;; NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(declare (unit mailman3))
(module
mailman3
(
list-mailman3-lists
list-mailman3-list-members
add-email-to-mailman3-list
remove-email-from-mailman3-list
)
(import scheme
(chicken base)
(chicken string)
(chicken sort)
(chicken format)
configuration
util-io
srfi-1
mailman3-sql)
;; Just a convenient converter
(define (mailman3:cmd+args0)
(let ((cmd+args0 (string-split (*mailman3-bin*) " ")))
(values (car cmd+args0)
(cdr cmd+args0))))
;; Runs the mailman3 binary with any arguments and returns the lines
;; produced on stdout
(define (get-mailman3-output-lines . args1)
(let-values (((cmd args0) (mailman3:cmd+args0)))
(apply
get-process-output-lines
cmd
(append args0 args1))))
;; Sends all lines to the process
(define (mailman3-send/recv args1 . lines)
(let-values (((cmd args0) (mailman3:cmd+args0)))
(apply
process-send/recv
cmd
(append args0 args1)
lines)))
;; Returns the list of available lists
(define (list-mailman3-lists)
(if (*mailman3-sql*)
(list-mailman3-sql-lists)
(filter
identity
(map (lambda (line)
(let ((sline (string-split line "@")))
(if (null? sline)
#f
(car sline))))
(get-mailman3-output-lines "lists" "-q")))))
;; Returns the list of members of given list
(define (list-mailman3-list-members lst)
(if (*mailman3-sql*)
(list-mailman3-sql-list-members lst)
(sort
(get-mailman3-output-lines "members"
(format "~A@brmlab.cz" lst)
"-e")
string-ci<?)))
;; Adds given email
(define (add-email-to-mailman3-list lst email)
(print "Add " email " to " lst ".")
(let ((result
(mailman3-send/recv
(list "addmembers" "-" (format "~A@brmlab.cz" lst))
email)))
(let loop ((lines result))
(when (not (null? lines))
(print " | " (car lines))
(loop (cdr lines))))))
;; Removes given email from given listname
(define (remove-email-from-mailman3-list lst email)
(print "Remove " email " from " lst ".")
(let ((result
(get-mailman3-output-lines
"delmembers"
"-l" (format "~A@brmlab.cz" lst)
"-m" email)))
(let loop ((lines result))
(when (not (null? lines))
(print " | " (car lines))
(loop (cdr lines))))))
)