            (org.apache.lucene.store FSDirectory)
            (org.apache.lucene.util Version)
            (org.apache.lucene.queryParser QueryParser$Operator
+                                          QueryParser
                                           MultiFieldQueryParser)
            (java.net ServerSocket InetAddress BindException)
            (java.util Calendar Date SimpleTimeZone Vector Properties)

  (doseq [[field rule] parse-rules]
    (when (:value-fn rule)
      (when-let [value (try ((:value-fn rule) msg)
                            (catch Exception _
                              (error "Warning: missing value for field '%s'"
                                     field)
                              nil))]
        (.add doc (fieldpool/tokenized-field field value))))))


          (.getAddress address)))


(defn address-to-tokens [^InternetAddress address]
  (.replace (str (.getAddress address))
            "@" " "))


(defvar parse-rules
  {
   "date" {:value-fn (fn [^MimeMessage msg]
                       (DateTools/dateToString
                        (or (.getSentDate msg)
                            (.getReceivedDate msg))
                        DateTools$Resolution/DAY))
           :boost 10}

   "subject" {:value-fn (fn [^MimeMessage msg] (.getSubject msg)) :boost 5}

   "to" {:value-fn
         (fn [^MimeMessage msg] (join
                                 " "
                                 (map address-to-str
                                      (.getRecipients msg Message$RecipientType/TO))))
         :boost 2
         :linked-fields ["to_tokens"]}

   "to_tokens" {:value-fn (fn [^MimeMessage msg]
                            (join " "
                                  (map address-to-tokens
                                       (.getRecipients
                                        msg
                                        Message$RecipientType/TO))))
                :boost 2}

   "from" {:value-fn (fn [^MimeMessage msg] (address-to-str (first (.getFrom msg))))
           :boost 3
           :linked-fields ["from_tokens"]}

   "from_tokens" {:value-fn (fn [^MimeMessage msg]
                              (address-to-tokens
                               (first (.getFrom msg))))
                  :boost 2}

   "body" {}

   "msgid" {:value-fn (fn [^MimeMessage msg] (.getMessageID msg))}
   })


