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
     -- Handle action if it's valid
34
     -- Handle action if it's valid
35
     --
35
     --
36
     local action = os.getenv("IMAPDOMO_ACTION")
36
     local action = os.getenv("IMAPDOMO_ACTION")
37
+    local file = "handlers/" .. action .. ".lua"
37
     local valid = {
38
     local valid = {
38
         newmail = true,
39
         newmail = true,
39
         rewind = true,
40
         rewind = true,
40
         cleanup = true,
41
         cleanup = true,
41
         migrate = true
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
         return nil
49
         return nil
46
     end
50
     end
47
-    do_if_exists("init.lua")
48
-    do_if_exists("handlers/" .. action .. ".lua")
49
 end
51
 end
50
 
52
 
51
 
53
 

+ 12
- 10
src/imapdomo.skel View File

14
     mkusage "$@" "[options] ACTION" \
14
     mkusage "$@" "[options] ACTION" \
15
         -o                                                                    \
15
         -o                                                                    \
16
             "-c DIR      change to DIR before doing anything"                 \
16
             "-c DIR      change to DIR before doing anything"                 \
17
+            "-l          list handlers and exit"                              \
17
             "-d          turn on debugging mode"                              \
18
             "-d          turn on debugging mode"                              \
18
        --                                                                     \
19
        --                                                                     \
19
        "imapdomo will try to read init.lua and handlers/ACTION.lua from its"  \
20
        "imapdomo will try to read init.lua and handlers/ACTION.lua from its"  \
20
        "configuration directory."                                             \
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
        "See imapfilter_config(5)) for guide and API reference.  Few functions"\
23
        "See imapfilter_config(5)) for guide and API reference.  Few functions"\
31
        "are also available in .imapdomo/common.lua"
24
        "are also available in .imapdomo/common.lua"
32
 }
25
 }
58
     exit 0
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
 main() {
62
 main() {
62
     local Action    # what to do
63
     local Action    # what to do
63
     local Debug     # true if debugging
64
     local Debug     # true if debugging
74
     while true; do case $1 in
75
     while true; do case $1 in
75
         -c) CdTo="$2"; shift 2 || usage -w "missing value to: $1" ;;
76
         -c) CdTo="$2"; shift 2 || usage -w "missing value to: $1" ;;
76
         -d) Debug=true; PRETTY_DEBUG=true; shift ;;
77
         -d) Debug=true; PRETTY_DEBUG=true; shift ;;
78
+        -l) lshandlers; exit ;;
77
         -V|--version-semver) show_semversion ;;
79
         -V|--version-semver) show_semversion ;;
78
         --version) show_version ;;
80
         --version) show_version ;;
79
         -*) usage -w "unknown argument: '$1'" ;;
81
         -*) usage -w "unknown argument: '$1'" ;;
80
         *)  break ;;
82
         *)  break ;;
81
     esac done
83
     esac done
82
     Action="$1"; shift
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
     cmd=$(mkcmd)
87
     cmd=$(mkcmd)
86
     debug -v cmd
88
     debug -v cmd
87
     bash -n <<<"$cmd" || die
89
     bash -n <<<"$cmd" || die