forked from oubiwann/lfebot
/
lfebot-util.lfe
67 lines (57 loc) · 1.79 KB
/
lfebot-util.lfe
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
(defmodule lfebot-util
(export all))
(defun split (string delimiter)
(re:split
string
delimiter
(list #(parts 2) #(return binary))))
(defun split-all (string delimiter)
(re:split
string
delimiter
(list #(return binary))))
(defun timestamp ()
(timestamp (now)))
(defun timestamp (time)
(let* (((tuple (tuple year mon day)
(tuple hour min sec)) (calendar:now_to_local_time time))
(stamp (io_lib:format "~4..0w-~2..0w-~2..0w ~2..0w:~2..0w:~2..0w"
(list year mon day hour min sec))))
(list_to_binary stamp)))
(defun format-irc-line (nick line)
(binary ((timestamp) binary)
" <" (nick binary) "> "
(line binary)))
(defun split-nick (full-nick)
(split full-nick "!"))
(defun parse-irc
(((binary ":" (rest binary)))
(let (((list prefix new-rest)
(split rest " ")))
(parse-irc prefix new-rest)))
((rest)
(parse-irc (binary) rest)))
(defun parse-irc (prefix rest)
(case (re:run rest " :")
((tuple match _)
(let* (((list new-rest remainder) (split rest " :"))
(arg-parts (split-all new-rest "\s+")))
(lists:append (list (list prefix)
arg-parts
(list remainder)))))
(_
(lists:append (list (list prefix)
(list (split-all rest "\s+")))))))
(defun get-command-args
(((binary "@" (command-args binary)))
(case (split command-args " ")
((list command args)
(list command args))
((list command)
(list command (binary)))))
((command-args)
(case (split command-args " ")
((list _ good-command-args)
(get-command-args (binary "@" (good-command-args binary))))
((list _)
(get-command-args (binary "@"))))))