From 21060816f2b61447d89d60312b8abebef7dc0a92 Mon Sep 17 00:00:00 2001
From: Arnaud Fontaine <arnau@debian.org>
Date: Mon, 3 Oct 2011 13:10:35 +0900
Subject: [PATCH] Write received PRIVMSG to the sender log rather than the
 server nick log.

Before, when receiving a PRIVMSG from bar with the server nick set to
foo, the message was written to foo.log rather than bar.log, thus
queries were written into foo.log and bar.log, which makes the
conversation unreadable.
---
 src/irc.c |   21 ++++++++++++++-------
 1 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/irc.c b/src/irc.c
index edcb96e..6c074b7 100644
--- a/src/irc.c
+++ b/src/irc.c
@@ -1823,6 +1823,7 @@ static int irc_kick(struct link_server *server, struct line *line)
 }
 
 static void irc_privmsg_check_ctcp(struct link_server *server,
+				   const char *nick,
 				   struct line *line)
 {
 	if (irc_line_count(line) != 3)
@@ -1831,23 +1832,29 @@ static void irc_privmsg_check_ctcp(struct link_server *server,
 	if (!line->origin)
 		return;
 
-	char *nick;
-	nick = nick_from_ircmask(line->origin);
 	if (irc_line_elem_equals(line, 2, "\001VERSION\001")) {
 		WRITE_LINE2(CONN(server), NULL, "NOTICE", nick,
 				"\001VERSION bip-" PACKAGE_VERSION "\001");
 	}
-	free(nick);
 }
 
 static int irc_privmsg(struct link_server *server, struct line *line)
 {
 	if (!irc_line_includes(line, 2))
 		return ERR_PROTOCOL;
-	if (LINK(server)->s_state == IRCS_CONNECTED)
-		log_privmsg(LINK(server)->log, line->origin,
-				irc_line_elem(line, 1), irc_line_elem(line, 2));
-	irc_privmsg_check_ctcp(server, line);
+	char *nick = nick_from_ircmask(line->origin);
+	if (LINK(server)->s_state == IRCS_CONNECTED) {
+		const char *origin = line->origin;
+		const char *destination = irc_line_elem(line, 1);
+		if (strcmp(server->nick, destination) == 0) {
+			origin = nick;
+			destination = nick;
+		}
+		log_privmsg(LINK(server)->log, origin,
+				destination, irc_line_elem(line, 2));
+	}
+	irc_privmsg_check_ctcp(server, nick, line);
+	free(nick);
 	return OK_COPY;
 }
 
-- 
1.7.6.3


