(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 "

Caught Exception in " action " --" (.getMessage e) "


" 
                     (exception-to-string e)
                     "
")))] [(flash-assoc :msg msg) (redirect-to "/debug")]))) ;; Muting (def *mute-refresh-period-sec* 60) (def fetch-mutes-query " SELECT m.*, (m.set_on + m.duration) AS expiry, u.nick AS admin_nick FROM mutes m, users u WHERE (m.set_on + m.duration) > now() AND u.user_id = m.admin_id AND NOT m.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)) time (params :time) unit (params :unit) duration (parse-pos-interval time (s/butlast unit 1)) reason (params :reason) admin-id (session :user_id) admin-nick (session :nick)] (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)] (do-cmds q) (send-mute-email nick admin-nick reason time unit) "OK"))))) (defn format-mute [mute] (format (str "I'm sorry, you've been muted for %s. " "You'll be able to post again on %s EST.") (mute :reason) (mute :expiry)))