static gint lua_html_get_blocks (lua_State *L) { struct html_content *hc = lua_check_html (L, 1); struct html_block *bl; guint i; if (hc != NULL) { lua_createtable (L, hc->blocks->len, 0); if (hc->blocks && hc->blocks->len > 0) { for (i = 0; i < hc->blocks->len; i ++) { bl = g_ptr_array_index (hc->blocks, i); lua_html_push_block (L, bl); lua_rawseti (L, -2, i + 1); } } else { lua_pushnil (L); } } else { return luaL_error (L, "invalid arguments"); } return 1; }
static gint lua_html_get_images (lua_State *L) { struct html_content *hc = lua_check_html (L, 1); struct html_image *img; guint i; if (hc != NULL) { lua_newtable (L); if (hc->images && hc->images->len > 0) { for (i = 0; i < hc->images->len; i ++) { img = g_ptr_array_index (hc->images, i); lua_html_push_image (L, img); lua_rawseti (L, -2, i + 1); } } else { lua_pushnil (L); } } else { lua_pushnil (L); } return 1; }
static gint lua_html_has_tag (lua_State *L) { struct html_content *hc = lua_check_html (L, 1); const gchar *tagname = luaL_checkstring (L, 2); gboolean ret = FALSE; if (hc && tagname) { if (rspamd_html_tag_seen (hc, tagname)) { ret = TRUE; } } lua_pushboolean (L, ret); return 1; }
static gint lua_html_foreach_tag (lua_State *L) { struct html_content *hc = lua_check_html (L, 1); struct lua_html_traverse_ud ud; const gchar *tagname; gint id; tagname = luaL_checkstring (L, 2); if (hc && tagname && lua_isfunction (L, 3)) { if (hc->html_tags) { if (strcmp (tagname, "any") == 0) { id = -1; } else { id = rspamd_html_tag_by_name (tagname); if (id == -1) { return luaL_error (L, "invalid tagname: %s", tagname); } } lua_pushvalue (L, 3); ud.cbref = luaL_ref (L, LUA_REGISTRYINDEX); ud.L = L; ud.tag_id = id; g_node_traverse (hc->html_tags, G_PRE_ORDER, G_TRAVERSE_ALL, -1, lua_html_node_foreach_cb, &ud); luaL_unref (L, LUA_REGISTRYINDEX, ud.cbref); } } else { return luaL_error (L, "invalid arguments"); } return 0; }
static gint lua_html_has_property (lua_State *L) { struct html_content *hc = lua_check_html (L, 1); const gchar *propname = luaL_checkstring (L, 2); gboolean ret = FALSE; if (hc && propname) { /* * - `no_html` * - `bad_element` * - `xml` * - `unknown_element` * - `duplicate_element` * - `unbalanced` */ if (strcmp (propname, "no_html") == 0) { ret = hc->flags & RSPAMD_HTML_FLAG_BAD_START; } else if (strcmp (propname, "bad_element") == 0) { ret = hc->flags & RSPAMD_HTML_FLAG_BAD_ELEMENTS; } else if (strcmp (propname, "xml") == 0) { ret = hc->flags & RSPAMD_HTML_FLAG_XML; } else if (strcmp (propname, "unknown_element") == 0) { ret = hc->flags & RSPAMD_HTML_FLAG_UNKNOWN_ELEMENTS; } else if (strcmp (propname, "duplicate_element") == 0) { ret = hc->flags & RSPAMD_HTML_FLAG_DUPLICATE_ELEMENTS; } else if (strcmp (propname, "unbalanced") == 0) { ret = hc->flags & RSPAMD_HTML_FLAG_UNBALANCED; } } lua_pushboolean (L, ret); 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 && hc->blocks->len > 0) { 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); } } else { lua_pushnil (L); } return 1; }