-/*-
- * Copyright 2016 Vsevolod Stakhov
+/*
+ * Copyright 2024 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
{
LUA_TRACE_POINT;
struct rspamd_cryptobox_pubkey *pkey = NULL, **ppkey;
- const char *buf, *arg;
- gsize len;
+ const char *arg;
int type = RSPAMD_KEYPAIR_SIGN;
int alg = RSPAMD_CRYPTOBOX_MODE_25519;
- buf = luaL_checklstring(L, 1, &len);
+ struct rspamd_lua_text *buf;
+
+ buf = lua_check_text_or_string(L, 1);
if (buf != NULL) {
if (lua_type(L, 2) == LUA_TSTRING) {
/* keypair type */
}
}
- pkey = rspamd_pubkey_from_base32(buf, len, type, alg);
+ pkey = rspamd_pubkey_from_base32(buf->start, buf->len, type, alg);
if (pkey == NULL) {
msg_err("cannot load pubkey from string");
LUA_TRACE_POINT;
rspamd_fstring_t *sig, **psig;
struct rspamd_lua_text *t;
- const char *data;
- gsize dlen;
-
- if (lua_isuserdata(L, 1)) {
- t = lua_check_text(L, 1);
- if (!t) {
- return luaL_error(L, "invalid arguments");
- }
-
- data = t->start;
- dlen = t->len;
- }
- else {
- data = luaL_checklstring(L, 1, &dlen);
- }
+ t = lua_check_text_or_string(L, 1);
- if (data != NULL) {
- if (dlen == rspamd_cryptobox_signature_bytes(RSPAMD_CRYPTOBOX_MODE_25519)) {
- sig = rspamd_fstring_new_init(data, dlen);
+ if (t != NULL) {
+ if (t->len == rspamd_cryptobox_signature_bytes(RSPAMD_CRYPTOBOX_MODE_25519)) {
+ sig = rspamd_fstring_new_init(t->start, t->len);
psig = lua_newuserdata(L, sizeof(rspamd_fstring_t *));
rspamd_lua_setclass(L, rspamd_cryptobox_signature_classname, -1);
*psig = sig;
-/*-
- * Copyright 2020 Vsevolod Stakhov
+/*
+ * Copyright 2024 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
int lua_parsers_tokenize_text(lua_State *L)
{
LUA_TRACE_POINT;
- const char *in = NULL;
- gsize len = 0, pos, ex_len, i;
+ gsize pos, ex_len, i;
GList *exceptions = NULL, *cur;
struct rspamd_lua_text *t;
struct rspamd_process_exception *ex;
GArray *res;
rspamd_stat_token_t *w;
- if (lua_type(L, 1) == LUA_TSTRING) {
- in = luaL_checklstring(L, 1, &len);
- }
- else if (lua_type(L, 1) == LUA_TUSERDATA) {
- t = lua_check_text(L, 1);
-
- if (t) {
- in = t->start;
- len = t->len;
- }
- }
+ t = lua_check_text_or_string(L, 1);
- if (in == NULL) {
- lua_pushnil(L);
- return 1;
+ if (t == NULL) {
+ return luaL_error(L, "invalid arguments");
}
if (lua_gettop(L) > 1 && lua_type(L, 2) == LUA_TTABLE) {
UErrorCode uc_err = U_ZERO_ERROR;
utext_openUTF8(&utxt,
- in,
- len,
+ t->start,
+ t->len,
&uc_err);
- res = rspamd_tokenize_text((char *) in, len,
+ res = rspamd_tokenize_text((char *) t->start, t->len,
&utxt,
RSPAMD_TOKENIZE_UTF, NULL,
exceptions,
LUA_TRACE_POINT;
rspamd_regexp_t *re;
struct rspamd_lua_regexp *new, **pnew;
- const char *string, *flags_str = NULL;
+ const char *flags_str = NULL;
char *escaped;
- gsize pat_len;
GError *err = NULL;
- string = luaL_checklstring(L, 1, &pat_len);
-
- if (lua_gettop(L) == 2) {
- flags_str = luaL_checkstring(L, 2);
- }
+ struct rspamd_lua_text *t = lua_check_text(L, 1);
- if (string) {
- escaped = rspamd_str_regexp_escape(string, pat_len, NULL,
+ if (t) {
+ if (lua_gettop(L) == 2) {
+ flags_str = luaL_checkstring(L, 2);
+ }
+ escaped = rspamd_str_regexp_escape(t->start, t->len, NULL,
RSPAMD_REGEXP_ESCAPE_GLOB | RSPAMD_REGEXP_ESCAPE_UTF);
re = rspamd_regexp_new(escaped, flags_str, &err);
if (re == NULL) {
lua_pushnil(L);
- msg_info("cannot parse regexp: %s, error: %s",
- string,
+ msg_info("cannot parse regexp: %*s, error: %s",
+ (int) t->len,
+ t->start,
err == NULL ? "undefined" : err->message);
g_error_free(err);
g_free(escaped);
{
LUA_TRACE_POINT;
struct rspamd_task *task = NULL, **ptask;
- const char *str_message;
- gsize message_len;
struct rspamd_config *cfg = NULL;
- str_message = luaL_checklstring(L, 1, &message_len);
+ struct rspamd_lua_text *t = lua_check_text_or_string(L, 1);
- if (str_message) {
+ if (t) {
if (lua_type(L, 2) == LUA_TUSERDATA) {
gpointer p;
}
task = rspamd_task_new(NULL, cfg, NULL, NULL, NULL, FALSE);
- task->msg.begin = g_malloc(message_len);
- memcpy((char *) task->msg.begin, str_message, message_len);
- task->msg.len = message_len;
+ task->msg.begin = g_malloc(t->len);
+ memcpy((char *) task->msg.begin, t->start, t->len);
+ task->msg.len = t->len;
rspamd_mempool_add_destructor(task->task_pool, lua_task_free_dtor,
(gpointer) task->msg.begin);
}
static gboolean
inject_url_query_callback(struct rspamd_url *url, gsize start_offset,
- gsize end_offset, gpointer ud)
+ gsize end_offset, gpointer ud)
{
struct rspamd_url_query_to_inject_cbd *cbd =
(struct rspamd_url_query_to_inject_cbd *) ud;
static void
inject_url_query(struct rspamd_task *task, struct rspamd_url *url,
- GPtrArray *part_urls)
+ GPtrArray *part_urls)
{
if (url->querylen > 0) {
struct rspamd_url_query_to_inject_cbd cbd;
if (lua_isuserdata(L, 3)) {
/* We also have a mime part there */
mpart = *((struct rspamd_mime_part **)
- rspamd_lua_check_udata_maybe(L, 3, rspamd_mimepart_classname));
+ rspamd_lua_check_udata_maybe(L, 3, rspamd_mimepart_classname));
}
if (task && task->message && url && url->url) {
if (rspamd_url_set_add_or_increase(MESSAGE_FIELD(task, urls), url->url, false)) {
- if(mpart && mpart->urls) {
+ if (mpart && mpart->urls) {
inject_url_query(task, url->url, mpart->urls);
}
}