static void do_chat_deliver_msg()

in prpl/chat.c [142:212]


static void do_chat_deliver_msg(ChimeConnection *cxn, struct chime_msgs *msgs,
				JsonNode *node, time_t msg_time, gboolean new_msg)
{
	struct chime_chat *chat = (struct chime_chat *)msgs;
	PurpleConnection *conn = chat->conv->account->gc;
	struct purple_chime *pc = purple_connection_get_protocol_data(conn);
	int id = purple_conv_chat_get_id(PURPLE_CONV_CHAT(chat->conv));
	const gchar *content, *sender;

	if (!parse_string(node, "Sender", &sender))
		return;

	const gchar *from = _("Unknown sender");
	int msg_flags;

	if (!strcmp(sender, chime_connection_get_profile_id(cxn))) {
		from = chime_connection_get_email(cxn);
		msg_flags = PURPLE_MESSAGE_SEND;
	} else {
		ChimeContact *who = chime_connection_contact_by_id(cxn, sender);
		if (who)
			from = chime_contact_get_email(who);
		msg_flags = PURPLE_MESSAGE_RECV;
	}

	ChimeAttachment *att = extract_attachment(node);
	if (att) {
		AttachmentContext *ctx = g_new(AttachmentContext, 1);
		ctx->conn = conn;
		ctx->chat_id = id;
		ctx->from = from;
		ctx->im_email = "";
		ctx->when = msg_time;
		/* The attachment and context structs will be owned by the code doing the download and will be disposed of at the end. */
		download_attachment(cxn, att, ctx);
	}

	/* If the message is over a day old, don't beep for it. */
	if (!new_msg)
		msg_flags |= PURPLE_MESSAGE_DELAYED;

	if (parse_string(node, "Content", &content)) {
		gchar *escaped = g_markup_escape_text(content, -1);

		gchar *parsed = NULL;
		if (CHIME_IS_ROOM(chat->m.obj)) {
			if (parse_inbound_mentions(cxn, pc->mention_regex, escaped, &parsed)
					&& (msg_flags & PURPLE_MESSAGE_RECV)) {
				// Presumably this will trigger a notification.
				msg_flags |= PURPLE_MESSAGE_NICK;
			}
			g_free(escaped);
		} else
			parsed = escaped;

		/* Process markdown */
		if (g_str_has_prefix(parsed, "/md") && (parsed[3] == ' ' || parsed[3] == '\n')) {
			gchar *processed;
			if (!do_markdown(parsed + 4, &processed)) {
				g_free(parsed);
				parsed = processed;
			}
		}
		serv_got_chat_in(conn, id, from, msg_flags, parsed, msg_time);
		g_free(parsed);
	}
	/* If the conversation already had focus and unseen-count didn't change, fake
	   a PURPLE_CONV_UPDATE_UNSEEN notification anyway, so that we see that it's
	   (still) zero and tell the server it's read. */
	purple_conversation_update(chat->conv, PURPLE_CONV_UPDATE_UNSEEN);
}