aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_html.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-08-20 18:47:08 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-08-20 18:47:08 +0100
commitf30d092776e45c68c784a3448b110df772430bcf (patch)
tree880fceecebe7bfea97f24f10fdbcaa7ef8b020a5 /src/lua/lua_html.c
parentf030ec7c59163dfd67b86987b5ba0a5ea69a327e (diff)
downloadrspamd-f30d092776e45c68c784a3448b110df772430bcf.tar.gz
rspamd-f30d092776e45c68c784a3448b110df772430bcf.zip
Add LUA API for HTML blocks.
Diffstat (limited to 'src/lua/lua_html.c')
-rw-r--r--src/lua/lua_html.c81
1 files changed, 80 insertions, 1 deletions
diff --git a/src/lua/lua_html.c b/src/lua/lua_html.c
index 855904c38..4b7ddea69 100644
--- a/src/lua/lua_html.c
+++ b/src/lua/lua_html.c
@@ -82,7 +82,7 @@ LUA_FUNCTION_DEF (html, has_property);
/***
* @method html:get_images()
- * Returns table of images found in html. Each image is, in turn, a table with the following fields:
+ * Returns a table of images found in html. Each image is, in turn, a table with the following fields:
*
* - `src` - link to the source
* - `height` - height in pixels
@@ -92,10 +92,23 @@ LUA_FUNCTION_DEF (html, has_property);
*/
LUA_FUNCTION_DEF (html, get_images);
+/***
+ * @method html:get_blocks()
+ * Retruns a table of html blocks. Each block provides the following data:
+ *
+ * `tag` - corresponding tag
+ * `color` - a triplet (r g b) for font color
+ * `bgcolor` - a triplet (r g b) for background color
+ * `style` - rspamd{text} with the full style description
+ * @return {table} table of blocks in html part
+ */
+LUA_FUNCTION_DEF (html, get_blocks);
+
static const struct luaL_reg htmllib_m[] = {
LUA_INTERFACE_DEF (html, has_tag),
LUA_INTERFACE_DEF (html, has_property),
LUA_INTERFACE_DEF (html, get_images),
+ LUA_INTERFACE_DEF (html, get_blocks),
{"__tostring", rspamd_lua_class_tostring},
{NULL, NULL}
};
@@ -210,6 +223,72 @@ lua_html_get_images (lua_State *L)
return 1;
}
+static gint
+lua_html_get_blocks (lua_State *L)
+{
+ struct html_content *hc = lua_check_html (L, 1);
+ struct html_block *bl;
+ struct rspamd_lua_text *t;
+ guint i;
+
+ if (hc != NULL) {
+ lua_newtable (L);
+
+ if (hc->blocks) {
+ for (i = 0; i < hc->blocks->len; i ++) {
+ bl = g_ptr_array_index (hc->blocks, i);
+
+ lua_newtable (L);
+
+ if (bl->tag) {
+ lua_pushstring (L, "tag");
+ lua_pushlstring (L, bl->tag->name.start, bl->tag->name.len);
+ lua_settable (L, -3);
+ }
+
+ if (bl->font_color.valid) {
+ lua_pushstring (L, "color");
+ lua_newtable (L);
+ lua_pushnumber (L, bl->font_color.d.comp.r);
+ lua_rawseti (L, -2, 1);
+ lua_pushnumber (L, bl->font_color.d.comp.g);
+ lua_rawseti (L, -2, 2);
+ lua_pushnumber (L, bl->font_color.d.comp.b);
+ lua_rawseti (L, -2, 3);
+ lua_settable (L, -3);
+ }
+ if (bl->background_color.valid) {
+ lua_pushstring (L, "color");
+ lua_newtable (L);
+ lua_pushnumber (L, bl->background_color.d.comp.r);
+ lua_rawseti (L, -2, 1);
+ lua_pushnumber (L, bl->background_color.d.comp.g);
+ lua_rawseti (L, -2, 2);
+ lua_pushnumber (L, bl->background_color.d.comp.b);
+ lua_rawseti (L, -2, 3);
+ lua_settable (L, -3);
+ }
+
+ if (bl->style.len > 0) {
+ lua_pushstring (L, "style");
+ t = lua_newuserdata (L, sizeof (*t));
+ t->start = bl->style.start;
+ t->len = bl->style.len;
+ t->own = FALSE;
+ lua_settable (L, -3);
+ }
+
+ lua_rawseti (L, -2, i + 1);
+ }
+ }
+ }
+ else {
+ lua_pushnil (L);
+ }
+
+ return 1;
+}
+
void
luaopen_html (lua_State * L)
{