0

我试图找出这段代码有什么问题,因为它给了我错误,阻止它正确编译成一个梁文件。我看不出语法有什么问题。有没有可以帮助我的IDE?

这些是错误:

    parallels@parallels-Parallels-Virtual-Platform:/var/backend/ejabberd_modules# erlc -I         /var/tmp/ejabberd/src/ mod_stanza_ack.erl
   ./mod_stanza_ack.erl:97: syntax error before: '.'
   ./mod_stanza_ack.erl:98: syntax error before: Body
   ./mod_stanza_ack.erl:16: function route/3 undefined
   ./mod_stanza_ack.erl:3: Warning: behaviour gen_mod undefined
   ./mod_stanza_ack.erl:111: Warning: function strip_bom/1 is unused
   ./mod_stanza_ack.erl:114: Warning: function send_presence/3 is unused
   ./mod_stanza_ack.erl:120: Warning: function echo/3 is unused
   ./mod_stanza_ack.erl:123: Warning: function send_message/4 is unused

这是代码:

-module(mod_stanza_ack).
-behavior(gen_server).
-behavior(gen_mod).

-export([start_link/2]).

-export([start/2,
         stop/1,
         init/1,
         handle_call/3,
         handle_cast/2,
         handle_info/2,
         terminate/2,
         code_change/3]).

-export([route/3]).

-include("ejabberd.hrl").

-define(PROCNAME, ejabberd_mod_stanza_ack).
-define(BOTNAME, stanza_ack).

start_link(Host, Opts) ->
    Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
    gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []).

start(Host, Opts) ->
    Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
    ChildSpec = {Proc,
        {?MODULE, start_link, [Host, Opts]},
        temporary,
        1000,
        worker,
        [?MODULE]},
    supervisor:start_child(ejabberd_sup, ChildSpec).

stop(Host) ->
    Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
    gen_server:call(Proc, stop),
    supervisor:terminate_child(ejabberd_sup, Proc),
    supervisor:delete_child(ejabberd_sup, Proc).

init([Host, Opts]) ->
    ?DEBUG("ECHO_BOT: Starting echo_bot", []),
    % add a new virtual host / subdomain "echo".example.com
    MyHost = gen_mod:get_opt_host(Host, Opts, "echo.@HOST@"),
    ejabberd_router:register_route(MyHost, {apply, ?MODULE, route}),
    {ok, Host}.

handle_call(stop, _From, Host) ->
    {stop, normal, ok, Host}.

handle_cast(_Msg, Host) ->
    {noreply, Host}.

handle_info(_Msg, Host) ->
    {noreply, Host}.

terminate(_Reason, Host) ->
    ejabberd_router:unregister_route(Host),
    ok.

code_change(_OldVsn, Host, _Extra) ->
    {ok, Host}.

% Checks a presence /subscription/ is a part of this.
% we may want to impliment blacklisting / some kind of
% protection here to prevent malicious users
%route(From, #jid{luser = ?BOTNAME} = To, {xmlelement, "presence", _, _} = Packet) ->
route(From, To, {xmlelement, "presence", _, _} = Packet) ->
    case xml:get_tag_attr_s("type", Packet) of
        "subscribe" ->
            send_presence(To, From, "subscribe");
        "subscribed" ->
            send_presence(To, From, "subscribed"),
            send_presence(To, From, "");
        "unsubscribe" ->
            send_presence(To, From, "unsubscribed"),
            send_presence(To, From, "unsubscribe");
        "unsubscribed" ->
            send_presence(To, From, "unsubscribed");
        "" ->
            send_presence(To, From, "");
        "unavailable" ->
            ok;
        "probe" ->
            send_presence(To, From, "");
        _Other ->
            ?INFO_MSG("Other kind of presence~n~p", [Packet])
    end,
    ok;

%route(From, #jid{luser = ?BOTNAME} = To, {xmlelement, "message", _, _} = Packet) ->
route(From, To, {xmlelement, "message", _, _} = Packet) ->
    case xml:get_subtag_cdata(Packet, "body") of
    "" ->
        ok.
    Body ->
        case xml:get_tag_attr_s("type", Packet) of

        "error" ->
            ?ERROR_MSG("Received error message~n~p -> ~p~n~p", [From, To, Packet]);
        _ ->
            echo(To, From, strip_bom(Body))
        end
    end,
    ok.

%% HELPER FUNCTIONS

strip_bom([239,187,191|C]) -> C;
strip_bom(C) -> C.

send_presence(From, To, "") ->
    ejabberd_router:route(From, To, {xmlelement, "presence", [], []});

send_presence(From, To, TypeStr) ->
    ejabberd_router:route(From, To, {xmlelement, "presence", [{"type", TypeStr}], []}).

echo(From, To, Body) ->
    send_message(From, To, "chat", Body).

send_message(From, To, TypeStr, BodyStr) ->
    XmlBody = {xmlelement, "message",
           [{"type", TypeStr},
        {"from", jlib:jid_to_string(From)},
        {"to", jlib:jid_to_string(To)}],
           [{xmlelement, "body", [],
         [{xmlcdata, BodyStr}]}]},
    ejabberd_router:route(From, To, XmlBody).
4

2 回答 2

3

./mod_stanza_ack.erl:97: syntax error before: '.'说错误在第 97 行。在第 97 行更改ok.ok;. 这将解决问题。

Eclipse 的 Erlide 插件是一个很好的 IDE 试用。

于 2013-04-04T15:44:27.393 回答
3

在函数的第 97 行, isroute/3的第一个子句case

    "" ->
        ok.

这里.结束了a中间非法的函数,case应该是a ;。这也意味着解析器假定Body下一行的变量启动了一个新函数,这也是非法的,因为函数名不能是变量。

看到函数route/3尚未定义,因此无法导出,这是第 16 行未定义函数错误的原因。

有时编译器错误消息有点神秘,但行号通常会有所帮助。

于 2013-04-04T15:49:54.153 回答