summaryrefslogtreecommitdiff
path: root/brisket.hy
blob: 7d45d36618fa51d8791263c06f0343364f3af5e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
(import [os.path [expanduser]])
(import [sys [argv exit stderr]])
(import re)
(import [nntplib [NNTP-SSL]])
(import [subprocess [Popen PIPE]])

(require [hy.contrib.walk [let]])

(setv *auth-source* (expanduser "~/.authinfo.gpg"))
(setv *recognized-commands* ["describe-group"
                             "help"
                             "list-group"
                             "list-groups"])

(defn help [&rest args]
  "Display the documentation for a command to stdout."
  (unless (= (len args) 1)
    (err (.format "usage: help [command]"))
    (return))

  (let [command (first args)]
    (unless (in command *recognized-commands*)
      (err (.format "unrecognized command: {}" command))
      (exit 1))

    (let [handler (get (globals) (.replace command "-" "_"))]
      (print (. handler __doc__)))))

(defn get-user-info [host]
  (let [process (Popen ["gpg" "-q" "-d" *auth-source*] :stdout PIPE)
        output (first (.communicate process))]
    (for [line output]
      (when (.startswith line (.format "machine {}" host))
        (return line)))))

(defn err [&rest args]
  "Displays ARGS to stdout."
  (.write stderr (+ (first args) "\n") #* (rest args)))

(when (= __name__ "__main__")
  (unless (>= (len argv) 2)
    (err (.format "usage: {} [command] [args]" (first argv)))
    (err "recognized commands:")
    (for [command *recognized-commands*]
      (err (.format "  - {}" command)))
    (exit 1))

  (let [command (nth argv 1)]
    (unless (in command *recognized-commands*)
      (err (.format "unrecognized command: {}" command))
      (exit 1))

    ;; Special case for 'help', as it's the one command that doesn't require a
    ;; connection to the NNTP server.
    (if (= command "help")
        (help #* (drop 2 argv))
        (with [session (NNTP_SSL HOST :user USER :password PASSWORD)]
          ;; Otherwise, pull the function out of the global namespace.
          (let [handler (get (globals) (.replace command "-" "_"))]
            (handler #* (drop 2 argv)))))))

;; # To-be-converted Python
;;
;;
;; def list_groups(n, *args):
;;     pattern = args[0] if len(args) >= 1 else None
;; 
;;     _, groups = n.list()
;; 
;;     for group in groups:
;;         if pattern is None or re.match(pattern, group.group):
;;             print(group.group)
;; 
;; def describe_group(n, *args):
;;     if len(args) < 1:
;;         err("usage: describe-group [group]")
;;         return
;; 
;;     _, descs = n.descriptions(args[0])
;; 
;;     for desc in descs:
;;         print(desc)
;; 
;; def list_group(n, *args):
;;     if len(args) < 1:
;;         err("usage: list-group [group]")
;;         return
;; 
;;     _, _, first, last, _ = n.group(args[0])
;;     _, overviews = n.over((first, last))
;; 
;;     for article_num, overview in overviews:
;;         print("\"{}\" by {} on {}".format(
;;             overview.get("subject"),
;;             overview.get("from"),
;;             overview.get("date"),
;;         ))