You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_classifier.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "lua_common.h"
  17. /* Classifier methods */
  18. LUA_FUNCTION_DEF(classifier, get_statfiles);
  19. LUA_FUNCTION_DEF(classifier, get_statfile_by_label);
  20. LUA_FUNCTION_DEF(classifier, get_param);
  21. static const struct luaL_reg classifierlib_m[] = {
  22. LUA_INTERFACE_DEF(classifier, get_statfiles),
  23. LUA_INTERFACE_DEF(classifier, get_param),
  24. LUA_INTERFACE_DEF(classifier, get_statfile_by_label),
  25. {"__tostring", rspamd_lua_class_tostring},
  26. {NULL, NULL}};
  27. LUA_FUNCTION_DEF(statfile, get_symbol);
  28. LUA_FUNCTION_DEF(statfile, get_label);
  29. LUA_FUNCTION_DEF(statfile, is_spam);
  30. LUA_FUNCTION_DEF(statfile, get_param);
  31. static const struct luaL_reg statfilelib_m[] = {
  32. LUA_INTERFACE_DEF(statfile, get_symbol),
  33. LUA_INTERFACE_DEF(statfile, get_label),
  34. LUA_INTERFACE_DEF(statfile, is_spam),
  35. LUA_INTERFACE_DEF(statfile, get_param),
  36. {"__tostring", rspamd_lua_class_tostring},
  37. {NULL, NULL}};
  38. static struct rspamd_statfile_config *lua_check_statfile(lua_State *L);
  39. /* Classifier implementation */
  40. static struct rspamd_classifier_config *
  41. lua_check_classifier(lua_State *L)
  42. {
  43. void *ud = rspamd_lua_check_udata(L, 1, rspamd_classifier_classname);
  44. luaL_argcheck(L, ud != NULL, 1, "'classifier' expected");
  45. return ud ? *((struct rspamd_classifier_config **) ud) : NULL;
  46. }
  47. /* Return table of statfiles indexed by name */
  48. static int
  49. lua_classifier_get_statfiles(lua_State *L)
  50. {
  51. struct rspamd_classifier_config *ccf = lua_check_classifier(L);
  52. GList *cur;
  53. struct rspamd_statfile_config *st, **pst;
  54. int i;
  55. if (ccf) {
  56. lua_newtable(L);
  57. cur = g_list_first(ccf->statfiles);
  58. i = 1;
  59. while (cur) {
  60. st = cur->data;
  61. pst = lua_newuserdata(L, sizeof(struct rspamd_statfile_config *));
  62. rspamd_lua_setclass(L, rspamd_statfile_classname, -1);
  63. *pst = st;
  64. lua_rawseti(L, -2, i++);
  65. cur = g_list_next(cur);
  66. }
  67. }
  68. else {
  69. lua_pushnil(L);
  70. }
  71. return 1;
  72. }
  73. static int
  74. lua_classifier_get_param(lua_State *L)
  75. {
  76. struct rspamd_classifier_config *ccf = lua_check_classifier(L);
  77. const char *param;
  78. const ucl_object_t *value;
  79. param = luaL_checkstring(L, 2);
  80. if (ccf != NULL && param != NULL) {
  81. value = ucl_object_lookup(ccf->opts, param);
  82. if (value != NULL) {
  83. ucl_object_push_lua(L, value, true);
  84. return 1;
  85. }
  86. }
  87. lua_pushnil(L);
  88. return 1;
  89. }
  90. /* Get statfile with specified label */
  91. static int
  92. lua_classifier_get_statfile_by_label(lua_State *L)
  93. {
  94. struct rspamd_classifier_config *ccf = lua_check_classifier(L);
  95. struct rspamd_statfile_config *st, **pst;
  96. const char *label;
  97. GList *cur;
  98. int i;
  99. label = luaL_checkstring(L, 2);
  100. if (ccf && label) {
  101. cur = g_hash_table_lookup(ccf->labels, label);
  102. if (cur) {
  103. lua_newtable(L);
  104. i = 1;
  105. while (cur) {
  106. st = cur->data;
  107. pst =
  108. lua_newuserdata(L,
  109. sizeof(struct rspamd_statfile_config *));
  110. rspamd_lua_setclass(L, rspamd_statfile_classname, -1);
  111. *pst = st;
  112. lua_rawseti(L, -2, i++);
  113. cur = g_list_next(cur);
  114. }
  115. return 1;
  116. }
  117. }
  118. lua_pushnil(L);
  119. return 1;
  120. }
  121. /* Statfile functions */
  122. static int
  123. lua_statfile_get_symbol(lua_State *L)
  124. {
  125. struct rspamd_statfile_config *st = lua_check_statfile(L);
  126. if (st != NULL) {
  127. lua_pushstring(L, st->symbol);
  128. }
  129. else {
  130. lua_pushnil(L);
  131. }
  132. return 1;
  133. }
  134. static int
  135. lua_statfile_get_label(lua_State *L)
  136. {
  137. struct rspamd_statfile_config *st = lua_check_statfile(L);
  138. if (st != NULL && st->label != NULL) {
  139. lua_pushstring(L, st->label);
  140. }
  141. else {
  142. lua_pushnil(L);
  143. }
  144. return 1;
  145. }
  146. static int
  147. lua_statfile_is_spam(lua_State *L)
  148. {
  149. struct rspamd_statfile_config *st = lua_check_statfile(L);
  150. if (st != NULL) {
  151. lua_pushboolean(L, st->is_spam);
  152. }
  153. else {
  154. lua_pushnil(L);
  155. }
  156. return 1;
  157. }
  158. static int
  159. lua_statfile_get_param(lua_State *L)
  160. {
  161. struct rspamd_statfile_config *st = lua_check_statfile(L);
  162. const char *param;
  163. const ucl_object_t *value;
  164. param = luaL_checkstring(L, 2);
  165. if (st != NULL && param != NULL) {
  166. value = ucl_object_lookup(st->opts, param);
  167. if (value != NULL) {
  168. lua_pushstring(L, ucl_object_tostring_forced(value));
  169. return 1;
  170. }
  171. }
  172. lua_pushnil(L);
  173. return 1;
  174. }
  175. static struct rspamd_statfile_config *
  176. lua_check_statfile(lua_State *L)
  177. {
  178. void *ud = rspamd_lua_check_udata(L, 1, rspamd_statfile_classname);
  179. luaL_argcheck(L, ud != NULL, 1, "'statfile' expected");
  180. return ud ? *((struct rspamd_statfile_config **) ud) : NULL;
  181. }
  182. /* Open functions */
  183. void luaopen_classifier(lua_State *L)
  184. {
  185. rspamd_lua_new_class(L, rspamd_classifier_classname, classifierlib_m);
  186. lua_pop(L, 1); /* remove metatable from stack */
  187. }
  188. void luaopen_statfile(lua_State *L)
  189. {
  190. rspamd_lua_new_class(L, rspamd_statfile_classname, statfilelib_m);
  191. lua_pop(L, 1); /* remove metatable from stack */
  192. }