(ns message (:use user utils)) ;; Message parsing ;; http://snippets.dzone.com/posts/show/6995 (def url-regex #"(?i)^((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$") (def pic-regex #"(?i)\.(jpg|jpeg|png|gif|bmp|svg)(\?|&|$)") (defn is-image? [word] (and (re-find url-regex word) (re-find pic-regex word))) (defn take-images [content] (filter is-image? (.split content " "))) (defn classify-msg [msg] (let [words (.split msg " ") imgs (map is-image? words)] (cond (every? boolean imgs) :image (some boolean imgs) :mixed :else :text))) (def recip-regex #"(?:^|\s)@\w+") (defn get-recips [content] (filter boolean (for [at-nick (set (re-seq recip-regex content))] (fetch-nick (.substring (.trim at-nick) 1))))) (defn get-recips-from-msgs [msgs] (let [recips (set (apply concat (for [m msgs] (re-seq recip-regex (:content m)))))] (filter boolean (for [r recips] (fetch-nick (.substring (.trim r) 1)))))) (def topic-regex #"(?:^|\s)#\w+") (defn get-topics [content] (set (for [r (re-seq topic-regex content)] (lower-case (.substring (.trim r) 1)))))