From be10e61c90d5752123bce1095c20c1925a6037e0 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch <p.janouch@gmail.com>
Date: Mon, 29 Jun 2015 21:39:40 +0200
Subject: degesch: fix /msg with CAP echo-message

---
 degesch.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/degesch.c b/degesch.c
index e8e0ae3..2dd1ebc 100644
--- a/degesch.c
+++ b/degesch.c
@@ -2587,6 +2587,11 @@ log_full (struct app_context *ctx, struct server *s, struct buffer *buffer,
 #define log_outcoming_action(s, buffer, who, text)                             \
 	log_server ((s), (buffer), 0, " #a*#r  #n #m", ATTR_ACTION, (who), (text))
 
+#define log_outcoming_orphan_notice(s, target, text)                           \
+	log_server_status ((s), (s)->buffer, "Notice -> #n: #m", (target), (text))
+#define log_outcoming_orphan_privmsg(s, target, text)                          \
+	log_server_status ((s), (s)->buffer, "MSG(#n): #m", (target), (text))
+
 #define log_ctcp_query(s, target, tag)                                         \
 	log_server_status ((s), (s)->buffer, "CTCP query to #S: #S", target, tag)
 #define log_ctcp_reply(s, target, reply /* freed! */)                          \
@@ -4055,13 +4060,11 @@ irc_get_buffer_for_message (struct server *s,
 		char *nickname = irc_cut_nickname (msg->prefix);
 		if (irc_is_this_us (s, target))
 			buffer = irc_get_or_make_user_buffer (s, nickname);
-		// With the IRCv3.2 echo-message capability, we can receive messages
-		// as they are delivered to the target; in that case, check the origin
-		// FIXME: this breaks /msg with CAP echo-message, we should return
-		//   NULL and the caller should do this check instead and handle it
-		else if (soft_assert (irc_is_this_us (s, nickname)))
-			buffer = irc_get_or_make_user_buffer (s, target);
 		free (nickname);
+
+		// With the IRCv3.2 echo-message capability, we can receive messages
+		// as they are delivered to the target; in that case we return NULL
+		// and the caller should check the origin
 	}
 	return buffer;
 }
@@ -4643,7 +4646,11 @@ irc_handle_notice_text (struct server *s,
 	const char *target = msg->params.vector[0];
 	struct buffer *buffer = irc_get_buffer_for_message (s, msg, target);
 	if (!buffer)
+	{
+		if (irc_is_this_us (s, msg->prefix))
+			log_outcoming_orphan_notice (s, target, text->str);
 		return;
+	}
 
 	char *nick = irc_cut_nickname (msg->prefix);
 	// IRCv3.2 echo-message could otherwise cause us to highlight ourselves
@@ -4807,7 +4814,11 @@ irc_handle_privmsg_text (struct server *s,
 	const char *target = msg->params.vector[0];
 	struct buffer *buffer = irc_get_buffer_for_message (s, msg, target);
 	if (!buffer)
+	{
+		if (irc_is_this_us (s, msg->prefix))
+			log_outcoming_orphan_privmsg (s, target, text->str);
 		return;
+	}
 
 	char *nickname = irc_cut_nickname (msg->prefix);
 	const char *prefixes = irc_get_privmsg_prefix
@@ -4826,7 +4837,7 @@ irc_handle_privmsg_text (struct server *s,
 			" #a*#r  #n #m", ATTR_HIGHLIGHT, msg->prefix, text->str);
 	else
 		log_server (s, buffer, BUFFER_LINE_HIGHLIGHT,
-			"#a<#s#s>#r #m", ATTR_HIGHLIGHT, prefixes, nickname, text->str);
+			"#a<#S#S>#r #m", ATTR_HIGHLIGHT, prefixes, nickname, text->str);
 
 	free (nickname);
 }
@@ -5769,7 +5780,7 @@ log_autosplit_privmsg (struct server *s,
 		log_outcoming_privmsg (s, buffer,
 			prefixes, s->irc_user->nickname, line);
 	else
-		log_server_status (s, s->buffer, "MSG(#n): #m", a->target, line);
+		log_outcoming_orphan_privmsg (s, a->target, line);
 }
 
 #define SEND_AUTOSPLIT_PRIVMSG(s, target, message)                             \
@@ -5783,7 +5794,7 @@ log_autosplit_notice (struct server *s,
 	if (buffer && soft_assert (s->irc_user))
 		log_outcoming_notice (s, buffer, s->irc_user->nickname, line);
 	else
-		log_server_status (s, s->buffer, "Notice -> #n: #m", a->target, line);
+		log_outcoming_orphan_notice (s, a->target, line);
 }
 
 #define SEND_AUTOSPLIT_NOTICE(s, target, message)                              \
-- 
cgit v1.2.3-70-g09d2