plugins_(on|pre) API now maybe returns NULL.

Instead of always returning a `strdup()`'ed version, maybe return NULL and
handle this in the calling code.

This is still not true for `plugins_pre_chat_message_display()`, where we
return the passed `messag` in case there are no plugins, since this would
require a bigger refactor of more parts.

This also
* merges the implementation of `sv_ev_incoming_private_message()` and
  `sv_ev_delayed_private_message()`, since they differed only by a single
  line.
* untangles the `#ifdef` mess in `cl_ev_send_muc_msg_corrected()`.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
This commit is contained in:
Steffen Jaeckel
2025-03-12 10:15:35 +01:00
parent 95c2199ca2
commit 83a4165d92
3 changed files with 112 additions and 97 deletions

View File

@@ -137,9 +137,7 @@ cl_ev_send_msg_correct(ProfChatWin* chatwin, const char* const msg, const char*
gboolean request_receipt = prefs_get_boolean(PREF_RECEIPTS_REQUEST);
auto_char char* plugin_msg = plugins_pre_chat_message_send(chatwin->barejid, msg);
if (plugin_msg == NULL) {
return;
}
const char* const message = plugin_msg ?: msg;
char* replace_id = NULL;
if (correct_last_msg) {
@@ -148,46 +146,46 @@ cl_ev_send_msg_correct(ProfChatWin* chatwin, const char* const msg, const char*
if (chatwin->is_omemo) {
#ifdef HAVE_OMEMO
auto_char char* id = omemo_on_message_send((ProfWin*)chatwin, plugin_msg, request_receipt, FALSE, replace_id);
auto_char char* id = omemo_on_message_send((ProfWin*)chatwin, message, request_receipt, FALSE, replace_id);
if (id != NULL) {
chat_log_omemo_msg_out(chatwin->barejid, plugin_msg, NULL);
log_database_add_outgoing_chat(id, chatwin->barejid, plugin_msg, replace_id, PROF_MSG_ENC_OMEMO);
chatwin_outgoing_msg(chatwin, plugin_msg, id, PROF_MSG_ENC_OMEMO, request_receipt, replace_id);
chat_log_omemo_msg_out(chatwin->barejid, message, NULL);
log_database_add_outgoing_chat(id, chatwin->barejid, message, replace_id, PROF_MSG_ENC_OMEMO);
chatwin_outgoing_msg(chatwin, message, id, PROF_MSG_ENC_OMEMO, request_receipt, replace_id);
}
#endif
} else if (chatwin->is_ox) {
#ifdef HAVE_LIBGPGME
// XEP-0373: OpenPGP for XMPP
auto_char char* id = message_send_chat_ox(chatwin->barejid, plugin_msg, request_receipt, replace_id);
auto_char char* id = message_send_chat_ox(chatwin->barejid, message, request_receipt, replace_id);
if (id != NULL) {
chat_log_pgp_msg_out(chatwin->barejid, plugin_msg, NULL);
log_database_add_outgoing_chat(id, chatwin->barejid, plugin_msg, replace_id, PROF_MSG_ENC_OX);
chatwin_outgoing_msg(chatwin, plugin_msg, id, PROF_MSG_ENC_OX, request_receipt, replace_id);
chat_log_pgp_msg_out(chatwin->barejid, message, NULL);
log_database_add_outgoing_chat(id, chatwin->barejid, message, replace_id, PROF_MSG_ENC_OX);
chatwin_outgoing_msg(chatwin, message, id, PROF_MSG_ENC_OX, request_receipt, replace_id);
}
#endif
} else if (chatwin->pgp_send) {
#ifdef HAVE_LIBGPGME
auto_char char* id = message_send_chat_pgp(chatwin->barejid, plugin_msg, request_receipt, replace_id);
auto_char char* id = message_send_chat_pgp(chatwin->barejid, message, request_receipt, replace_id);
if (id != NULL) {
chat_log_pgp_msg_out(chatwin->barejid, plugin_msg, NULL);
log_database_add_outgoing_chat(id, chatwin->barejid, plugin_msg, replace_id, PROF_MSG_ENC_PGP);
chatwin_outgoing_msg(chatwin, plugin_msg, id, PROF_MSG_ENC_PGP, request_receipt, replace_id);
chat_log_pgp_msg_out(chatwin->barejid, message, NULL);
log_database_add_outgoing_chat(id, chatwin->barejid, message, replace_id, PROF_MSG_ENC_PGP);
chatwin_outgoing_msg(chatwin, message, id, PROF_MSG_ENC_PGP, request_receipt, replace_id);
}
#endif
} else {
gboolean handled = FALSE;
#ifdef HAVE_LIBOTR
handled = otr_on_message_send(chatwin, plugin_msg, request_receipt, replace_id);
handled = otr_on_message_send(chatwin, message, request_receipt, replace_id);
#endif
if (!handled) {
auto_char char* id = message_send_chat(chatwin->barejid, plugin_msg, oob_url, request_receipt, replace_id);
chat_log_msg_out(chatwin->barejid, plugin_msg, NULL);
log_database_add_outgoing_chat(id, chatwin->barejid, plugin_msg, replace_id, PROF_MSG_ENC_NONE);
chatwin_outgoing_msg(chatwin, plugin_msg, id, PROF_MSG_ENC_NONE, request_receipt, replace_id);
auto_char char* id = message_send_chat(chatwin->barejid, message, oob_url, request_receipt, replace_id);
chat_log_msg_out(chatwin->barejid, message, NULL);
log_database_add_outgoing_chat(id, chatwin->barejid, message, replace_id, PROF_MSG_ENC_NONE);
chatwin_outgoing_msg(chatwin, message, id, PROF_MSG_ENC_NONE, request_receipt, replace_id);
}
}
plugins_post_chat_message_send(chatwin->barejid, plugin_msg);
plugins_post_chat_message_send(chatwin->barejid, message);
return;
}
@@ -201,9 +199,7 @@ void
cl_ev_send_muc_msg_corrected(ProfMucWin* mucwin, const char* const msg, const char* const oob_url, gboolean correct_last_msg)
{
auto_char char* plugin_msg = plugins_pre_room_message_send(mucwin->roomjid, msg);
if (plugin_msg == NULL) {
return;
}
const char* const message = plugin_msg ?: msg;
char* replace_id = NULL;
if (correct_last_msg) {
@@ -212,30 +208,21 @@ cl_ev_send_muc_msg_corrected(ProfMucWin* mucwin, const char* const msg, const ch
#ifdef HAVE_OMEMO
if (mucwin->is_omemo) {
auto_char char* id = omemo_on_message_send((ProfWin*)mucwin, plugin_msg, FALSE, TRUE, replace_id);
groupchat_log_omemo_msg_out(mucwin->roomjid, plugin_msg);
log_database_add_outgoing_muc(id, mucwin->roomjid, plugin_msg, replace_id, PROF_MSG_ENC_OMEMO);
mucwin_outgoing_msg(mucwin, plugin_msg, id, PROF_MSG_ENC_OMEMO, replace_id);
} else {
auto_char char* id = message_send_groupchat(mucwin->roomjid, plugin_msg, oob_url, replace_id);
groupchat_log_msg_out(mucwin->roomjid, plugin_msg);
log_database_add_outgoing_muc(id, mucwin->roomjid, plugin_msg, replace_id, PROF_MSG_ENC_NONE);
mucwin_outgoing_msg(mucwin, plugin_msg, id, PROF_MSG_ENC_NONE, replace_id);
auto_char char* id = omemo_on_message_send((ProfWin*)mucwin, message, FALSE, TRUE, replace_id);
groupchat_log_omemo_msg_out(mucwin->roomjid, message);
log_database_add_outgoing_muc(id, mucwin->roomjid, message, replace_id, PROF_MSG_ENC_OMEMO);
mucwin_outgoing_msg(mucwin, message, id, PROF_MSG_ENC_OMEMO, replace_id);
} else
#endif
{
auto_char char* id = message_send_groupchat(mucwin->roomjid, message, oob_url, replace_id);
groupchat_log_msg_out(mucwin->roomjid, message);
log_database_add_outgoing_muc(id, mucwin->roomjid, message, replace_id, PROF_MSG_ENC_NONE);
mucwin_outgoing_msg(mucwin, message, id, PROF_MSG_ENC_NONE, replace_id);
}
plugins_post_room_message_send(mucwin->roomjid, plugin_msg);
plugins_post_room_message_send(mucwin->roomjid, message);
return;
#endif
#ifndef HAVE_OMEMO
auto_char char* id = message_send_groupchat(mucwin->roomjid, plugin_msg, oob_url, replace_id);
groupchat_log_msg_out(mucwin->roomjid, plugin_msg);
log_database_add_outgoing_muc(id, mucwin->roomjid, plugin_msg, replace_id, PROF_MSG_ENC_NONE);
mucwin_outgoing_msg(mucwin, plugin_msg, id, PROF_MSG_ENC_NONE, replace_id);
plugins_post_room_message_send(mucwin->roomjid, plugin_msg);
return;
#endif
}
void
@@ -253,13 +240,14 @@ cl_ev_send_priv_msg(ProfPrivateWin* privwin, const char* const msg, const char*
privwin_message_left_room(privwin);
} else {
auto_char char* plugin_msg = plugins_pre_priv_message_send(privwin->fulljid, msg);
const char* const message = plugin_msg ?: msg;
auto_jid Jid* jidp = jid_create(privwin->fulljid);
auto_char char* id = message_send_private(privwin->fulljid, plugin_msg, oob_url);
chat_log_msg_out(jidp->barejid, plugin_msg, jidp->resourcepart);
log_database_add_outgoing_muc_pm(id, privwin->fulljid, plugin_msg, NULL, PROF_MSG_ENC_NONE);
privwin_outgoing_msg(privwin, plugin_msg);
auto_char char* id = message_send_private(privwin->fulljid, message, oob_url);
chat_log_msg_out(jidp->barejid, message, jidp->resourcepart);
log_database_add_outgoing_muc_pm(id, privwin->fulljid, message, NULL, PROF_MSG_ENC_NONE);
privwin_outgoing_msg(privwin, message);
plugins_post_priv_message_send(privwin->fulljid, plugin_msg);
plugins_post_priv_message_send(privwin->fulljid, message);
}
}

View File

@@ -345,7 +345,9 @@ sv_ev_room_message(ProfMessage* message)
}
char* old_plain = message->plain;
message->plain = plugins_pre_room_message_display(message->from_jid->barejid, message->from_jid->resourcepart, message->plain);
auto_char char* plugin_msg = plugins_pre_room_message_display(message->from_jid->barejid, message->from_jid->resourcepart, message->plain);
if (plugin_msg)
message->plain = plugin_msg;
GSList* mentions = get_mentions(prefs_get_boolean(PREF_NOTIFY_MENTION_WHOLE_WORD), prefs_get_boolean(PREF_NOTIFY_MENTION_CASE_SENSITIVE), message->plain, mynick);
gboolean mention = g_slist_length(mentions) > 0;
@@ -409,55 +411,44 @@ sv_ev_room_message(ProfMessage* message)
rosterwin_roster();
plugins_post_room_message_display(message->from_jid->barejid, message->from_jid->resourcepart, message->plain);
free(message->plain);
message->plain = old_plain;
}
static void
_sv_ev_private_message(ProfMessage* message)
{
char* old_plain = message->plain;
auto_char char* plugin_msg = plugins_pre_priv_message_display(message->from_jid->fulljid, message->plain);
if (plugin_msg)
message->plain = plugin_msg;
ProfPrivateWin* privatewin = wins_get_private(message->from_jid->fulljid);
if (privatewin == NULL) {
ProfWin* window = wins_new_private(message->from_jid->fulljid);
privatewin = (ProfPrivateWin*)window;
}
_clean_incoming_message(message);
privwin_incoming_msg(privatewin, message);
// Intentionally skipping log to DB because we can't authenticate the sender
chat_log_msg_in(message);
plugins_post_priv_message_display(message->from_jid->fulljid, message->plain);
message->plain = old_plain;
}
void
sv_ev_incoming_private_message(ProfMessage* message)
{
char* old_plain = message->plain;
message->plain = plugins_pre_priv_message_display(message->from_jid->fulljid, message->plain);
ProfPrivateWin* privatewin = wins_get_private(message->from_jid->fulljid);
if (privatewin == NULL) {
ProfWin* window = wins_new_private(message->from_jid->fulljid);
privatewin = (ProfPrivateWin*)window;
}
_clean_incoming_message(message);
privwin_incoming_msg(privatewin, message);
// Intentionally skipping log to DB because we can't authenticate the sender
chat_log_msg_in(message);
plugins_post_priv_message_display(message->from_jid->fulljid, message->plain);
free(message->plain);
message->plain = old_plain;
_sv_ev_private_message(message);
rosterwin_roster();
}
void
sv_ev_delayed_private_message(ProfMessage* message)
{
char* old_plain = message->plain;
message->plain = plugins_pre_priv_message_display(message->from_jid->fulljid, message->plain);
ProfPrivateWin* privatewin = wins_get_private(message->from_jid->fulljid);
if (privatewin == NULL) {
ProfWin* window = wins_new_private(message->from_jid->fulljid);
privatewin = (ProfPrivateWin*)window;
}
_clean_incoming_message(message);
privwin_incoming_msg(privatewin, message);
// Intentionally skipping log to DB because we can't authenticate the sender
chat_log_msg_in(message);
plugins_post_priv_message_display(message->from_jid->fulljid, message->plain);
free(message->plain);
message->plain = old_plain;
_sv_ev_private_message(message);
}
void

View File

@@ -517,10 +517,14 @@ plugins_on_disconnect(const char* const account_name, const char* const fulljid)
char*
plugins_pre_chat_message_display(const char* const barejid, const char* const resource, char* message)
{
GList* values = g_hash_table_get_values(plugins);
if (!values) {
return message;
}
char* new_message = NULL;
char* curr_message = message;
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
while (curr) {
ProfPlugin* plugin = curr->data;
@@ -552,10 +556,14 @@ plugins_post_chat_message_display(const char* const barejid, const char* const r
char*
plugins_pre_chat_message_send(const char* const barejid, const char* message)
{
GList* values = g_hash_table_get_values(plugins);
if (!values) {
return NULL;
}
char* new_message = NULL;
char* curr_message = strdup(message);
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
while (curr) {
ProfPlugin* plugin = curr->data;
@@ -595,10 +603,14 @@ plugins_post_chat_message_send(const char* const barejid, const char* message)
char*
plugins_pre_room_message_display(const char* const barejid, const char* const nick, const char* message)
{
GList* values = g_hash_table_get_values(plugins);
if (!values) {
return NULL;
}
char* new_message = NULL;
char* curr_message = strdup(message);
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
while (curr) {
ProfPlugin* plugin = curr->data;
@@ -631,10 +643,14 @@ plugins_post_room_message_display(const char* const barejid, const char* const n
char*
plugins_pre_room_message_send(const char* const barejid, const char* message)
{
GList* values = g_hash_table_get_values(plugins);
if (!values) {
return NULL;
}
char* new_message = NULL;
char* curr_message = strdup(message);
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
while (curr) {
ProfPlugin* plugin = curr->data;
@@ -697,11 +713,15 @@ plugins_on_room_history_message(const char* const barejid, const char* const nic
char*
plugins_pre_priv_message_display(const char* const fulljid, const char* message)
{
GList* values = g_hash_table_get_values(plugins);
if (!values) {
return NULL;
}
auto_jid Jid* jidp = jid_create(fulljid);
char* new_message = NULL;
char* curr_message = strdup(message);
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
while (curr) {
ProfPlugin* plugin = curr->data;
@@ -735,11 +755,15 @@ plugins_post_priv_message_display(const char* const fulljid, const char* message
char*
plugins_pre_priv_message_send(const char* const fulljid, const char* const message)
{
GList* values = g_hash_table_get_values(plugins);
if (!values) {
return NULL;
}
auto_jid Jid* jidp = jid_create(fulljid);
char* new_message = NULL;
char* curr_message = strdup(message);
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
while (curr) {
ProfPlugin* plugin = curr->data;
@@ -782,10 +806,14 @@ plugins_post_priv_message_send(const char* const fulljid, const char* const mess
char*
plugins_on_message_stanza_send(const char* const text)
{
GList* values = g_hash_table_get_values(plugins);
if (!values) {
return NULL;
}
char* new_stanza = NULL;
char* curr_stanza = strdup(text);
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
while (curr) {
ProfPlugin* plugin = curr->data;
@@ -825,10 +853,14 @@ plugins_on_message_stanza_receive(const char* const text)
char*
plugins_on_presence_stanza_send(const char* const text)
{
GList* values = g_hash_table_get_values(plugins);
if (!values) {
return NULL;
}
char* new_stanza = NULL;
char* curr_stanza = strdup(text);
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
while (curr) {
ProfPlugin* plugin = curr->data;
@@ -868,10 +900,14 @@ plugins_on_presence_stanza_receive(const char* const text)
char*
plugins_on_iq_stanza_send(const char* const text)
{
GList* values = g_hash_table_get_values(plugins);
if (!values) {
return NULL;
}
char* new_stanza = NULL;
char* curr_stanza = strdup(text);
GList* values = g_hash_table_get_values(plugins);
GList* curr = values;
while (curr) {
ProfPlugin* plugin = curr->data;