1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Improve MUC 1:1 logging

If we get a private message from a user in a MUC profanity shows this
like:
`profanity@roomsASDF.dismail.de/Martin: Hi`

This was so far logged at:
`~/.local/share/profanity/chatlogs/my-account-at-server/profanity_at_rooms.dismail.de/2019_09_04.log` as:

```
10:48:13 - profanity@rooms.dismail.de: Hi
```

So the nickname was not saved anywhere. This is due to us not knowing
whether we got a regular message from user@server.org/resource or a MUC
PM from room@server.org/user.

We now check for `<x xmlns='http://jabber.org/protocol/muc#user' />` and
add the resourcepart to the logging if we get it.

The file will be created at
`~/.local/share/profanity/chatlogs/my-account-at-server/profanity_at_rooms.dismail.de_nick` and look like:

```
23:59:43 - nick: Hi
```

Fix https://github.com/profanity-im/profanity/issues/1184
This commit is contained in:
Michael Vetter 2019-10-07 00:01:18 +02:00
parent f1d31e024c
commit b3ed213668
3 changed files with 84 additions and 25 deletions

104
src/log.c
View File

@ -90,7 +90,7 @@ static char* _get_groupchat_log_filename(const char *const room, const char *con
static void _rotate_log_file(void);
static char* _log_string_from_level(log_level_t level);
static void _chat_log_chat(const char *const login, const char *const other, const gchar *const msg,
chat_log_direction_t direction, GDateTime *timestamp);
chat_log_direction_t direction, GDateTime *timestamp, const char *const resourcepart);
static void _groupchat_log_chat(const gchar *const login, const gchar *const room, const gchar *const nick,
const gchar *const msg);
@ -272,7 +272,7 @@ chat_log_msg_out(const char *const barejid, const char *const msg)
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = connection_get_fulljid();
Jid *jidp = jid_create(jid);
_chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL);
_chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL, NULL);
jid_destroy(jidp);
}
}
@ -285,9 +285,9 @@ chat_log_otr_msg_out(const char *const barejid, const char *const msg)
Jid *jidp = jid_create(jid);
char *pref_otr_log = prefs_get_string(PREF_OTR_LOG);
if (strcmp(pref_otr_log, "on") == 0) {
_chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL);
_chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL, NULL);
} else if (strcmp(pref_otr_log, "redact") == 0) {
_chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_OUT_LOG, NULL);
_chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_OUT_LOG, NULL, NULL);
}
prefs_free_string(pref_otr_log);
jid_destroy(jidp);
@ -302,9 +302,9 @@ chat_log_pgp_msg_out(const char *const barejid, const char *const msg)
Jid *jidp = jid_create(jid);
char *pref_pgp_log = prefs_get_string(PREF_PGP_LOG);
if (strcmp(pref_pgp_log, "on") == 0) {
_chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL);
_chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL, NULL);
} else if (strcmp(pref_pgp_log, "redact") == 0) {
_chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_OUT_LOG, NULL);
_chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_OUT_LOG, NULL, NULL);
}
prefs_free_string(pref_pgp_log);
jid_destroy(jidp);
@ -319,9 +319,9 @@ chat_log_omemo_msg_out(const char *const barejid, const char *const msg)
Jid *jidp = jid_create(jid);
char *pref_omemo_log = prefs_get_string(PREF_OMEMO_LOG);
if (strcmp(pref_omemo_log, "on") == 0) {
_chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL);
_chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL, NULL);
} else if (strcmp(pref_omemo_log, "redact") == 0) {
_chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_OUT_LOG, NULL);
_chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_OUT_LOG, NULL, NULL);
}
prefs_free_string(pref_omemo_log);
jid_destroy(jidp);
@ -336,9 +336,17 @@ chat_log_otr_msg_in(ProfMessage *message)
Jid *jidp = jid_create(jid);
char *pref_otr_log = prefs_get_string(PREF_OTR_LOG);
if (message->enc == PROF_MSG_ENC_PLAIN || (strcmp(pref_otr_log, "on") == 0)) {
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp);
if (message->mucuser) {
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp, message->jid->resourcepart);
} else {
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp, NULL);
}
} else if (strcmp(pref_otr_log, "redact") == 0) {
_chat_log_chat(jidp->barejid, message->jid->barejid, "[redacted]", PROF_IN_LOG, message->timestamp);
if (message->mucuser) {
_chat_log_chat(jidp->barejid, message->jid->barejid, "[redacted]", PROF_IN_LOG, message->timestamp, message->jid->resourcepart);
} else {
_chat_log_chat(jidp->barejid, message->jid->barejid, "[redacted]", PROF_IN_LOG, message->timestamp, NULL);
}
}
prefs_free_string(pref_otr_log);
jid_destroy(jidp);
@ -353,9 +361,17 @@ chat_log_pgp_msg_in(ProfMessage *message)
Jid *jidp = jid_create(jid);
char *pref_pgp_log = prefs_get_string(PREF_PGP_LOG);
if (strcmp(pref_pgp_log, "on") == 0) {
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp);
if (message->mucuser) {
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp, message->jid->resourcepart);
} else {
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp, NULL);
}
} else if (strcmp(pref_pgp_log, "redact") == 0) {
_chat_log_chat(jidp->barejid, message->jid->barejid, "[redacted]", PROF_IN_LOG, message->timestamp);
if (message->mucuser) {
_chat_log_chat(jidp->barejid, message->jid->barejid, "[redacted]", PROF_IN_LOG, message->timestamp, message->jid->resourcepart);
} else {
_chat_log_chat(jidp->barejid, message->jid->barejid, "[redacted]", PROF_IN_LOG, message->timestamp, NULL);
}
}
prefs_free_string(pref_pgp_log);
jid_destroy(jidp);
@ -370,9 +386,17 @@ chat_log_omemo_msg_in(ProfMessage *message)
Jid *jidp = jid_create(jid);
char *pref_omemo_log = prefs_get_string(PREF_OMEMO_LOG);
if (strcmp(pref_omemo_log, "on") == 0) {
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp);
if (message->mucuser) {
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp, message->jid->resourcepart);
} else {
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp, NULL);
}
} else if (strcmp(pref_omemo_log, "redact") == 0) {
_chat_log_chat(jidp->barejid, message->jid->barejid, "[redacted]", PROF_IN_LOG, message->timestamp);
if (message->mucuser) {
_chat_log_chat(jidp->barejid, message->jid->barejid, "[redacted]", PROF_IN_LOG, message->timestamp, message->jid->resourcepart);
} else {
_chat_log_chat(jidp->barejid, message->jid->barejid, "[redacted]", PROF_IN_LOG, message->timestamp, message->jid->resourcepart);
}
}
prefs_free_string(pref_omemo_log);
jid_destroy(jidp);
@ -385,31 +409,53 @@ chat_log_msg_in(ProfMessage *message)
if (prefs_get_boolean(PREF_CHLOG)) {
const char *jid = connection_get_fulljid();
Jid *jidp = jid_create(jid);
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp);
if (message->mucuser) {
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp, message->jid->resourcepart);
} else {
_chat_log_chat(jidp->barejid, message->jid->barejid, message->plain, PROF_IN_LOG, message->timestamp, NULL);
}
jid_destroy(jidp);
}
}
static void
_chat_log_chat(const char *const login, const char *const other, const char *const msg,
chat_log_direction_t direction, GDateTime *timestamp)
chat_log_direction_t direction, GDateTime *timestamp, const char *const resourcepart)
{
struct dated_chat_log *dated_log = g_hash_table_lookup(logs, other);
char *other_name;
GString *other_str = NULL;
if (resourcepart) {
other_str = g_string_new(other);
g_string_append(other_str, "_");
g_string_append(other_str, resourcepart);
other_name = other_str->str;
} else {
other_name = (char*)other;
}
struct dated_chat_log *dated_log = g_hash_table_lookup(logs, other_name);
// no log for user
if (dated_log == NULL) {
dated_log = _create_log(other, login);
g_hash_table_insert(logs, strdup(other), dated_log);
dated_log = _create_log(other_name, login);
g_hash_table_insert(logs, strdup(other_name), dated_log);
// log entry exists but file removed
} else if (!g_file_test(dated_log->filename, G_FILE_TEST_EXISTS)) {
dated_log = _create_log(other, login);
g_hash_table_replace(logs, strdup(other), dated_log);
dated_log = _create_log(other_name, login);
g_hash_table_replace(logs, strdup(other_name), dated_log);
// log file needs rolling
} else if (_log_roll_needed(dated_log)) {
dated_log = _create_log(other, login);
g_hash_table_replace(logs, strdup(other), dated_log);
dated_log = _create_log(other_name, login);
g_hash_table_replace(logs, strdup(other_name), dated_log);
}
if (resourcepart) {
g_string_free(other_str, TRUE);
}
if (timestamp == NULL) {
@ -424,9 +470,17 @@ _chat_log_chat(const char *const login, const char *const other, const char *con
if (logp) {
if (direction == PROF_IN_LOG) {
if (strncmp(msg, "/me ", 4) == 0) {
fprintf(logp, "%s - *%s %s\n", date_fmt, other, msg + 4);
if (resourcepart) {
fprintf(logp, "%s - *%s %s\n", date_fmt, resourcepart, msg + 4);
} else {
fprintf(logp, "%s - *%s %s\n", date_fmt, other, msg + 4);
}
} else {
fprintf(logp, "%s - %s: %s\n", date_fmt, other, msg);
if (resourcepart) {
fprintf(logp, "%s - %s: %s\n", date_fmt, resourcepart, msg);
} else {
fprintf(logp, "%s - %s: %s\n", date_fmt, other, msg);
}
}
} else {
if (strncmp(msg, "/me ", 4) == 0) {

View File

@ -1089,6 +1089,10 @@ _handle_chat(xmpp_stanza_t *const stanza)
ProfMessage *message = message_init();
message->jid = jid;
if (mucuser) {
message->mucuser = TRUE;
}
message->timestamp = stanza_get_delay(stanza);
if (body) {
message->body = xmpp_stanza_get_text(body);

View File

@ -56,6 +56,7 @@ typedef struct prof_message_t {
GDateTime *timestamp;
prof_enc_t enc;
gboolean trusted;
gboolean mucuser;
} ProfMessage;
typedef int(*ProfMessageCallback)(xmpp_stanza_t *const stanza, void *const userdata);