Browse Source

Let user define all actions

Having just 4 valid actions (albeit they do form a meaningful workflow)
was rather historical limitation caused by author's negligence.
User should be able to define any actions they want by simply creating
handlers for them.

Later we might want to provide a guidance (template) for that, but
that's a TODO item.
Alois Mahdal 7 years ago
parent
commit
919fa12b82
2 changed files with 18 additions and 14 deletions
  1. 6
    4
      src/common.lua
  2. 12
    10
      src/imapdomo.skel

+ 6
- 4
src/common.lua View File

@@ -34,18 +34,20 @@ function handle()
34 34
     -- Handle action if it's valid
35 35
     --
36 36
     local action = os.getenv("IMAPDOMO_ACTION")
37
+    local file = "handlers/" .. action .. ".lua"
37 38
     local valid = {
38 39
         newmail = true,
39 40
         rewind = true,
40 41
         cleanup = true,
41 42
         migrate = true
42 43
     }
43
-    if not valid[action] then
44
-        error("invalid action: " .. action)
44
+    if file_exists(file) then
45
+        do_if_exists("init.lua")
46
+        dofile(file)
47
+    else
48
+        error("no handler for action: " .. action)
45 49
         return nil
46 50
     end
47
-    do_if_exists("init.lua")
48
-    do_if_exists("handlers/" .. action .. ".lua")
49 51
 end
50 52
 
51 53
 

+ 12
- 10
src/imapdomo.skel View File

@@ -14,19 +14,12 @@ usage() {
14 14
     mkusage "$@" "[options] ACTION" \
15 15
         -o                                                                    \
16 16
             "-c DIR      change to DIR before doing anything"                 \
17
+            "-l          list handlers and exit"                              \
17 18
             "-d          turn on debugging mode"                              \
18 19
        --                                                                     \
19 20
        "imapdomo will try to read init.lua and handlers/ACTION.lua from its"  \
20 21
        "configuration directory."                                             \
21 22
        ""                                                                     \
22
-       "four valid actions are understood; since you must write handler for"  \
23
-       "each action you want to use, the meanings below are purely a guide:"  \
24
-       ""                                                                     \
25
-       "    newmail - check for and file new mail"                            \
26
-       "    rewind  - re-add messages from FILTER_FAIL back to FILTER_QUEUE"  \
27
-       "    cleanup - delete or archive old messages"                         \
28
-       "    migrate - move mail between mailboxes"                            \
29
-       ""                                                                     \
30 23
        "See imapfilter_config(5)) for guide and API reference.  Few functions"\
31 24
        "are also available in .imapdomo/common.lua"
32 25
 }
@@ -58,6 +51,14 @@ show_semversion() {
58 51
     exit 0
59 52
 }
60 53
 
54
+lshandlers() {
55
+    #
56
+    # List recognized handlers
57
+    #
58
+    find "$IMAPDOMO_HOME/handlers" -name "*.lua" -printf "%f\n" \
59
+      | sed 's/.lua$//'
60
+}
61
+
61 62
 main() {
62 63
     local Action    # what to do
63 64
     local Debug     # true if debugging
@@ -74,14 +75,15 @@ main() {
74 75
     while true; do case $1 in
75 76
         -c) CdTo="$2"; shift 2 || usage -w "missing value to: $1" ;;
76 77
         -d) Debug=true; PRETTY_DEBUG=true; shift ;;
78
+        -l) lshandlers; exit ;;
77 79
         -V|--version-semver) show_semversion ;;
78 80
         --version) show_version ;;
79 81
         -*) usage -w "unknown argument: '$1'" ;;
80 82
         *)  break ;;
81 83
     esac done
82 84
     Action="$1"; shift
83
-    grep -qw "$Action" <<< "newmail|rewind|cleanup|migrate" \
84
-     || usage -w "invalid action: $Action"
85
+    lshandlers | grep -qw "$Action" \
86
+     || dle "no handler for action: $Action.lua in $IMAPDOMO_HOME/handlers"
85 87
     cmd=$(mkcmd)
86 88
     debug -v cmd
87 89
     bash -n <<<"$cmd" || die