summaryrefslogtreecommitdiff
path: root/src/message.clj
blob: 5b3b66f0c33f02ba4a763ebfb29c665a289bf93b (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
(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)))))