various cleanups.
[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 static int madmutt_pwd(lua_State *L)
30 {
31     char path[_POSIX_PATH_MAX];
32     getcwd(path, sizeof(path));
33     lua_pushstring(L, path);
34     return 1;
35 }
36
37 static int madmutt_folder_path(lua_State *L)
38 {
39     lua_pushstring(L, CurrentFolder ?: "");
40     return 1;
41 }
42
43 static int madmutt_folder_name(lua_State *L)
44 {
45     const char *p;
46
47     if (!m_strisempty(Maildir)
48     && m_strstart(CurrentFolder, Maildir, &p) && *p) {
49         while (*p == '/')
50             p++;
51         lua_pushstring(L, p);
52     } else {
53         p = strchr(CurrentFolder ?: "", '/');
54         lua_pushstring(L, p ? p + 1 : (CurrentFolder ?: ""));
55     }
56     return 1;
57 }
58
59 static int madmutt_assign(lua_State *L)
60 {
61     const char *idx = luaL_checkstring(L, 2);
62     const char *val = luaL_checkstring(L, 3);
63     char buf[STRING];
64
65     switch (lua_which_token(idx, -1)) {
66       default:
67         luaL_error(L, "bad subscript to madmutt: %s", val);
68         return 0;
69
70       case LTK_SENDMAIL:
71       case LTK_SHELL:
72         _mutt_expand_path(buf, sizeof(buf), val, 0);
73         val = buf;
74         break;
75     }
76
77     lua_getmetatable(L, 1);
78     lua_pushstring(L, idx);
79     lua_pushstring(L, val);
80     lua_rawset(L, -3);
81
82     return 0;
83 }
84
85 static const struct luaL_Reg madmutt_module_funcs[] = {
86     {"pwd",         madmutt_pwd},
87     /*
88      ** .pp
89      ** \fIThis is a read-only system property and, at runtime,
90      ** specifies the current working directory of the madmutt
91      ** binary.\fP
92      */
93     {"folder_path", madmutt_folder_path},
94     /*
95      ** .pp
96      ** \fIThis is a read-only system property and, at runtime,
97      ** specifies the full path or URI of the folder currently
98      ** open (if any).\fP
99      */
100     {"folder_name", madmutt_folder_name},
101     /*
102      ** .pp
103      ** \fIThis is a read-only system property and, at runtime,
104      ** specifies the actual name of the folder as far as it could
105      ** be detected.\fP
106      ** .pp
107      ** For detection, $$$folder is first taken into account
108      ** and simply stripped to form the result when a match is found. For
109      ** example, with $$$folder being \fTimap://host\fP and the folder is
110      ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
111      ** \fTINBOX/foo\fP.)
112      ** .pp
113      ** Second, if the initial portion of a name is not $$$folder,
114      ** the result will be everything after the last ``/''.
115      ** .pp
116      ** Third and last, the result will be just the name if neither
117      ** $$$folder nor a ``/'' were found in the name.
118      */
119
120     {"__newindex",  madmutt_assign},
121     {NULL, NULL}
122 };
123
124 static const struct {
125     const char *key;
126     const char *value;
127 } madmutt_module_vars[] = {
128     { "version",    VERSION },
129     /*
130      ** .pp
131      ** \fIThis is a read-only system property and specifies madmutt's
132      ** version string.\fP
133      */
134     { "sysconfdir", SYSCONFDIR },
135     /*
136      ** .pp
137      ** \fIThis is a read-only system property and specifies madmutt's
138      ** subversion revision string.\fP
139      */
140     { "bindir",     BINDIR },
141     /*
142      ** .pp
143      ** \fIThis is a read-only system property and specifies the
144      ** directory containing the madmutt binary.\fP
145      */
146     { "docdir",     PKGDOCDIR },
147     /*
148      ** .pp
149      ** \fIThis is a read-only system property and specifies the
150      ** directory containing the madmutt documentation.\fP
151      */
152 #ifdef USE_HCACHE
153 #if defined(HAVE_QDBM)
154     { "hcache_backend", "qdbm" },
155 #elif defined(HAVE_GDBM)
156     { "hcache_backend", "gdbm" },
157 #elif defined(HAVE_DB4)
158     { "hcache_backend", "db4" },
159 #else
160     { "hcache_backend", "unknown" },
161 #endif
162     /*
163      ** .pp
164      ** \fIThis is a read-only system property and specifies the
165      ** header chaching's database backend.\fP
166      */
167 #endif
168 };
169
170 static void madmutt_init_shell(char *buf, ssize_t len)
171 {
172     struct passwd *pw = getpwuid(getuid());
173
174     if (pw) {
175         m_strcpy(buf, len, pw->pw_shell);
176         _mutt_expand_path(buf, len, pw->pw_shell, 0);
177     } else {
178         m_strcpy(buf, len, getenv("SHELL") ?: "/bin/sh");
179     }
180 }
181
182 static const struct {
183     const char *key;
184     void (*fun)(char *buf, ssize_t len);
185 } madmutt_module_vars2[] = {
186     { "shell",          madmutt_init_shell },
187     /*
188      ** .pp
189      ** Command to use when spawning a subshell.  By default, the user's login
190      ** shell from \fT/etc/passwd\fP is used.
191      */
192 };
193
194 int luaopen_madmutt(lua_State *L)
195 {
196     int i;
197
198     lua_newuserdata(L, sizeof(void*));
199     luaL_newmetatable(L, "madmutt.core");
200
201     luaL_openlib(L, NULL, madmutt_module_funcs, 0);
202
203     for (i = 0; i < countof(madmutt_module_vars); i++) {
204         lua_pushstring(L, madmutt_module_vars[i].value);
205         lua_setfield(L, -2, madmutt_module_vars[i].key);
206     }
207
208     for (i = 0; i < countof(madmutt_module_vars2); i++) {
209         char buf[STRING];
210         (madmutt_module_vars2[i].fun)(buf, sizeof(buf));
211         lua_pushstring(L, buf);
212         lua_setfield(L, -2, madmutt_module_vars2[i].key);
213     }
214
215     lua_pushstring(L, "__index");
216     lua_pushvalue(L, -2);
217     lua_settable(L, -3);
218
219     lua_setmetatable(L, -2);
220     lua_setglobal(L, "madmutt");
221
222     return 1;
223 }