aboutsummaryrefslogtreecommitdiff
path: root/mboxc.lua
blob: 67078e32f8fc49fa838ddfe11bd26e0f558556b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
----------------------------------------------------------
-- Licensed under the GNU General Public License version 2
--  * Copyright (C) 2009 Adrian C. <anrxc_sysphere_org>
----------------------------------------------------------

-- {{{ Grab environment
local io = { open = io.open }
local setmetatable = setmetatable
local string = { find = string.find }
-- }}}


-- Mboxc: provides the count of total, old and new messages in a mbox
module("vicious.mboxc")


-- {{{ Mbox count widget type
local function worker(format, mbox)
    -- Initialise counters
    local old = 0
    local total = 0

    -- Open the mbox
    local f = io.open(mbox)

    while true do
      -- Read the mbox line by line, if we are going to read some
      -- *HUGE* folders then switch to reading chunks
      local lines = f:read("*line")
      if not lines then break end

      -- Find all messages
      --  * http://www.jwz.org/doc/content-length.html
      local _, from = string.find(lines, "^From[%s]")
      if from ~= nil then total = total + 1 end

      -- Read messages have the Status header
      local _, status = string.find(lines, "^Status:[%s]RO$")
      if status ~= nil then old = old + 1 end

      -- Skip the folder internal data
      local _, intdata = string.find(lines, "^Subject:[%s].*FOLDER[%s]INTERNAL[%s]DATA")
      if intdata ~= nil then total = total -1 end
    end
    f:close()

    -- Substract total from old to get the new count
    local new = total - old

    return {total, old, new}
end
-- }}}

setmetatable(_M, { __call = function(_, ...) return worker(...) end })