Selaa lähdekoodia

Add support for hooks

Alois Mahdal 5 vuotta sitten
vanhempi
commit
c4dd99c13d
1 muutettua tiedostoa jossa 38 lisäystä ja 0 poistoa
  1. 38
    0
      src/common.lua

+ 38
- 0
src/common.lua Näytä tiedosto

@@ -86,6 +86,44 @@ function notifirc_all(seq)
86 86
     end
87 87
 end
88 88
 
89
+function hook_all(seq, hname, ...)
90
+    --
91
+    -- Call hook hname for all messages in seq
92
+    --
93
+    local hfile = os.getenv("IMAPFILTER_HOME") .. "/hooks/" .. hname
94
+    local hargs = {...}
95
+    local harg
96
+    local hcmd = hfile
97
+    local fmt = ' %q'
98
+    if file_exists(hfile) then
99
+        for _, harg in pairs(hargs) do
100
+            hcmd = hcmd .. fmt:format(harg)
101
+        end
102
+        for _, mesg in ipairs(seq) do
103
+            mbox, uid = table.unpack(mesg)
104
+            subj = mbox[uid]:fetch_field('Subject')
105
+            from = mbox[uid]:fetch_field('From')
106
+            to = mbox[uid]:fetch_field('To')
107
+            date = mbox[uid]:fetch_field('Date')
108
+            body = mbox[uid]:fetch_body()
109
+            _hook1(hcmd, subj, from, to, date, body)
110
+        end
111
+    else
112
+        error("no such hook: " .. hname)
113
+        return nil
114
+    end
115
+end
116
+
117
+function _hook1(hcmd, subj, from, to, date, body)
118
+    --
119
+    -- push message through hook script
120
+    --
121
+    local fd = assert(io.popen(hcmd, "w"))
122
+    local fmt = "subj=%s\nfrom=%s\nto=%s\ndate=%s\n%s"
123
+    fd:write(fmt:format(subj, from, to, date, body))
124
+    fd:close()
125
+end
126
+
89 127
 function _partinf_compare(a, b)
90 128
     if not type(a) == type(b) then
91 129
         return false