lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Excuse my newb question, using Michal's example code from https://gist.github.com/mkottman/6108316 I used the following code which produced the error below:

#!/usr/bin/lua

local imap = require "imap"
    local gmail = imap.connect("imap.gmail.com", 993, true)
    gmail:capability()
    gmail:login("myuser@gmail.com", "my.password")
    -- gmail:select "CustomFilter" -- open a folder
    local message = gmail:fetch "593 BODY[TEXT]"


# lua5.1 test.lua 
Connected to    74.125.136.109  993
lua5.1: ./imap.lua:26: attempt to index local 'conn' (a nil value)
stack traceback:
        ./imap.lua:26: in function 'connect'
        test.lua:4: in main chunk
        [C]: ?

Can someone explain what the above error means and what is causing it? Another question, where do the capability() and select() functions come from (I don't see them in the imap code)?

Thanks!
Chris


On Tue, Jul 30, 2013 at 1:19 AM, Michal Kottman <michal.kottman@gmail.com> wrote:
On 29 July 2013 18:50, Chris Datfung <chris.datfung@gmail.com> wrote:
I want to write a script to log into my gmail account via imap, search for a specific message (based on the subject and date) and download and untar the attachment from that message (there will only be one for each day). Can this be easily done in lua? Can someone point me to an example?

Hi, it certainly can be done. There are some IMAP libraries for Lua, search for "Lua IMAP", for example pointing to here [1].

It is not very hard to write your own Lua IMAP parser, which is what I did for fun (and to parse fail2ban messages about "attacks" on my server to draw them on map). Take a look at my small IMAP module [2], which provides direct access to the IMAP protocol, saving the responses in a table for further processing. Keep an IMAP reference or examples [3] at hand to get an idea how to get to a certain message.

I suggest creating a GMail filter to collect message of your interest into a "folder" for easier searching.

Example of use:

    local imap = require "imap"
    local gmail = imap.connect("imap.gmail.com", 993, true)
    gmail:capability()
    gmail:login("your_username", "your_password")
    gmail:select "CustomFilter" -- open a folder

    -- fetch all message headers in the folder into an array
    local all = gmail:fetch "1:* FULL"

    -- fetch the text of the message 593 into a table, each element being one line
    local message = gmail:fetch "593 BODY[TEXT]"

You have to handle the MIME parsing of the message to extract the attachment, or look for other libraries which can handle it.