rationalize settings into a central 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 int madmutt_assign(lua_State *L)
62 {
63     const char *idx = luaL_checkstring(L, 2);
64     const char *val = luaL_checkstring(L, 3);
65     int tk;
66
67     switch ((tk = lua_which_token(idx, -1))) {
68         char buf[STRING];
69
70       default:
71         luaL_error(L, "read-only or inexistant property '%s'", idx, tk);
72         return 0;
73
74       case LTK_DOTLOCK:
75       case LTK_SENDMAIL:
76       case LTK_SHELL:
77         _mutt_expand_path(buf, sizeof(buf), val, 0);
78         mlua_regsets(tk, buf);
79         return 0;
80     }
81 }
82
83 static int madmutt_get(lua_State *L)
84 {
85     const char *idx = luaL_checkstring(L, 2);
86     int tk;
87
88     switch ((tk = lua_which_token(idx, -1))) {
89       default:
90         lua_getmetatable(L, 1);
91         lua_replace(L, 1);
92         lua_rawget(L, 1);
93         return 1;
94
95       case LTK_DOTLOCK:
96       case LTK_SENDMAIL:
97       case LTK_SHELL:
98         lua_pushstring(L, mlua_reggets(tk));
99         break;
100     }
101
102     return 1;
103 }
104
105 static const struct luaL_Reg madmutt_module_funcs[] = {
106     { "pwd",         madmutt_pwd },
107     /*
108      ** .pp
109      ** \fIThis is a read-only system property and, at runtime,
110      ** specifies the current working directory of the madmutt
111      ** binary.\fP
112      */
113     { "folder_path", madmutt_folder_path },
114     /*
115      ** .pp
116      ** \fIThis is a read-only system property and, at runtime,
117      ** specifies the full path or URI of the folder currently
118      ** open (if any).\fP
119      */
120     { "folder_name", madmutt_folder_name },
121     /*
122      ** .pp
123      ** \fIThis is a read-only system property and, at runtime,
124      ** specifies the actual name of the folder as far as it could
125      ** be detected.\fP
126      ** .pp
127      ** For detection, $$$folder is first taken into account
128      ** and simply stripped to form the result when a match is found. For
129      ** example, with $$$folder being \fTimap://host\fP and the folder is
130      ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
131      ** \fTINBOX/foo\fP.)
132      ** .pp
133      ** Second, if the initial portion of a name is not $$$folder,
134      ** the result will be everything after the last ``/''.
135      ** .pp
136      ** Third and last, the result will be just the name if neither
137      ** $$$folder nor a ``/'' were found in the name.
138      */
139
140     { "__newindex",  madmutt_assign },
141     { "__index",     madmutt_get },
142     { NULL, NULL }
143 };
144
145 /* }}} */
146
147 /* {{{ read-only properties */
148
149 static const struct {
150     const char *key;
151     const char *value;
152 } madmutt_module_vars[] = {
153     { "version",    VERSION },
154     /*
155      ** .pp
156      ** \fIThis is a read-only system property and specifies madmutt's
157      ** version string.\fP
158      */
159     { "sysconfdir", SYSCONFDIR },
160     /*
161      ** .pp
162      ** \fIThis is a read-only system property and specifies madmutt's
163      ** subversion revision string.\fP
164      */
165     { "bindir",     BINDIR },
166     /*
167      ** .pp
168      ** \fIThis is a read-only system property and specifies the
169      ** directory containing the madmutt binary.\fP
170      */
171     { "docdir",     PKGDOCDIR },
172     /*
173      ** .pp
174      ** \fIThis is a read-only system property and specifies the
175      ** directory containing the madmutt documentation.\fP
176      */
177 #ifdef USE_HCACHE
178 #if defined(HAVE_QDBM)
179     { "hcache_backend", "qdbm" },
180 #elif defined(HAVE_GDBM)
181     { "hcache_backend", "gdbm" },
182 #elif defined(HAVE_DB4)
183     { "hcache_backend", "db4" },
184 #else
185     { "hcache_backend", "unknown" },
186 #endif
187     /*
188      ** .pp
189      ** \fIThis is a read-only system property and specifies the
190      ** header chaching's database backend.\fP
191      */
192 #endif
193 };
194
195 /* }}} */
196
197 /* {{{ madmutt magic properties */
198
199 static void madmutt_init_shell(char *buf, ssize_t len)
200 {
201     struct passwd *pw = getpwuid(getuid());
202
203     if (pw) {
204         m_strcpy(buf, len, pw->pw_shell);
205         _mutt_expand_path(buf, len, pw->pw_shell, 0);
206     } else {
207         m_strcpy(buf, len, getenv("SHELL") ?: "/bin/sh");
208     }
209 }
210
211 static const struct {
212     const char *key;
213     void (*fun)(char *buf, ssize_t len);
214     const char *val;
215 } madmutt_module_vars2[] = {
216     { "dotlock",     NULL, BINDIR "/mutt_dotlock" },
217     /*
218      ** .pp
219      ** Contains the path of the \fTmadmutt_dotlock(1)\fP binary to be used by
220      ** Madmutt.
221      */
222     { "sendmail",    NULL, SENDMAIL " -oem -oi" },
223     /*
224      ** .pp
225      ** Specifies the program and arguments used to deliver mail sent by Madmutt.
226      ** Madmutt expects that the specified program interprets additional
227      ** arguments as recipient addresses.
228      */
229     { "shell",       madmutt_init_shell, NULL },
230     /*
231      ** .pp
232      ** Command to use when spawning a subshell.  By default, the user's login
233      ** shell from \fT/etc/passwd\fP is used.
234      */
235 };
236
237 /* }}} */
238
239 int luaopen_madmutt(lua_State *L)
240 {
241     int i;
242
243     lua_newuserdata(L, sizeof(void*));
244     luaL_newmetatable(L, "madmutt.core");
245
246     luaL_openlib(L, NULL, madmutt_module_funcs, 0);
247
248     for (i = 0; i < countof(madmutt_module_vars); i++) {
249         lua_pushstring(L, madmutt_module_vars[i].value);
250         lua_setfield(L, -2, madmutt_module_vars[i].key);
251     }
252
253     lua_setmetatable(L, -2);
254
255     for (i = 0; i < countof(madmutt_module_vars2); i++) {
256         if (madmutt_module_vars2[i].fun) {
257             char buf[STRING];
258             (madmutt_module_vars2[i].fun)(buf, sizeof(buf));
259             lua_pushstring(L, buf);
260         } else {
261             lua_pushstring(L, madmutt_module_vars2[i].val);
262         }
263         lua_setfield(L, -2, madmutt_module_vars2[i].key);
264     }
265
266     lua_setglobal(L, "madmutt");
267     return 1;
268 }