static gboolean do_conv_deliver_msg()

in prpl/conversations.c [38:126]


static gboolean do_conv_deliver_msg(ChimeConnection *cxn, struct chime_im *im,
				    JsonNode *record, time_t msg_time, gboolean new_msg)
{
	const gchar *sender, *message;
	gint64 sys;
	if (!parse_string(record, "Sender", &sender) ||
	    !parse_int(record, "IsSystemMessage", &sys))
		return FALSE;

	PurpleMessageFlags flags = 0;
	if (sys)
		flags |= PURPLE_MESSAGE_SYSTEM;
	if (!new_msg)
		flags |= PURPLE_MESSAGE_DELAYED;

	const gchar *email = chime_contact_get_email(im->peer);
	const gchar *from = _("Unknown sender");
	if (!strcmp(sender, chime_connection_get_profile_id(cxn))) {
		from = chime_connection_get_email(cxn);
	} else {
		ChimeContact *who = chime_connection_contact_by_id(cxn, sender);
		if (who)
			from = chime_contact_get_email(who);
	}

	ChimeAttachment *att = extract_attachment(record);
	if (att) {
		AttachmentContext *ctx = g_new(AttachmentContext, 1);
		ctx->conn = im->m.conn;
		ctx->chat_id = -1;
		ctx->from = from;
		ctx->im_email = 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);
	}

	// Download messages don't have 'content' but normal messages do.
	// if you receive one, parse it:
	if (parse_string(record, "Content", &message)) {
		gchar *escaped = g_markup_escape_text(message, -1);

		/* Process markdown */
		if (g_str_has_prefix(escaped, "/md") && (escaped[3] == ' ' || escaped[3] == '\n')) {
			gchar *processed;
			if (!do_markdown(escaped + 4, &processed)) {
				g_free(escaped);
				escaped = processed;
			}
		}

		if (!strcmp(sender, chime_connection_get_profile_id(cxn))) {
			/* Ick, how do we inject a message from ourselves? */
			PurpleAccount *account = im->m.conn->account;
			PurpleConversation *pconv = purple_find_conversation_with_account(
					PURPLE_CONV_TYPE_IM, email, account);
			if (!pconv) {
				pconv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account,
												email);
				if (!pconv) {
					purple_debug_error("chime", "NO CONV FOR %s\n", email);
					g_free(escaped);
					return FALSE;
				}
			}
			purple_conversation_write(pconv, NULL, escaped,
					flags | PURPLE_MESSAGE_SEND, msg_time);
			purple_signal_emit(purple_connection_get_prpl(account->gc),
					"chime-got-convmsg", pconv, TRUE, record);
		} else {
			serv_got_im(im->m.conn, email, escaped, flags | PURPLE_MESSAGE_RECV,
						msg_time);

			/* 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. */
			PurpleConversation *pconv = purple_find_conversation_with_account(
					PURPLE_CONV_TYPE_IM, email, im->m.conn->account);
			if (pconv) {
				purple_conversation_update(pconv, PURPLE_CONV_UPDATE_UNSEEN);
				purple_signal_emit(purple_connection_get_prpl(im->m.conn),
								   "chime-got-convmsg", pconv, FALSE, record);
			}

		}
		g_free(escaped);
	}
	return TRUE;
}