Add a pretty mode for MAM

So it will display `me@domain.org/resource: body text`.
Which is more readable to users who just want to get their MAM and not
debug things :-)
This commit is contained in:
Michael Vetter 2020-12-21 16:50:56 +01:00
parent 0c12189d85
commit 9b00e75aa5

View File

@ -42,24 +42,37 @@
#include "mam.h"
#include "stdio.h"
#include "stdbool.h"
#include "stdlib.h"
#include "string.h"
static void _mam_request(xmppc_t *xmppc, char* to);
static void _mam_request(xmppc_t *xmppc, char* to, bool pretty);
static int _mam_show(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
static int _mam_display_message(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
static int _mam_display_pretty_message(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
void mam_execute_command (xmppc_t *xmppc, int argc, char *argv[]) {
if(argc == 2 && strcmp("list", argv[0]) == 0) {
_mam_request(xmppc, argv[1]);
} else {
printf("Command unbekannt!");
xmpp_disconnect(xmppc->conn);
if(argc == 2) {
if (strcmp("list", argv[0]) == 0) {
_mam_request(xmppc, argv[1], false);
return;
} else if (strcmp("pretty", argv[0]) == 0) {
_mam_request(xmppc, argv[1], true);
return;
}
}
printf("Command unbekannt!");
xmpp_disconnect(xmppc->conn);
}
static void _mam_request(xmppc_t *xmppc, char* to) {
static void _mam_request(xmppc_t *xmppc, char* to, bool pretty) {
if (pretty) {
xmpp_handler_add (xmppc->conn, _mam_display_pretty_message, NULL,"message",NULL, xmppc);
} else {
xmpp_handler_add (xmppc->conn, _mam_display_message, NULL,"message",NULL, xmppc);
}
char* id = xmpp_uuid_gen(xmppc->ctx);
xmpp_stanza_t *iq = xmpp_iq_new(xmppc->ctx, "set", id);
@ -127,3 +140,29 @@ static int _mam_display_message(xmpp_conn_t *const conn, xmpp_stanza_t *const st
}
return 1;
}
static int _mam_display_pretty_message(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata)
{
xmpp_stanza_t* result = xmpp_stanza_get_child_by_name(stanza,"result");
if( result && strcmp("urn:xmpp:mam:2", xmpp_stanza_get_ns(result)) == 0 ) {
xmpp_stanza_t* forwarded = xmpp_stanza_get_child_by_ns(result, "urn:xmpp:forward:0");
if (forwarded) {
xmpp_stanza_t* message = xmpp_stanza_get_child_by_name(forwarded, "message");
if (message) {
const char* const from = xmpp_stanza_get_from(message);
xmpp_stanza_t* body = xmpp_stanza_get_child_by_name(message, "body");
if (body) {
char *text = xmpp_stanza_get_text(body);
printf("%s%s\x1b[m: %s\n", ANSI_COLOR_YELLOW, from, text);
free(text);
}
}
}
}
return 1;
}