summaryrefslogtreecommitdiffstats
path: root/.bin/whois-at-hsp
diff options
context:
space:
mode:
authorjdlugosz963 <jdlugosz963@gmail.com>2024-04-26 18:21:56 +0200
committerjdlugosz963 <jdlugosz963@gmail.com>2024-04-27 18:20:11 +0200
commit73af89d935df5a8166114c9257f9f247f29850bf (patch)
treec8addfe86575b030ca5a61644dbc77adb8f63c43 /.bin/whois-at-hsp
parent9ca1ae3ceca5944765af94514e75a96195de5716 (diff)
downloaddotfiles-73af89d935df5a8166114c9257f9f247f29850bf.tar.gz
dotfiles-73af89d935df5a8166114c9257f9f247f29850bf.zip
Migrate from stumpwm to sway and from manifests to home-services.
Diffstat (limited to '.bin/whois-at-hsp')
-rwxr-xr-x.bin/whois-at-hsp84
1 files changed, 84 insertions, 0 deletions
diff --git a/.bin/whois-at-hsp b/.bin/whois-at-hsp
new file mode 100755
index 0000000..0cd03f7
--- /dev/null
+++ b/.bin/whois-at-hsp
@@ -0,0 +1,84 @@
1#!/usr/bin/env -S guix shell guile guile-json -- guile --no-auto-compile -e main -s
2-*- scheme -*-
3!#
4
5(use-modules (srfi srfi-1)
6 (ice-9 iconv)
7 (ice-9 receive)
8 (web client)
9 (json))
10
11(define whois-at-hsp-endpoint "https://whois.at.hsp.sh/api/now")
12
13(define (http-get-serialize-json url)
14 (receive (response data) (http-request url)
15 (values (json-string->scm (bytevector->string data "UTF-8"))
16 response)))
17
18(define (whois-data->users whois-data)
19 (assoc-ref whois-data "users"))
20
21(define (whois-data->unknown-devices whois-data)
22 (assoc-ref whois-data "unknown_devices"))
23
24(define (whois-data->head-count whois-data)
25 (assoc-ref whois-data "headcount"))
26
27(define (whois-data->formated-users whois-data)
28 (let ((users (vector->list (whois-data->users whois-data))))
29 (if (> (length users) 0)
30 (fold-right
31 (lambda (a b) (string-append a " " b))
32 ""
33 users)
34 "No visible users!")))
35
36(define (whois-data->summary whois-data)
37 (string-append
38 "Unknown devices: "
39 (number->string (whois-data->unknown-devices whois-data))
40 "\nUsers: [" (number->string (whois-data->head-count whois-data)) "] "
41 (whois-data->formated-users whois-data)))
42
43(define (notify data)
44 (system* "notify-send"
45 "WHOIS AT HSP"
46 (whois-data->summary data)))
47
48(define (command-line-option? option args)
49 (> (length (or (member option args)
50 '()))
51 0))
52
53(define-syntax-rule (command-line-args-handle args default ((option ...) body ...) ...)
54 (let ((option? (lambda (o) (command-line-option? o args)))
55 (something-executed? #f))
56 (when (or (option? option) ...)
57 (set! something-executed? #t)
58 body ...) ...
59 (when (not something-executed?)
60 default)))
61
62(define (main args)
63 (let ((whois-data (http-get-serialize-json whois-at-hsp-endpoint)))
64 (command-line-args-handle args
65 (begin (display (whois-data->summary whois-data))
66 (newline))
67 (("-h" "--help")
68 (display (string-append "-h, --help Help message\n"
69 "-n, --notify Notification massage with whois information.\n"
70 "-u Get users\n"
71 "-H Get head count\n"
72 "-U Get unknown devices\n")))
73 (("-n" "--notify")
74 (notify whois-data))
75 (("-u")
76 (display (whois-data->formated-users whois-data))
77 (newline))
78 (("-H")
79 (display (whois-data->head-count whois-data))
80 (newline))
81 (("-U")
82 (display (whois-data->unknown-devices whois-data))
83 (newline)))))
84