Skip to content

Commit 67d1763

Browse files
Hitoshi Mitakegitster
authored andcommitted
git-imap-send: Convert LF to CRLF before storing patch to draft box
When storing a message over IMAP (RFC 3501 6.3.11), the message should be in the format of an RFC 2822 message; most notably, CRLF must be used as a line terminator. Convert "\n" line endings in the payload to CRLF before feeding it to IMAP APPEND command. Signed-off-by: Hitoshi Mitake <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 902f235 commit 67d1763

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

imap-send.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ struct msg_data {
9191
char *data;
9292
int len;
9393
unsigned char flags;
94-
unsigned int crlf:1;
9594
};
9695

9796
static const char imap_send_usage[] = "git imap-send < <mbox>";
@@ -1166,6 +1165,44 @@ static int imap_make_flags(int flags, char *buf)
11661165
return d;
11671166
}
11681167

1168+
static void lf_to_crlf(struct msg_data *msg)
1169+
{
1170+
char *new;
1171+
int i, j, lfnum = 0;
1172+
1173+
if (msg->data[0] == '\n')
1174+
lfnum++;
1175+
for (i = 1; i < msg->len; i++) {
1176+
if (msg->data[i - 1] != '\r' && msg->data[i] == '\n')
1177+
lfnum++;
1178+
}
1179+
1180+
new = xmalloc(msg->len + lfnum);
1181+
if (msg->data[0] == '\n') {
1182+
new[0] = '\r';
1183+
new[1] = '\n';
1184+
i = 1;
1185+
j = 2;
1186+
} else {
1187+
new[0] = msg->data[0];
1188+
i = 1;
1189+
j = 1;
1190+
}
1191+
for ( ; i < msg->len; i++) {
1192+
if (msg->data[i] != '\n') {
1193+
new[j++] = msg->data[i];
1194+
continue;
1195+
}
1196+
if (msg->data[i - 1] != '\r')
1197+
new[j++] = '\r';
1198+
/* otherwise it already had CR before */
1199+
new[j++] = '\n';
1200+
}
1201+
msg->len += lfnum;
1202+
free(msg->data);
1203+
msg->data = new;
1204+
}
1205+
11691206
static int imap_store_msg(struct store *gctx, struct msg_data *data)
11701207
{
11711208
struct imap_store *ctx = (struct imap_store *)gctx;
@@ -1175,6 +1212,7 @@ static int imap_store_msg(struct store *gctx, struct msg_data *data)
11751212
int ret, d;
11761213
char flagstr[128];
11771214

1215+
lf_to_crlf(data);
11781216
memset(&cb, 0, sizeof(cb));
11791217

11801218
cb.dlen = data->len;

0 commit comments

Comments
 (0)