| 1 | -- widget type providing the count of total, old and new messages in mbox files |
| 2 | -- Copyright (C) 2010 Adrian C. <[email protected]> |
| 3 | -- Copyright (C) 2017 mutlusun <[email protected]> |
| 4 | -- |
| 5 | -- This file is part of Vicious. |
| 6 | -- |
| 7 | -- Vicious is free software: you can redistribute it and/or modify |
| 8 | -- it under the terms of the GNU General Public License as |
| 9 | -- published by the Free Software Foundation, either version 2 of the |
| 10 | -- License, or (at your option) any later version. |
| 11 | -- |
| 12 | -- Vicious is distributed in the hope that it will be useful, |
| 13 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | -- GNU General Public License for more details. |
| 16 | -- |
| 17 | -- You should have received a copy of the GNU General Public License |
| 18 | -- along with Vicious. If not, see <https://www.gnu.org/licenses/>. |
| 19 | |
| 20 | -- {{{ Grab environment |
| 21 | local io = { open = io.open } |
| 22 | local helpers = require"vicious.helpers" |
| 23 | -- }}} |
| 24 | |
| 25 | -- {{{ Mbox count widget type |
| 26 | return helpers.setcall(function (format, warg) |
| 27 | if not warg then return end |
| 28 | |
| 29 | -- Initialize counters |
| 30 | local count = { old = 0, total = 0, new = 0 } |
| 31 | |
| 32 | -- Get data from mbox files |
| 33 | for i=1, #warg do |
| 34 | local f = io.open(warg[i]) |
| 35 | |
| 36 | while true do |
| 37 | -- Read the mbox line by line, if we are going to read |
| 38 | -- some *HUGE* folders then switch to reading chunks |
| 39 | local lines = f:read("*line") |
| 40 | if not lines then break end |
| 41 | |
| 42 | -- Find all messages |
| 43 | -- * http://www.jwz.org/doc/content-length.html |
| 44 | local _, from = lines:find"^From[%s]" |
| 45 | if from ~= nil then count.total = count.total + 1 end |
| 46 | |
| 47 | -- Read messages have the Status header |
| 48 | local _, status = lines:find"^Status:[%s]RO$" |
| 49 | if status ~= nil then count.old = count.old + 1 end |
| 50 | |
| 51 | -- Skip the folder internal data |
| 52 | local _, int = lines:find"^Subject:[%s].*FOLDER[%s]INTERNAL[%s]DATA" |
| 53 | if int ~= nil then count.total = count.total - 1 end |
| 54 | end |
| 55 | f:close() |
| 56 | end |
| 57 | |
| 58 | -- Substract total from old to get the new count |
| 59 | count.new = count.total - count.old |
| 60 | |
| 61 | return {count.total, count.old, count.new} |
| 62 | end) |
| 63 | -- }}} |