smtp.c was eatten by mime.c

This commit is contained in:
Diego Nehab 2004-06-17 06:23:13 +00:00
parent 94b97bdc00
commit 613534c795
13 changed files with 92 additions and 48 deletions

2
TODO
View File

@ -1,6 +1,8 @@
ajeitar os README.* ajeitar os README.*
ajeitar as referencias a RFCS e LTNS em todos os arquivos. ajeitar as referencias a RFCS e LTNS em todos os arquivos.
smtp.o goes to mime.dll
make sure sockets are closed when exceptions are raised make sure sockets are closed when exceptions are raised
check garbage collection in test*.lua check garbage collection in test*.lua

View File

@ -230,6 +230,35 @@ print((mime.b64("diego:password")))
--> ZGllZ286cGFzc3dvcmQ= --> ZGllZ286cGFzc3dvcmQ=
</pre> </pre>
<!-- dot +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id="dot">
A, B = mime.<b>dot(</b>C [, D]<b>)</b>
</p>
<p class=description>
Low-level filter to perform Base64 encoding.
</p>
<p class=description>
<tt>A</tt> is the encoded version of the largest prefix of
<tt>C..D</tt>
that can be encoded unambiguously. <tt>B</tt> has the remaining bytes of
<tt>C..D</tt>, <em>before</em> encoding.
If <tt>D</tt> is <tt><b>nil</b></tt>, <tt>A</tt> is padded with
the encoding of the remaining bytes of <tt>C</tt>.
</p>
<p class=note>
Note: The simplest use of this function is to encode a string into it's
Base64 transfer content encoding. Notice the extra parenthesis around the
call to <tt>mime.b64</tt>, to discard the second return value.
</p>
<pre class=example>
print((mime.b64("diego:password")))
--&gt; ZGllZ286cGFzc3dvcmQ=
</pre>
<!-- eol ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> <!-- eol ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id="eol"> <p class=name id="eol">

View File

@ -1,3 +1,9 @@
-----------------------------------------------------------------------------
-- Little program to adjust end of line markers.
-- LuaSocket sample files
-- Author: Diego Nehab
-- RCS ID: $Id$
-----------------------------------------------------------------------------
local mime = require("mime") local mime = require("mime")
local ltn12 = require("ltn12") local ltn12 = require("ltn12")
local marker = '\n' local marker = '\n'

View File

@ -12,11 +12,6 @@ local socket = require("socket")
local ltn12 = require("ltn12") local ltn12 = require("ltn12")
local url = require("url") local url = require("url")
-----------------------------------------------------------------------------
-- Setup namespace
-----------------------------------------------------------------------------
_LOADED["tftp"] = getfenv(1)
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Program constants -- Program constants
----------------------------------------------------------------------------- -----------------------------------------------------------------------------

View File

@ -13,11 +13,6 @@ local ltn12 = require("ltn12")
local url = require("url") local url = require("url")
local tp = require("tp") local tp = require("tp")
-----------------------------------------------------------------------------
-- Setup namespace
-----------------------------------------------------------------------------
_LOADED["ftp"] = getfenv(1)
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Program constants -- Program constants
----------------------------------------------------------------------------- -----------------------------------------------------------------------------

View File

@ -13,11 +13,6 @@ local ltn12 = require("ltn12")
local mime = require("mime") local mime = require("mime")
local url = require("url") local url = require("url")
-----------------------------------------------------------------------------
-- Setup namespace
-------------------------------------------------------------------------------
_LOADED["http"] = getfenv(1)
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Program constants -- Program constants
----------------------------------------------------------------------------- -----------------------------------------------------------------------------

View File

@ -5,11 +5,6 @@
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- Setup namespace
-----------------------------------------------------------------------------
_LOADED["ltn12"] = getfenv(1)
filter = {} filter = {}
source = {} source = {}
sink = {} sink = {}

View File

