blob: a3fc076a27f18e419f88446309f2a923633f94e6 (
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
|
(ns admin
(:import java.io.File)
(:require [clojure.contrib.str-utils2 :as s])
(:use compojure
email
scheduled-agent
utils))
;; Debug Page
(defn exception-to-string [e]
(let [sw (java.io.StringWriter.)
pw (java.io.PrintWriter. sw)]
(.printStackTrace e pw)
(.toString sw)))
(defn lookup-templates [dir selected]
(for [f (.listFiles (File. dir))
:when (and (.isFile f) (.endsWith (.getName f) ".st"))]
(let [n (s/butlast (.getName f) 3)]
{"template" n
"selected" (= selected n)})))
(defn debug-page [session flash]
(if-vip
(let [st (fetch-template "debug" session)]
(.setAttribute st "flash" (:msg flash))
(.setAttribute st "mailtemps" (lookup-templates "template/mail" "welcome"))
(.toString st))))
(defn debug-commmand! [session params]
(if-vip
(let [action (:action params)
msg (try
(cond (= action "regemail")
(do (send-registration-email (params :nick) (params :to) (params :template))
(str "Sent registration mail to " (params :to)))
:else (str "Unknown action: " action))
(catch Exception e
(str "<h2 color=\"red\">Caught Exception in " action " --"
(.getMessage e)
"</h2><br><pre>"
(exception-to-string e)
"</pre>")))]
[(flash-assoc :msg msg)
(redirect-to "/debug")])))
;; Muting
(def *mute-refresh-period-sec* 60)
(def fetch-mutes-query "
SELECT *, set_on + duration AS expiry
FROM mutes
WHERE (set_on + duration) < now()
AND NOT is_canceled
")
(defn update-mutes []
(let [res (do-select [fetch-mutes-query])]
(zipmap (map :user_id res) res)))
(def *active-mutes*
(scheduled-agent (no-args-adaptor update-mutes)
*mute-refresh-period-sec*
nil))
(defn mute-status [session]
(if-vip
(println session)))
(defn parse-pos-interval [time unit]
(let [t (maybe-parse-int time 0)
u (lower-case unit)]
(and (> t 0)
(#{"minute" "hour" "day"} u)
(str time " " u))))
(defn mute! [session params]
(if-vip
(let [nick (params :nick)
user-id (:user_id (fetch-nick nick))
duration (parse-pos-interval (params :time) (s/butlast (params :unit) 1))
reason (params :reason)
admin-id (session :user_id)]
(cond (not user-id) [400 "INVALID_NICK"]
(not duration) [400 "INVALID_DURATION"]
;; TODO: Ugly interval hack, w/ no escaping. Totally unsafe.
:else (let [q (format "INSERT INTO mutes (user_id, admin_id, duration, reason)
VALUES (%s, %s, '%s', '%s')"
user-id admin-id duration reason)]
(and (do-cmds q) "OK"))))))
|