[package] xmail: cope with empty flags options (#9452)
[openwrt/svn-archive/archive.git] / mail / xmail / files / xmail.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008 OpenWrt.org
3
4 START=90
5 STOP=10
6
7 MAIL_ROOT=
8 DAEMON=/usr/bin/XMail
9 NAME=XMail
10
11 append_bool() {
12 local section="$1"
13 local option="$2"
14 local value="$3"
15 local _loctmp
16 config_get_bool _loctmp "$section" "$option" 0
17 [ "$_loctmp" -le 0 ] && return 0
18 append args "$value"
19 }
20
21 append_parm() {
22 local section="$1"
23 local option="$2"
24 local switch="$3"
25 local _loctmp
26 config_get _loctmp "$section" "$option"
27 [ -z "$_loctmp" ] && return 0
28 append args "$switch $_loctmp"
29 }
30
31 xmail() {
32 local cfg="$1"
33
34 append_parm "$cfg" mail_root "-Ms"
35 append_bool "$cfg" debug "-Md"
36 append_parm "$cfg" log_rotate "-Mr"
37 append_parm "$cfg" split_level "-Mx"
38 append_parm "$cfg" rx_buffer "-MR"
39 append_parm "$cfg" tx_buffer "-MS"
40 append_bool "$cfg" maildir_delivery "-MM"
41 append_bool "$cfg" mailbox_delivery "-Mm"
42 append_parm "$cfg" dns_cache_dirs "-MD"
43 append_bool "$cfg" ipv4_only "-M4"
44 append_bool "$cfg" ipv6_only "-M6"
45 append_bool "$cfg" ipv4_then_ipv6 "-M5"
46 append_bool "$cfg" ipv6_then_ipv4 "-M7"
47
48 config_get mail_root $cfg mail_root
49 [ -n "$mail_root" ] || return 0
50 MAIL_ROOT=$mail_root
51 }
52
53 pop3() {
54 local cfg="$1"
55
56 append_bool "$cfg" disable "-P-"
57 append_bool "$cfg" ipv6 "-P6"
58 append_parm "$cfg" server_port "-Pp"
59 append_parm "$cfg" session_timeout "-Pt"
60 append_bool "$cfg" log "-Pl"
61 append_parm "$cfg" delay_timeout "-Pw"
62 append_bool "$cfg" bad_login_hang "-Ph"
63 append_parm "$cfg" local_address "-PI"
64 append_parm "$cfg" num_threads "-PX"
65 }
66
67 pop3s() {
68 local cfg="$1"
69
70 append_bool "$cfg" disable "-B-"
71 append_bool "$cfg" ipv6 "-B6"
72 append_parm "$cfg" server_port "-Bp"
73 append_parm "$cfg" local_address "-BI"
74 }
75
76 smtp() {
77 local cfg="$1"
78
79 append_bool "$cfg" disable "-S-"
80 append_bool "$cfg" ipv6 "-S6"
81 append_parm "$cfg" server_port "-Sp"
82 append_parm "$cfg" session_timeout "-St"
83 append_bool "$cfg" log "-Sl"
84 append_parm "$cfg" local_address "-SI"
85 append_parm "$cfg" num_threads "-SX"
86 append_parm "$cfg" max_recipients "-Sr"
87 append_parm "$cfg" expire_timeout "-Se"
88 }
89
90 smtps() {
91 local cfg="$1"
92
93 append_bool "$cfg" disable "-X-"
94 append_bool "$cfg" ipv6 "-X6"
95 append_parm "$cfg" server_port "-Xp"
96 append_parm "$cfg" local_address "-XI"
97 }
98
99 smail() {
100 local cfg="$1"
101
102 append_parm "$cfg" num_threads "-Qn"
103 append_parm "$cfg" next_try_timeout "-Qt"
104 append_parm "$cfg" ratio "-Qi"
105 append_parm "$cfg" retries "-Qr"
106 append_bool "$cfg" log "-Ql"
107 append_parm "$cfg" filters_timeout "-QT"
108 append_bool "$cfg" filter_log "-Qg"
109 }
110
111 psync() {
112 local cfg="$1"
113
114 append_bool "$cfg" disable "-Y-"
115 append_parm "$cfg" interval "-Yi"
116 append_parm "$cfg" num_threads "-Yt"
117 append_parm "$cfg" timeout "-Yi"
118 append_bool "$cfg" log "-Yl"
119 }
120
121 finger() {
122 local cfg="$1"
123
124 append_bool "$cfg" disable "-F-"
125 append_bool "$cfg" ipv6 "-F6"
126 append_parm "$cfg" server_port "-Fp"
127 append_bool "$cfg" log "-Fl"
128 append_parm "$cfg" local_address "-FI"
129 }
130
131 ctrl() {
132 local cfg="$1"
133
134 append_bool "$cfg" disable "-C-"
135 append_bool "$cfg" ipv6 "-C6"
136 append_parm "$cfg" server_port "-Cp"
137 append_parm "$cfg" session_timeout "-Ct"
138 append_bool "$cfg" log "-Cl"
139 append_parm "$cfg" local_address "-CI"
140 append_parm "$cfg" num_threads "-CX"
141 }
142
143 ctrls() {
144 local cfg="$1"
145
146 append_bool "$cfg" disable "-W-"
147 append_bool "$cfg" ipv6 "-W6"
148 append_parm "$cfg" server_port "-Wp"
149 append_parm "$cfg" session_timeout "-Wt"
150 }
151
152 lmail() {
153 local cfg="$1"
154
155 append_parm "$cfg" num_threads "-Ln"
156 append_parm "$cfg" sleep_timeout "-Lt"
157 append_bool "$cfg" log "-Ll"
158 }
159
160 start() {
161 test -f $DAEMON || exit 0
162
163 set -e
164 ulimit -c 10000
165 ulimit -s 128
166
167 config_load xmail
168
169 args=""
170
171 config_foreach xmail xmail
172 config_foreach pop3 pop3
173 config_foreach pop3s pop3s
174 config_foreach smtp smtp
175 config_foreach smtps smtps
176 config_foreach smail smail
177 config_foreach psync psync
178 config_foreach finger finger
179 config_foreach ctrl ctrl
180 config_foreach ctrls ctrls
181 config_foreach lmail lmail
182
183 [ -z "$MAIL_ROOT" ] && exit 0
184 [ -d $MAIL_ROOT ] || exit 0
185
186 export MAIL_ROOT
187
188 rm -f /var/run/$NAME.pid
189
190 $DAEMON $args
191
192 MAX_WAIT=20
193 while [ ! -f /var/run/$NAME.pid -a $MAX_WAIT -gt 0 ]
194 do
195 sleep 1
196 MAX_WAIT=`expr $MAX_WAIT - 1`
197 done
198 }
199
200 stop() {
201 if [ -f /var/run/$NAME.pid ]
202 then
203 config_load xmail
204 config_foreach xmail xmail
205 [ -z "$MAIL_ROOT" ] || echo `date` > $MAIL_ROOT/.shutdown
206 kill -INT `cat /var/run/$NAME.pid`
207
208 MAX_WAIT=20
209 while [ -f $MAIL_ROOT/.shutdown -a $MAX_WAIT -gt 0 ]
210 do
211 sleep 1
212 MAX_WAIT=`expr $MAX_WAIT - 1`
213 done
214 fi
215 }
216
217 restart() {
218 stop
219 sleep 1
220 start
221 }