"Fossies" - the Fresh Open Source Software Archive

Member "imapfilter-2.8.2/src/lua.c" (26 Dec 2023, 5242 Bytes) of package /linux/privat/imapfilter-2.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "lua.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.7.6_vs_2.8.0.

    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <string.h>
    4 
    5 #include <lua.h>
    6 #include <lauxlib.h>
    7 #include <lualib.h>
    8 
    9 #include "imapfilter.h"
   10 #include "pathnames.h"
   11 
   12 
   13 extern options opts;
   14 extern struct sessionhead sessions;
   15 
   16 static lua_State *lua;      /* Lua interpreter state. */
   17 
   18 
   19 static int traceback_handler(lua_State *lua);
   20 void init_options(void);
   21 void interactive_mode(void);
   22 
   23 
   24 /*
   25  * Start the Lua interpreter, export IMAP core and system functions, load the
   26  * Lua interface functions, load and execute imapfilter's configuration file.
   27  */
   28 void
   29 start_lua(void)
   30 {
   31 
   32     lua = luaL_newstate();
   33 
   34     luaL_openlibs(lua);
   35 
   36     luaopen_ifcore(lua);
   37     luaopen_ifsys(lua);
   38     luaopen_ifre(lua);
   39 
   40     lua_settop(lua, 0);
   41 
   42     init_options();
   43 
   44     if (luaL_loadfile(lua, PATHNAME_COMMON) ||
   45         lua_pcall(lua, 0, LUA_MULTRET, 0))
   46         fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1));
   47 
   48     if (luaL_loadfile(lua, PATHNAME_SET) ||
   49         lua_pcall(lua, 0, LUA_MULTRET, 0))
   50         fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1));
   51 
   52     if (luaL_loadfile(lua, PATHNAME_REGEX) ||
   53         lua_pcall(lua, 0, LUA_MULTRET, 0))
   54         fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1));
   55 
   56     if (luaL_loadfile(lua, PATHNAME_ACCOUNT) ||
   57         lua_pcall(lua, 0, LUA_MULTRET, 0))
   58         fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1));
   59 
   60     if (luaL_loadfile(lua, PATHNAME_MAILBOX) ||
   61         lua_pcall(lua, 0, LUA_MULTRET, 0))
   62         fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1));
   63 
   64     if (luaL_loadfile(lua, PATHNAME_MESSAGE) ||
   65         lua_pcall(lua, 0, LUA_MULTRET, 0))
   66         fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1));
   67 
   68     if (luaL_loadfile(lua, PATHNAME_OPTIONS) ||
   69         lua_pcall(lua, 0, LUA_MULTRET, 0))
   70         fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1));
   71 
   72     if (luaL_loadfile(lua, PATHNAME_AUXILIARY) ||
   73         lua_pcall(lua, 0, LUA_MULTRET, 0))
   74         fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1));
   75 
   76     if (opts.oneline != NULL) {
   77         if (luaL_loadbuffer(lua, opts.oneline, strlen(opts.oneline),
   78             "=<command line>") || lua_pcall(lua, 0, LUA_MULTRET, 0))
   79             fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1));
   80     } else {
   81         if (luaL_loadfile(lua, strcmp(opts.config, "-") == 0 ? NULL :
   82             opts.config))
   83             fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1));
   84         lua_pushcfunction(lua, traceback_handler);
   85         lua_insert(lua, 1);
   86         if (lua_pcall(lua, 0, LUA_MULTRET, -2))
   87             fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1));
   88     }
   89 
   90     if (opts.interactive)
   91         interactive_mode();
   92 }
   93 
   94 
   95 /*
   96  * Add a stack traceback to the error message.
   97  */
   98 static int
   99 traceback_handler(lua_State *lua)
  100 {
  101 
  102 #if LUA_VERSION_NUM < 502
  103     lua_getfield(lua, LUA_GLOBALSINDEX, "debug");
  104     lua_getfield(lua, -1, "traceback");
  105     lua_pushvalue(lua, 1);
  106     lua_pushinteger(lua, 2);
  107     lua_call(lua, 2, 1);
  108 #else
  109     luaL_traceback(lua, lua, lua_tostring(lua, 1), 0);
  110 #endif
  111     return 1;
  112 }
  113 
  114 
  115 /*
  116  * Stop the Lua interpreter.
  117  */
  118 void
  119 stop_lua(void)
  120 {
  121 
  122     lua_close(lua);
  123 }
  124 
  125 
  126 /*
  127  * Set default values to program's options.
  128  */
  129 void
  130 init_options(void)
  131 {
  132 
  133     lua_newtable(lua);
  134 
  135     set_table_boolean("certificates", 1);
  136     set_table_boolean("create", 0);
  137     set_table_boolean("expunge", 1);
  138     set_table_boolean("hostnames", 1);
  139     set_table_number("keepalive", 29);
  140     set_table_boolean("namespace", 1);
  141     set_table_boolean("starttls", 1);
  142     set_table_boolean("subscribe", 0);
  143     set_table_number("timeout", 60);
  144     set_table_boolean("wakeonany", 0);
  145 
  146     set_table_boolean("dryrun", opts.dryrun);
  147 
  148     lua_setglobal(lua, "options");
  149 }
  150 
  151 
  152 /*
  153  * Interactive mode.
  154  */
  155 void
  156 interactive_mode(void)
  157 {
  158     char buf[LINE_MAX];
  159     
  160     for (;;) {
  161         printf("> ");
  162         fflush(stdout);
  163 
  164         if (fgets(buf, sizeof(buf), stdin) == NULL) {
  165             printf("\n");
  166             break;
  167         }
  168 
  169         if (luaL_loadbuffer(lua, buf, strlen(buf), "=<line>") ||
  170             lua_pcall(lua, 0, LUA_MULTRET, 0)) {
  171             error("%s\n", lua_tostring(lua, -1));
  172             lua_pop(lua, 1);
  173         }
  174     }
  175 }
  176 
  177 
  178 /*
  179  * Get from the configuration file the value of a boolean option variable.
  180  */
  181 int
  182 get_option_boolean(const char *opt)
  183 {
  184     int b;
  185 
  186     lua_getglobal(lua, "options");
  187     lua_getfield(lua, -1, opt);
  188     b = lua_toboolean(lua, -1);
  189     lua_pop(lua, 2);
  190 
  191     return b;
  192 }
  193 
  194 
  195 /*
  196  * Get from the configuration file the value of a number option variable.
  197  */
  198 lua_Number
  199 get_option_number(const char *opt)
  200 {
  201     lua_Number n;
  202 
  203     lua_getglobal(lua, "options");
  204     lua_getfield(lua, -1, opt);
  205     n = lua_tonumber(lua, -1);
  206     lua_pop(lua, 2);
  207 
  208     return n;
  209 }
  210 
  211 
  212 /*
  213  * Get from the configuration file the value of a string option variable.
  214  */
  215 const char *
  216 get_option_string(const char *opt)
  217 {
  218     const char *s;
  219 
  220     lua_getglobal(lua, "options");
  221     lua_getfield(lua, -1, opt);
  222     s = lua_tostring(lua, -1);
  223     lua_pop(lua, 2);
  224 
  225     if (!s)
  226         return "";
  227 
  228     return s;
  229 }
  230 
  231 
  232 /*
  233  * Set a table's element value to the specified boolean.
  234  */
  235 int
  236 set_table_boolean(const char *key, int value)
  237 {
  238 
  239     lua_pushstring(lua, key);
  240     lua_pushboolean(lua, value);
  241     lua_settable(lua, -3);
  242 
  243     return 0;
  244 }
  245 
  246 
  247 /*
  248  * Set a table's element value to the specified number.
  249  */
  250 int
  251 set_table_number(const char *key, lua_Number value)
  252 {
  253 
  254     lua_pushstring(lua, key);
  255     lua_pushnumber(lua, value);
  256     lua_settable(lua, -3);
  257 
  258     return 0;
  259 }
  260 
  261 /*
  262  * Set a table's element value to the specified string.
  263  */
  264 int
  265 set_table_string(const char *key, const char *value)
  266 {
  267 
  268     lua_pushstring(lua, key);
  269     lua_pushstring(lua, value);
  270     lua_settable(lua, -3);
  271 
  272     return 0;
  273 }
  274 
  275