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