imap.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ---
  2. -- Working Example: https://www.youtube.com/watch?v=PDxTR_KJLhc
  3. -- IMPORTANT: run node.compile("imap.lua") after uploading this script
  4. -- to create a compiled module. Then run file.remove("imap.lua")
  5. -- @name imap
  6. -- @description An IMAP 4rev1 module that can be used to read email.
  7. -- Tested on NodeMCU 0.9.5 build 20150213.
  8. -- @date March 12, 2015
  9. -- @author Miguel
  10. -- GitHub: https://github.com/AllAboutEE
  11. -- YouTube: https://www.youtube.com/user/AllAboutEE
  12. -- Website: http://AllAboutEE.com
  13. --
  14. -- Visit the following URLs to learn more about IMAP:
  15. -- "How to test an IMAP server by using telnet" http://www.anta.net/misc/telnet-troubleshooting/imap.shtml
  16. -- "RFC 2060 - Internet Message Access Protocol - Version 4rev1" http://www.faqs.org/rfcs/rfc2060.html
  17. -------------------------------------------------------------------------------------------------------------
  18. local moduleName = ...
  19. local M = {}
  20. _G[moduleName] = M
  21. local USERNAME = ""
  22. local PASSWORD = ""
  23. local SERVER = ""
  24. local PORT = ""
  25. local TAG = ""
  26. local DEBUG = false
  27. local body = "" -- used to store an email's body / main text
  28. local header = "" -- used to store an email's last requested header field e.g. SUBJECT, FROM, DATA etc.
  29. local most_recent_num = 1 -- used to store the latest/newest email number/id
  30. local response_processed = false -- used to know if the last IMAP response has been processed
  31. ---
  32. -- @name response_processed
  33. -- @returns The response process status of the last IMAP command sent
  34. function M.response_processed()
  35. return response_processed
  36. end
  37. ---
  38. -- @name display
  39. -- @description A generic IMAP response processing function.
  40. -- Can disply the IMAP response if DEBUG is set to true.
  41. -- Sets the reponse processed variable to true when the string "complete"
  42. -- is found in the IMAP reply/response
  43. local function display(socket, response)
  44. -- If debuggins is enabled print the IMAP response
  45. if(DEBUG) then
  46. print(response)
  47. end
  48. -- Some IMAP responses are long enough that they will cause the display
  49. -- function to be called several times. One thing is certain, IMAP will replay with
  50. -- "<tag> OK <command> complete" when it's done sending data back.
  51. if(string.match(response,'complete') ~= nil) then
  52. response_processed = true
  53. end
  54. end
  55. ---
  56. -- @name config
  57. -- @description Initiates the IMAP settings
  58. function M.config(username,password,tag,debug)
  59. USERNAME = username
  60. PASSWORD = password
  61. TAG = tag
  62. DEBUG = debug
  63. end
  64. ---
  65. -- @name login
  66. -- @descrpiton Logs into a new email session
  67. function M.login(socket)
  68. response_processed = false -- we are sending a new command
  69. -- which means that the response for it has not been processed
  70. socket:send(TAG .. " LOGIN " .. USERNAME .. " " .. PASSWORD .. "\r\n")
  71. socket:on("receive",display)
  72. end
  73. ---
  74. -- @name get_most_recent_num
  75. -- @returns The most recent email number. Should only be called after examine()
  76. function M.get_most_recent_num()
  77. return most_recent_num
  78. end
  79. ---
  80. -- @name set_most_recent_num
  81. -- @description Gets the most recent email number from the EXAMINE command.
  82. -- i.e. if EXAMINE returns "* 4 EXISTS" this means that there are 4 emails,
  83. -- so the latest/newest will be identified by the number 4
  84. local function set_most_recent_num(socket,response)
  85. if(DEBUG) then
  86. print(response)
  87. end
  88. local _, _, num = string.find(response,"([0-9]+) EXISTS(\.)") -- the _ and _ keep the index of the string found
  89. -- but we don't care about that.
  90. if(num~=nil) then
  91. most_recent_num = num
  92. end
  93. if(string.match(response,'complete') ~= nil) then
  94. response_processed = true
  95. end
  96. end
  97. ---
  98. -- @name examine
  99. -- @description IMAP examines the given mailbox/folder. Sends the IMAP EXAMINE command
  100. function M.examine(socket,mailbox)
  101. response_processed = false
  102. socket:send(TAG .. " EXAMINE " .. mailbox .. "\r\n")
  103. socket:on("receive",set_most_recent_num)
  104. end
  105. ---
  106. -- @name get_header
  107. -- @returns The last fetched header field
  108. function M.get_header()
  109. return header
  110. end
  111. ---
  112. -- @name set_header
  113. -- @description Records the IMAP header field response in a variable
  114. -- so that it may be read later
  115. local function set_header(socket,response)
  116. if(DEBUG) then
  117. print(response)
  118. end
  119. header = header .. response
  120. if(string.match(response,'complete') ~= nil) then
  121. response_processed = true
  122. end
  123. end
  124. ---
  125. -- @name fetch_header
  126. -- @description Fetches an emails header field e.g. SUBJECT, FROM, DATE
  127. -- @param socket The IMAP socket to use
  128. -- @param msg_number The email number to read e.g. 1 will read fetch the latest/newest email
  129. -- @param field A header field such as SUBJECT, FROM, or DATE
  130. function M.fetch_header(socket,msg_number,field)
  131. header = "" -- we are getting a new header so clear this variable
  132. response_processed = false
  133. socket:send(TAG .. " FETCH " .. msg_number .. " BODY[HEADER.FIELDS (" .. field .. ")]\r\n")
  134. socket:on("receive",set_header)
  135. end
  136. ---
  137. -- @name get_body
  138. -- @return The last email read's body
  139. function M.get_body()
  140. return body
  141. end
  142. ---
  143. -- @name set_body
  144. -- @description Records the IMAP body response in a variable
  145. -- so that it may be read later
  146. local function set_body(socket,response)
  147. if(DEBUG) then
  148. print(response)
  149. end
  150. body = body .. response
  151. if(string.match(response,'complete') ~= nil) then
  152. response_processed = true
  153. end
  154. end
  155. ---
  156. -- @name fetch_body_plain_text
  157. -- @description Sends the IMAP command to fetch a plain text version of the email's body
  158. -- @param socket The IMAP socket to use
  159. -- @param msg_number The email number to obtain e.g. 1 will obtain the latest email
  160. function M.fetch_body_plain_text(socket,msg_number)
  161. response_processed = false
  162. body = "" -- clear the body variable since we'll be fetching a new email
  163. socket:send(TAG .. " FETCH " .. msg_number .. " BODY[1]\r\n")
  164. socket:on("receive",set_body)
  165. end
  166. ---
  167. -- @name logout
  168. -- @description Sends the IMAP command to logout of the email session
  169. function M.logout(socket)
  170. response_processed = false
  171. socket:send(TAG .. " LOGOUT\r\n")
  172. socket:on("receive",display)
  173. end