[PATCH 4/4] git-imap-send: Add method to convert from LF to CRLF

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Some strict IMAP servers (e.g. Cyrus) don't
allow "bare newlines ('\n')" in messages.
So I added new boolean option "lf-to-crlf" to imap section.
If this option enabled, git-imap-send converts LF to CRLF("\r\n").

If you want to use it, add line:
	lf-to-crlf
to [imap] section of your .gitconfig .

This patch also adds description to Documentation/git-imap-send.txt .

Cc: Jeremy White <jwhite@xxxxxxxxxxxxxxx>
Cc: Robert Shearman <robertshearman@xxxxxxxxx>
Signed-off-by: Hitoshi Mitake <mitake@xxxxxxxxxxxxxxxxxxxxx>
---
 Documentation/git-imap-send.txt |    5 +++++
 imap-send.c                     |   30 ++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/Documentation/git-imap-send.txt b/Documentation/git-imap-send.txt
index 9b2052f..c4c0670 100644
--- a/Documentation/git-imap-send.txt
+++ b/Documentation/git-imap-send.txt
@@ -75,6 +75,11 @@ imap.auth-method::
 	Specify authenticate method for authentication with IMAP server.
 	Current supported method is 'CRAM-MD5' only.
 
+imap.lf-to-crlf::
+	If you use strict IMAP server (e.g. Cyrus),
+	"bare newlines ('\n')" in messages are not allowed.
+	If this option enabled, git-imap-send converts LF to CRLF("\r\n").
+
 Examples
 ~~~~~~~~
 
diff --git a/imap-send.c b/imap-send.c
index 3ed9fc2..5329e28 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -147,6 +147,7 @@ struct imap_server_conf {
 	int ssl_verify;
 	int use_html;
 	char *auth_method;
+	int lf_to_crlf;
 };
 
 static struct imap_server_conf server = {
@@ -160,6 +161,7 @@ static struct imap_server_conf server = {
 	1,   	/* ssl_verify */
 	0,   	/* use_html */
 	NULL,	/* auth_method */
+	0,	/* lf_to_crlf */
 };
 
 struct imap_store_conf {
@@ -1242,6 +1244,29 @@ static int imap_make_flags(int flags, char *buf)
 	return d;
 }
 
+static void lf_to_crlf(struct msg_data *msg)
+{
+	char *new;
+	int i, j, lfnum = 0;
+
+	for (i = 0; i < msg->len; i++) {
+		if (msg->data[i] == '\n')
+			lfnum++;
+	}
+	new = xcalloc(msg->len + lfnum, sizeof(char));
+	for (i = 0, j = 0; i < msg->len; i++) {
+		if (msg->data[i] != '\n') {
+			new[j++] = msg->data[i];
+			continue;
+		}
+		new[j++] = '\r';
+		new[j++] = '\n';
+	}
+	msg->len += lfnum;
+	free(msg->data);
+	msg->data = new;
+}
+
 static int imap_store_msg(struct store *gctx, struct msg_data *data)
 {
 	struct imap_store *ctx = (struct imap_store *)gctx;
@@ -1253,6 +1278,9 @@ static int imap_store_msg(struct store *gctx, struct msg_data *data)
 
 	memset(&cb, 0, sizeof(cb));
 
+	if (server.lf_to_crlf)
+		lf_to_crlf(data);
+
 	cb.dlen = data->len;
 	cb.data = xmalloc(cb.dlen);
 	memcpy(cb.data, data->data, data->len);
@@ -1408,6 +1436,8 @@ static int git_imap_config(const char *key, const char *val, void *cb)
 		server.ssl_verify = git_config_bool(key, val);
 	else if (!strcmp("preformattedhtml", key))
 		server.use_html = git_config_bool(key, val);
+	else if (!strcmp("lf-to-crlf", key))
+		server.lf_to_crlf = git_config_bool(key, val);
 	else if (!val)
 		return config_error_nonbool(key);
 
-- 
1.7.0.rc1.52.gf7cc2.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]