add more lua wrapper
This commit is contained in:
parent
ecdd5de636
commit
35affa3085
228
limap.c
228
limap.c
@ -1,8 +1,6 @@
|
|||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
#include "c-client.h"
|
#include "c-client.h"
|
||||||
#include "imap4r1.h"
|
#include "imap4r1.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -16,6 +14,30 @@ extern int errno; /* just in case */
|
|||||||
#define M_ERROR(a,...) syslog (LOG_ERR, "limap@[%s: %d]: " a "\n", __FILE__, \
|
#define M_ERROR(a,...) syslog (LOG_ERR, "limap@[%s: %d]: " a "\n", __FILE__, \
|
||||||
__LINE__, ##__VA_ARGS__)
|
__LINE__, ##__VA_ARGS__)
|
||||||
|
|
||||||
|
#define add_property_string(L,k,v) \
|
||||||
|
lua_pushstring(L,k); \
|
||||||
|
lua_pushstring(L,v); \
|
||||||
|
lua_settable(L,-3);
|
||||||
|
|
||||||
|
unsigned long find_rightmost_bit(unsigned long *valptr);
|
||||||
|
|
||||||
|
size_t strlcat(char *dst, const char *src, size_t n) {
|
||||||
|
char *p = dst;
|
||||||
|
|
||||||
|
while (n != 0 && *p != '\0') {
|
||||||
|
p++;
|
||||||
|
n--;
|
||||||
|
}
|
||||||
|
if (n != 0) {
|
||||||
|
for (; --n != 0; p++, src++) {
|
||||||
|
if ((*p = *src) == '\0')
|
||||||
|
return p - dst;
|
||||||
|
}
|
||||||
|
*p = '\0';
|
||||||
|
}
|
||||||
|
return (p - dst) + strlen(src);
|
||||||
|
}
|
||||||
|
|
||||||
/* Co-routines from MAIL library */
|
/* Co-routines from MAIL library */
|
||||||
|
|
||||||
|
|
||||||
@ -352,6 +374,159 @@ static int l_mail_check(lua_State *L)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_mail_get_raw_header(lua_State* L)
|
||||||
|
{
|
||||||
|
MAILSTREAM *stream = (MAILSTREAM *) lua_touserdata(L, 1);
|
||||||
|
if(!stream)
|
||||||
|
{
|
||||||
|
lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const int id = luaL_checknumber(L,2);
|
||||||
|
char* msg_header = mail_fetchheader_full(stream, id, NIL, NIL, 0);
|
||||||
|
lua_pushstring(L, msg_header);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_mail_getnumber(lua_State* L)
|
||||||
|
{
|
||||||
|
MAILSTREAM *stream = (MAILSTREAM *) lua_touserdata(L, 1);
|
||||||
|
if(!stream)
|
||||||
|
{
|
||||||
|
lua_pushnumber(L, -1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
lua_pushnumber(L, stream->nmsgs);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void _imap_parse_address (lua_State* L, ADDRESS *addresslist)
|
||||||
|
{
|
||||||
|
ADDRESS *addresstmp;
|
||||||
|
addresstmp = addresslist;
|
||||||
|
int count = 1;
|
||||||
|
lua_newtable(L);
|
||||||
|
do {
|
||||||
|
lua_pushnumber(L,count);
|
||||||
|
lua_newtable(L);
|
||||||
|
if (addresstmp->personal)
|
||||||
|
{
|
||||||
|
add_property_string(L, "personal", addresstmp->personal);
|
||||||
|
}
|
||||||
|
if (addresstmp->adl)
|
||||||
|
{
|
||||||
|
add_property_string(L, "adl", addresstmp->adl);
|
||||||
|
}
|
||||||
|
if (addresstmp->mailbox)
|
||||||
|
{
|
||||||
|
add_property_string(L, "mailbox", addresstmp->mailbox);
|
||||||
|
}
|
||||||
|
if (addresstmp->host)
|
||||||
|
{
|
||||||
|
add_property_string(L, "host", addresstmp->host);
|
||||||
|
}
|
||||||
|
lua_settable(L, -3);
|
||||||
|
count++;
|
||||||
|
} while ((addresstmp = addresstmp->next));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void _make_header_object(lua_State* L, ENVELOPE *en)
|
||||||
|
{
|
||||||
|
|
||||||
|
lua_newtable(L);
|
||||||
|
if (en->remail)
|
||||||
|
{
|
||||||
|
add_property_string(L, "remail", en->remail);
|
||||||
|
}
|
||||||
|
if(en->date)
|
||||||
|
{
|
||||||
|
add_property_string(L, "date", (char*)en->date);
|
||||||
|
}
|
||||||
|
if (en->subject)
|
||||||
|
{
|
||||||
|
add_property_string(L, "subject", en->subject);
|
||||||
|
}
|
||||||
|
if (en->in_reply_to)
|
||||||
|
{
|
||||||
|
add_property_string(L, "in_reply_to", en->in_reply_to);
|
||||||
|
}
|
||||||
|
if (en->message_id)
|
||||||
|
{
|
||||||
|
add_property_string(L, "message_id", en->message_id);
|
||||||
|
}
|
||||||
|
if (en->newsgroups)
|
||||||
|
{
|
||||||
|
add_property_string(L, "newsgroups", en->newsgroups);
|
||||||
|
}
|
||||||
|
if (en->followup_to)
|
||||||
|
{
|
||||||
|
add_property_string(L, "followup_to", en->followup_to);
|
||||||
|
}
|
||||||
|
if (en->references)
|
||||||
|
{
|
||||||
|
add_property_string(L, "references", en->references);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (en->to) {
|
||||||
|
lua_pushstring(L, "to");
|
||||||
|
_imap_parse_address(L, en->to);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (en->from) {
|
||||||
|
lua_pushstring(L, "from");
|
||||||
|
_imap_parse_address(L, en->from);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (en->cc) {
|
||||||
|
lua_pushstring(L, "cc");
|
||||||
|
_imap_parse_address(L, en->cc);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (en->bcc) {
|
||||||
|
lua_pushstring(L, "bcc");
|
||||||
|
_imap_parse_address(L, en->bcc);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (en->reply_to) {
|
||||||
|
lua_pushstring(L, "reply_to");
|
||||||
|
_imap_parse_address(L, en->reply_to);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (en->sender) {
|
||||||
|
lua_pushstring(L, "sender");
|
||||||
|
_imap_parse_address(L, en->sender);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (en->return_path) {
|
||||||
|
lua_pushstring(L, "return_path");
|
||||||
|
_imap_parse_address(L, en->return_path);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_rfc822_parse_header(lua_State* L)
|
||||||
|
{
|
||||||
|
ENVELOPE *en = NULL;
|
||||||
|
const char* headers = luaL_checkstring(L,1);
|
||||||
|
rfc822_parse_msg(&en, NULL, (char*)headers, strlen(headers), NULL, "UNKNOWN", NIL);
|
||||||
|
if(en)
|
||||||
|
{
|
||||||
|
_make_header_object(L, en);
|
||||||
|
free(en);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lua_pushnil(L);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int l_reopen(lua_State *L)
|
static int l_reopen(lua_State *L)
|
||||||
{
|
{
|
||||||
MAILSTREAM *stream = (MAILSTREAM *) lua_touserdata(L, 1);
|
MAILSTREAM *stream = (MAILSTREAM *) lua_touserdata(L, 1);
|
||||||
@ -385,6 +560,51 @@ static int l_reopen(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int l_mail_get_headers(lua_State* L)
|
||||||
|
{
|
||||||
|
MAILSTREAM *stream = (MAILSTREAM *) lua_touserdata(L, 1);
|
||||||
|
if(!stream)
|
||||||
|
{
|
||||||
|
lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
char tmp[MAILTMPLEN];
|
||||||
|
unsigned int msgno;
|
||||||
|
char *t;
|
||||||
|
unsigned long i;
|
||||||
|
lua_newtable(L);
|
||||||
|
for (msgno = 1; msgno <= stream->nmsgs; msgno++) {
|
||||||
|
lua_pushnumber(L, msgno);
|
||||||
|
MESSAGECACHE * cache = mail_elt (stream, msgno);
|
||||||
|
mail_fetchstructure(stream, msgno, NIL);
|
||||||
|
tmp[0] = cache->recent ? (cache->seen ? 'R': 'N') : ' ';
|
||||||
|
tmp[1] = (cache->recent | cache->seen) ? ' ' : 'U';
|
||||||
|
tmp[2] = cache->flagged ? 'F' : ' ';
|
||||||
|
tmp[3] = cache->answered ? 'A' : ' ';
|
||||||
|
tmp[4] = cache->deleted ? 'D' : ' ';
|
||||||
|
tmp[5] = cache->draft ? 'X' : ' ';
|
||||||
|
snprintf(tmp + 6, sizeof(tmp) - 6, "%4ld) ", cache->msgno);
|
||||||
|
mail_date(tmp+11, cache);
|
||||||
|
tmp[22] = ' ';
|
||||||
|
tmp[23] = '\0';
|
||||||
|
mail_fetchfrom(tmp+23, stream, msgno, (long)20);
|
||||||
|
strcat(tmp, " ");
|
||||||
|
if ((i = cache->user_flags)) {
|
||||||
|
strcat(tmp, "{");
|
||||||
|
while (i) {
|
||||||
|
strlcat(tmp, stream->user_flags[find_rightmost_bit (&i)], sizeof(tmp));
|
||||||
|
if (i) strlcat(tmp, " ", sizeof(tmp));
|
||||||
|
}
|
||||||
|
strlcat(tmp, "} ", sizeof(tmp));
|
||||||
|
}
|
||||||
|
mail_fetchsubject(t = tmp + strlen(tmp), stream, msgno, (long)25);
|
||||||
|
snprintf(t += strlen(t), sizeof(tmp) - strlen(tmp), " (%ld chars)", cache->rfc822_size);
|
||||||
|
lua_pushstring(L, tmp);
|
||||||
|
lua_settable(L,-3);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static const struct luaL_Reg _lib [] = {
|
static const struct luaL_Reg _lib [] = {
|
||||||
{"open", l_mail_open},
|
{"open", l_mail_open},
|
||||||
{"close", l_mail_close},
|
{"close", l_mail_close},
|
||||||
@ -392,6 +612,10 @@ static const struct luaL_Reg _lib [] = {
|
|||||||
{"reopen", l_reopen},
|
{"reopen", l_reopen},
|
||||||
{"ping", l_mail_ping},
|
{"ping", l_mail_ping},
|
||||||
{"check", l_mail_check},
|
{"check", l_mail_check},
|
||||||
|
{"get_raw_header", l_mail_get_raw_header},
|
||||||
|
{"get_nmail", l_mail_getnumber},
|
||||||
|
{"get_headers", l_mail_get_headers},
|
||||||
|
{"rfc822_parse_header", l_rfc822_parse_header},
|
||||||
{NULL,NULL}
|
{NULL,NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
20
test.lua
20
test.lua
@ -15,10 +15,21 @@ local handle, box = imap.open(url,OP_HALFOPEN|OP_DEBUG)
|
|||||||
|
|
||||||
local r,v = false,nil;
|
local r,v = false,nil;
|
||||||
|
|
||||||
|
function dump(obj)
|
||||||
|
if(type(obj) == "table") then
|
||||||
|
for k,v in pairs(obj) do
|
||||||
|
print(k)
|
||||||
|
dump(v)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print(obj)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if(handle) then
|
if(handle) then
|
||||||
print("mailbox openned")
|
print("mailbox openned")
|
||||||
|
|
||||||
local boxes = imap.get_mail_boxes(handle, box:gsub("[^}]*$",""),"*");
|
local boxes = imap.get_mail_boxes(handle, box:gsub("[^}]*$",""),"*")
|
||||||
|
|
||||||
for k, v in ipairs(boxes) do
|
for k, v in ipairs(boxes) do
|
||||||
print("=================")
|
print("=================")
|
||||||
@ -39,6 +50,13 @@ if(handle) then
|
|||||||
print("Unable to ping")
|
print("Unable to ping")
|
||||||
end
|
end
|
||||||
imap.check(handle);
|
imap.check(handle);
|
||||||
|
-- get nmail
|
||||||
|
local nmail = imap.get_nmail(handle);
|
||||||
|
print("umber of mail", nmail);
|
||||||
|
|
||||||
|
if nmail > 0 then
|
||||||
|
local headers = imap.get_headers(handle)
|
||||||
|
dump(headers)
|
||||||
|
end
|
||||||
imap.close(handle)
|
imap.close(handle)
|
||||||
end
|
end
|
Loading…
x
Reference in New Issue
Block a user