Julien a écrit :
> The idea consist of making a jabber bot that would allow me to
> remotely execute clojure code from a jabber account(Jabber is an
> instant messaging protocol). It would be kind of a remote repl.
>
Been there, done that but with Rhino.
> I need to access to a local binding because I must put the (eval (read-
> string code)) into a messageListener method. I can execute clojure
> code remotely over jabber but I want the bot to send me back the
> output of a form. And for that I think I need to refer to the chat
> connection instance.
>
I don't think so. What you need is: first to display the return value
of your eval, second to capture *out*.
(processMessage [chat message]
(do
(.sendMessage chat "trying to...")
(try
(.sendMessage chat
(pr-str
(binding [*ns* (the-ns 'replbot.main)]
(eval (read-string (.getBody message))))))
(catch Exception ex
(.sendMessage chat (str ex))))))
With that if you type (+ 2 2), the bot should reply 4.
But to (println "hello") it will reply nil.
(processMessage [chat message]
(do
(.sendMessage chat "trying to...")
(try
(let [out (java.io.StringWriter.)
result (binding [*ns* (the-ns 'replbot.main)
*out* out]
(eval (read-string (.getBody message))))
s (str out)]
(when-not (empty? s)
(.sendMessage chat s))
(.sendMessage chat (pr-str result)))
(catch Exception ex
(.sendMessage chat (str ex))))))
Now it should send two messages in reply to (println "hello"): hello and
nil.
Does this work?
> I don't fully understand the function to explicitly pass argument.
> Could you clarify Christophe ?
>
Around the form returned by read-string, I build a function form taking
one argument (test2) -- thus making test2 a local inside this function.
When you eval the form and get a function value.
--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---