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