fix whatnow2 send
[mmh] / uip / whatnow2.sh
1 #!/bin/sh
2
3 printhelp()
4 {
5         echo "Usage: $0 command"
6         echo "  commands are:"
7         echo "  edit [editor]"
8         echo "  list"
9         echo "  send [sendargs]"
10         echo "  delete"
11         echo "  display"
12         echo "  attach files"
13         echo "  alist"
14         echo "  detach anum"
15         echo "  refile +folder"
16         echo "  -help"
17         echo "  -Version"
18 }
19
20 version()
21 {
22         if [ $1 -eq 0 ]
23         then
24                 echo "$0 has no own version number, thus this instead:"
25                 folder -Version
26                 exit 0
27         fi
28         echo "$0 has no own version number, thus this instead:" 1>&2
29         folder -Version 1>&2
30         exit 1
31 }
32
33 usage()
34 {
35         if [ $1 -eq 0 ]
36         then
37                 printhelp
38                 exit 0
39         fi
40         printhelp 1>&2
41         exit 1
42 }
43
44 get_editor()
45 {
46         if [ -f "$mhmetafile" ]
47         then
48                 lasteditor=`anno -list -component 'last-editor' $mhmetafile`
49                 if [ -n "$lasteditor" ]
50                 then
51                         editor=`echo $lasteditor | cut -d ' ' -f 1`
52                         mheditor=`mhparam "$editor-next"`
53                         [ -n "$mheditor" ] && return
54                         mheditor=$lasteditor
55                         return
56                 fi
57         fi
58         if [ -n "$MMHEDITOR" ]
59         then
60                 mheditor=$MMHEDITOR
61                 return
62         fi
63         mheditor=`mhparam 'Editor'`
64 }
65
66 get_showproc()
67 {
68         mhshowproc=`mhparam 'listproc'`
69         return
70 }
71
72 get_realpath()
73 {
74         reldir=`dirname "$1"`
75         filename=`basename "$1"`
76         cd $reldir
77         echo "$PWD/$filename"
78         cd -
79 }
80
81 get_attachmentheader()
82 {
83         header=`mhparam 'Attachment-Header'`
84 }
85
86 set_lasteditor()
87 {
88         anno -delete -number all -component 'last-editor' $mhmetafile
89         anno -nodate -component 'last-editor' -text "$1" $mhmetafile
90 }
91
92 create()
93 {
94         if [ -z "$mhdraft" ]
95         then
96                 usage 1
97         fi
98         mhmetafile=$mhdraft.meta
99         touch $mhmetafile
100         if [ -z $mheditor ]
101         then
102                 get_editor
103         fi
104         if [ -n "$mhaltmsg" ]
105         then
106                 anno -nodate -component 'mhaltmsg' -text "$mhaltmsg" $mhmetafile
107         fi
108         if [ -n "$mhdist" ]
109         then
110                 anno -nodate -component 'mhdist' -text "$mhdist" $mhmetafile
111         fi
112         if [ -n "$mhdist" ]
113         then
114                 anno -nodate -component 'mhuse' -text "$mhuse" $mhmetafile
115         fi
116         if [ -n "$mhfolder" ]
117         then
118                 anno -nodate -component 'mhfolder' -text "$mhfolder" $mhmetafile
119         fi
120         if [ -n "$mhmessages" ]
121         then
122                 anno -nodate -component 'mhmessages' -text "$mhmessages" $mhmetafile
123         fi
124         if [ -n "$mhannotate" ]
125         then
126                 anno -nodate -component 'mhannotate' -text "$mhannotate" $mhmetafile
127         fi
128         set_lasteditor "$mheditor"
129         exec $mheditor $mhdraft
130 }
131
132 edit()
133 {
134         if [ $# -eq 0 ]
135         then
136                 get_editor
137         else
138                 mheditor="$@"
139         fi
140
141         set_lasteditor "$mheditor"
142         exec $mheditor $mhdraft
143 }
144
145 list()
146 {
147         get_showproc
148         exec $mhshowproc -file $mhdraft
149 }
150
151 sendfunktion()
152 {
153         export mhaltmsg=`anno -list -component 'mhaltmsg' $mhmetafile`
154         export mhdist=`anno -list -component 'mhdist' $mhmetafile`
155         export mhuse=`anno -list -component 'mhuse' $mhmetafile`
156         export mhfolder=`anno -list -component 'mhfolder' $mhmetafile`
157         export mhmessages=`anno -list -component 'mhmessages' $mhmetafile`
158         export mhannotate=`anno -list -component 'mhannotate' $mhmetafile`
159         send "$@" $mhdraft || exit $?
160         rm -f $mhmetafile
161         exit 0
162 }
163
164 delete()
165 {
166         folder -push $draftfolder >/dev/null 2>&1
167         rmm $draftfolder c
168         folder -pop >/dev/null 2>&1
169         rm $mhmetafile
170 }
171
172 attach()
173 {
174         get_attachmentheader
175         while [ -n "$1" ]
176         do
177                 if [ ! -f "$1" ]
178                 then
179                         echo "file not found: $1" 1>&2
180                         shift
181                         echo -n "folloing files are not attached: " 1>&2
182                         echo -n "$1" 1>&2
183                         echo "$@" 1>&2
184                         exit 1
185                 fi
186                 file=`get_realpath "$1"`
187                 anno -nodate -append -component $header -text "$file" $mhdraft
188                 shift
189         done
190 }
191
192 alist()
193 {
194         get_attachmentheader
195         anno -list -number -component $header $mhdraft
196 }
197
198 detach()
199 {
200         get_attachmentheader
201         while [ -n "$1" ]
202         do
203                 anno -delete -component $header -number "$1" $mhdraft
204                 if [ $? -ne 0 ]
205                 then
206                         echo "can't delet attachment $1" 1>&2
207                         exit 1
208                 fi
209                 shift
210         done
211 }
212
213 display()
214 {
215         mhaltmsg=`anno -list -component 'mhaltmsg' $mhmetafile`
216         get_showproc
217         if [ -z "$mhaltmsg" ]
218         then
219                 echo "no altmsg" 1>&2
220                 exit 1
221         fi
222         exec $mhshowproc -file $mhaltmsg
223 }
224
225 if [ $# -eq 0 ]
226 then
227         create
228         exit
229 fi
230
231 command=$1
232 shift
233
234 draftfolder=`mhparam draftfolder`
235
236 mhdraft=`mhpath $draftfolder c 2>/dev/null`
237 if [ -z "$mhdraft" ]
238 then
239         case $command in
240         -h*)
241                 usage $#
242                 ;;
243         -V*)
244                 version $#
245                 ;;
246         *)
247                 echo "no current message in $draftsfolder" 1>&2
248                 usage 1
249                 ;;
250         esac
251 fi
252 mhmetafile=$mhdraft.meta
253 touch $mhmetafile
254
255
256 case $command in
257 e|ed|edi|edit)
258         edit "$@"
259         ;;
260 l|li|lis|list)
261         [ $# -eq 0 ] || usage 1
262         list
263         ;;
264 s|se|sen|send)
265         sendfunktion "$@"
266         ;;
267 del|dele|delet|delete)
268         [ $# -eq 0 ] || usage 1
269         delete
270         ;;
271 di|dis|disp|displ|displa|display)
272         [ $# -eq 0 ] || usage 1
273         display
274         ;;
275 at|att|atta|attac|attach)
276         attach "$@"
277         ;;
278 al|ali|alis|alist)
279         [ $# -eq 0 ] || usage 1
280         alist
281         ;;
282 det|deta|detac|detach)
283         detach "$@"
284         ;;
285 r|re|ref|refi|refil|refile)
286         refile -file $mhdraft "$@"
287         ;;
288 w|wh|who|whom)
289         whom "$@" $mhdraft
290         ;;
291 -h|-he|-hel|-help)
292         usage $#
293         ;;
294 -V|-Ve|-Ver|-Vers|-Versi|-Versio|-Version)
295         version $#
296         ;;
297 *)
298         usage 1
299         ;;
300 esac