move OPT_QUIT into the lua registry
[apps/madmutt.git] / lib-lua / madmutt.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
22 #include <sys/types.h>
23 #include <pwd.h>
24
25 #include "lib-lua_priv.h"
26
27 #include "../mutt.h"
28
29 /* {{{ madmutt functions */
30
31 static int madmutt_pwd(lua_State *L)
32 {
33     char path[_POSIX_PATH_MAX];
34     getcwd(path, sizeof(path));
35     lua_pushstring(L, path);
36     return 1;
37 }
38
39 static int madmutt_folder_path(lua_State *L)
40 {
41     lua_pushstring(L, CurrentFolder ?: "");
42     return 1;
43 }
44
45 static int madmutt_folder_name(lua_State *L)
46 {
47     const char *p;
48
49     if (!m_strisempty(Maildir)
50     && m_strstart(CurrentFolder, Maildir, &p) && *p) {
51         while (*p == '/')
52             p++;
53         lua_pushstring(L, p);
54     } else {
55         p = strchr(CurrentFolder ?: "", '/');
56         lua_pushstring(L, p ? p + 1 : (CurrentFolder ?: ""));
57     }
58     return 1;
59 }
60
61 static quadopt_t quadopt_parse(const char *s)
62 {
63     if (!m_strcasecmp("yes", s))
64         return M_YES;
65     if (!m_strcasecmp("no", s))
66         return M_NO;
67     if (!m_strcasecmp("ask-yes", s))
68         return M_ASKYES;
69     if (!m_strcasecmp("ask-no", s))
70         return M_ASKNO;
71     return -1;
72 }
73
74 static int madmutt_assign(lua_State *L)
75 {
76     const char *idx = luaL_checkstring(L, 2);
77     const char *val = luaL_checkstring(L, 3);
78     int tk;
79
80     switch ((tk = lua_which_token(idx, -1))) {
81         char buf[STRING];
82         int i;
83
84       default:
85         return luaL_error(L, "read-only or inexistant property '%s'", idx, tk);
86
87       case LTK_DOTLOCK:
88       case LTK_SENDMAIL:
89       case LTK_SHELL:
90       case LTK_EDITOR:
91         _mutt_expand_path(buf, sizeof(buf), val, 0);
92         val = buf;
93         /* FALLTHROUGH */
94
95         mlua_regsets(tk, val);
96         return 0;
97
98       case LTK_QUIT:
99         i = quadopt_parse(val);
100         if (i < 0)
101             return luaL_error(L, "invalid quad option value: '%s'", val);
102         mlua_regsetq(tk, i);
103         return 0;
104     }
105 }
106
107 static int madmutt_get(lua_State *L)
108 {
109     const char *idx = luaL_checkstring(L, 2);
110     int tk;
111
112     switch ((tk = lua_which_token(idx, -1))) {
113       default:
114         lua_getmetatable(L, 1);
115         lua_replace(L, 1);
116         lua_rawget(L, 1);
117         return 1;
118
119       case LTK_DOTLOCK:
120       case LTK_SENDMAIL:
121       case LTK_SHELL:
122         lua_pushstring(L, mlua_reggets(tk));
123         break;
124     }
125
126     return 1;
127 }
128
129 static const struct luaL_Reg madmutt_module_funcs[] = {
130     { "pwd",         madmutt_pwd },
131     /*
132      ** .pp
133      ** \fIThis is a read-only system property and, at runtime,
134      ** specifies the current working directory of the madmutt
135      ** binary.\fP
136      */
137     { "folder_path", madmutt_folder_path },
138     /*
139      ** .pp
140      ** \fIThis is a read-only system property and, at runtime,
141      ** specifies the full path or URI of the folder currently
142      ** open (if any).\fP
143      */
144     { "folder_name", madmutt_folder_name },
145     /*
146      ** .pp
147      ** \fIThis is a read-only system property and, at runtime,
148      ** specifies the actual name of the folder as far as it could
149      ** be detected.\fP
150      ** .pp
151      ** For detection, $$$folder is first taken into account
152      ** and simply stripped to form the result when a match is found. For
153      ** example, with $$$folder being \fTimap://host\fP and the folder is
154      ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
155      ** \fTINBOX/foo\fP.)
156      ** .pp
157      ** Second, if the initial portion of a name is not $$$folder,
158      ** the result will be everything after the last ``/''.
159      ** .pp
160      ** Third and last, the result will be just the name if neither
161      ** $$$folder nor a ``/'' were found in the name.
162      */
163
164     { "__newindex",  madmutt_assign },
165     { "__index",     madmutt_get },
166     { NULL, NULL }
167 };
168
169 /* }}} */
170
171 /* {{{ read-only properties */
172
173 static const struct {
174     const char *key;
175     const char *value;
176 } madmutt_module_vars[] = {
177     { "version",    VERSION },
178     /*
179      ** .pp
180      ** \fIThis is a read-only system property and specifies madmutt's
181      ** version string.\fP
182      */
183     { "sysconfdir", SYSCONFDIR },
184     /*
185      ** .pp
186      ** \fIThis is a read-only system property and specifies madmutt's
187      ** subversion revision string.\fP
188      */
189     { "bindir",     BINDIR },
190     /*
191      ** .pp
192      ** \fIThis is a read-only system property and specifies the
193      ** directory containing the madmutt binary.\fP
194      */
195     { "docdir",     PKGDOCDIR },
196     /*
197      ** .pp
198      ** \fIThis is a read-only system property and specifies the
199      ** directory containing the madmutt documentation.\fP
200      */
201 #ifdef USE_HCACHE
202 #if defined(HAVE_QDBM)
203     { "hcache_backend", "qdbm" },
204 #elif defined(HAVE_GDBM)
205     { "hcache_backend", "gdbm" },
206 #elif defined(HAVE_DB4)
207     { "hcache_backend", "db4" },
208 #else
209     { "hcache_backend", "unknown" },
210 #endif
211     /*
212      ** .pp
213      ** \fIThis is a read-only system property and specifies the
214      ** header chaching's database backend.\fP
215      */
216 #endif
217 };
218
219 /* }}} */
220
221 /* {{{ madmutt magic properties */
222
223 static void madmutt_init_editor(char *buf, ssize_t len)
224 {
225     m_strcpy(buf, len, getenv("VISUAL") ?: getenv("EDITOR") ?: "vi");
226 }
227
228 static void madmutt_init_shell(char *buf, ssize_t len)
229 {
230     struct passwd *pw = getpwuid(getuid());
231
232     if (pw) {
233         m_strcpy(buf, len, pw->pw_shell);
234         _mutt_expand_path(buf, len, pw->pw_shell, 0);
235     } else {
236         m_strcpy(buf, len, getenv("SHELL") ?: "/bin/sh");
237     }
238 }
239
240 static const struct {
241     const char *key;
242     void (*fun)(char *buf, ssize_t len);
243     const char *val;
244 } madmutt_module_vars2[] = {
245     { "dotlock",     NULL, BINDIR "/mutt_dotlock" },
246     /*
247      ** .pp
248      ** Contains the path of the \fTmadmutt_dotlock(1)\fP binary to be used by
249      ** Madmutt.
250      */
251     {"editor",       madmutt_init_editor, NULL },
252     /*
253      ** .pp
254      ** This variable specifies which editor is used by Madmutt.
255      ** It defaults to the value of the \fT$$$VISUAL\fP, or \fT$$$EDITOR\fP, environment
256      ** variable, or to the string "\fTvi\fP" if neither of those are set.
257      */
258     { "sendmail",    NULL, SENDMAIL " -oem -oi" },
259     /*
260      ** .pp
261      ** Specifies the program and arguments used to deliver mail sent by Madmutt.
262      ** Madmutt expects that the specified program interprets additional
263      ** arguments as recipient addresses.
264      */
265     { "shell",       madmutt_init_shell, NULL },
266     /*
267      ** .pp
268      ** Command to use when spawning a subshell.  By default, the user's login
269      ** shell from \fT/etc/passwd\fP is used.
270      */
271     {"quit",         NULL, "yes" }
272     /*
273      ** .pp
274      ** This variable controls whether ``quit'' and ``exit'' actually quit
275      ** from Madmutt.  If it set to \fIyes\fP, they do quit, if it is set to \fIno\fP, they
276      ** have no effect, and if it is set to \fIask-yes\fP or \fIask-no\fP, you are
277      ** prompted for confirmation when you try to quit.
278      */
279 };
280
281 /* }}} */
282
283 int luaopen_madmutt(lua_State *L)
284 {
285     int i;
286
287     lua_newuserdata(L, sizeof(void*));
288     luaL_newmetatable(L, "madmutt.core");
289
290     luaL_openlib(L, NULL, madmutt_module_funcs, 0);
291
292     for (i = 0; i < countof(madmutt_module_vars); i++) {
293         lua_pushstring(L, madmutt_module_vars[i].value);
294         lua_setfield(L, -2, madmutt_module_vars[i].key);
295     }
296
297     lua_setmetatable(L, -2);
298
299     for (i = 0; i < countof(madmutt_module_vars2); i++) {
300         if (madmutt_module_vars2[i].fun) {
301             char buf[STRING];
302             (madmutt_module_vars2[i].fun)(buf, sizeof(buf));
303             lua_pushstring(L, buf);
304         } else {
305             lua_pushstring(L, madmutt_module_vars2[i].val);
306         }
307         lua_setfield(L, -2, madmutt_module_vars2[i].key);
308     }
309
310     lua_setglobal(L, "madmutt");
311     return 1;
312 }