Move the event loop to evtloop.c, and wake up sleeping jobs every 10 seconds.
[apps/madmutt.git] / lib-lua / runtime.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2007 Pierre Habouzit
18  */
19
20 #include <lib-lib/lib-lib.h>
21 #include <lib-lua/lib-lua.h>
22
23 #include "../mutt.h"
24 #include "../alias.li"
25 #include "../buffy.li"
26 #include "../charset.li"
27 #include "../crypt.li"
28 #include "../score.li"
29 #include <lib-sys/mutt_ssl.li>
30
31 static lua_State *L;
32
33 void luaM_initialize(void)
34 {
35     static const luaL_Reg lualibs[] = {
36         {"",              luaopen_base},
37         {LUA_OSLIBNAME,   luaopen_os},
38         {LUA_LOADLIBNAME, luaopen_package},
39         {LUA_TABLIBNAME,  luaopen_table},
40         {LUA_IOLIBNAME,   luaopen_io},
41         {LUA_STRLIBNAME,  luaopen_string},
42         {LUA_MATHLIBNAME, luaopen_math},
43         {LUA_DBLIBNAME,   luaopen_debug},
44         {"mod_core",      luaopen_mod_core},
45         {"MTransport",    luaopen_MTransport},
46         {"MAlias",        luaopen_MAlias},
47         {"mod_cset",      luaopen_mod_cset},
48         {"mod_mime",      luaopen_mod_mime},
49         {"mod_buffy",     luaopen_mod_buffy},
50         {"mod_ssl",       luaopen_mod_ssl},
51         {"mod_crypt",     luaopen_mod_crypt},
52         {"mod_score",     luaopen_mod_score},
53     };
54
55     int i;
56
57     L = lua_open();
58     for (i = 0; i < countof(lualibs); i++) {
59         lua_pushcfunction(L, lualibs[i].func);
60         lua_pushstring(L, lualibs[i].name);
61         lua_call(L, 1, 0);
62     }
63 }
64
65 void luaM_shutdown(void)
66 {
67     lua_close(L);
68 }
69
70 lua_State *luaM_getruntime(void)
71 {
72     return L;
73 }
74
75 int luaM_dofile(const char *filename)
76 {
77     return luaL_dofile(L, filename);
78 }
79
80 int luaM_wrap(void (*errfun)(const char *fmt, ...), int status)
81 {
82     if (status) {
83         (*errfun)("-[lua]-: %s\n", lua_tostring(L, -1));
84         lua_pop(L, 1);
85     }
86     return status;
87 }
88
89
90 quadopt_t luaM_checkquadopt(lua_State *Ls, int narg)
91 {
92     const char *s;
93     int i;
94
95     if (lua_type(Ls, narg) == LUA_TSTRING) {
96         s = lua_tostring(Ls, narg);
97         switch (mlua_which_token(s, -1)) {
98           case LTK_YES:     return M_YES;
99           case LTK_NO:      return M_NO;
100           case LTK_ASK_YES: return M_ASKYES;
101           case LTK_ASK_NO:  return M_ASKNO;
102           default:
103             break;
104         }
105     }
106
107     i = luaL_checkinteger(Ls, narg);
108     if (i & ~3) {
109         s = lua_pushfstring(Ls, "int in [0-3] expected, got %d", i);
110         return luaL_argerror(Ls, narg, s);
111     }
112     return i;
113 }
114
115 void luaM_pushquadopt(lua_State *Ls, int val)
116 {
117     switch (val) {
118       case M_YES:    return lua_pushstring(Ls, "yes");
119       case M_NO:     return lua_pushstring(Ls, "no");
120       case M_ASKYES: return lua_pushstring(Ls, "ask-yes");
121       case M_ASKNO:  return lua_pushstring(Ls, "ask-no");
122       default:       return lua_pushnil(Ls);
123     }
124 }
125
126 const char *luaM_checkrx(lua_State *Ls, int narg)
127 {
128     const char *s = luaL_checkstring(Ls, narg);
129     char buf[STRING];
130
131     if (rx_validate(s, buf, ssizeof(buf))) {
132         s = lua_pushfstring(Ls, "invalid regexp: `%s'", buf);
133         luaL_argerror(Ls, narg, s);
134         return NULL;
135     }
136
137     return s;
138 }
139
140 rx_t *luaM_rxnew(const char *val)
141 {
142     if (m_strisempty(val))
143         val = ".";
144
145     return rx_compile(val, mutt_which_case(val));
146 }
147
148 char *luaM_pathnew(const char *val)
149 {
150     char path[_POSIX_PATH_MAX];
151     _mutt_expand_path(path, sizeof(path), val, 0);
152     return m_strdup(path);
153 }
154
155
156 void luaM_pushaddr(lua_State *Ls, address_t *addr)
157 {
158     char s[HUGE_STRING] = "";
159     rfc822_addrcat(s, sizeof(s), addr, 0);
160     lua_pushstring(Ls, s);
161 }