mail.c
4.83 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
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 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"
const char *argp_program_version = "mail (" PACKAGE ") " VERSION;
const char *argp_program_bug_address = "<bug-mailutils@gnu.org>";
static char doc[] = "GNU mail -- the standard /bin/mail interface";
static char args_doc[] = "[address...]";
static struct argp_option options[] = {
{"exist", 'e', 0, 0, "Return true if mail exists"},
{"file", 'f', "FILE", OPTION_ARG_OPTIONAL,
"Operate on mailbox FILE (default ~/mbox)"},
{"FIXME", 'F', 0, 0, "Save messages according to sender"},
{"headers", 'H', 0, 0, "Write a header summary and exit"},
{"ignore", 'i', 0, 0, "Ignore interrupts"},
{"norc", 'n', 0, 0, "Do not read the system mailrc file"},
{"nosum", 'N', 0, 0, "Do not display initial header summary"},
{"print", 'p', 0, 0, "Print all mail to standard out"},
{"quit", 'q', 0, 0, "Cause interrupts to terminate program"},
{"read", 'r', 0, 0, "Write messages in FIFO order"},
{"subject", 's', "SUBJ", 0, "Send a message with a Subject of SUBJ"},
{"to", 't', 0, 0, "Preced message by a list of addresses"},
{"user", 'u', "USER", 0, "Operate on USER's mailbox"},
{ 0 }
};
struct arguments
{
char **args;
int exist, print, quit, read, to, header, ignore, norc, nosum;
char *file;
char *subject;
char *user;
};
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
struct arguments *args = state->input;
switch (key)
{
case 'e':
args->exist = 1;
break;
case 'f':
if (arg != NULL)
args->file = arg;
else
{
char *home = getenv("HOME");
int len = strlen (home) + strlen ("/mbox") + 1;
args->file = malloc(len * sizeof (char));
strcpy (args->file, home);
strcat (args->file, "/mbox");
free (home);
}
break;
case 'p':
args->print = 1;
break;
case 'q':
args->quit = 1;
break;
case 'r':
args->read = 1;
break;
case 't':
args->to = 1;
break;
case 'H':
args->header = 1;
break;
case 'i':
args->ignore = 1;
break;
case 'n':
args->norc = 1;
break;
case 'N':
args->nosum = 1;
break;
case 's':
args->subject = arg;
break;
case 'u':
args->user = arg;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, doc };
int
main (int argc, char **argv)
{
char *command = NULL, *cmd = NULL;
char *from[] = { "from", "*" };
struct arguments args;
cursor = 1;
realcursor = cursor;
args.exist = 0;
args.print = 0;
args.quit = 0;
args.read = 0;
args.to = 0;
args.header = 0;
args.ignore = 0;
args.norc = 0;
args.nosum = 0;
args.file = NULL;
args.subject = NULL;
args.user = NULL;
argp_parse (&argp, argc, argv, 0, 0, &args);
if (args.file == NULL)
{
if (mailbox_create_default (&mbox, args.user) != 0)
exit (EXIT_FAILURE);
}
else if (mailbox_create (&mbox, args.file, 0) != 0)
exit (EXIT_FAILURE);
if (mailbox_open (mbox, MU_STREAM_READ) != 0)
exit (EXIT_FAILURE);
if (mailbox_messages_count (mbox, &total) != 0)
exit (EXIT_FAILURE);
if (args.exist)
{
if (total < 1)
exit (1);
else
exit (0);
}
/* mail_from (2, from); */
/* FIXME: this is bad form */
for (cursor=1; cursor < total; cursor++)
mail_from (1, from);
cursor = realcursor;
/* Initialize readline */
rl_readline_name = "mail";
rl_attempted_completion_function = (CPPFunction *)util_command_completion;
while (1)
{
int len;
free (command);
command = readline ("? ");
len = strlen (command);
while (command[len-1] == '\\')
{
char *buf;
char *command2 = readline ("> ");
command[len-1] = '\0';
buf = malloc ((len + strlen (command2)) * sizeof (char));
strcpy (buf, command);
strcat (buf, command2);
free (command);
command = buf;
len = strlen (command);
}
cmd = util_stripwhite (command);
util_do_command (cmd);
add_history (cmd);
}
}