Пример #1
0
        private void OnSyncProgressChanged(ImapEnvelope envelope)
        {
            var handler = EnvelopeFetched;

            if (handler != null)
            {
                handler(this, new EnvelopeFetchedEventArgs(envelope));
            }
        }
Пример #2
0
        public static ImapEnvelope Parse(string text)
        {
            // Escape nested quotes.
            // TODO: Should be included in the regex.
            text = text.Replace("\\\"", "%%%");

            var envelope = new ImapEnvelope();
            var matches  = FetchMetaRegex.Matches(text);

            foreach (Match match in matches)
            {
                if (match.Value.StartsWith("RFC822.SIZE", StringComparison.InvariantCultureIgnoreCase))
                {
                    envelope.Size = long.Parse(match.Value.Split(' ').Last());
                    continue;
                }
                if (match.Value.StartsWith("UID", StringComparison.InvariantCultureIgnoreCase))
                {
                    envelope.Uid = long.Parse(match.Value.Split(' ').Last());
                    continue;
                }
                if (match.Value.StartsWith("INTERNALDATE", StringComparison.InvariantCultureIgnoreCase))
                {
                    var date = DateRegex.Match(match.Value).Value;
                    envelope.InternalDate = DateTime.Parse(date);
                    continue;
                }
                if (match.Value.StartsWith("FLAGS", StringComparison.InvariantCultureIgnoreCase))
                {
                    const string pattern = @"\\[A-za-z0-9\*]+";
                    var          flags   = Regex.Matches(match.Value, pattern, RegexOptions.CultureInvariant)
                                           .Cast <Match>()
                                           .Select(x => x.Value);

                    envelope.AddFlags(flags);
                }
            }

            String[] t    = { "ENVELOPE" };
            var      temp = text.Split(t, StringSplitOptions.None);

            matches          = EnvelopeRegex.Matches(temp[1]);
            envelope.Subject = TransferEncoder.Decode(matches[1].Value)
                               .TrimQuotes()
                               .Replace("%%%", "\"");

            envelope.AddContactsToFrom(ParseContacts(matches[2].Value));
            envelope.AddContactsToSender(ParseContacts(matches[3].Value));
            envelope.ReplyTo = ParseContacts(matches[4].Value).FirstOrDefault();
            envelope.AddContactsToRecipients(ParseContacts(matches[5].Value));
            envelope.AddContactsToCc(ParseContacts(matches[6].Value));
            envelope.AddContactsToBcc(ParseContacts(matches[7].Value));
            envelope.InReplyTo = matches[8].Value.TrimQuotes().TrimNil();
            envelope.MessageId = matches[9].Value.TrimQuotes().TrimNil();

            return(envelope);
        }
Пример #3
0
        private async Task <IEnumerable <ImapEnvelope> > ReadFetchEnvelopesResponseAsync(string commandId)
        {
            var lines = new List <ImapResponseLine> {
                await _connection.ReadAsync()
            };
            var envelopes = new List <ImapEnvelope>();

            while (true)
            {
                var line = await _connection.ReadAsync();

                if (line.IsUntagged || line.TerminatesCommand(commandId))
                {
                    using (var writer = new StringWriter()) {
                        foreach (var l in lines)
                        {
                            await writer.WriteAsync(l.Text);

                            await writer.WriteLineAsync();
                        }
                        ImapEnvelope envelope = null;
                        try {
                            envelope = ImapEnvelope.Parse(writer.ToString());
                            envelopes.Add(envelope);
                        } catch (Exception ex) {
                            Debug.WriteLine(ex);
                        } finally {
                            OnSyncProgressChanged(envelope);
                        }
                    }
                    lines.Clear();
                }

                if (line.TerminatesCommand(commandId))
                {
                    break;
                }

                lines.Add(line);
            }

            return(envelopes);
        }
 public EnvelopeFetchedEventArgs(ImapEnvelope envelope)
 {
     _envelope = envelope;
 }