0

I'm trying to send packet from ejabberd to Python script then python script will do his function and return with (true or false). I found project name of it: ErlPort which helps connect Erlang to a number of other programming languages.

And it's working 100% but when received a lot of packet ejabberd shutdown.

This example working 100% but crashed when a lot of packet sent like what I say before :

My Python script

from erlport import Port, Protocol, String

class SecurityProtocol(Protocol):
    def handle_security(self, body):
        BadList = ["bad1", "bad2"]
        body = String(body).lower()
        body = u" " + body + u" "
        for x in BadList:
            if body.count(x): return True
        return False

if __name__ == "__main__":
    proto = SecurityProtocol()
    proto.run(Port(use_stdio=True))

In (ejabberd_c2s.erl) line 1896 --> ejabberd v2.1.13 :

security(Body) ->
    Port = open_port({spawn, "python -u /home/ejabberd/lib/ejabberd/ebin/security.py"}, [{packet, 1}, binary, use_stdio]),
    ReqData = term_to_binary({security, Body}),
    port_command(Port, ReqData),
    receive
        {Port, {data, RespData}} ->
            binary_to_term(RespData)
    after
        5000 ->
            {error, timeout}
    end.

privacy_check_packet(StateData, From, To, Packet, Dir) ->
    case Packet of
        {xmlelement, "message", _, _} ->
            LBody = xml:get_subtag_cdata(Packet, "body"),
            case security(LBody) of
                true -> deny;
                false -> ejabberd_hooks:run_fold(privacy_check_packet, StateData#state.server, allow, [StateData#state.user, StateData#state.server, StateData#state.privacy_list, {From, To, Packet}, Dir]);
                _ -> "Error TimeOut"
            end
    end.

And the error is : http://paste.ubuntu.com/15414683/

Is there any way to connect ejabberd with a Python script without any errors or problem?

Mr. zero
  • 245
  • 4
  • 18
  • 1
    "without any errors or problem?" Can you describe your error or problem, please ? – Mickaël Rémond Mar 18 '16 at 12:08
  • @MickaëlRémond added example + errors. thnx. – Mr. zero Mar 18 '16 at 14:00
  • Broken pipe means that ejabberd was stopped. You see it was killed in logs. You need to find what killed it. It is possible that you are consuming too much memory and get killed by OS. Check for errors in ejabberd logs and operating system Kernel logs. – Mickaël Rémond Mar 18 '16 at 14:01
  • @MickaëlRémond this error happened also : http://paste.ubuntu.com/15414746/ – Mr. zero Mar 18 '16 at 14:08
  • Basically your ejabberd code is unsafe and does not manage error. You are trying to write to a port and gets an error (einval) and you do not handle that nicely to catch and recover from error in port commands. Your first steps should be to handle errors in a proper way in your Erlang code. – Mickaël Rémond Mar 18 '16 at 14:12
  • @MickaëlRémond thank u. Actually I'm not good in Erlang language I found it very difficult and I don't know how can I let ejabberd to work with python. is there any example or any advice or any thing can help me with. thanks. – Mr. zero Mar 18 '16 at 14:22
  • 1
    I would recommend you to learn Erlang. Link with Python through port is likely going to be fragile, especially if you do not have good Erlang knowledge. – Mickaël Rémond Mar 18 '16 at 14:27

0 Answers0