bye bye DT_SYS variables.
[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 const struct luaL_Reg madmutt_module_funcs[] = {
56     {"pwd",         madmutt_pwd},
57     /*
58      ** .pp
59      ** \fIThis is a read-only system property and, at runtime,
60      ** specifies the current working directory of the madmutt
61      ** binary.\fP
62      */
63     {"folder_path", madmutt_folder_path},
64     /*
65      ** .pp
66      ** \fIThis is a read-only system property and, at runtime,
67      ** specifies the full path or URI of the folder currently
68      ** open (if any).\fP
69      */
70     {"folder_name", madmutt_folder_name},
71     /*
72      ** .pp
73      ** \fIThis is a read-only system property and, at runtime,
74      ** specifies the actual name of the folder as far as it could
75      ** be detected.\fP
76      ** .pp
77      ** For detection, $$$folder is first taken into account
78      ** and simply stripped to form the result when a match is found. For
79      ** example, with $$$folder being \fTimap://host\fP and the folder is
80      ** \fTimap://host/INBOX/foo\fP, $$$madmutt_folder_name will be just
81      ** \fTINBOX/foo\fP.)
82      ** .pp
83      ** Second, if the initial portion of a name is not $$$folder,
84      ** the result will be everything after the last ``/''.
85      ** .pp
86      ** Third and last, the result will be just the name if neither
87      ** $$$folder nor a ``/'' were found in the name.
88      */
89     {NULL, NULL}
90 };
91
92 static const struct {
93     const char *key;
94     const char *value;
95 } madmutt_module_vars[] = {
96     { "version",    VERSION },
97     /*
98      ** .pp
99      ** \fIThis is a read-only system property and specifies madmutt's
100      ** version string.\fP
101      */
102     { "sysconfdir", SYSCONFDIR },
103     /*
104      ** .pp
105      ** \fIThis is a read-only system property and specifies madmutt's
106      ** subversion revision string.\fP
107      */
108     { "bindir",     BINDIR },
109     /*
110      ** .pp
111      ** \fIThis is a read-only system property and specifies the
112      ** directory containing the madmutt binary.\fP
113      */
114     { "docdir",     PKGDOCDIR },
115     /*
116      ** .pp
117      ** \fIThis is a read-only system property and specifies the
118      ** directory containing the madmutt documentation.\fP
119      */
120 #ifdef USE_HCACHE
121 #if defined(HAVE_QDBM)
122     {"hcache_backend", "qdbm" },
123 #elif defined(HAVE_GDBM)
124     {"hcache_backend", "gdbm" },
125 #elif defined(HAVE_DB4)
126     {"hcache_backend", "db4" },
127 #else
128     {"hcache_backend", "unknown" },
129 #endif
130     /*
131      ** .pp
132      ** \fIThis is a read-only system property and specifies the
133      ** header chaching's database backend.\fP
134      */
135 #endif
136 };
137
138 int luaopen_madmutt(lua_State *L)
139 {
140     int i;
141     luaL_register(L, LUA_MADMUTT, madmutt_module_funcs);
142
143     for (i = 0; i < countof(madmutt_module_vars); i++) {
144         lua_pushstring(L, madmutt_module_vars[i].value);
145         lua_setfield(L, -2, madmutt_module_vars[i].key);
146     }
147     return 1;
148 }