Index: src/mod_log_chat.erl
===================================================================
--- src/mod_log_chat.erl	(revisión: 1114)
+++ src/mod_log_chat.erl	(copia de trabajo)
@@ -16,7 +16,7 @@
 	 log_packet_send/3,
 	 log_packet_receive/4]).
 
-%-define(ejabberd_debug, true).
+%%-define(ejabberd_debug, true).
 
 -include("ejabberd.hrl").
 -include("jlib.hrl").
@@ -75,14 +75,14 @@
     ok.
 
 log_packet_send(From, To, Packet) ->
-    log_packet(From, To, Packet, From#jid.lserver).
+    log_packet(From, To, Packet, From#jid.lserver, out).
 
 log_packet_receive(_JID, From, To, _Packet) when From#jid.lserver == To#jid.lserver->
     ok; % only log at send time if the message is local to the server
 log_packet_receive(_JID, From, To, Packet) ->
-    log_packet(From, To, Packet, To#jid.lserver).
+    log_packet(From, To, Packet, To#jid.lserver, in).
 
-log_packet(From, To, Packet = {xmlelement, "message", Attrs, _Els}, Host) ->
+log_packet(From, To, Packet = {xmlelement, "message", Attrs, _Els}, Host, Direction) ->
     case xml:get_attr_s("type", Attrs) of
 	"groupchat" -> %% mod_muc_log already does it
 	    ?DEBUG("dropping groupchat: ~s", [xml:element_to_string(Packet)]),
@@ -91,17 +91,17 @@
 	    ?DEBUG("dropping error: ~s", [xml:element_to_string(Packet)]),
 	    ok;
 	_ ->
-	    write_packet(From, To, Packet, Host)
+	    write_packet(From, To, Packet, Host, Direction)
     end;
-log_packet(_From, _To, _Packet, _Host) ->
+log_packet(_From, _To, _Packet, _Host, _Direction) ->
     ok.
 
-write_packet(From, To, Packet, Host) ->
+write_packet(From, To, Packet, Host, Direction) ->
     gen_mod:get_module_proc(Host, ?PROCNAME) ! {call, self(), get_config},
     Config = receive
-	       {config, Result} ->
-		   Result
-	   end,
+		 {config, Result} ->
+		     Result
+	     end,
     Format = Config#config.format,
     {Subject, Body} = {case xml:get_path_s(Packet, [{elem, "subject"}, cdata]) of
 			   false ->
@@ -110,35 +110,124 @@
 			       escape(Format, Text)
 		       end,
 		       escape(Format, xml:get_path_s(Packet, [{elem, "body"}, cdata]))},
+
     case Subject ++ Body of
         "" -> %% don't log empty messages
             ?DEBUG("not logging empty message from ~s",[jlib:jid_to_string(From)]),
             ok;
         _ ->
-	    Path = Config#config.path,
-	    FromJid = From#jid.luser++"@"++From#jid.lserver,
-	    ToJid = To#jid.luser++"@"++To#jid.lserver,
-	    {FilenameTemplate, DateString, Header, MessageType} =
-		case calendar:local_time() of
-		    {{Y, M, D}, {H, Min, S}} ->
-			SortedJid = lists:sort([FromJid, ToJid]),
-			Title = io_lib:format(template(Format, title), [FromJid, ToJid, Y, M, D]),
-			{lists:flatten(io_lib:format("~s/~~p-~~2.2.0w-~~2.2.0w ~s - ~s~s",
-				       [Path | SortedJid]++[template(Format, extension)])),
-			 io_lib:format(template(Format, date), [Y, M, D, H, Min, S]),
+	    FromPart = From#jid.luser++"@"++From#jid.lserver,
+	    ToPart = To#jid.luser++"@"++To#jid.lserver,
+	    Participants = lists:sort( [FromPart, ToPart] ),
+	    PartFlat = lists:concat(Participants),
+	    TS = get_md5(PartFlat),
 
+	    ?DEBUG("MD5: (~s)",[TS]),
+	    ?DEBUG("PartFlat: (~s)",[PartFlat]),
+
+	    LocalUsername = case Direction of
+				in -> To#jid.luser;
+				out -> From#jid.luser
+			    end,
+	    LocalPath = Config#config.path++"/"++LocalUsername++"@"++Host++"/new",
+	    LocalFromJid = From#jid.luser++"@"++From#jid.lserver,
+	    LocalToJid = To#jid.luser++"@"++To#jid.lserver,
+	    LocalUsernameHost = LocalUsername++"@"++Host,
+
+	    if
+		LocalFromJid == LocalUsernameHost ->
+		    LocalReplyToUsername = To#jid.luser++"@"++To#jid.lserver;
+		LocalToJid == LocalUsernameHost ->
+		    LocalReplyToUsername = From#jid.luser++"@"++From#jid.lserver
+	    end,
+
+	    {LocalFilenameTemplate, LocalDateString, LocalHeader, LocalMessageType} =
+		case calendar:universal_time() of
+		    {{LocalY, LocalM, LocalD}, {LocalH, LocalMin, LocalS}} ->
+			LocalSortedJid = lists:sort([LocalFromJid, LocalToJid]),
+			LocalCurrTime = erlang:localtime(),
+			LocalEmailDate = httpd_util:rfc1123_date(LocalCurrTime),
+			?DEBUG("Date: (~s)",[LocalEmailDate]),
+			LocalTitle = io_lib:format(template(Format, title), [LocalReplyToUsername, LocalReplyToUsername,
+									     LocalEmailDate, LocalFromJid, LocalToJid,
+									     LocalReplyToUsername, LocalY, LocalM, LocalD]),
+			{lists:flatten(io_lib:format("~s/1~~p-~~2.2.0w-~~2.2.0w-~s-~s-~s~s",
+						     [LocalPath | LocalSortedJid]++[TS]++[template(Format, extension)])),
+			 io_lib:format(template(Format, date), [LocalY, LocalM, LocalD, LocalH, LocalMin, LocalS]),
 			 io_lib:format(template(Format, header),
 				       lists:duplicate(count(template(Format, header), "~s"),
-						       Title)
+						       LocalTitle)
 				      ),
-			 case hd(SortedJid) of
-			     FromJid ->
+			 case hd(LocalSortedJid) of
+			     LocalFromJid ->
 				 message1;
-			     ToJid ->
+			     LocalToJid ->
 				 message2
 			 end
 			}
 		end,
+	    ?DEBUG("FilenameTemplate ~p~n",[LocalFilenameTemplate]),
+	    LocalFilename = io_lib:format(LocalFilenameTemplate, [LocalY, LocalM, LocalD]),
+	    ?DEBUG("logging message from ~s into ~s~n",[jlib:jid_to_string(From), LocalFilename]),
+	    LocalFile = case file:read_file_info(LocalFilename) of
+			    {ok, _} ->
+				open_logfile(LocalFilename);
+			    {error, enoent} ->
+				close_previous_logfile(LocalFilenameTemplate, Format, {LocalY, LocalM, LocalD}),
+				LocalNewFile = open_logfile(LocalFilename),
+				io:format(LocalNewFile, LocalHeader, []),
+				LocalNewFile
+			end,
+	    LocalMessageText = case Subject of
+				   "" ->
+				       Body;
+				   _ ->
+				       io_lib:format(template(Format, subject), [Subject])++Body
+			       end,
+	    ?DEBUG("MessageTemplate ~s~n",[template(Format, LocalMessageType)]),
+	    io:format(LocalFile, lists:flatten(template(Format, LocalMessageType)),
+		      [LocalDateString, LocalFromJid, LocalMessageText]),
+	    file:close(LocalFile),
+
+	    Username = case Direction of
+			   out -> To#jid.luser;
+			   in -> From#jid.luser
+		       end,
+	    Path = Config#config.path++"/"++Username++"@"++Host++"/new",
+	    FromJid = From#jid.luser++"@"++From#jid.lserver,
+	    ToJid = To#jid.luser++"@"++To#jid.lserver,
+	    UsernameHost = Username++"@"++Host,
+	    if
+		FromJid == UsernameHost ->
+		    ReplyToUsername = To#jid.luser++"@"++To#jid.lserver;
+		ToJid == UsernameHost ->
+		    ReplyToUsername = From#jid.luser++"@"++From#jid.lserver
+	    end,
+	    {FilenameTemplate, DateString, Header, MessageType} =
+		case calendar:universal_time() of
+                    {{Y, M, D}, {H, Min, S}} ->
+                        SortedJid = lists:sort([FromJid, ToJid]),
+			CurrTime = erlang:localtime(),
+			EmailDate = httpd_util:rfc1123_date(CurrTime),
+		        ?DEBUG("Date: (~s)",[EmailDate]),
+
+			Title = io_lib:format(template(Format, title), [ReplyToUsername, ReplyToUsername, EmailDate, FromJid, ToJid, ReplyToUsername, Y, M, D]),
+ 			{lists:flatten(io_lib:format("~s/1~~p-~~2.2.0w-~~2.2.0w-~s-~s-~s~s",
+						     [Path | SortedJid]++[TS]++[template(Format, extension)])),
+                         io_lib:format(template(Format, date), [Y, M, D, H, Min, S]),
+
+                         io_lib:format(template(Format, header),
+                                       lists:duplicate(count(template(Format, header), "~s"),
+                                                       Title)
+                                      ),
+                         case hd(SortedJid) of
+                             FromJid ->
+                                 message1;
+                             ToJid ->
+                                 message2
+                         end
+                        }
+                end,
 	    ?DEBUG("FilenameTemplate ~p~n",[FilenameTemplate]),
 	    Filename = io_lib:format(FilenameTemplate, [Y, M, D]),
 	    ?DEBUG("logging message from ~s into ~s~n",[jlib:jid_to_string(From), Filename]),
@@ -152,23 +241,28 @@
 			   NewFile
 		   end,
 	    MessageText = case Subject of
-		       "" ->
-			   Body;
-		       _ ->
-			   io_lib:format(template(Format, subject), [Subject])++Body
-		   end,
+			      "" ->
+				  Body;
+			      _ ->
+				  io_lib:format(template(Format, subject), [Subject])++Body
+			  end,
 	    ?DEBUG("MessageTemplate ~s~n",[template(Format, MessageType)]),
-	    io:format(File, lists:flatten(template(Format, MessageType)), [DateString, FromJid, From#jid.lresource, ToJid,
-							    To#jid.lresource, MessageText]),
+	    io:format(File, lists:flatten(template(Format, MessageType)), [DateString, FromJid, MessageText]),
 	    file:close(File)
     end.
 
+get_md5(String) ->
+    lists:flatten([io_lib:format("~.16B", [X])
+                   || X <- binary_to_list(crypto:md5(String))]).
+
 open_logfile(Filename) ->
     case file:open(Filename, [append]) of
 	{ok, File} ->
 	    File;
 	{error, Reason} ->
-	    ?ERROR_MSG("Cannot write into file ~s: ~p~n", [Filename, Reason])
+	    os:cmd("mkdir -p "++filename:dirname(Filename)), %% This doesn't work on Windows
+	    ?ERROR_MSG("Cannot write into file ~s: ~p~n", [Filename, Reason]),
+	    open_logfile(Filename) %% This loop may create an endless loop!!! TODO +++
     end.
 
 close_previous_logfile(FilenameTemplate, Format, Date) ->
@@ -195,7 +289,7 @@
     [Char | escape(html, Text)].
 
 
-% return the number of occurence of Word in String
+						% return the number of occurence of Word in String
 count(String, Word) ->
     case string:str(String, Word) of
 	0 ->
@@ -209,21 +303,21 @@
 template(text, extension) ->
     ".log";
 template(text, title) ->
-    "Messages log between ~s and ~s on ~p-~2.2.0w-~2.2.0w";
+    "Return-Path: <~s>~nReply-To: <~s>~nDate: ~s~nFrom: <~s>~nTo: <~s>~nSubject: Chat with ~s on ~p-~2.2.0w-~2.2.0w ~nContent-Type: text/plain; charset=UTF-8; format=flowed ~nContent-Transfer-Encoding: 8bit ~nUser-Agent: ejabberd/mod_log_chat_maildir";
 template(text, header) ->
-    "~s~n-----------------------------------------------------------------------~n";
+    "~s~n~n";
 template(text, subject) ->
-    "Subject: ~s~n";
+    "(~s) ~n";
 template(text, message) ->
-    "~~s ~~s/~~s -> ~~s/~~s~n~s~~s~n";
+    "(~~s UTC) ~~s: ~s~~s~n";
 template(text, message1) ->
-    io_lib:format(template(text, message), ["> "]);
+    io_lib:format(template(text, message), [""]);
 template(text, message2) ->
-    io_lib:format(template(text, message), ["< "]);
+    io_lib:format(template(text, message), [""]);
 template(text, date) ->
     "~p-~2.2.0w-~2.2.0w ~2.2.0w:~2.2.0w:~2.2.0w";
 template(text, footer) ->
-    "---- End ----~n";
+    "~n";
 
 template(html, extension) ->
     ".html";
@@ -237,7 +331,7 @@
 template(html, subject) ->
     "<div class=\"subject\"><span>Subject:</span> ~s</div>";
 template(html, message) ->
-    "<div class=\"message~w\"><span class=\"date\">~~s</span> <span class=\"jid\">~~s</span>/<span class=\"ressource\">~~s</span> -&gt; <span class=\"jid\">~~s</span>/<span class=\"ressource\">~~s</span>~~n<span class=\"messagetext\">~~s</span></div>~~n";
+    "<div class=\"message~w\"><span class=\"date\">~~s</span> <span class=\"jid\">~~s</span>: <span class=\"messagetext\">~~s</span></div>~~n";
 template(html, message1) ->
     io_lib:format(template(html, message), [1]);
 template(html, message2) ->
