Browse Source

Sweep handlers under separate function

For code readability and maintenance
Alois Mahdal 7 years ago
parent
commit
f5a754e2dd
1 changed files with 33 additions and 13 deletions
  1. 33
    13
      src/imapdomo.skel

+ 33
- 13
src/imapdomo.skel View File

31
     local msg=$1
31
     local msg=$1
32
     local self
32
     local self
33
     self=$(basename "$0")
33
     self=$(basename "$0")
34
-    die "bug in $self (__MKIT_PROJ_VERSION__): $msg"
34
+    warn "bug in $self (__MKIT_PROJ_VERSION__): $msg"
35
 }
35
 }
36
 
36
 
37
 mkcmd() {
37
 mkcmd() {
73
       | sed 's/.lua$//'
73
       | sed 's/.lua$//'
74
 }
74
 }
75
 
75
 
76
+handle() {
77
+    #
78
+    # Handle action $Action
79
+    #
80
+    local cmd       # imapfilter command
81
+    lshandlers | grep -qw "$Action" || {
82
+        warn "no handler for action: $Action.lua in $CfgDir/handlers"
83
+        return 2
84
+    }
85
+    cmd=$(mkcmd)
86
+    debug -v cmd
87
+    bash -n <<<"$cmd" || {
88
+        bug "bad syntax of cmd: '$cmd'"
89
+        return 3
90
+    }
91
+    if test -n "$CdTo";
92
+    then
93
+        cd "$CdTo" || {
94
+            warn "cannot chdir to: $CdTo"
95
+            return 3
96
+        }
97
+    fi
98
+    mkdir -p "$HeaderDir" || {
99
+        warn "cannot create header directory: $HeaderDir"
100
+        return 3
101
+    }
102
+    eval "$cmd"
103
+}
104
+
76
 main() {
105
 main() {
77
     local Action    # what to do
106
     local Action    # what to do
78
     local Debug     # true if debugging
107
     local Debug     # true if debugging
79
-    local cmd       # imapfilter command
80
     local CfgDir    # config directory
108
     local CfgDir    # config directory
81
     local LogDir    # directory to store logs
109
     local LogDir    # directory to store logs
82
     local HeaderDir # directory to store headers by save_header()
110
     local HeaderDir # directory to store headers by save_header()
83
     local CdTo      # change dir to this before running imapfilter
111
     local CdTo      # change dir to this before running imapfilter
84
     local Certs     # certificate storage
112
     local Certs     # certificate storage
113
+    local es=0      # exit status of this function
85
     CfgDir="$IMAPDOMO_CFGDIR"
114
     CfgDir="$IMAPDOMO_CFGDIR"
86
     LogDir="$IMAPDOMO_USER_CACHE/logs"
115
     LogDir="$IMAPDOMO_USER_CACHE/logs"
87
     HeaderDir="$IMAPDOMO_USER_CACHE/headers"
116
     HeaderDir="$IMAPDOMO_USER_CACHE/headers"
101
     Action="$1"; shift
130
     Action="$1"; shift
102
     test -n "$Action" || usage -w "no action specified"
131
     test -n "$Action" || usage -w "no action specified"
103
     debug -v Action CfgDir LogDir HeaderDir Debug CdTo
132
     debug -v Action CfgDir LogDir HeaderDir Debug CdTo
104
-    lshandlers | grep -qw "$Action" \
105
-     || die "no handler for action: $Action.lua in $CfgDir/handlers"
106
-    cmd=$(mkcmd)
107
-    debug -v cmd
108
-    bash -n <<<"$cmd" || bug "bad syntax of cmd: '$cmd'"
109
-    if test -n "$CdTo";
110
-    then
111
-        cd "$CdTo" || die "cannot chdir to: $CdTo"
112
-    fi
113
-    mkdir -p "$HeaderDir" || die "cannot create header directory: $HeaderDir"
114
-    eval "$cmd"
133
+    handle "$Action"; es=$?
134
+    return $es
115
 }
135
 }
116
 
136
 
117
 main "$@"
137
 main "$@"