From 4876985c0ddee31b1356b2f2718ea90869aa9b92 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Mon, 30 Oct 2017 05:46:57 +0000 Subject: [PATCH] Strip HTML tags. --- util.c | 22 ++++++++++++++++++++++ util.h | 1 + xmpp.c | 31 ++++++++++++++++++++++++------- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/util.c b/util.c index 56534fd..b571924 100644 --- a/util.c +++ b/util.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -141,6 +142,27 @@ int prefix(const char *str, const char *prefix, const char **suffix) return 1; } +char *despace(char *str) +{ + int chr, spaces = 1; + char *src = str; + char *dst = str; + while ((chr = *src++)) { + if (isspace(chr)) + spaces++; + else + spaces=0; + if (spaces == 0) + *dst++ = chr; + if (spaces == 1) + *dst++ = ' '; + } + if (dst > str && spaces) + dst--; + *dst = '\0'; + return str; +} + /* Memory functions */ void *alloc0(int size) { diff --git a/util.h b/util.h index eac8cd3..7cd6555 100644 --- a/util.h +++ b/util.h @@ -49,6 +49,7 @@ char *strcopy(const char *str); void strset(char **old, const char *str); int match(const char *a, const char *b); int prefix(const char *str, const char *prefix, const char **suffix); +char *despace(char *text); /* Memory functions */ void *alloc0(int size); diff --git a/xmpp.c b/xmpp.c index 87ff36d..cea509d 100644 --- a/xmpp.c +++ b/xmpp.c @@ -104,6 +104,7 @@ typedef struct { XML_Parser expat; xmpp_state_t state; buf_t buf; + buf_t html; int level; int id; @@ -111,8 +112,10 @@ typedef struct { xmpp_channel_t *msg_chan; xmpp_user_t *msg_usr; char *msg_body; + char *msg_html; stamp_t msg_stamp; + int in_html; int in_error; } xmpp_server_t; @@ -544,7 +547,8 @@ static void xmpp_run(xmpp_server_t *srv, int idle, xmpp_channel_t *chan = (xmpp_channel_t *)cur; if (!chan->join) continue; - chan_notice(chan, "XMPP Channel: %s", chan->channel.name); + if (!srv->quiet) + chan_notice(chan, "XMPP Channel: %s", chan->channel.name); net_print(&srv->net, "" "" @@ -700,21 +704,34 @@ static void xmpp_run(xmpp_server_t *srv, int idle, if (match(end, "body") && data) { strset(&srv->msg_body, data); } + if (match(start, "html")) { + srv->in_html = 1; + } + if (srv->in_html && data) { + append(&srv->html, data, strlen(data)); + } + if (match(end, "html")) { + strset(&srv->msg_html, reset(&srv->html)); + srv->in_html = 0; + } if (match(end, "message")) { debug("xmpp: body (%s) -- chan=[%s] from=[%s]", chan->type, chan->channel.name, usr ? usr->user.name : "(none)"); - if (srv->msg_body) { - chat_recv(&chan->channel, - &usr->user, - srv->msg_body); + char *content = srv->msg_body; + if (srv->msg_html) + content = despace(srv->msg_html); + if (content) { + chat_recv(&chan->channel, &usr->user, content); message_t *msg = &messages[history-1]; msg->when = srv->msg_stamp ?: msg->when; } srv->msg_stamp = 0; strset(&srv->msg_body, NULL); + strset(&srv->msg_html, NULL); } + } /* Presence */ @@ -798,10 +815,10 @@ void xmpp_init(void) srv->myself.user.server = &srv->server; srv->myself.user.name = strcopy(srv->nick); - if (srv->connect) { + if (srv->connect && !srv->quiet) srv_notice(srv, "XMPP Server: %s", srv->server.name); + if (srv->connect) xmpp_run(srv, 0, NULL, NULL, NULL, NULL); - } } for (channel_t *cur = channels; cur; cur = cur->next) { if (cur->server->protocol != XMPP) -- 2.43.2