diff options
author | Badlop <badlop@process-one.net> | 2013-03-14 10:33:02 +0100 |
---|---|---|
committer | Badlop <badlop@process-one.net> | 2013-03-14 10:33:02 +0100 |
commit | 9deb294328bb3f9eb6bd2c0e7cd500732e9b5830 (patch) | |
tree | 7e1066c130250627ee0abab44a135f583a28d07f /src/mod_pres_counter.erl | |
parent | list_to_integer/2 only works in OTP R14 and newer (diff) |
Accumulated patch to binarize and indent code
Diffstat (limited to '')
-rw-r--r-- | src/mod_pres_counter.erl | 149 |
1 files changed, 68 insertions, 81 deletions
diff --git a/src/mod_pres_counter.erl b/src/mod_pres_counter.erl index 314f05fb..9246f8d5 100644 --- a/src/mod_pres_counter.erl +++ b/src/mod_pres_counter.erl @@ -28,18 +28,18 @@ -behavior(gen_mod). --export([start/2, - stop/1, - check_packet/6]). +-export([start/2, stop/1, check_packet/6]). -include("ejabberd.hrl"). + -include("jlib.hrl"). --record(pres_counter, {dir, start, count, logged = false}). +-record(pres_counter, + {dir, start, count, logged = false}). start(Host, _Opts) -> - ejabberd_hooks:add(privacy_check_packet, Host, - ?MODULE, check_packet, 25), + ejabberd_hooks:add(privacy_check_packet, Host, ?MODULE, + check_packet, 25), ok. stop(Host) -> @@ -47,88 +47,75 @@ stop(Host) -> ?MODULE, check_packet, 25), ok. -check_packet(_, _User, Server, - _PrivacyList, - {From, To, {xmlelement, Name, Attrs, _}}, - Dir) -> +check_packet(_, _User, Server, _PrivacyList, + {From, To, #xmlel{name = Name, attrs = Attrs}}, Dir) -> case Name of - "presence" -> - IsSubscription = - case xml:get_attr_s("type", Attrs) of - "subscribe" -> true; - "subscribed" -> true; - "unsubscribe" -> true; - "unsubscribed" -> true; - _ -> false - end, - if - IsSubscription -> - JID = case Dir of - in -> To; - out -> From - end, - update(Server, JID, Dir); - true -> - allow - end; - _ -> - allow + <<"presence">> -> + IsSubscription = case xml:get_attr_s(<<"type">>, Attrs) + of + <<"subscribe">> -> true; + <<"subscribed">> -> true; + <<"unsubscribe">> -> true; + <<"unsubscribed">> -> true; + _ -> false + end, + if IsSubscription -> + JID = case Dir of + in -> To; + out -> From + end, + update(Server, JID, Dir); + true -> allow + end; + _ -> allow end. update(Server, JID, Dir) -> - %% get options - StormCount = gen_mod:get_module_opt(Server, ?MODULE, count, 5), - TimeInterval = gen_mod:get_module_opt(Server, ?MODULE, interval, 60), + StormCount = gen_mod:get_module_opt(Server, ?MODULE, count, + fun(I) when is_integer(I), I>0 -> I end, + 5), + TimeInterval = gen_mod:get_module_opt(Server, ?MODULE, interval, + fun(I) when is_integer(I), I>0 -> I end, + 60), {MegaSecs, Secs, _MicroSecs} = now(), TimeStamp = MegaSecs * 1000000 + Secs, case read(Dir) of - undefined -> - write(Dir, #pres_counter{dir = Dir, - start = TimeStamp, - count = 1}), - allow; - #pres_counter{start = TimeStart, count = Count, logged = Logged} = R -> - %% record for this key exists, check if we're - %% within TimeInterval seconds, and whether the StormCount is - %% high enough. or else just increment the count. - if - TimeStamp - TimeStart > TimeInterval -> - write(Dir, R#pres_counter{ - start = TimeStamp, - count = 1}), - allow; - (Count =:= StormCount) and Logged -> - {stop, deny}; - Count =:= StormCount -> - write(Dir, R#pres_counter{logged = true}), - case Dir of - in -> - ?WARNING_MSG( - "User ~s is being flooded, " - "ignoring received presence subscriptions", - [jlib:jid_to_string(JID)]); - out -> - IP = ejabberd_sm:get_user_ip( - JID#jid.luser, - JID#jid.lserver, - JID#jid.lresource), - ?WARNING_MSG( - "Flooder detected: ~s, on IP: ~s " - "ignoring sent presence subscriptions~n", - [jlib:jid_to_string(JID), - jlib:ip_to_list(IP)]) - end, - {stop, deny}; - true -> - write(Dir, R#pres_counter{ - start = TimeStamp, - count = Count + 1}), - allow - end + undefined -> + write(Dir, + #pres_counter{dir = Dir, start = TimeStamp, count = 1}), + allow; + #pres_counter{start = TimeStart, count = Count, + logged = Logged} = + R -> + if TimeStamp - TimeStart > TimeInterval -> + write(Dir, + R#pres_counter{start = TimeStamp, count = 1}), + allow; + (Count =:= StormCount) and Logged -> {stop, deny}; + Count =:= StormCount -> + write(Dir, R#pres_counter{logged = true}), + case Dir of + in -> + ?WARNING_MSG("User ~s is being flooded, ignoring received " + "presence subscriptions", + [jlib:jid_to_string(JID)]); + out -> + IP = ejabberd_sm:get_user_ip(JID#jid.luser, + JID#jid.lserver, + JID#jid.lresource), + ?WARNING_MSG("Flooder detected: ~s, on IP: ~s ignoring " + "sent presence subscriptions~n", + [jlib:jid_to_string(JID), + jlib:ip_to_list(IP)]) + end, + {stop, deny}; + true -> + write(Dir, + R#pres_counter{start = TimeStamp, count = Count + 1}), + allow + end end. -read(K)-> - get({pres_counter, K}). +read(K) -> get({pres_counter, K}). -write(K, V)-> - put({pres_counter, K}, V). +write(K, V) -> put({pres_counter, K}, V). |