Created
September 29, 2014 19:21
-
-
Save developerworks/798d67182b38eda72e25 to your computer and use it in GitHub Desktop.
Ejabberd module: get room members
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 参考资源 | |
%% https://www.ejabberd.im/node/4159?q=node/4159 | |
%% http://blog.zlxstar.me/blog/2013/07/21/dicorvery-user-muclist/ | |
-module(mod_muc_member_list). | |
-author('hezhiqiang'). | |
-behaviour(gen_mod). | |
-include("ejabberd.hrl"). | |
-include("jlib.hrl"). | |
-include("logger.hrl"). | |
-define(NS_MUC_MEMBER_LIST, <<"http://jabber.org/protocol/muc#member-list">>). | |
-export([ | |
start/2, | |
stop/1, | |
process_local_iq/3 | |
]). | |
start(Host, Opts) -> | |
IQDisc = gen_mod:get_opt( | |
iqdisc, | |
Opts, | |
fun gen_iq_handler:check_type/1, | |
one_queue | |
), | |
mod_disco:register_feature( | |
Host, ?NS_MUC_MEMBER_LIST | |
), | |
gen_iq_handler:add_iq_handler(ejabberd_local, | |
Host, ?NS_MUC_MEMBER_LIST, ?MODULE, process_local_iq, IQDisc), | |
%% gen_iq_handler:add_iq_handler(ejabberd_sm, | |
%% Host, ?NS_MUC_MEMBER_LIST, ?MODULE, process_sm_iq, IQDisc), | |
ok. | |
stop(Host) -> | |
mod_disco:unregister_feature(Host, ?NS_MUC_MEMBER_LIST), | |
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_MUC_MEMBER_LIST), | |
%% gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_MUC_MEMBER_LIST), | |
ok. | |
process_local_iq(From, To, #iq{type = Type, sub_el = SubEl} = IQ) -> | |
case Type of | |
set -> | |
#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]}; | |
get -> | |
%% Node = xml:get_tag_attr_s(<<"node">>, SubEl), | |
%% ANode = case Node of | |
%% <<"">> -> []; | |
%% _ -> [{<<"node">>, Node}] | |
%% end, | |
IQ#iq{type = result, sub_el = [ | |
#xmlel{ | |
name = <<"query">>, | |
attrs = [{<<"xmlns">>, ?NS_MUC_MEMBER_LIST}], | |
children = iq_user_muclist(From, To) | |
} | |
]} | |
end. | |
iq_user_muclist(From, _To) -> | |
MucRooms = mnesia:dirty_all_keys(muc_room), | |
lists:map( | |
fun({Name, Host}) -> | |
#xmlel{ | |
name = <<"item">>, | |
attrs = [{<<"jid">>, jlib:jid_to_string({Name, Host, ""})}], | |
children = [] | |
} | |
end, | |
lists:filter( | |
fun({Name, Host}) -> | |
is_user_in_room(From, Name, Host) | |
end, | |
MucRooms | |
) | |
). | |
is_user_in_room(From, Name, Host) -> | |
[{muc_room, _, Opts}] = A = mnesia:dirty_read(muc_room, {Name, Host}), | |
%% 新版本的表结构有变化,具体可通过日志输出查看 | |
?INFO_MSG("Affiliations ~p~n", [A]), | |
{affiliations, Affiliations} = lists:nth(26, Opts), | |
lists:any( | |
%% { | |
%% {<<"user">>,<<"host.example.info">>,<<>>}, | |
%% {owner,<<>>} | |
%% } | |
fun({{User, Server, Resource}, _}) -> | |
B = jlib:make_jid(User, Server, Resource), | |
C = jlib:make_jid(From#jid.user, From#jid.server, Resource), | |
?INFO_MSG("JID EQUAL TEST ~p == ~p~n", [B, C]), | |
%% 只有一个用户是房间的成员,并且和当前用户JID不同,为了测试这里直接放回true, | |
%% B == C | |
%% There are just one member in the 'test' room, so just return true for testing. | |
true | |
end, | |
Affiliations | |
). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment