"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "imapfilter-2.0.10/account.lua" of archive imapfilter-2.0.10.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 -- The Account class represents an IMAP account.
    2 
    3 Account = {}
    4 IMAP = Account
    5 imap = Account
    6 
    7 Account._mt = {}
    8 setmetatable(Account, Account._mt)
    9 
   10 
   11 Account._mt.__call = function (self, arg)
   12     _check_required(arg.server, 'string')
   13     _check_required(arg.username, 'string')
   14     _check_optional(arg.password, 'string')
   15     _check_optional(arg.port, 'number')
   16     _check_optional(arg.ssl, 'string')
   17 
   18     local object = {}
   19 
   20     for key, value in pairs(Account) do
   21         if (type(value) == 'function') then
   22             object[key] = value
   23         end
   24     end
   25 
   26     object._mt = {}
   27     setmetatable(object, object._mt)
   28     object._mt.__index = object._attach_mailbox
   29 
   30     object._imap = arg
   31 
   32     return object
   33 end
   34 
   35 
   36 function Account._login_user(self)
   37     if (self._imap.password == nil) then
   38         self._imap.password = get_password('Enter password for ' ..
   39             self._imap.username .. '@' .. self._imap.server .. ': ')
   40     end
   41 
   42     return ifcore.login(self._imap)
   43 end
   44 
   45 
   46 function Account._attach_mailbox(self, mailbox)
   47     self[mailbox] = Mailbox(self, mailbox)
   48     return self[mailbox]
   49 end
   50 
   51 function Account._detach_mailbox(self, mailbox)
   52     self[mailbox] = nil
   53 end
   54 
   55 
   56 function Account.list_all(self, folder, mbox)
   57     _check_optional(folder, 'string')
   58     _check_optional(mbox, 'string')
   59 
   60     if (folder == nil) then
   61         folder = ''
   62     else
   63         if (type(options) == 'table' and options.namespace == true) then
   64             if (folder == '/') then
   65                 folder = ''
   66             end
   67             if (folder ~= '') then
   68                 folder = folder .. '/'
   69             end
   70         end
   71     end
   72     if (mbox == nil) then
   73         mbox = '%'
   74     end
   75 
   76     if (self._login_user(self) ~= true) then
   77         return
   78     end
   79 
   80     local _, mailboxes, folders = ifcore.list(self._imap, '', folder .. mbox)
   81 
   82     local m = {}
   83     for s in string.gmatch(mailboxes, '%C+') do
   84         table.insert(m, s)
   85     end
   86 
   87     local f = {}
   88     for s in string.gmatch(folders, '%C+') do
   89         if s ~= folder and s ~= folder .. '/' then
   90             table.insert(f, s)
   91         end
   92     end
   93 
   94     return m, f
   95 end
   96 
   97 function Account.list_subscribed(self, folder, mbox)
   98     _check_optional(folder, 'string')
   99     _check_optional(mbox, 'string')
  100 
  101     if (folder == nil) then
  102         folder = ''
  103     else
  104         if (type(options) == 'table' and options.namespace == true) then
  105             if (folder == '/') then
  106                 folder = ''
  107             end
  108             if (folder ~= '') then
  109                 folder = folder .. '/'
  110             end
  111         end
  112     end
  113     if (mbox == nil) then
  114         mbox = '*'
  115     end
  116 
  117     if (self._login_user(self) ~= true) then
  118         return
  119     end
  120 
  121     local _, mailboxes, folders = ifcore.lsub(self._imap, '', folder .. mbox)
  122 
  123     local m = {}
  124     for s in string.gmatch(mailboxes, '%C+') do
  125         table.insert(m, s)
  126     end
  127 
  128     local f = {}
  129     for s in string.gmatch(folders, '%C+') do
  130         if s ~= folder and s ~= folder .. '/' then
  131             table.insert(f, s)
  132         end
  133     end
  134 
  135     return m, f
  136 end
  137 
  138 
  139 function Account.create_mailbox(self, name)
  140     _check_required(name, 'string')
  141 
  142     if (self._login_user(self) ~= true) then
  143         return
  144     end
  145 
  146     local r = ifcore.create(self._imap, name)
  147 
  148     if (type(options) == 'table' and options.info == true) then
  149         print(string.format("Created mailbox %s@%s/%s.",
  150                             self._imap.username, self._imap.server, name))
  151     end
  152 
  153     return r
  154 end
  155 
  156 function Account.delete_mailbox(self, name)
  157     _check_required(name, 'string')
  158 
  159     if (self._login_user(self) ~= true) then
  160         return
  161     end
  162 
  163     local r = ifcore.delete(self._imap, name)
  164 
  165     if (type(options) == 'table' and options.info == true) then
  166         print(string.format("Deleted mailbox %s@%s/%s.",
  167                             self._imap.username, self._imap.server, name))
  168     end
  169 
  170     return r
  171 end
  172 
  173 function Account.rename_mailbox(self, oldname, newname)
  174     _check_required(oldname, 'string')
  175     _check_required(newname, 'string')
  176 
  177     if (self._login_user(self) ~= true) then
  178         return
  179     end
  180 
  181     local r = ifcore.rename(self._imap, oldname, newname)
  182 
  183     if (type(options) == 'table' and options.info == true) then
  184         print(string.format("Renamed mailbox %s@%s/%s to %s@%s/%s.",
  185                             self._imap.username, self._imap.server, oldname,
  186                             self._imap.username, self._imap.server, newname))
  187     end
  188 
  189     return r
  190 end
  191 
  192 function Account.subscribe_mailbox(self, name)
  193     _check_required(name, 'string')
  194 
  195     if (self._login_user(self) ~= true) then
  196         return
  197     end
  198 
  199     local r = ifcore.subscribe(self._imap, name)
  200 
  201     if (type(options) == 'table' and options.info == true) then
  202         print(string.format("Subscribed mailbox %s@%s/%s.",
  203                             self._imap.username, self._imap.server, name))
  204     end
  205 
  206     return r
  207 end
  208 
  209 function Account.unsubscribe_mailbox(self, name)
  210     _check_required(name, 'string')
  211 
  212     if (self._login_user(self) ~= true) then
  213         return
  214     end
  215 
  216     local r = ifcore.unsubscribe(self._imap, name)
  217 
  218     if (type(options) == 'table' and options.info == true) then
  219         print(string.format("Unsubscribed mailbox %s@%s/%s.",
  220                             self._imap.username, self._imap.server, name))
  221     end
  222 
  223     return r
  224 end
  225 
  226 
  227 Account._mt.__index = function () end
  228 Account._mt.__newindex = function () end