-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Diego Nehab
committed
Oct 11, 2007
1 parent
e394956
commit 52ac60a
Showing
27 changed files
with
791 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
local CRLF = "\013\010" | ||
local input = source.chain(source.file(io.stdin), normalize(CRLF)) | ||
local output = sink.file(io.stdout) | ||
pump.all(input, output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function pump.step(src, snk) | ||
local chunk, src_err = src() | ||
local ret, snk_err = snk(chunk, src_err) | ||
if chunk and ret then return 1 | ||
else return nil, src_err or snk_err end | ||
end | ||
|
||
function pump.all(src, snk, step) | ||
step = step or pump.step | ||
while true do | ||
local ret, err = step(src, snk) | ||
if not ret then | ||
if err then return nil, err | ||
else return 1 end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
local input = source.chain( | ||
source.file(io.open("input.bin", "rb")), | ||
encode("base64")) | ||
local output = sink.chain( | ||
wrap(76), | ||
sink.file(io.open("output.b64", "w"))) | ||
pump.all(input, output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
local smtp = require"socket.smtp" | ||
local mime = require"mime" | ||
local ltn12 = require"ltn12" | ||
|
||
CRLF = "\013\010" | ||
|
||
local message = smtp.message{ | ||
headers = { | ||
from = "Sicrano <sicrano@example.com>", | ||
to = "Fulano <fulano@example.com>", | ||
subject = "A message with an attachment"}, | ||
body = { | ||
preamble = "Hope you can see the attachment" .. CRLF, | ||
[1] = { | ||
body = "Here is our logo" .. CRLF}, | ||
[2] = { | ||
headers = { | ||
["content-type"] = 'image/png; name="luasocket.png"', | ||
["content-disposition"] = | ||
'attachment; filename="luasocket.png"', | ||
["content-description"] = 'LuaSocket logo', | ||
["content-transfer-encoding"] = "BASE64"}, | ||
body = ltn12.source.chain( | ||
ltn12.source.file(io.open("luasocket.png", "rb")), | ||
ltn12.filter.chain( | ||
mime.encode("base64"), | ||
mime.wrap()))}}} | ||
|
||
assert(smtp.send{ | ||
rcpt = "<diego@cs.princeton.edu>", | ||
from = "<diego@cs.princeton.edu>", | ||
server = "localhost", | ||
port = 2525, | ||
source = message}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function filter.cycle(lowlevel, context, extra) | ||
return function(chunk) | ||
local ret | ||
ret, context = lowlevel(context, chunk, extra) | ||
return ret | ||
end | ||
end | ||
|
||
function normalize(marker) | ||
return filter.cycle(eol, 0, marker) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
local function chainpair(f1, f2) | ||
return function(chunk) | ||
local ret = f2(f1(chunk)) | ||
if chunk then return ret | ||
else return (ret or "") .. (f2() or "") end | ||
end | ||
end | ||
|
||
function filter.chain(...) | ||
local f = select(1, ...) | ||
for i = 2, select('#', ...) do | ||
f = chainpair(f, select(i, ...)) | ||
end | ||
return f | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
local qp = filter.chain(normalize(CRLF), encode("quoted-printable"), | ||
wrap("quoted-printable")) | ||
local input = source.chain(source.file(io.stdin), qp) | ||
local output = sink.file(io.stdout) | ||
pump.all(input, output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function source.empty(err) | ||
return function() | ||
return nil, err | ||
end | ||
end | ||
|
||
function source.file(handle, io_err) | ||
if handle then | ||
return function() | ||
local chunk = handle:read(20) | ||
if not chunk then handle:close() end | ||
return chunk | ||
end | ||
else return source.empty(io_err or "unable to open file") end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function source.chain(src, f) | ||
return function() | ||
if not src then | ||
return nil | ||
end | ||
local chunk, err = src() | ||
if not chunk then | ||
src = nil | ||
return f(nil) | ||
else | ||
return f(chunk) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function sink.table(t) | ||
t = t or {} | ||
local f = function(chunk, err) | ||
if chunk then table.insert(t, chunk) end | ||
return 1 | ||
end | ||
return f, t | ||
end | ||
|
||
local function null() | ||
return 1 | ||
end | ||
|
||
function sink.null() | ||
return null | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
local input = source.file(io.stdin) | ||
local output, t = sink.table() | ||
output = sink.chain(normalize(CRLF), output) | ||
pump.all(input, output) | ||
io.write(table.concat(t)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
for chunk in source.file(io.stdin) do | ||
io.write(chunk) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "lua.h" | ||
#include "lauxlib.h" | ||
|
||
#define CR '\xD' | ||
#define LF '\xA' | ||
#define CRLF "\xD\xA" | ||
|
||
#define candidate(c) (c == CR || c == LF) | ||
static int pushchar(int c, int last, const char *marker, | ||
luaL_Buffer *buffer) { | ||
if (candidate(c)) { | ||
if (candidate(last)) { | ||
if (c == last) | ||
luaL_addstring(buffer, marker); | ||
return 0; | ||
} else { | ||
luaL_addstring(buffer, marker); | ||
return c; | ||
} | ||
} else { | ||
luaL_putchar(buffer, c); | ||
return 0; | ||
} | ||
} | ||
|
||
static int eol(lua_State *L) { | ||
int context = luaL_checkint(L, 1); | ||
size_t isize = 0; | ||
const char *input = luaL_optlstring(L, 2, NULL, &isize); | ||
const char *last = input + isize; | ||
const char *marker = luaL_optstring(L, 3, CRLF); | ||
luaL_Buffer buffer; | ||
luaL_buffinit(L, &buffer); | ||
if (!input) { | ||
lua_pushnil(L); | ||
lua_pushnumber(L, 0); | ||
return 2; | ||
} | ||
while (input < last) | ||
context = pushchar(*input++, context, marker, &buffer); | ||
luaL_pushresult(&buffer); | ||
lua_pushnumber(L, context); | ||
return 2; | ||
} | ||
|
||
static luaL_reg func[] = { | ||
{ "eol", eol }, | ||
{ NULL, NULL } | ||
}; | ||
|
||
int luaopen_gem(lua_State *L) { | ||
luaL_openlib(L, "gem", func, 0); | ||
return 0; | ||
} |
Oops, something went wrong.