nyxthulhu
nyxthulhu

Reputation: 9762

Run a command in erlang with a parameter

I'm trying to write a erlang module for ejabberd and can't quite work out the syntax.

What I'm trying to do is when a user sends a off-line message, run a script I've written.

on_offline(_From, To, _Packet) ->
        ?INFO_MSG("recieved offline packet to ~p", [To]),
        ?INFO_MSG("Got offline message", []),
        osReturn = os:cmd("/opt/ejabberd-2.1.10/newmods/handleoffline.py " ++ To),
        ?INFO_MSG("Send to handler and got return ~s", [osReturn]),
        none.

However when I run it, I get a few errors, if I have ++ To there I get the error

E(<0.423.0>:ejabberd_hooks:294) : {function_clause,

However even if I run it without the ++ I get another error telling me Bad Match.

All I'd like to do is run handleoffline.py with the first part of the user ID, anyone got any suggestions?

Upvotes: 0

Views: 291

Answers (1)

Nuno Freitas
Nuno Freitas

Reputation: 993

From the little information you have provided and assuming that your functions is being called with the ejabberd offline_message_hook I can at least find a problem. In

osReturn = os:cmd("/opt/ejabberd-2.1.10/newmods/handleoffline.py " ++ To)

you are trying to use the ++ to concatenate the list you provided with To. But the major problem is that To isn't a string (list), and that generates your error. Your function is in the form of

on_offline(From, To, Packet) -> ok

where To = From = #jid (see jlib)

one way to correct that would be to use

on_offline(_From, To=#jid{user = User, server=Server}, _Packet) ->
  ...
  OsReturn = os:cmd("/opt/ejabberd-2.1.10/newmods/handleoffline.py " ++ User),
  ?INFO_MSG("Send to handler and got return ~s", [OsReturn]),
  none.

because the field user on #jid is a string.

Edit: another error is that you need to chang osReturn to OsReturn because the first is an atom and will raise an error on your code, like selle pointed out


Edit your question and add more information if you have more problems, or to make this one clearer.

Upvotes: 4

Related Questions