1 %%%----------------------------------------------------------------------
2 %%% File    : mod_sic.erl
3 %%% Author  : Karim Gemayel <[email protected]>
4 %%% Purpose : XEP-0279 Server IP Check
5 %%% Created : 6 Mar 2010 by Karim Gemayel <[email protected]>
6 %%%
7 %%%
8 %%% ejabberd, Copyright (C) 2002-2012   ProcessOne
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 
28 -module(mod_sic).
29 
30 -author('[email protected]').
31 
32 -behaviour(gen_mod).
33 
34 -export([start/2, stop/1, process_local_iq/3, process_sm_iq/3]).
35 
36 -include_lib("exmpp/include/exmpp.hrl").
37 
38 -include_lib("exmpp/include/exmpp_jid.hrl").
39 
40 -include("ejabberd.hrl").
41 
42 start(Host, Opts) when is_list(Host) -> start(list_to_binary(Host), Opts);
43 start(HostB, Opts) ->
44     IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
45     mod_disco:register_feature(HostB, ?NS_SIC_0_s),
46     gen_iq_handler:add_iq_handler(ejabberd_local, HostB, ?NS_SIC_0_s, ?MODULE,
47 				  process_local_iq, IQDisc),
48     gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_SIC_0_s, ?MODULE, process_sm_iq,
49 				  IQDisc).
50 
51 stop(Host) ->
52     HostB = list_to_binary(Host),
53     mod_disco:unregister_feature(HostB, ?NS_SIC_0_s),
54     gen_iq_handler:remove_iq_handler(ejabberd_local, HostB, ?NS_SIC_0_s),
55     gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB, ?NS_SIC_0_s).
56 
57 process_local_iq(From, _To, #iq{type = get} = IQ) -> get_ip(From, IQ);
58 process_local_iq(_From, _To, #iq{type = set} = IQ) -> exmpp_iq:error(IQ, 'not-allowed').
59 
60 process_sm_iq(#jid{node = Node, domain = Domain} = From,
61 	      #jid{node = Node, domain = Domain} = _To, #iq{type = get} = IQ) ->
62     get_ip(From, IQ);
63 process_sm_iq(_From, _To, #iq{type = get} = IQ) -> exmpp_iq:error(IQ, forbidden);
64 process_sm_iq(_From, _To, #iq{type = set} = IQ) -> exmpp_iq:error(IQ, 'not-allowed').
65 
66 get_ip(From, IQ) ->
67     case ejabberd_sm:get_user_ip(From) of
68       {IP, _} when is_tuple(IP) ->
69 	  exmpp_iq:result(IQ,
70 			  #xmlel{name = ip, ns = ?NS_SIC_0_s,
71 				 children =
72 				     [?XMLCDATA((list_to_binary(inet_parse:ntoa(IP))))]});
73       _ -> exmpp_iq:error(IQ, 'internal-server-error')
74     end.