summaryrefslogtreecommitdiff
path: root/src/utils.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.clj')
-rwxr-xr-xsrc/utils.clj59
1 files changed, 57 insertions, 2 deletions
diff --git a/src/utils.clj b/src/utils.clj
index 3f2f4e4..3e77710 100755
--- a/src/utils.clj
+++ b/src/utils.clj
@@ -1,7 +1,8 @@
(ns utils
(:import java.text.SimpleDateFormat
java.util.Date
- java.net.URLDecoder)
+ java.net.URLDecoder
+ org.antlr.stringtemplate.StringTemplateGroup)
(:use clojure.contrib.json.write
clojure.contrib.sql))
@@ -94,4 +95,58 @@
(defn #^String lower-case
"Converts string to all lower-case."
[#^String s]
- (.toLowerCase s)) \ No newline at end of file
+ (.toLowerCase s))
+
+;; 404
+
+(defn unknown-page [& more]
+ [404 "Page not Found"])
+
+;; Templates
+
+(def template-group (new StringTemplateGroup "dumpfm" "template"))
+(.setRefreshInterval template-group 3)
+
+;; TODO: handle exception
+(defn fetch-template [template session]
+ (let [st (.getInstanceOf template-group template)]
+ (if (session :nick)
+ (do (.setAttribute st "user_email" (session :email))
+ (.setAttribute st "user_nick" (session :nick))
+ (if (non-empty-string? (session :avatar)) (.setAttribute st "user_avatar" (session :avatar)))
+ (.setAttribute st "isadmin" (session :is_admin)))) ;; TODO: consolidate session/user code
+ st))
+
+(defn serve-template [template session]
+ (.toString (fetch-template template session)))
+
+
+;; User authentication
+; TODO: move to user module
+
+(def nick-regex #"^[A-Za-z0-9\-_∆˚†]*$")
+
+(defn is-invalid-nick? [n]
+ (cond
+ (< (count n) 3) "NICK_TOO_SHORT"
+ (not (re-matches nick-regex n)) "NICK_INVALID_CHARS"))
+
+(defn check-nick [nick]
+ (let [query "SELECT * FROM users WHERE LOWER(nick) = ? LIMIT 1"]
+ (> (count (do-select [query (lower-case nick)])) 0)))
+
+(defn fetch-nick [nick]
+ (let [query "SELECT * FROM users WHERE nick = ? LIMIT 1"]
+ (first (do-select [query nick]))))
+
+(defn authorize-nick-hash [nick hash]
+ (let [db-user (fetch-nick nick)]
+ (and db-user (= (db-user :hash) hash) db-user)))
+
+(defn is-vip? [session]
+ (session :is_admin))
+
+(defmacro if-vip [e]
+ "Evaluates expr if user is vipm otherwise returns 404 string. Can only be used
+ where session is defined."
+ `(if (is-vip? ~'session) ~e (unknown-page)))