Added imap lua module and updated read_email_imap.lua example to use module. There is a bug where Examine is not sent sometimes.
This commit is contained in:
parent
36d42010c2
commit
56aa416535
|
@ -3,86 +3,69 @@
|
||||||
-- @description Reads email via IMAP
|
-- @description Reads email via IMAP
|
||||||
|
|
||||||
-- this could be your email (and it is in most cases even in you have a "username")
|
-- this could be your email (and it is in most cases even in you have a "username")
|
||||||
local IMAP_USERNAME = "myemail@xxx.rr.com"
|
require("imap")
|
||||||
local IMAP_PASSWORD = "mypassword"
|
|
||||||
|
|
||||||
local IMAP_SERVER = "mail.twc.com"
|
local IMAP_USERNAME = "myemail@domain.com"
|
||||||
|
local IMAP_PASSWORD = "myemailpassword"
|
||||||
|
|
||||||
|
local IMAP_SERVER = "imap.domain.com"
|
||||||
local IMAP_PORT = "143"
|
local IMAP_PORT = "143"
|
||||||
local IMAP_TAG = "t1"
|
local IMAP_TAG = "t1"
|
||||||
|
|
||||||
local SSID = "ssid"
|
local SSID = "ssid"
|
||||||
local SSID_PASSWORD = "password"
|
local SSID_PASSWORD = "ssidpassword"
|
||||||
|
|
||||||
|
|
||||||
|
local count = 0
|
||||||
|
|
||||||
wifi.setmode(wifi.STATION)
|
wifi.setmode(wifi.STATION)
|
||||||
wifi.sta.config(SSID,SSID_PASSWORD)
|
wifi.sta.config(SSID,SSID_PASSWORD)
|
||||||
wifi.sta.autoconnect(1)
|
wifi.sta.autoconnect(1)
|
||||||
|
|
||||||
|
local imap_socket = net.createConnection(net.TCP,0)
|
||||||
|
|
||||||
function print_logout(sck,logout_reply)
|
|
||||||
print("=========== LOGOUT REPLY ==========\r\n")
|
function setup(sck)
|
||||||
print(logout_reply)
|
imap.config(IMAP_USERNAME,
|
||||||
|
IMAP_PASSWORD,
|
||||||
|
IMAP_TAG,
|
||||||
|
sck)
|
||||||
|
|
||||||
|
imap.login(imap_socket)
|
||||||
end
|
end
|
||||||
|
|
||||||
function logout(sck)
|
imap_socket:on("connection",setup)
|
||||||
print("Logging out...\r\n")
|
imap_socket:connect(IMAP_PORT,IMAP_SERVER)
|
||||||
sck:on("receive",print_logout)
|
|
||||||
sck:send(IMAP_TAG .. " LOGOUT\r\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
function print_body(sck,email_body)
|
function do_next()
|
||||||
print("========== EMAIL BODY =========== \r\n")
|
|
||||||
print(email_body)
|
|
||||||
print("\r\n")
|
|
||||||
logout(sck)
|
|
||||||
end
|
|
||||||
|
|
||||||
function fetch_body(sck)
|
if(imap.receive_complete() == true) then
|
||||||
print("\r\nFetching first mail body...\r\n")
|
print("receive complete")
|
||||||
sck:on("receive",print_body)
|
|
||||||
sck:send(IMAP_TAG .. " FETCH 1 BODY[TEXT]\r\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
if (count == 0) then
|
||||||
function print_header(sck, email_header)
|
print("Examine:\r\n")
|
||||||
print("============== EMAIL HEADERS ==========\r\n")
|
imap.examine(imap_socket,"INBOX")
|
||||||
print(email_header) -- prints the email headers
|
count = count + 1
|
||||||
print("\r\n")
|
elseif (count == 1) then
|
||||||
fetch_body(sck)
|
imap.fetch_header(imap_socket,1,"SUBJECT")
|
||||||
end
|
count = count + 1
|
||||||
|
elseif (count == 2) then
|
||||||
function fetch_header(sck)
|
imap.fetch_header(imap_socket,1,"FROM")
|
||||||
print("\r\nFetching first mail headers...\r\n")
|
count = count + 1
|
||||||
sck:on("receive",print_header)
|
elseif (count == 3) then
|
||||||
sck:send(IMAP_TAG .. " FETCH 1 BODY[HEADER]\r\n")
|
imap.fetch_body_plain_text(imap_socket,1)
|
||||||
end
|
count = count + 1
|
||||||
|
elseif (count == 4) then
|
||||||
function print_login(sck,login_reply)
|
imap.logout(imap_socket)
|
||||||
print("========== LOGIN REPLY ==========\r\n")
|
count = count + 1
|
||||||
print(login_reply)
|
else
|
||||||
select(sck,"INBOX")
|
print("The body is: ".. imap.get_body())
|
||||||
|
tmr.stop(0)
|
||||||
|
imap_socket:close()
|
||||||
|
collectgarbage()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function print_select(sck,response)
|
tmr.alarm(0,1000,1, do_next)
|
||||||
print(response)
|
|
||||||
fetch_header(sck)
|
|
||||||
end
|
|
||||||
|
|
||||||
function select(sck,mailbox)
|
|
||||||
print("Selecting inbox...\r\n")
|
|
||||||
sck:on("receive",print_select)
|
|
||||||
sck:send(IMAP_TAG .. " SELECT "..mailbox.."\r\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function login(sck)
|
|
||||||
print("Logging in...\r\n")
|
|
||||||
sck:on("receive", print_login)
|
|
||||||
sck:send(IMAP_TAG .. " LOGIN " .. IMAP_USERNAME .. " ".. IMAP_PASSWORD.."\r\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local socket = net.createConnection(net.TCP,0)
|
|
||||||
socket:on("connection",login)
|
|
||||||
socket:connect(IMAP_PORT,IMAP_SERVER)
|
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
local moduleName = ...
|
||||||
|
local M = {}
|
||||||
|
_G[moduleName] = M
|
||||||
|
|
||||||
|
local USERNAME = ""
|
||||||
|
local PASSWORD = ""
|
||||||
|
|
||||||
|
local SERVER = ""
|
||||||
|
local PORT = ""
|
||||||
|
local TAG = ""
|
||||||
|
|
||||||
|
local body = ""
|
||||||
|
|
||||||
|
|
||||||
|
local receive_complete = false
|
||||||
|
|
||||||
|
function M.receive_complete()
|
||||||
|
return receive_complete
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function display(socket, response)
|
||||||
|
print(response)
|
||||||
|
if(string.match(response,'complete') ~= nil) then
|
||||||
|
receive_complete = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.config(username,password,tag,sk)
|
||||||
|
USERNAME = username
|
||||||
|
PASSWORD = password
|
||||||
|
TAG = tag
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.login(socket)
|
||||||
|
receive_complete = false
|
||||||
|
socket:send(TAG .. " LOGIN " .. USERNAME .. " " .. PASSWORD .. "\r\n")
|
||||||
|
socket:on("receive",display)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.examine(socket,mailbox)
|
||||||
|
|
||||||
|
receive_complete = false
|
||||||
|
socket:send(TAG .. " EXAMINE " .. mailbox .. "\r\n")
|
||||||
|
socket:on("receive",display)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.fetch_header(socket,msg_number,field)
|
||||||
|
|
||||||
|
receive_complete = false
|
||||||
|
socket:send(TAG .. " FETCH " .. msg_number .. " BODY[HEADER.FIELDS (" .. field .. ")]\r\n")
|
||||||
|
socket:on("receive",display)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.get_body()
|
||||||
|
return body
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function set_body(socket,response)
|
||||||
|
print(response)
|
||||||
|
body = body .. response
|
||||||
|
if(string.match(response,'complete') ~= nil) then
|
||||||
|
receive_complete = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.fetch_body_plain_text(socket,msg_number)
|
||||||
|
receive_complete = false
|
||||||
|
body = ""
|
||||||
|
socket:send(TAG .. " FETCH " .. msg_number .. " BODY[1]\r\n")
|
||||||
|
socket:on("receive",set_body)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.logout(socket)
|
||||||
|
receive_complete = false
|
||||||
|
socket:send(TAG .. " LOGOUT\r\n")
|
||||||
|
socket:on("receive",display)
|
||||||
|
end
|
Loading…
Reference in New Issue