"Fossies" - the Fresh Open Source Software Archive

Member "imapfilter-2.8.2/src/account.lua" (26 Dec 2023, 7633 Bytes) of package /linux/privat/imapfilter-2.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lua source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "account.lua": 2.8.0_vs_2.8.1.

    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 _imap = {}
   12 setmetatable(_imap, { __mode = "v" })
   13 
   14 
   15 _undefined = 0
   16 function _next_undefined()
   17     _undefined = _undefined + 1
   18     return 'undefined' .. _undefined
   19 end
   20 
   21 
   22 Account._mt.__call = function (self, arg)
   23     _check_required(arg.server, 'string')
   24     if not arg.oauth2 then
   25         _check_required(arg.username, 'string')
   26     else
   27         _check_optional(arg.username, 'string')
   28     end
   29     _check_optional(arg.password, 'string')
   30     _check_optional(arg.oauth2, 'string')
   31     _check_optional(arg.port, 'number')
   32     _check_optional(arg.ssl, 'string')
   33 
   34     local object = {}
   35 
   36     object._type = 'account'
   37     object._account = {}
   38     object._account.server = arg.server
   39     object._account.username = arg.username or _next_undefined()
   40     object._account.password = arg.password
   41     object._account.oauth2 = arg.oauth2
   42     object._account.port = tostring(arg.port or arg.ssl and 993 or 143)
   43     object._account.ssl = arg.ssl
   44     object._account.session = nil
   45     object._account.selected = nil
   46     object._account.readonly = nil
   47     object._string = object._account.username .. '@' .. object._account.server
   48 
   49     for key, value in pairs(Account) do
   50         if type(value) == 'function' then object[key] = value end
   51     end
   52 
   53     object._mt = {}
   54     object._mt.__index = object._attach_mailbox
   55     object._mt.__gc = object._logout_user
   56     setmetatable(object, object._mt)
   57 
   58     table.insert(_imap, object)
   59 
   60     return object
   61 end
   62 
   63 
   64 function Account._check_connection(self)
   65     if not self._account.session then
   66         self._login_user(self)
   67     end
   68 end
   69 
   70 function Account._check_result(self, request, result)
   71     if result == nil then
   72         self._account.session = nil
   73         self._account.selected = nil
   74         self._account.readonly = nil
   75         error(request .. ' request to ' .. self._string ..  ' failed', 0)
   76     end
   77 end
   78 
   79 
   80 function Account._login_user(self)
   81     if self._account.password == nil and self._account.oauth2 == nil then
   82             self._account.password = get_password('Enter password for ' ..
   83                                                   self._string .. ': ')
   84     end
   85     if type(self._account.password) == 'string' then
   86         self._account.password = string.gsub(self._account.password, '"', '\\"')
   87     end
   88 
   89     if self._account.session then return true end
   90     local r, s = ifcore.login(self._account.server, self._account.port,
   91                               self._account.ssl, self._account.username,
   92                               self._account.password, self._account.oauth2)
   93     self._check_result(self, 'login', r)
   94     if r == false then
   95         error('authentication of ' .. self._string .. ' failed.', 0)
   96     end
   97     if not r then return false end
   98 
   99     self._account.session = s
  100     self._account.selected = nil
  101     self._account.readonly = nil
  102 
  103     return true
  104 end
  105 
  106 function Account._logout_user(self)
  107     self._check_connection(self)
  108     local r = ifcore.logout(self._account.session)
  109     self._check_result(self, 'logout', r)
  110     if r == false then return false end
  111 
  112     self._account.session = nil
  113     self._account.selected = nil
  114     self._account.readonly = nil
  115 
  116     return true
  117 end
  118 
  119 
  120 function Account._attach_mailbox(self, mailbox)
  121     self[mailbox] = Mailbox(self, mailbox)
  122     return self[mailbox]
  123 end
  124 
  125 function Account._detach_mailbox(self, mailbox)
  126     self[mailbox] = nil
  127 end
  128 
  129 
  130 function Account.list_all(self, folder, mbox)
  131     _check_optional(folder, 'string')
  132     _check_optional(mbox, 'string')
  133 
  134     if folder == nil then
  135         folder = ''
  136     else
  137         if options.namespace == true then
  138             if folder == '/' then folder = '' end
  139             if folder ~= '' then folder = folder .. '/' end
  140         end
  141     end
  142     if mbox == nil then mbox = '%' end
  143 
  144     self._check_connection(self)
  145     local r, mailboxes, folders = ifcore.list(self._account.session, '',
  146                                               folder .. mbox)
  147     self._check_result(self, 'list', r)
  148     if r == false then return false end
  149 
  150     local m = {}
  151     for s in string.gmatch(mailboxes, '%C+') do table.insert(m, s) end
  152 
  153     local f = {}
  154     for s in string.gmatch(folders, '%C+') do
  155         if s ~= folder and s ~= folder .. '/' then table.insert(f, s) end
  156     end
  157 
  158     return m, f
  159 end
  160 
  161 function Account.list_subscribed(self, folder, mbox)
  162     _check_optional(folder, 'string')
  163     _check_optional(mbox, 'string')
  164 
  165     if folder == nil then
  166         folder = ''
  167     else
  168         if options.namespace == true then
  169             if folder == '/' then
  170                 folder = ''
  171             end
  172             if folder ~= '' then
  173                 folder = folder .. '/'
  174             end
  175         end
  176     end
  177     if mbox == nil then mbox = '*' end
  178 
  179     self._check_connection(self)
  180     local r, mailboxes, folders = ifcore.lsub(self._account.session, '',
  181                                               folder .. mbox)
  182     self._check_result(self, 'lsub', r)
  183     if r == false then return false end
  184 
  185     local m = {}
  186     for s in string.gmatch(mailboxes, '%C+') do table.insert(m, s) end
  187 
  188     local f = {}
  189     for s in string.gmatch(folders, '%C+') do
  190         if s ~= folder and s ~= folder .. '/' then
  191             table.insert(f, s)
  192         end
  193     end
  194 
  195     return m, f
  196 end
  197 
  198 
  199 function Account.create_mailbox(self, name)
  200     _check_required(name, 'string')
  201 
  202     self._check_connection(self)
  203     local r = ifcore.create(self._account.session, name)
  204     self._check_result(self, 'create', r)
  205     if r == false then return false end
  206 
  207     if options.info == true then
  208         print('Created mailbox ' .. self._string .. '/' .. name .. '.')
  209     end
  210 
  211     return r
  212 end
  213 
  214 function Account.delete_mailbox(self, name)
  215     _check_required(name, 'string')
  216 
  217     self._check_connection(self)
  218     self[name]._cached_close(self[name])
  219     local r = ifcore.delete(self._account.session, name)
  220     self._check_result(self, 'delete', r)
  221     if r == false then return false end
  222 
  223     if options.info == true then
  224         print('Deleted mailbox ' .. self._string .. '/' .. name .. '.')
  225     end
  226 
  227     return r
  228 end
  229 
  230 function Account.rename_mailbox(self, oldname, newname)
  231     _check_required(oldname, 'string')
  232     _check_required(newname, 'string')
  233 
  234     self._check_connection(self)
  235     local r = ifcore.rename(self._account.session, oldname, newname)
  236     self._check_result(self, 'rename', r)
  237     if r == false then return false end
  238 
  239     if options.info == true then
  240         print('Renamed mailbox ' .. self._string .. '/' .. oldname .. ' to ' ..
  241               self._string .. '/' .. newname .. '.')
  242     end
  243 
  244     return r
  245 end
  246 
  247 function Account.subscribe_mailbox(self, name)
  248     _check_required(name, 'string')
  249 
  250     self._check_connection(self)
  251     local r = ifcore.subscribe(self._account.session, name)
  252     self._check_result(self, 'subscribe', r)
  253     if r == false then return false end
  254 
  255     if options.info == true then
  256         print('Subscribed mailbox ' .. self._string .. '/' .. name .. '.')
  257     end
  258 
  259     return r
  260 end
  261 
  262 function Account.unsubscribe_mailbox(self, name)
  263     _check_required(name, 'string')
  264 
  265     self._check_connection(self)
  266     local r = ifcore.unsubscribe(self._account.session, name)
  267     self._check_result(self, 'unsubscribe', r)
  268     if r == false then return false end
  269 
  270     if options.info == true then
  271         print('Unsubscribed mailbox ' .. self._string .. '/' .. name .. '.')
  272     end
  273 
  274     return r
  275 end
  276 
  277 Account.login = Account._login_user
  278 Account.logout = Account._logout_user
  279 
  280 Account._mt.__index = function () end
  281 Account._mt.__newindex = function () end