Created
December 5, 2008 17:41
-
-
Save hiredman/32417 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns hiredman.xmpp | |
(:import (org.jivesoftware.smack XMPPConnection ConnectionConfiguration | |
RosterListener PrivacyListManager | |
PacketListener) | |
(org.jivesoftware.smack.packet Presence Presence$Type) | |
(org.jivesoftware.smack.filter PacketFilter))) | |
(defstruct xmpp :connection :chats) | |
(defn connect [jid pass] | |
(struct xmpp | |
(doto (XMPPConnection. (last (.split jid "@"))) | |
(.connect) | |
(.login (first (.split jid "@")) pass)) | |
{})) | |
(defn presence [type & msg] | |
(let [p (Presence. (.valueOf Presence$Type (name type)))] | |
(when msg | |
(.setStatus p (apply str msg))) | |
p)) | |
(defn msg-listener [func] | |
(proxy [org.jivesoftware.smack.MessageListener] [] | |
(processMessage [& args] | |
(apply func args )))) | |
(defmacro message-lr [ch me & body] | |
`(proxy [org.jivesoftware.smack.MessageListener] [] | |
(processMessage [c# m#] | |
(let [~ch c# ~me m#] | |
~@body)))) | |
(defn roster-listener [func] | |
(proxy [RosterListener] [] | |
(entriesAdded [& args] | |
(apply func args)))) | |
(defn create-chat [conn who ml] | |
(.createChat (.getChatManager conn) who ml)) | |
(defn chat [conn who] | |
(if ((:chats conn) who) | |
conn | |
(assoc-in conn | |
[:chats who] | |
(create-chat (:connection conn) | |
who | |
(msg-listener (fn [& _] nil)))))) | |
(defn msg [conn who msg] | |
(let [conn (chat conn who) | |
chat ((:chats conn) who)] | |
(.sendMessage chat msg) | |
conn)) | |
(defn add-ml [conn who ml] | |
(let [conn (chat conn who)] | |
(.addMessageListener ((:chats conn) who) ml) | |
conn)) | |
(defn add-to-roster [conn who & [nickname group]] | |
(.createEntry (.getRoster (:connection conn)) who (str nickname) group) | |
conn) | |
(defn flush-roster [conn] | |
(dorun | |
(map #(.removeEntry(.getRoster (:connection conn)) %) | |
(.getEntries (.getRoster (:connection conn))))) | |
conn) | |
(defn roster [conn] | |
(.getEntries (.getRoster (:connection conn)))) | |
(defn chute [con who & infunc] | |
(let [inq (ref [])] | |
(if (not infunc) | |
(send-off con add-ml | |
who | |
(message-lr c m | |
(dosync | |
(ref-set inq (cons [(str (.getParticipant c)) (.getBody m)] @inq))))) | |
(send-off con add-ml | |
who | |
(message-lr c m | |
(infunc c m)))) | |
(fn [& args] | |
(cond | |
(= :send (first args)) | |
(send-off con msg who (apply str (rest args))) | |
(= :recv (first args)) | |
(let [v (last @inq)] | |
(dosync (ref-set inq (butlast @inq))) | |
v))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment