Permalink
Browse files

add a way to explicitely set user and passwd in XMPP (can be used with

an external CSV, therefore it fixes TSUN-123)
  • Loading branch information...
nniclausse committed Aug 23, 2010
1 parent b527593 commit 7a8063f9f553103eae217ba38f30c46ec5aff33b
View
@@ -149,18 +149,16 @@ parse_config(Element, Conf) ->
%% if we are testing a single domain (the default case), we change from {domain,D}.
%% to the specified domain (D). If {vhost,FileId}, we choose a domain from that file
%% and set it.
-add_dynparams(Subst,DynData, Param =#jabber{domain={domain,Domain}}, Host) ->
- UserId = choose_or_cache_user_id(),
- add_dynparams(Subst,DynData, Param#jabber{id=UserId,
- domain=Domain,
- user_server=default},Host);
+add_dynparams(Subst,DynData, Param =#jabber{id=OldId,username=OldUser,passwd=OldPasswd, domain={domain,Domain}}, Host) ->
+ {Id,User,Pwd} = choose_or_cache_user_id(OldId,OldUser,OldPasswd),
+ add_dynparams(Subst,DynData, Param#jabber{id=Id,username=User,passwd=Pwd,domain=Domain, user_server=default},Host);
-add_dynparams(Subst,DynData,Param =#jabber{domain={vhost,FileId}}, Host) ->
- {UserId,Domain,UserServer} = choose_domain(FileId),
- add_dynparams(Subst,DynData, Param#jabber{id=UserId,
+add_dynparams(Subst,DynData,Param =#jabber{id=OldId, username=OldUser,passwd=OldPasswd, domain={vhost,FileId}}, Host) ->
+ {Id,User,Pwd} = choose_or_cache_user_id(OldId,OldUser,OldPasswd),
+ {Domain,UserServer} = choose_domain(FileId),
+ add_dynparams(Subst,DynData, Param#jabber{id=Id,username=User, passwd=Pwd,
domain=Domain,
user_server=UserServer},Host);
-
add_dynparams(_Subst,[], Param, _Host) ->
Param;
add_dynparams(false,#dyndata{proto=_DynData}, Param, _Host) ->
@@ -177,11 +175,10 @@ add_dynparams(true,#dyndata{proto=_JabDynData, dynvars=DynVars}, Param, _Host) -
choose_domain(VHostFileId) ->
case get(xmpp_vhost_domain) of
undefined ->
- Domain = do_choose_domain(VHostFileId),
- UserServer = global:whereis_name(list_to_atom("us_"++Domain)),
- UserId = choose_user_id(UserServer),
- put(xmpp_vhost_domain,{UserId,Domain,UserServer}),
- {UserId,Domain,UserServer};
+ Domain = do_choose_domain(VHostFileId),
+ UserServer = global:whereis_name(list_to_atom("us_"++Domain)),
+ put(xmpp_vhost_domain,{Domain,UserServer}),
+ {Domain,UserServer};
X ->
X
end.
@@ -212,14 +209,20 @@ choose_user_id(UserServer) ->
Id
end.
-choose_or_cache_user_id() ->
+choose_or_cache_user_id(user_defined,User,Pwd) ->
+ put(xmpp_user_id,{User,Pwd}),
+ {user_defined, User,Pwd};
+
+choose_or_cache_user_id(Id,User,Pwd) ->
case get(xmpp_user_id) of
- undefined ->
- Id = choose_user_id(default),
- put(xmpp_user_id,Id),
- Id;
- Id ->
- Id
+ undefined ->
+ Id = choose_user_id(default),
+ put(xmpp_user_id,Id),
+ Id;
+ {DefinedUser,DefinedPwd} ->
+ {user_defined, DefinedUser,DefinedPwd} ;
+ Id ->
+ {Id,User,Pwd}
end.
@@ -233,6 +236,12 @@ subst(Req=#jabber{type = Type}, DynData) when Type == 'muc:chat' ; Type == 'muc:
subst(Req=#jabber{type = Type}, DynData) when Type == 'pubsub:create' ; Type == 'pubsub:subscribe'; Type == 'pubsub:publish'; Type == 'pubsub:delete' ->
Req#jabber{node = ts_search:subst(Req#jabber.node, DynData)};
+subst(Req=#jabber{id=user_defined, username=Name,passwd=Pwd}, DynData) ->
+ NewUser=ts_search:subst(Name,DynData),
+ NewPwd=ts_search:subst(Pwd,DynData),
+ put(xmpp_user_id,{NewUser,NewPwd}),% we need to keep the substitution for futures requests
+ Req#jabber{username=NewUser,passwd=NewPwd};
+
subst(Req=#jabber{data=Data}, DynData) ->
Req#jabber{data=ts_search:subst(Data,DynData)}.
@@ -39,33 +39,36 @@
%% Returns: binary
%% Purpose: Build a message/request from a #jabber record
%%----------------------------------------------------------------------
+get_message(Jabber=#jabber{id=user_defined, username=User,passwd=Pwd,type = 'connect'}) ->
+ ts_user_server:add_to_connected({User,Pwd}),
+ connect(Jabber);
get_message(Jabber=#jabber{type = 'connect'}) ->
connect(Jabber);
-get_message(#jabber{type = 'close', id=Id,user_server=UserServer}) ->
- ts_user_server:remove_connected(UserServer,Id),
+get_message(#jabber{type = 'close', id=Id,username=User,passwd=Pwd,user_server=UserServer}) ->
+ ts_user_server:remove_connected(UserServer,set_id(Id,User,Pwd)),
close();
get_message(#jabber{type = 'presence'}) ->
presence();
-get_message(#jabber{type = 'presence:initial', id=Id,user_server=UserServer}) ->
- ts_user_server:add_to_online(UserServer,Id),
+get_message(#jabber{type = 'presence:initial', id=Id,username=User,passwd=Pwd,user_server=UserServer}) ->
+ ts_user_server:add_to_online(UserServer,set_id(Id,User,Pwd)),
presence();
-get_message(#jabber{type = 'presence:final', id=Id,user_server=UserServer}) ->
- ts_user_server:remove_from_online(UserServer,Id),
+get_message(#jabber{type = 'presence:final', id=Id,username=User,passwd=Pwd,user_server=UserServer}) ->
+ ts_user_server:remove_from_online(UserServer,set_id(Id,User,Pwd)),
presence(unavailable);
get_message(#jabber{type = 'presence:broadcast', show=Show, status=Status}) ->
presence(broadcast, Show, Status);
-get_message(Jabber=#jabber{type = 'presence:directed', id=Id,
+get_message(Jabber=#jabber{type = 'presence:directed', id=Id,username=User,passwd=Pwd,
show=Show, status=Status,user_server=UserServer}) ->
- case ts_user_server:get_online(UserServer,Id) of
+ case ts_user_server:get_online(UserServer,set_id(Id,User,Pwd)) of
{ok, Dest} ->
presence(directed, Dest, Jabber, Show, Status);
{error, no_online} ->
ts_mon:add({ count, error_no_online }),
<< >>
end;
-get_message(Jabber=#jabber{id=Id}) when is_integer(Id)->
- get_message(Jabber#jabber{id=id_to_string(Id)});
+
+
get_message(Jabber=#jabber{dest=previous}) ->
Dest = get(previous),
get_message(Jabber#jabber{dest=Dest});
@@ -79,9 +82,9 @@ get_message(#jabber{type = 'presence:subscribe'}) -> %% must be called AFTER iq:
RosterJid ->
presence(subscribe, RosterJid)
end;
-get_message(Jabber=#jabber{type = 'chat', id=Id, dest=online,
+get_message(Jabber=#jabber{type = 'chat', id=Id, dest=online,username=User,passwd=Pwd,
domain=Domain,user_server=UserServer})->
- case ts_user_server:get_online(UserServer,Id) of
+ case ts_user_server:get_online(UserServer,set_id(Id,User,Pwd)) of
{ok, Dest} ->
message(Dest, Jabber, Domain);
{error, no_online} ->
@@ -104,12 +107,16 @@ get_message(Jabber=#jabber{type = 'chat', dest=random, domain=Domain,user_server
get_message(Jabber=#jabber{type = 'chat', dest=unique, domain=Domain,user_server=UserServer})->
{Dest, _} = ts_user_server:get_first(UserServer),
message(Dest, Jabber, Domain);
+get_message(Jabber=#jabber{type = 'chat', id=_Id, dest = undefined, domain=Domain}) ->
+ %% this can happen if previous is set but undefined, skip
+ ts_mon:add({ count, error_no_previous }),
+ << >>;
get_message(Jabber=#jabber{type = 'chat', id=_Id, dest = Dest, domain=Domain}) ->
?DebugF("~w -> ~w ~n", [_Id, Dest]),
message(Dest, Jabber, Domain);
-get_message(#jabber{type = 'iq:roster:add', id=Id, dest = online, username=User,
+get_message(#jabber{type = 'iq:roster:add', id=Id, dest = online, username=User,passwd=Pwd,
domain=Domain, group=Group,user_server=UserServer}) ->
- case ts_user_server:get_online(UserServer,Id) of
+ case ts_user_server:get_online(UserServer,set_id(Id,User,Pwd)) of
{ok, Dest} ->
request(roster_add, User, Domain, Dest, Group);
{error, no_online} ->
@@ -157,8 +164,9 @@ get_message(#jabber{type = 'pubsub:create', id=Id, username=User,
%% For node subscription, data contain the pubsub nodename (relative to user
%% hierarchy or absolute)
get_message(#jabber{type = 'pubsub:subscribe', id=Id, username=User, user_server=UserServer,
+ passwd=Pwd,
dest=online, node=Node, pubsub_service = PubSubComponent, domain = Domain}) ->
- case ts_user_server:get_online(UserServer,Id) of
+ case ts_user_server:get_online(UserServer,set_id(Id,User,Pwd)) of
{ok, Dest} ->
UserFrom = username(User,Id),
UserTo = username(User, id_to_string(Dest)),
@@ -208,6 +216,8 @@ get_message(#jabber{type = 'muc:nick', room = Room, muc_service = Service, nick
get_message(#jabber{type = 'muc:exit', room = Room, muc_service = Service, nick = Nick}) ->
muc_exit(Room, Nick, Service);
+get_message(Jabber=#jabber{id=user_defined}) ->
+ get_message2(Jabber);
get_message(Jabber=#jabber{username=Name, passwd=Passwd, id=Id}) ->
FullName = username(Name, Id),
FullPasswd = password(Passwd,Id),
@@ -386,7 +396,7 @@ auth_sasl_session(#jabber{username=Name,passwd=Passwd,domain=Domain})->
%%----------------------------------------------------------------------
%% Func: auth_sasl_session/3
%%----------------------------------------------------------------------
-auth_sasl_session(Username, _Passwd, Domain) ->
+auth_sasl_session(Username, _Passwd, _Domain) ->
list_to_binary(["<iq type='set' id='",ts_msg_server:get_id(list),
"' ><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>"]).
@@ -401,8 +411,6 @@ registration(#jabber{username=Name,passwd=Passwd})->
%% Func: message/3
%% Purpose: send message to defined user at the Service (aim, ...)
%%----------------------------------------------------------------------
-message(Dest, Jabber, Service) when is_integer(Dest) ->
- message(id_to_string(Dest),Jabber, Service);
message(Dest, #jabber{size=Size,data=undefined, username=User}, Service) when is_integer(Size) ->
put(previous, Dest),
Username = username(User,Dest),
@@ -489,8 +497,6 @@ request(roster_remove, RosterJid) ->
%%----------------------------------------------------------------------
%% Func: request/5
%%----------------------------------------------------------------------
-request(roster_add, UserName, Domain, Id, Group) when is_integer(Id)->
- request(roster_add, UserName, Domain, integer_to_list(Id), Group);
request(roster_add, UserName, Domain, Id, Group)->
Name = username(UserName,Id),
RosterJid = Name ++ "@" ++ Domain,
@@ -600,16 +606,27 @@ muc_exit(Room,Nick, Service) ->
%%% Func: username/2
%%% Generate the username given a prefix and id
%%%----------------------------------------------------------------------
-username(Prefix, Id) ->
- Prefix ++ Id.
+username(Prefix, DestId) when is_integer(DestId)->
+ Prefix ++ integer_to_list(DestId);
+username(_Prefix, DestUser) ->
+ DestUser.
+
%%% Convert Id to string
%%% Change this if you want to have padding
-id_to_string(Id) ->
- integer_to_list(Id).
+id_to_string(Id) when is_integer(Id)->
+ integer_to_list(Id);
+id_to_string(Id) -> Id.
%%%----------------------------------------------------------------------
%%% Func: password/1
%%% Generate password for a given username
%%%----------------------------------------------------------------------
password(Prefix,Id) ->
Prefix ++ Id.
+
+%% set the real Id; by default use the Id; but it user and passwd is
+%% defined statically (using csv for example), Id is the tuple { User, Passwd }
+set_id(user_defined,User,Passwd) ->
+ {User,Passwd};
+set_id(Id,_User,_Passwd) ->
+ Id.
@@ -90,11 +90,27 @@ parse_config(Element = #xmlElement{name=jabber},
%% The specified string
Domain =ts_config:get_default(Tab, jabber_domain_name, jabber_domain),
- UserName=ts_config:get_default(Tab, jabber_username, jabber_username),
- Passwd =ts_config:get_default(Tab, jabber_passwd, jabber_passwd),
MUC_service = ts_config:get_default(Tab, muc_service, muc_service),
PubSub_service =ts_config:get_default(Tab, pubsub_service, pubsub_service),
+
+ %% Authentication
+ {XMPPId, UserName, Passwd} = case lists:keysearch(xmpp_authenticate, #xmlElement.name,
+ Element#xmlElement.content) of
+ {value, AuthEl=#xmlElement{} } ->
+ User= ts_config:getAttr(string,AuthEl#xmlElement.attributes,
+ username, undefined),
+ PWD= ts_config:getAttr(string,AuthEl#xmlElement.attributes,
+ passwd, undefined),
+ {user_defined,User,PWD};
+ _ ->
+ GUserName=ts_config:get_default(Tab, jabber_username, jabber_username),
+ GPasswd =ts_config:get_default(Tab, jabber_passwd, jabber_passwd),
+ {0,GUserName,GPasswd}
+ end,
+
+
+
Msg=#ts_request{ack = Ack,
dynvar_specs= DynVar,
endpage = true,
@@ -103,6 +119,7 @@ parse_config(Element = #xmlElement{name=jabber},
param = #jabber{domain = Domain,
username = UserName,
passwd = Passwd,
+ id = XMPPId,
data = Data,
type = Type,
dest = Dest,
Oops, something went wrong.

0 comments on commit 7a8063f

Please sign in to comment.