@ -28,7 +28,9 @@ static int mime_global_qp(lua_State *L);
static int mime_global_unqp(lua_State *L); static int mime_global_unqp(lua_State *L);
static int mime_global_qpwrp(lua_State *L); static int mime_global_qpwrp(lua_State *L);
static int mime_global_eol(lua_State *L); static int mime_global_eol(lua_State *L);
static int mime_global_dot(lua_State *L);
static size_t dot(int c, size_t state, luaL_Buffer *buffer);
static void b64setup(UC *b64unbase); static void b64setup(UC *b64unbase);
static size_t b64encode(UC c, UC *input, size_t size, luaL_Buffer *buffer); static size_t b64encode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
static size_t b64pad(const UC *input, size_t size, luaL_Buffer *buffer); static size_t b64pad(const UC *input, size_t size, luaL_Buffer *buffer);
@ -43,6 +45,7 @@ static size_t qppad(UC *input, size_t size, luaL_Buffer *buffer);
/* code support functions */ /* code support functions */
static luaL_reg func[] = { static luaL_reg func[] = {
{ "dot", mime_global_dot },
{ "b64", mime_global_b64 }, { "b64", mime_global_b64 },
{ "eol", mime_global_eol }, { "eol", mime_global_eol },
{ "qp", mime_global_qp }, { "qp", mime_global_qp },
@ -659,3 +662,48 @@ static int mime_global_eol(lua_State *L)
lua_pushnumber(L, ctx); lua_pushnumber(L, ctx);
return 2; return 2;
} }
/*-------------------------------------------------------------------------*\
* Takes one byte and stuff it if needed.
\*-------------------------------------------------------------------------*/
static size_t dot(int c, size_t state, luaL_Buffer *buffer)
{
luaL_putchar(buffer, c);
switch (c) {
case '\r':
return 1;
case '\n':
return (state == 1)? 2: 0;
case '.':
if (state == 2)
luaL_putchar(buffer, '.');
default:
return 0;
}
}
/*-------------------------------------------------------------------------*\
* Incrementally applies smtp stuffing to a string
* A, n = dot(l, D)
\*-------------------------------------------------------------------------*/
static int mime_global_dot(lua_State *L)
{
size_t isize = 0, state = (size_t) luaL_checknumber(L, 1);
const char *input = luaL_optlstring(L, 2, NULL, &isize);
const char *last = input + isize;
luaL_Buffer buffer;
/* end-of-input blackhole */
if (!input) {
lua_pushnil(L);
lua_pushnumber(L, 2);
return 2;
}
/* process all input */
luaL_buffinit(L, &buffer);
while (input < last)
state = dot(*input++, state, &buffer);
luaL_pushresult(&buffer);
lua_pushnumber(L, state);
return 2;
}

View File

@ -11,11 +11,6 @@
local mime = requirelib("mime", "luaopen_mime", getfenv(1)) local mime = requirelib("mime", "luaopen_mime", getfenv(1))
local ltn12 = require("ltn12") local ltn12 = require("ltn12")
-----------------------------------------------------------------------------
-- Setup namespace
-----------------------------------------------------------------------------
_LOADED["mime"] = mime
-- encode, decode and wrap algorithm tables -- encode, decode and wrap algorithm tables
encodet = {} encodet = {}
decodet = {} decodet = {}
@ -73,3 +68,8 @@ wrap = choose(wrapt)
function normalize(marker) function normalize(marker)
return ltn12.filter.cycle(eol, 0, marker) return ltn12.filter.cycle(eol, 0, marker)
end end
-- high level stuffing filter
function stuff()
return ltn12.filter.cycle(dot, 2)
end

View File

@ -8,17 +8,11 @@
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Load required modules -- Load required modules
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local smtp = requirelib("smtp", "luaopen_smtp", getfenv(1))
local socket = require("socket") local socket = require("socket")
local ltn12 = require("ltn12") local ltn12 = require("ltn12")
local mime = require("mime") local mime = require("mime")
local tp = require("tp") local tp = require("tp")
-----------------------------------------------------------------------------
-- Setup namespace
-----------------------------------------------------------------------------
_LOADED["smtp"] = smtp
-- timeout for connection -- timeout for connection
TIMEOUT = 60 TIMEOUT = 60
-- default server used to send e-mails -- default server used to send e-mails
@ -31,11 +25,6 @@ DOMAIN = os.getenv("SERVER_NAME") or "localhost"
-- default time zone (means we don't know) -- default time zone (means we don't know)
ZONE = "-0000" ZONE = "-0000"
-- high level stuffing filter
function stuff()
return ltn12.filter.cycle(dot, 2)
end
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
-- Low level SMTP API -- Low level SMTP API
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
@ -110,7 +99,7 @@ function metat.__index:send(mailt)
else else
self:rcpt(mailt.rcpt) self:rcpt(mailt.rcpt)
end end
self:data(ltn12.source.chain(mailt.source, stuff()), mailt.step) self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step)
end end
function open(server, port) function open(server, port)

View File

@ -11,11 +11,6 @@
local socket = require("socket") local socket = require("socket")
local ltn12 = require("ltn12") local ltn12 = require("ltn12")
-----------------------------------------------------------------------------
-- Setup namespace
-----------------------------------------------------------------------------
_LOADED["tp"] = getfenv(1)
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Program constants -- Program constants
----------------------------------------------------------------------------- -----------------------------------------------------------------------------

View File

@ -5,11 +5,6 @@
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- Setup namespace
-----------------------------------------------------------------------------
_LOADED["url"] = getfenv(1)
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Encodes a string into its escaped hexadecimal representation -- Encodes a string into its escaped hexadecimal representation
-- Input -- Input

View File

@ -390,7 +390,7 @@ local r, c, h = http.request {
method = "HEAD", method = "HEAD",
url = "http://www.cs.princeton.edu/~diego/" url = "http://www.cs.princeton.edu/~diego/"
} }
assert(r and h and c == 200) assert(r and h and (c == 200), c)
print("ok") print("ok")
------------------------------------------------------------------------ ------------------------------------------------------------------------