9front - general discussion about 9front
 help / color / mirror / Atom feed
From: risto.salminen@gmx.com
To: 9front@9front.org
Subject: [9front] upas/fs: handle outlook's quirks
Date: Sat, 09 Oct 2021 12:36:02 +0200	[thread overview]
Message-ID: <405ABA392F41295D7D6361D94B2A8E08@gmx.com> (raw)

Hello all,

Outlook's IMAP server often tells incorrect message
sizes.  This patch makes upas/fs handle that at least
much better than before.

If the length of a message chunk received from the server
is less than the requested chunk length, then the whole
message has been received, which means that the message
was smaller than expected, and upas/fs must adjust its
internal notion of the message length accordingly.
If this is not done, the message body as presented by
upas/fs will contain trailing garbage, that is, previous
memory contents.

The handling of a message larger than expected works
exactly in the same way as the current quirk for Gmail.
When requesting the last chunk of a message, upas/fs
increases the chunk length, so that hopefully the whole
message will be read, and then afterwards also adjusts
its internal notion of the message length accordingly.

To benefit from this patch, the desired mailbox indices
have to be recreated, by manually removing them, so that
upas/fs will then create them automatically.  Otherwise,
things will continue to work as before.

Would this be useful for the broader audience?

Thanks,
Risto

diff a8ad3fb3d00551bad97e7b10a9666821fe4b601a uncommitted
--- a/sys/src/cmd/upas/fs/cache.c
+++ b/sys/src/cmd/upas/fs/cache.c
@@ -199,7 +199,7 @@
 		return -1;
 	}
 	if(m->size - sz0)
-		l += m->size - sz0;	/* awful botch for gmail */
+		l += m->size - sz0;	/* awful botch for gmail and outlook */
 	if(expand){
 		/* grumble.  poor planning. */
 		if(m->badchars > 0)
--- a/sys/src/cmd/upas/fs/imap.c
+++ b/sys/src/cmd/upas/fs/imap.c
@@ -29,6 +29,7 @@
 	Fssl	= 1<<0,
 	Fdebug	= 1<<1,
 	Fgmail	= 1<<2,
+	Foutlook= 1<<3,
 };

 typedef struct {
@@ -331,7 +332,7 @@
 }

 static char*
-fetchrsp(Imap *imap, char *p, Mailbox *, Message *m, int idx)
+fetchrsp(Imap *imap, char *p, Mailbox *, Message *m, int idx, ulong l0)
 {
 	char *f[15], *s, *q;
 	int i, n, a;
@@ -383,10 +384,12 @@
 				return confused;
 			l = xnum(f[i + 1], '{', '}');
 			a = o + l - m->ibadchars - m->size;
-			if(a > 0){
-				assert(imap->flags & Fgmail);
+			if(l < l0 || a > 0){
+				/* message smaller or larger than expected */
+				assert((imap->flags & Fgmail) || (imap->flags & Foutlook));
 				m->size = o + l;
 				msgrealloc(m, m->size);
+				assert(m->ibadchars < m->size);
 				m->size -= m->ibadchars;
 			}
 			if(Bread(&imap->bin, m->start + o, l) != l){
@@ -441,7 +444,7 @@
  *  data or other informational lines mixed in.
  */
 static char*
-imap4resp0(Imap *imap, Mailbox *mb, Message *m)
+imap4resp0(Imap *imap, Mailbox *mb, Message *m, ulong l)
 {
 	char *e, *line, *p, *ep, *op, *q, *verb;
 	int n, idx, unexp;
@@ -508,7 +511,7 @@
 					if(ep[-1] == ')')
 						*--ep = 0;
 				}
-				if(e = fetchrsp(imap, p, mb, m, n - 1))
+				if(e = fetchrsp(imap, p, mb, m, n - 1, l))
 					eprint("imap: fetchrsp: %s\n", e);
 				if(n > 0 && n <= imap->muid && n > imap->nuid)
 					imap->nuid = n;
@@ -553,7 +556,7 @@
 static char*
 imap4resp(Imap *i)
 {
-	return imap4resp0(i, 0, 0);
+	return imap4resp0(i, 0, 0, 0);
 }

 static int
@@ -590,7 +593,7 @@
 	if(p > buf){
 		p[-1] = 0;
 		imap4cmd(imap, "uid store %lud flags (%s)", (ulong)m->imapuid, buf);
-		imap4resp0(imap, mb, m);
+		imap4resp0(imap, mb, m, 0);
 	}
 }

@@ -861,6 +864,16 @@
 	return l;
 }

+/* outlook also lies about message sizes */
+static ulong
+outlookdiscount(Message *m, uvlong o, ulong l)
+{
+	if((m->cstate&Cidx) == 0)
+	if(o + l == m->size)
+		return l + 100 + (o + l)/3;
+	return l;
+}
+
 static int
 imap4fetch(Mailbox *mb, Message *m, uvlong o, ulong l)
 {
@@ -870,9 +883,11 @@
 	imap = mb->aux;
 	if(imap->flags & Fgmail)
 		l = gmaildiscount(m, o, l);
+	if(imap->flags & Foutlook)
+		l = outlookdiscount(m, o, l);
 	idprint(imap, "uid fetch %lud (flags body.peek[]<%llud.%lud>)\n", (ulong)m->imapuid, o, l);
 	imap4cmd(imap, "uid fetch %lud (flags body.peek[]<%llud.%lud>)", (ulong)m->imapuid, o, l);
-	resp = imap4resp0(imap, mb, m);
+	resp = imap4resp0(imap, mb, m, (m->cstate&Cidx) ? 0 : l);
 	if(!isokay(resp)){
 		eprint("imap: imap fetch failed: %s\n", resp);
 		return -1;
@@ -1165,6 +1180,8 @@
 	imap->host = f[2];
 	if(strstr(imap->host, "gmail.com"))
 		imap->flags |= Fgmail;
+	if(strstr(imap->host, "outlook.office365.com"))
+		imap->flags |= Foutlook;
 	imap->refreshtime = 60;
 	if(nf < 4)
 		imap->user = nil;

             reply	other threads:[~2021-10-09 10:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-09 10:36 risto.salminen [this message]
2021-10-09 14:22 ` ori
2021-10-12  7:28   ` Risto Salminen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=405ABA392F41295D7D6361D94B2A8E08@gmx.com \
    --to=risto.salminen@gmx.com \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).