util.c
10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "mail.h"
typedef struct _node {
/* for the msglist expander */
int data;
/* for the environment table */
struct mail_env_entry env_entry;
struct _node *next;
} node;
static node *environment = NULL;
static node *env_cursor = NULL;
/*
* add a new node to the list
*/
static node *
util_ll_add (node *c, int data)
{
c->next = malloc (sizeof (node));
c->data = data;
c->next->env_entry.var = NULL;
c->next->env_entry.set = 0;
c->next->env_entry.value = NULL;
c->next->next = NULL;
return c->next;
}
/*
* free a linked list
*/
static void
util_ll_free (node *c)
{
node *t = c;
while (t != NULL)
{
c = t;
t = t->next;
free (c);
}
}
/*
* expands a standard message list into an array of numbers
* argc is the number of elements being passed in
* argv is the array of strings to parse
* list will be populated with message numbers
* returns the number of items in list
*/
int
util_expand_msglist (const int argc, char **argv, int **list)
{
int i = 0, lc = 0;
int undelete = 0;
int *ret = NULL;
/* let's try a linked list */
node *first = malloc (sizeof (node));
node *current = first;
first->next = NULL;
if (util_command_get (argv[0]) == util_command_get ("undelete"))
undelete = 1;
for (i = 1; i < argc; i++)
{
if (!strcmp (argv[i], "+"))
{
/* first [un]deleted message */
int n = realcursor + 1;
while (n <= total)
{
if (util_isdeleted (n) && !undelete)
n++;
else
break;
}
current = util_ll_add (current, n);
}
else if (!strcmp (argv[i], "-"))
{
/* previous [un]deleted message */
int n = realcursor - 1;
while (n > 0)
{
if (util_isdeleted (n) && !undelete)
n--;
else
break;
}
current = util_ll_add (current, n);
}
else if (!strcmp (argv[i], "."))
{
/* the current cursor location */
current = util_ll_add (current, realcursor);
}
else if (!strcmp (argv[i], "^"))
{
/* first [un]deleted message */
int n = 1;
while (n <= total)
{
if (util_isdeleted (n) && !undelete)
n++;
else
break;
}
current = util_ll_add (current, 1);
}
else if (!strcmp (argv[i], "$"))
{
/* last [un]deleted message */
int n = total;
while (n > 0)
{
if (util_isdeleted (n) && !undelete)
n--;
else
break;
}
current = util_ll_add (current, total);
}
else if (!strcmp (argv[i], "*"))
{
/* all messages */
util_ll_free (first);
current = first = malloc (sizeof (node));
for (i = 1; i <= total; i++)
current = util_ll_add (current, i);
i = argc + 1;
}
else if (argv[i][0] == '/')
{
/* FIXME: all messages with pattern following / in
the subject line, case insensitive */
/* This currently appears to be quit b0rked */
message_t msg;
header_t hdr;
char subj[128];
int j = 1, k = 0, l2 = 0;
int len = strlen (&argv[i][1]);
for (j = 1; j <= total; j++)
{
mailbox_get_message (mbox, j, &msg);
message_get_header (msg, &hdr);
header_get_value (hdr, MU_HEADER_SUBJECT, subj, 128, NULL);
l2 = strlen (subj);
for (k = 0; i < strlen (subj); k++)
{
if (l2-k >= len && !strncasecmp (&argv[i][1], &subj[k], len))
{
current = util_ll_add (current, j);
k = 128;
}
}
}
}
else if (argv[i][0] == ':')
{
/* FIXME: all messages of type argv[i][1] */
}
else if (isalpha(argv[i][0]))
{
/* FIXME: all messages from sender argv[i] */
}
else if (strchr (argv[i], '-') != NULL)
{
/* message range */
int j, x, y;
char *arg = strdup (argv[i]);
for (j=0; j < strlen (arg); j++)
if (arg[j] == '-')
break;
arg[j] = '\0';
x = strtol (arg, NULL, 10);
y = strtol (&(arg[j + 1]), NULL, 10);
for (; x <= y; x++)
current = util_ll_add (current, x);
free (arg);
}
else
{
/* single message */
current = util_ll_add (current, strtol (argv[i], NULL, 10));
}
}
for (current = first; current != NULL; current = current->next)
lc++;
ret = malloc (lc * sizeof (int));
lc = 0;
for (current = first; current != NULL; current = current->next)
ret [lc++] = current->data;
util_ll_free (first);
*list = ret;
return lc-1;
}
/*
* expands command into its command and arguments, then runs command
* cmd is the command to parse and run
* returns exit status of the command
*/
int
util_do_command (const char *c, ...)
{
int argc = 0;
char **argv = NULL;
int status = 0;
function_t *command;
char *cmd = NULL;
va_list ap;
va_start (ap, c);
if (vasprintf (&cmd, c, ap) < 1)
return 0;
if (cmd[0] == '#')
return 0;
if (cmd)
{
if (argcv_get (cmd, &argc, &argv) != 0)
return argcv_free (argc, argv);
command = util_command_get (argv[0]);
}
else
command = util_command_get ("quit");
if (command != NULL)
status = command (argc, argv);
else
{
printf ("Unknown command: %s\n", argv[0]);
status = 1;
}
argcv_free (argc, argv);
return status;
}
/*
* runs a function repeatedly on a msglist
* func is the function to run
* argc is the number of arguments inculding the command and msglist
* argv is the list of strings containing the command and msglist
*/
int
util_msglist_command (function_t *func, int argc, char **argv)
{
int i;
int *list = NULL;
int status = 0;
int number = util_expand_msglist (argc, argv, &list);
realcursor = cursor;
for (i = 0; i < number; i++)
{
cursor = list[i];
if (func (1, argv) != 0)
status = 1;
}
free (list);
cursor = realcursor;
return status;
}
/*
* returns the function to run for command
*/
function_t *
util_command_get (char *cmd)
{
struct mail_command_entry entry = util_find_entry (cmd);
return entry.func;
}
/*
* returns the mail_command_entry structure for the command matching cmd
*/
struct mail_command_entry
util_find_entry (char *cmd)
{
int i = 0, ll = 0, sl = 0;
int len = strlen (cmd);
while (mail_command_table[i].shortname != 0)
{
sl = strlen (mail_command_table[i].shortname);
ll = strlen (mail_command_table[i].longname);
if (sl > ll && !strncmp (mail_command_table[i].shortname, cmd, sl))
return mail_command_table[i];
else if (sl == len && !strcmp (mail_command_table[i].shortname, cmd))
return mail_command_table[i];
else if (sl < len && !strncmp (mail_command_table[i].longname, cmd, len))
return mail_command_table[i];
i++;
}
return mail_command_table[i];
}
/*
* readline tab completion
*/
char **
util_command_completion (char *cmd, int start, int end)
{
if (start == 0)
return completion_matches (cmd, util_command_generator);
return NULL;
}
/*
* more readline
*/
char *
util_command_generator (char *text, int state)
{
static int i, len;
char *name;
if (!state)
{
i = 0;
len = strlen (text);
}
while ((name = mail_command_table[i].longname))
{
if (strlen (mail_command_table[i].shortname) > strlen(name))
name = mail_command_table[i].shortname;
i++;
if (strncmp (name, text, len) == 0)
return (strdup(name));
}
return NULL;
}
/*
* removes whitespace from the beginning and end of a string
*/
char *
util_stripwhite (char *string)
{
register char *s, *t;
for (s = string; whitespace (*s); s++)
;
if (*s == 0)
return s;
t = s + strlen (s) - 1;
while (t > s && whitespace (*t))
t--;
*++t = '\0';
return s;
}
/*
* get the number of columns on the screen
*/
int
util_getcols (void)
{
int columns = 80;
char *col = getenv ("COLUMNS");
if (col)
columns = strtol (col, NULL, 10);
return columns;
}
/*
* get the number of lines on the screen
*/
int
util_getlines (void)
{
int lines = 24;
char *lin = getenv ("LINES");
if (lin)
lines = strtol (lin, NULL, 10);
return lines;
}
/*
* find environment entry var
*/
struct mail_env_entry *
util_find_env (char *variable)
{
char *var = variable;
int len = strlen (var), need_free = 0;
node *t;
if (len < 1)
return NULL;
if (len == strlen ("ask") && !strcmp ("ask", var))
{
var = strdup ("asksub");
len = strlen (var);
need_free = 1;
}
if (environment == NULL)
{
environment = malloc (sizeof (node));
environment->env_entry.var = NULL;
environment->env_entry.set = 0;
environment->env_entry.value = NULL;
environment->next = NULL;
}
for (env_cursor = environment; env_cursor->next != NULL;
env_cursor = env_cursor->next)
{
if (strlen (env_cursor->env_entry.var) == len &&
!strcmp (var, env_cursor->env_entry.var))
{
if (need_free)
free (var);
return &(env_cursor->env_entry);
}
}
env_cursor->env_entry.var = strdup (var);
env_cursor->env_entry.set = 0;
env_cursor->env_entry.value = NULL;
t = env_cursor;
env_cursor = util_ll_add (env_cursor, 0);
if (need_free)
free (var);
return &(t->env_entry);
}
/*
* print the environment
*/
int
util_printenv (int set)
{
for (env_cursor = environment; env_cursor != NULL;
env_cursor = env_cursor->next)
{
if (env_cursor->env_entry.set == set)
{
printf ("%s", env_cursor->env_entry.var);
if (env_cursor->env_entry.value != NULL)
printf ("=%s", env_cursor->env_entry.value);
printf ("\n");
}
}
return 0;
}
/*
* return 1 if a message is deleted
*/
int
util_isdeleted (int n)
{
message_t msg;
attribute_t attr;
if (mailbox_get_message (mbox, n, &msg) != 0)
return 0;
message_get_attribute (msg, &attr);
if (attribute_is_deleted (attr))
return 1;
return 0;
}