⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ejabberd_web_admin.erl

📁 ejabberd-0.7.5 分布式Jabber服务器
💻 ERL
📖 第 1 页 / 共 4 页
字号:
	       ], Lang);process_admin(#request{user = User,		       path = ["style.css"],		       q = Query,		       lang = Lang} = Request) ->    {200, [{"Content-Type", "text/css"}], css()};process_admin(#request{user = User,		       path = ["logo.png"],		       q = Query,		       lang = Lang} = Request) ->    {200, [{"Content-Type", "image/png"}], logo()};process_admin(#request{user = User,		       path = ["logo-fill.png"],		       q = Query,		       lang = Lang} = Request) ->    {200, [{"Content-Type", "image/png"}], logo_fill()};process_admin(#request{user = User,		       path = ["1x1tr.gif"],		       q = Query,		       lang = Lang} = Request) ->    {200, [{"Content-Type", "image/gif"}], empty()};process_admin(#request{user = User,		       path = ["acls-raw"],		       q = Query,		       lang = Lang} = Request) ->    Res = case lists:keysearch("acls", 1, Query) of	      {value, {_, String}} ->		  case erl_scan:string(String) of		      {ok, Tokens, _} ->			  case erl_parse:parse_term(Tokens) of			      {ok, NewACLs} ->				  case acl:add_list(NewACLs, true) of				      ok ->					  ok;				      _ ->					  error				  end;			      _ ->				  error			  end;		      _ ->			  error		  end;	      _ ->		  nothing	  end,    ACLs = lists:flatten(io_lib:format("~p.", [ets:tab2list(acl)])),    make_xhtml([?XCT("h1", "ejabberd access control lists configuration")] ++	       case Res of		   ok -> [?CT("submitted"), ?P];		   error -> [?CT("bad format"), ?P];		   nothing -> []	       end ++	       [?XAE("form", [{"method", "post"}],		     [?XAC("textarea", [{"name", "acls"},					{"rows", "16"},					{"cols", "80"}],			   ACLs),		      ?BR,		      ?INPUT("submit", "submit", "Submit")		     ])	       ], Lang);process_admin(#request{method = Method,			user = User,			path = ["acls"],			q = Query,			lang = Lang} = Request) ->    ?INFO_MSG("query: ~p", [Query]),    Res = case Method of	      'POST' ->		  case catch acl_parse_query(Query) of		      {'EXIT', _} ->			  error;		      NewACLs ->			  ?INFO_MSG("NewACLs: ~p", [NewACLs]),			  case acl:add_list(NewACLs, true) of			      ok ->				  ?INFO_MSG("NewACLs: ok", []),				  ok;			      _ ->				  error			  end		  end;	      _ ->		  nothing	  end,    ACLs = lists:keysort(2, ets:tab2list(acl)),    make_xhtml([?XCT("h1", "ejabberd access control lists configuration")] ++	       case Res of		   ok -> [?CT("submitted"), ?P];		   error -> [?CT("bad format"), ?P];		   nothing -> []	       end ++	       [?XE("p", [?ACT("../acls-raw/", "raw")])] ++	       [?XAE("form", [{"method", "post"}],		     [acls_to_xhtml(ACLs),		      ?BR,		      ?INPUTT("submit", "delete", "Delete Selected"),		      ?C(" "),		      ?INPUTT("submit", "submit", "Submit")		     ])	       ], Lang);process_admin(#request{user = User,			path = ["access-raw"],			q = Query,			lang = Lang} = Request) ->    SetAccess =	fun(Rs) ->		mnesia:transaction(		  fun() ->			  Os = mnesia:select(config,					     [{{config, {access, '$1'}, '$2'},					       [],					       ['$_']}]),			  lists:foreach(fun(O) ->						mnesia:delete_object(O)					end, Os),			  lists:foreach(			    fun({access, Name, Rules}) ->				    mnesia:write({config,						  {access, Name},						  Rules})			    end, Rs)		  end)	end,    Res = case lists:keysearch("access", 1, Query) of	      {value, {_, String}} ->		  case erl_scan:string(String) of		      {ok, Tokens, _} ->			  case erl_parse:parse_term(Tokens) of			      {ok, Rs} ->				  case SetAccess(Rs) of				      {atomic, _} ->					  ok;				      _ ->					  error				  end;			      _ ->				  error			  end;		      _ ->			  error		  end;	      _ ->		  nothing	  end,    Access =	lists:flatten(	  io_lib:format(	    "~p.", [ets:select(config,			       [{{config, {access, '$1'}, '$2'},				 [],				 [{{access, '$1', '$2'}}]}])])),    make_xhtml([?XCT("h1", "ejabberd access rules configuration")] ++	       case Res of		   ok -> [?CT("submitted"), ?P];		   error -> [?CT("bad format"), ?P];		   nothing -> []	       end ++	       [?XAE("form", [{"method", "post"}],		     [?XAC("textarea", [{"name", "access"},					{"rows", "16"},					{"cols", "80"}],			   Access),		      ?BR,		      ?INPUT("submit", "submit", "Submit")		     ])	       ], Lang);process_admin(#request{method = Method,		       user = User,		       path = ["access"],		       q = Query,		       lang = Lang} = Request) ->    ?INFO_MSG("query: ~p", [Query]),    Res = case Method of	      'POST' ->		  case catch access_parse_query(Query) of		      {'EXIT', _} ->			  error;		      ok ->			  ok		  end;	      _ ->		  nothing	  end,    AccessRules =	ets:select(config,		   [{{config, {access, '$1'}, '$2'},		     [],		     [{{access, '$1', '$2'}}]}]),    make_xhtml([?XCT("h1", "ejabberd access rules configuration")] ++	       case Res of		   ok -> [?CT("submitted"), ?P];		   error -> [?CT("bad format"), ?P];		   nothing -> []	       end ++	       [?XE("p", [?ACT("../access-raw/", "raw")])] ++	       [?XAE("form", [{"method", "post"}],		     [access_rules_to_xhtml(AccessRules, Lang),		      ?BR,		      ?INPUTT("submit", "delete", "Delete Selected")		     ])	       ], Lang);process_admin(#request{method = Method,		       user = User,		       path = ["access", SName],		       q = Query,		       lang = Lang} = Request) ->    ?INFO_MSG("query: ~p", [Query]),    Name = list_to_atom(SName),    Res = case lists:keysearch("rules", 1, Query) of	      {value, {_, String}} ->		  case parse_access_rule(String) of		      {ok, Rs} ->			  ejabberd_config:add_global_option(			    {access, Name}, Rs),			  ok;		      _ ->			  error		  end;	      _ ->		  nothing	  end,    Rules = case ejabberd_config:get_global_option({access, Name}) of		undefined ->		    [];		Rs1 ->		    Rs1	    end,    make_xhtml([?XC("h1",		    io_lib:format(?T("~s access rule configuration"), [SName]))] ++	       case Res of		   ok -> [?CT("submitted"), ?P];		   error -> [?CT("bad format"), ?P];		   nothing -> []	       end ++	       [?XAE("form", [{"method", "post"}],		     [access_rule_to_xhtml(Rules),		      ?BR,		      ?INPUTT("submit", "submit", "Submit")		     ])	       ], Lang);process_admin(#request{user = User,			path = ["users"],			q = Query,			lang = Lang} = Request) ->    Res = list_users(Query, Lang),    make_xhtml([?XCT("h1", "ejabberd users")] ++ Res, Lang);process_admin(#request{user = User,		       path = ["users", Diap],		       q = Query,		       lang = Lang} = Request) ->    Res = list_users_in_diapason(Diap, Lang),    make_xhtml([?XCT("h1", "ejabberd users")] ++ Res, Lang);process_admin(#request{user = User,			path = ["online-users"],			q = Query,			lang = Lang} = Request) ->    Res = list_online_users(Lang),    make_xhtml([?XCT("h1", "ejabberd users")] ++ Res, Lang);process_admin(#request{user = User,		       path = ["stats"],		       q = Query,		       lang = Lang} = Request) ->    Res = get_stats(Lang),    make_xhtml([?XCT("h1", "ejabberd stats")] ++ Res, Lang);process_admin(#request{user = User,		       path = ["user", U],		       q = Query,		       lang = Lang} = Request) ->    Res = user_info(U, Query, Lang),    make_xhtml(Res, Lang);process_admin(#request{user = User,		       path = ["user", U, "queue"],		       q = Query,		       lang = Lang} = Request) ->    Res = user_queue(U, Query, Lang),    make_xhtml(Res, Lang);process_admin(#request{user = User,		       path = ["user", U, "roster"],		       q = Query,		       lang = Lang} = Request) ->    Res = user_roster(U, Query, Lang, true),    make_xhtml(Res, Lang);process_admin(#request{user = User,		       path = ["nodes"],		       q = Query,		       lang = Lang} = Request) ->    Res = get_nodes(Lang),    make_xhtml(Res, Lang);process_admin(#request{user = User,		       path = ["node", SNode | NPath],		       q = Query,		       lang = Lang} = Request) ->    case search_running_node(SNode) of	false ->	    make_xhtml([?XCT("h1", "Node not found")], Lang);	Node ->	    Res = get_node(Node, NPath, Query, Lang),	    make_xhtml(Res, Lang)    end;process_admin(#request{lang = Lang}) ->    setelement(1, make_xhtml([?XC("h1", "Not found")], Lang), 404).acls_to_xhtml(ACLs) ->    ?XAE("table", [],	 [?XE("tbody",	      lists:map(		fun({acl, Name, Spec} = ACL) ->			SName = atom_to_list(Name),			ID = term_to_id(ACL),			?XE("tr",			    [?XE("td", [?INPUT("checkbox", "selected", ID)]),			     ?XC("td", SName)] ++			    acl_spec_to_xhtml(ID, Spec)			   )		end, ACLs) ++	      [?XE("tr",		   [?X("td"),		    ?XE("td", [?INPUT("text", "namenew", "")])		   ] ++		   acl_spec_to_xhtml("new", {user, ""})		  )]	     )]).-define(ACLINPUT(Text), ?XE("td", [?INPUT("text", "value" ++ ID, Text)])).acl_spec_to_text({user, U}) ->    {user, U};acl_spec_to_text({server, S}) ->    {server, S};acl_spec_to_text({user, U, S}) ->    {user_server, U ++ "@" ++ S};acl_spec_to_text(Spec) ->    {raw, term_to_string(Spec)}.acl_spec_to_xhtml(ID, Spec) ->    {Type, Str} = acl_spec_to_text(Spec),    [acl_spec_select(ID, Type), ?ACLINPUT(Str)].acl_spec_select(ID, Opt) ->    ?XE("td",	[?XAE("select", [{"name", "type" ++ ID}],	      lists:map(		fun(O) ->			Sel = if				  O == Opt -> [{"selected", "selected"}];				  true -> []			      end,			?XAC("option",			     Sel ++ [{"value", atom_to_list(O)}],			     atom_to_list(O))		end, [user, server, user_server, raw]))]).term_to_string(T) ->    lists:flatten(io_lib:format("~1000000p", [T])).term_to_id(T) ->    jlib:encode_base64(binary_to_list(term_to_binary(T))).acl_parse_query(Query) ->    ACLs = ets:tab2list(acl),    case lists:keysearch("submit", 1, Query) of	{value, _} ->	    acl_parse_submit(ACLs, Query);	_ ->	    case lists:keysearch("delete", 1, Query) of		{value, _} ->		    acl_parse_delete(ACLs, Query)	    end    end.acl_parse_submit(ACLs, Query) ->    NewACLs =	lists:map(	  fun({acl, Name, Spec} = ACL) ->		  SName = atom_to_list(Name),		  ID = term_to_id(ACL),		  case {lists:keysearch("type" ++ ID, 1, Query),			lists:keysearch("value" ++ ID, 1, Query)} of		      {{value, {_, T}}, {value, {_, V}}} ->			  {Type, Str} = acl_spec_to_text(Spec),			  case {atom_to_list(Type), Str} of			      {T, V} ->				  ACL;			      _ ->				  NewSpec = string_to_spec(T, V),				  {acl, Name, NewSpec}			  end;		      _ ->			  ACL		  end	  end, ACLs),    NewACL = case {lists:keysearch("namenew", 1, Query),		   lists:keysearch("typenew", 1, Query),		   lists:keysearch("valuenew", 1, Query)} of		 {{value, {_, ""}}, _, _} ->		     [];		 {{value, {_, N}}, {value, {_, T}}, {value, {_, V}}} ->		     NewName = list_to_atom(N),		     NewSpec = string_to_spec(T, V),		     [{acl, NewName, NewSpec}];		 _ ->		     []	     end,    NewACLs ++ NewACL.string_to_spec("user", Val) ->    {user, Val};string_to_spec("server", Val) ->    {server, Val};string_to_spec("user_server", Val) ->    #jid{luser = U, lserver = S, resource = ""} = jlib:string_to_jid(Val),    {user_server, U, S};string_to_spec("raw", Val) ->    {ok, Tokens, _} = erl_scan:string(Val ++ "."),    {ok, NewSpec} = erl_parse:parse_term(Tokens),    NewSpec.acl_parse_delete(ACLs, Query) ->    NewACLs =	lists:filter(	  fun({acl, Name, Spec} = ACL) ->		  ID = term_to_id(ACL),		  not lists:member({"selected", ID}, Query)	  end, ACLs),    NewACLs.access_rules_to_xhtml(AccessRules, Lang) ->    ?XAE("table", [],	 [?XE("tbody",	      lists:map(		fun({access, Name, Rules} = Access) ->			SName = atom_to_list(Name),			ID = term_to_id(Access),			?XE("tr",			    [?XE("td", [?INPUT("checkbox", "selected", ID)]),			     ?XE("td", [?AC(SName ++ "/", SName)]),			     ?XC("td", term_to_string(Rules))			    ]			   )		end, AccessRules) ++	      [?XE("tr",		   [?X("td"),		    ?XE("td", [?INPUT("text", "namenew", "")]),		    ?XE("td", [?INPUTT("submit", "addnew", "Add New")])		   ]		  )]	     )]).access_parse_query(Query) ->    AccessRules =	ets:select(config,		   [{{config, {access, '$1'}, '$2'},		     [],		     [{{access, '$1', '$2'}}]}]),    case lists:keysearch("addnew", 1, Query) of	{value, _} ->	    access_parse_addnew(AccessRules, Query);	_ ->	    case lists:keysearch("delete", 1, Query) of		{value, _} ->		    access_parse_delete(AccessRules, Query)	    end    end.access_parse_addnew(AccessRules, Query) ->    case lists:keysearch("namenew", 1, Query) of	{value, {_, String}} when String /= "" ->	    Name = list_to_atom(String),	    ejabberd_config:add_global_option({access, Name}, []),	    ok    end.access_parse_delete(AccessRules, Query) ->    lists:foreach(      fun({access, Name, _Rules} = AccessRule) ->	      ID = term_to_id(AccessRule),	      case lists:member({"selected", ID}, Query) of		  true ->		      mnesia:transaction(			fun() ->				mnesia:delete({config, {access, Name}})

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -