/
mu-cmd.c
198 lines (160 loc) · 4.52 KB
/
mu-cmd.c
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
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
/*
** Copyright (C) 2010-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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 3, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <stdlib.h>
#include <stdio.h>
#include "mu-msg.h"
#include "mu-msg-part.h"
#include "mu-cmd.h"
#include "mu-util.h"
#include "mu-str.h"
#include "mu-maildir.h"
#include "mu-contacts.h"
#include "mu-runtime.h"
static void
each_part (MuMsg *msg, MuMsgPart *part, gchar **attach)
{
if (mu_msg_part_looks_like_attachment (part, TRUE) &&
(part->file_name)) {
char *tmp = *attach;
*attach = g_strdup_printf ("%s%s'%s'",
*attach ? *attach : "",
*attach ? ", " : "",
part->file_name);
g_free (tmp);
}
}
/* return comma-sep'd list of attachments */
static gchar *
get_attach_str (MuMsg *msg)
{
gchar *attach;
attach = NULL;
mu_msg_part_foreach (msg, (MuMsgPartForeachFunc)each_part, &attach);
return attach;
}
#define color_maybe(C) do{ if (color) fputs ((C),stdout);}while(0)
static void
print_field (const char* field, const char *val, gboolean color)
{
if (!val)
return;
color_maybe (MU_COLOR_MAGENTA);
mu_util_fputs_encoded (field, stdout);
color_maybe (MU_COLOR_DEFAULT);
fputs (": ", stdout);
if (val) {
color_maybe (MU_COLOR_GREEN);
mu_util_fputs_encoded (val, stdout);
}
color_maybe (MU_COLOR_DEFAULT);
fputs ("\n", stdout);
}
/* we ignore fields for now */
static gboolean
view_msg (MuMsg *msg, const gchar *fields, gboolean summary,
gboolean color)
{
const char *field;
gchar *attachs;
time_t date;
const int SUMMARY_LEN = 5;
print_field ("From", mu_msg_get_from (msg), color);
print_field ("To", mu_msg_get_to (msg), color);
print_field ("Cc", mu_msg_get_cc (msg), color);
print_field ("Bcc", mu_msg_get_bcc (msg), color);
print_field ("Subject", mu_msg_get_subject (msg), color);
if ((date = mu_msg_get_date (msg)))
print_field ("Date", mu_str_date_s ("%c", date),
color);
if ((attachs = get_attach_str (msg))) {
print_field ("Attachments", attachs, color);
g_free (attachs);
}
if (!(field = mu_msg_get_body_text (msg)))
return TRUE; /* no body -- nothing more to do */
if (summary) {
gchar *summ;
summ = mu_str_summarize (field, SUMMARY_LEN);
print_field ("Summary", summ, color);
g_free (summ);
} else {
color_maybe (MU_COLOR_YELLOW);
mu_util_print_encoded ("\n%s\n", field);
color_maybe (MU_COLOR_DEFAULT);
}
return TRUE;
}
MuExitCode
mu_cmd_view (MuConfig *opts)
{
int rv, i;
g_return_val_if_fail (opts, MU_EXITCODE_ERROR);
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_VIEW,
MU_EXITCODE_ERROR);
/* note: params[0] will be 'view' */
if (!opts->params[0] || !opts->params[1]) {
g_warning ("usage: mu view [options] <file> [<files>]");
return MU_EXITCODE_ERROR;
}
;
for (i = 1, rv = MU_EXITCODE_OK;
opts->params[i] && rv == MU_EXITCODE_OK; ++i) {
GError *err = NULL;
MuMsg *msg = mu_msg_new_from_file (opts->params[i], NULL, &err);
if (!msg) {
g_warning ("error: %s", err->message);
g_error_free (err);
return MU_EXITCODE_ERROR;
}
if (!view_msg (msg, NULL, opts->summary, opts->color))
rv = MU_EXITCODE_ERROR;
mu_msg_unref (msg);
}
return rv;
}
MuExitCode
mu_cmd_mkdir (MuConfig *opts)
{
int i;
g_return_val_if_fail (opts, MU_EXITCODE_ERROR);
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_MKDIR,
MU_EXITCODE_ERROR);
if (!opts->params[1]) {
g_warning ("usage: mu mkdir [-u,--mode=<mode>] "
"<dir> [more dirs]");
return MU_EXITCODE_ERROR;
}
for (i = 1; opts->params[i]; ++i) {
GError *err;
err = NULL;
if (!mu_maildir_mkdir (opts->params[i], opts->dirmode,
FALSE, &err)) {
if (err && err->message) {
g_warning ("%s", err->message);
g_error_free (err);
}
return MU_EXITCODE_ERROR;
}
}
return MU_EXITCODE_OK;
}