@matheusbsbs / matheus-bagliano / commits / 67d176300c
Fork 0

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 <mitake@dcl.info.waseda.ac.jp> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Hitoshi Mitake committed Feb 12, 2010 at 20:36 UTC 67d176300c0a79d5cf65402c641fcec7c5388f29
1 file changed +39 -1
imap-send.c
+39 -1
@@ -91,7 +91,6 @@ struct msg_data {
91 char *data;
92 int len;
93 unsigned char flags;
94 - unsigned int crlf:1;
94 };
95
96 static const char imap_send_usage[] = "git imap-send < <mbox>";
@@ -1166,6 +1165,44 @@ static int imap_make_flags(int flags, char *buf)
1165 return d;
1166 }
1167
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 +
1206 static int imap_store_msg(struct store *gctx, struct msg_data *data)
1207 {
1208 struct imap_store *ctx = (struct imap_store *)gctx;
@@ -1175,6 +1212,7 @@ static int imap_store_msg(struct store *gctx, struct msg_data *data)
1212 int ret, d;
1213 char flagstr[128];
1214
1215 + lf_to_crlf(data);
1216 memset(&cb, 0, sizeof(cb));
1217
1218 cb.dlen = data->len;