pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Lisp pastebin - collaborative debugging tool View Help


Posted by yellowdog on Tue 4 Nov 17:16
report abuse | View followups from Yellowdog | download | new post

  1. %%%----------------------------------------------------------------------
  2. %%% File    : extauth.erl
  3. %%% Author  : Leif Johansson <leifj@it.su.se>
  4. %%% Purpose : External authentication using a simple port-driver
  5. %%% Created : 30 Jul 2004 by Leif Johansson <leifj@it.su.se>
  6. %%%
  7. %%%
  8. %%% ejabberd, Copyright (C) 2002-2008   Process-one
  9. %%%
  10. %%% This program is free software; you can redistribute it and/or
  11. %%% modify it under the terms of the GNU General Public License as
  12. %%% published by the Free Software Foundation; either version 2 of the
  13. %%% License, or (at your option) any later version.
  14. %%%
  15. %%% This program is distributed in the hope that it will be useful,
  16. %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. %%% General Public License for more details.
  19. %%%                        
  20. %%% You should have received a copy of the GNU General Public License
  21. %%% along with this program; if not, write to the Free Software
  22. %%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. %%% 02111-1307 USA
  24. %%%
  25. %%%----------------------------------------------------------------------
  26.  
  27. -module(extauth).
  28. -author('leifj@it.su.se').
  29.  
  30. -export([start/2, stop/1, init/2,
  31.    check_password/3, set_password/3, is_user_exists/2]).
  32.  
  33. -include("ejabberd.hrl").
  34.  
  35. -define(INIT_TIMEOUT, 60000). % Timeout is in milliseconds: 60 seconds == 60000
  36. -define(CALL_TIMEOUT, 10000). % Timeout is in milliseconds: 10 seconds == 10000
  37.  
  38. start(Host, ExtPrg) ->
  39.     spawn(?MODULE, init, [Host, ExtPrg]).
  40.  
  41. init(Host, ExtPrg) ->
  42.     register(gen_mod:get_module_proc(Host, eauth), self()),
  43.     process_flag(trap_exit,true),
  44.     Port = open_port({spawn, ExtPrg}, [{packet,2}]),
  45.     loop(Port, ?INIT_TIMEOUT).
  46.  
  47. stop(Host) ->
  48.     gen_mod:get_module_proc(Host, eauth) ! stop.
  49.  
  50. check_password(User, Server, Password) ->
  51.     call_port(Server, ["auth", User, Server, Password]).
  52.  
  53. is_user_exists(User, Server) ->
  54.     call_port(Server, ["isuser", User, Server]).
  55.  
  56. set_password(User, Server, Password) ->
  57.     call_port(Server, ["setpass", User, Server, Password]).
  58.  
  59. call_port(Server, Msg) ->
  60.     LServer = jlib:nameprep(Server),
  61.     gen_mod:get_module_proc(LServer, eauth) ! {call, self(), Msg},
  62.     receive
  63.   {eauth,Result} ->
  64.       Result
  65.     end.
  66.  
  67. loop(Port, Timeout) ->
  68.     receive
  69.   {call, Caller, Msg} ->
  70.       Port ! {self(), {command, encode(Msg)}},
  71.       receive
  72.     {Port, {data, Data}} ->
  73.                     ?INFO_MSG("extauth call '~p' received data response:~n~p", [Msg, Data]),
  74.                     Caller ! {eauth, decode(Data)};
  75.            {Port, Other} ->
  76.                     ?ERROR_MSG("extauth call '~p' received strange response:~n~p", [Msg, Other]),
  77.                     Caller ! {eauth, false}
  78.             after
  79.                 Timeout ->
  80.                     ?ERROR_MSG("extauth call '~p' didn't receive response", [Msg]),
  81.                     Caller ! {eauth, false}
  82.       end,
  83.       loop(Port, ?CALL_TIMEOUT);
  84.   stop ->
  85.       Port ! {self(), close},
  86.       receive
  87.     {Port, closed} ->
  88.         exit(normal)
  89.       end;
  90.   {'EXIT', Port, Reason} ->
  91.       ?CRITICAL_MSG("~p ~n", [Reason]),
  92.       exit(port_terminated)
  93.     end.
  94.  
  95. join(List, Sep) ->
  96.     lists:foldl(fun(A, "") -> A;
  97.        (A, Acc) -> Acc ++ Sep ++ A
  98.     end, "", List).
  99.  
  100. encode(L) ->
  101.     join(L,":").
  102.  
  103. decode([0,0]) ->
  104.     false;
  105. decode([0,1]) ->
  106.     true.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me