]> Pileus Git - ~andy/lamechat/commitdiff
Strip HTML tags.
authorAndy Spencer <andy753421@gmail.com>
Mon, 30 Oct 2017 05:46:57 +0000 (05:46 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 30 Oct 2017 05:46:57 +0000 (05:46 +0000)
util.c
util.h
xmpp.c

diff --git a/util.c b/util.c
index 56534fd3d38447b394a612c8cfba3c3d3553279f..b5719244721ecb936743adc4f642d1b15b80d36c 100644 (file)
--- a/util.c
+++ b/util.c
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
+#include <ctype.h>
 #include <errno.h>
 #include <time.h>
 
@@ -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 eac8cd3767f952f751f131f9a9d7002363102365..7cd655533d8e91acb5e829d07dbf9782b5f88d0f 100644 (file)
--- 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 87ff36d9236ecf9df2bef172439d5129beba9f5b..cea509d87b5f61b1f3aeda31e8160016dfd97a1e 100644 (file)
--- 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,
                                "<presence id='join' from='%s' to='%s/%s'>"
                                "<x xmlns='http://jabber.org/protocol/muc'/>"
@@ -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)