public ReceiveMessageDetailsTask(ChannelConfiguration config, IClientInputChannel channel, ChannelMessageHeader header, ChannelFolder folder) { this.config = config; this.channel = channel; this.header = header; this.folder = folder; }
public void MarkUnread(ChannelMessageHeader message) { var client = new ExchangeClient(Hostname, CredentialsProvider); var messageId = client.GetMessageId(message.MessageNumber).ItemId; client.SetMessageReadState(messageId, false); }
public IEnumerable <ChannelMessageHeader> GetHeaders() { var client = new ExchangeClient(Hostname, CredentialsProvider); foreach (MessageType inboxItem in client.GetHeaders(folder).Reverse().Skip((int)StartIndex).Take((int)PageSize)) { var header = new ChannelMessageHeader { MessageNumber = inboxItem.ItemId.Id, MessageIdentifier = inboxItem.InternetMessageId, Context = inboxItem.Subject, // Not sending size because exchange changes can the size due // to user actions usch as change priority. //Size = inboxItem.Size, DateReceived = inboxItem.DateTimeReceived, IsRead = inboxItem.IsRead, // Not processing IsStarred because somebody could send you an // email with importance: high and would end up getting starred; // feature just asking for abuse :-) //IsStarred = inboxItem.Importance == ImportanceChoicesType.High, }; yield return(header); } }
public void SetStarred(ChannelMessageHeader message, bool starred) { var client = new ExchangeClient(Hostname, CredentialsProvider); var messageId = client.GetMessageId(message.MessageNumber).ItemId; client.SetMessageImportance(messageId, starred); }
protected override List <ChannelMessageHeader> GetHeadersToDownload(List <ChannelMessageHeader> headers) { var headersToDownload = new List <ChannelMessageHeader>(); foreach (var header in headers) { Message message; ChannelMessageHeader header1 = header; using (mailbox.Messages.ReaderLock) message = mailbox.Messages.FirstOrDefault(m => (m.SourceChannelId == config.ChannelId || m.TargetChannelId == config.ChannelId) && m.MessageIdentifier == header1.MessageIdentifier && m.Size == header1.Size); if (message != null) { message.ReceiveLabels.Add(new Label(folder.Name, ParseLabelType(folder.Name), header.MessageNumber)); } else { // Message not found, perform a download headersToDownload.Add(header1); } } return(headersToDownload); }
public void CopyToFolder(ChannelMessageHeader message, ChannelFolder folder) { var client = new ExchangeClient(Hostname, CredentialsProvider); var messageId = client.GetMessageId(message.MessageNumber).ItemId; client.CopyMessageToFolder(messageId, folder.FolderId); }
public void Purge(ChannelMessageHeader message) { var client = new ExchangeClient(Hostname, CredentialsProvider); var messageId = client.GetMessageId(message.MessageNumber).ItemId; client.DeleteMessage(messageId); }
public void RemoveFromFolder(ChannelMessageHeader message, ChannelFolder folder) { var client = new ExchangeClient(Hostname, CredentialsProvider); var messageId = client.GetMessageId(message.MessageNumber).ItemId; client.DeleteMessage(messageId); }
public void AddLabel(ChannelMessageHeader message, string labelname) { var client = new ExchangeClient(Hostname, CredentialsProvider); var messageId = client.GetMessageId(message.MessageNumber).ItemId; client.CopyMessageToFolder(messageId, folders.First(f => f.Name.ToLower() == labelname.ToLower()).FolderId); }
public virtual void Purge(ChannelMessageHeader message) { // todo this is not working correctly with gmail because it moves stuff to the All Mail folder? //if (String.IsNullOrEmpty(message.SourceFolder)) return; //connection.Client.SelectFolder(message.SourceFolder); //connection.Client.DeleteMessages(new IMAP_SequenceSet(message.MessageNumber), true); }
/// <summary> /// Gets the headers to download based on the ones that are allready in the database. /// </summary> /// <param name="headers">The headers.</param> /// <returns>List of headers to download.</returns> protected virtual List <ChannelMessageHeader> GetHeadersToDownload(List <ChannelMessageHeader> headers) { var headersToDownload = new List <ChannelMessageHeader>(); foreach (var header in headers) { Message message = null; ChannelMessageHeader header1 = header; // The sent messages case ensures that we don't retrieve the item that has been sent from Inbox2 but is also // in (for example) your GMail sent items. We do this by appending the i2mpMessageid when sending the message. if (folder.ToStorageFolder() == Folders.SentItems) { if (!String.IsNullOrEmpty(header.Metadata.i2mpMessageId)) { using (mailbox.Messages.ReaderLock) message = mailbox.Messages.FirstOrDefault(m => m.MessageKey == header1.Metadata.i2mpMessageId); } } // Message can still be null (message did not have a match or we are looking at some other folder) if (message == null) { using (mailbox.Messages.ReaderLock) message = mailbox.Messages.FirstOrDefault(m => (m.SourceChannelId == config.ChannelId || m.TargetChannelId == config.ChannelId) && m.MessageNumber == header1.MessageNumber && m.Size == header1.Size); } // Try to find message based on message identifier (not all channels support this) if (message == null && !String.IsNullOrEmpty(header1.MessageIdentifier)) { using (mailbox.Messages.ReaderLock) message = mailbox.Messages.FirstOrDefault(m => (m.SourceChannelId == config.ChannelId || m.TargetChannelId == config.ChannelId) && m.MessageIdentifier == header1.MessageIdentifier && m.Size == header1.Size); } if (message == null) { headersToDownload.Add(header); } else { if (CheckReadStates(message, header1)) { // Check folder of message if (message.MessageFolder != Folders.Archive && message.MessageFolder != Folders.Trash && message.MessageFolder != folder.ToStorageFolder()) { message.MoveToFolder(folder.ToStorageFolder()); } } } } return(headersToDownload); }
public IEnumerable <ChannelMessage> GetMessage(ChannelMessageHeader header) { using (var ms = new MemoryStream()) { connection.Client.GetMessage(Int32.Parse(header.MessageNumber), ms); yield return(ChannelMessageParser.From(ms, header)); } }
public void MarkDeleted(ChannelMessageHeader message) { var trash = GetFolders().First(f => f.FolderType == ChannelFolderType.Trash); var client = new ExchangeClient(Hostname, CredentialsProvider); var messageId = client.GetMessageId(message.MessageNumber).ItemId; client.MoveMessageToFolder(messageId, trash.FolderId); }
/// <summary> /// Gets the headers to download based on the ones that are allready in the database. /// </summary> /// <param name="headers">The headers.</param> /// <returns>List of headers to download.</returns> protected virtual List <ChannelMessageHeader> GetHeadersToDownload(List <ChannelMessageHeader> headers) { var headersToDownload = new List <ChannelMessageHeader>(); foreach (var header in headers) { Message message = null; ChannelMessageHeader header1 = header; // The sent messages case ensures that we don't retrieve the item that has been sent from Inbox2 but is also // in (for example) your GMail sent items. We do this by appending the i2mpMessageid when sending the message. if (folder.ToStorageFolder() == Folders.SentItems) { if (!String.IsNullOrEmpty(header.Metadata.i2mpMessageId)) { message = dataService.SelectBy <Message>(new { MessageKey = header1.Metadata.i2mpMessageId }); } } // Message can still be null (message did not have a match or we are looking at some other folder) if (message == null) { message = dataService.SelectBy <Message>( String.Format("select * from Messages where (SourceChannelId='{0}' or TargetChannelId='{0}') and MessageNumber='{1}' and Size='{2}'", config.ChannelId, header1.MessageNumber, header1.Size)); } // Try to find message based on message identifier (not all channels support this) if (message == null && !String.IsNullOrEmpty(header1.MessageIdentifier)) { message = dataService.SelectBy <Message>( String.Format("select * from Messages where (SourceChannelId='{0}' or TargetChannelId='{0}') and MessageIdentifier='{1}' and Size='{2}'", config.ChannelId, header1.MessageIdentifier, header1.Size)); } if (message == null) { headersToDownload.Add(header); } else { if (CheckReadStates(message, header1)) { // Check folder of message if (message.MessageFolder != Folders.Archive && message.MessageFolder != Folders.Trash && message.MessageFolder != folder.ToStorageFolder()) { message.MoveToFolder(folder.ToStorageFolder()); } } } } return(headersToDownload); }
public void RemoveFromFolder(ChannelMessageHeader message, ChannelFolder folder) { if (String.IsNullOrEmpty(message.SourceFolder)) { return; } connection.Client.SelectFolder(folder.FolderId); connection.Client.DeleteMessages(new IMAP_SequenceSet(message.MessageNumber), true); }
public virtual void MarkDeleted(ChannelMessageHeader message) { if (String.IsNullOrEmpty(message.SourceFolder)) { return; } connection.Client.SelectFolder(message.SourceFolder); connection.Client.StoreMessageFlags(new IMAP_SequenceSet(message.MessageNumber), IMAP_MessageFlags.Deleted, true); }
public void CopyToFolder(ChannelMessageHeader message, ChannelFolder folder) { if (String.IsNullOrEmpty(message.SourceFolder)) { return; } connection.Client.SelectFolder(message.SourceFolder); connection.Client.CopyMessages(new IMAP_SequenceSet(message.MessageNumber), folder.FolderId, true); }
public IEnumerable <ChannelMessage> GetMessage(ChannelMessageHeader header) { ChannelMessage message = new ChannelMessage(); message.MessageNumber = header.MessageNumber; message.MessageIdentifier = header.MessageIdentifier; message.From = header.From; message.Context = header.Context; message.BodyText = header.Body.ToStream(); message.DateReceived = header.DateReceived; message.IsRead = true; yield return(message); }
protected virtual bool CheckReadStates(Message message, ChannelMessageHeader header) { // Channel doesn't support readstates, no point in checking if (!config.Charasteristics.SupportsReadStates) { return(false); } // Message has been marked for channel update, ignore for now if (message.TargetMessageState.HasValue) { return(false); } // Check readstate of message if (header.IsRead) { if (!message.IsRead) { message.MarkRead(false); } } else { if (message.IsRead) { message.MarkUnread(false); } } if (header.IsStarred) { // Star if (!message.IsStarred) { message.SetStarred(false); } } else { // Unstar if (message.IsStarred) { message.SetUnstarred(false); } } return(true); }
public IEnumerable <ChannelMessage> GetMessage(ChannelMessageHeader header) { ChannelMessage message = new ChannelMessage(); message.MessageNumber = header.MessageNumber; message.MessageIdentifier = header.MessageIdentifier; message.Context = header.Context; message.From = header.From; message.To.AddRange(header.To); message.InReplyTo = header.InReplyTo; message.BodyHtml = header.Body.ToStream(); message.DateReceived = header.DateReceived; yield return(message); }
void Delete(ChannelMessageHeader header) { Logger.Debug("Setting message {0} as deleted", LogSource.Command, message); var trash = availableFolders.FirstOrDefault(a => a.FolderType == ChannelFolderType.Trash); if (trash == null) { channel.MarkDeleted(header); } else { channel.MoveToFolder(header, trash); message.SourceFolder = trash.FolderId; } }
void Archive(ChannelMessageHeader header) { var archive = availableFolders.FirstOrDefault(a => a.FolderType == ChannelFolderType.Archive); if (archive == null) { Logger.Warn("Unable to find archive folder, ignoring channel archive action. Message = {0}", LogSource.Channel, message); } else { Logger.Debug("Setting message {0} as archived", LogSource.Command, message); channel.MoveToFolder(header, archive); message.SourceFolder = archive.FolderId; } }
void UnArchive(ChannelMessageHeader header) { var inbox = availableFolders.FirstOrDefault(a => a.FolderType == ChannelFolderType.Inbox); if (inbox == null) { Logger.Warn("Unable to find inbox folder, ignoring channel unarchive action. Message = {0}", LogSource.Channel, message); } else { Logger.Debug("Setting message {0} as unarchived", LogSource.Command, message); channel.MoveToFolder(header, inbox); message.SourceFolder = inbox.FolderId; } }
protected virtual void DownloadHeader(ChannelMessageHeader header) { Logger.Debug("Starting task for header {0}", LogSource.Receive, header); var task = new ReceiveMessageDetailsTask(config, channel, header, folder); try { // Execute receive details task synchronously task.Execute(); } catch (Exception ex) { Logger.Error("An exception has occured while getting message data. Exception = {0}", LogSource.Channel, ex); } }
void MarkAsSpam(ChannelMessageHeader header) { var spam = availableFolders.FirstOrDefault(a => a.FolderType == ChannelFolderType.Spam); if (spam == null) { Logger.Warn("Unable to find spam folder, ignoring channel spam action. Message = {0}", LogSource.Channel, message); } else { Logger.Debug("Setting message {0} as spam", LogSource.Command, message); channel.MoveToFolder(header, spam); message.SourceFolder = spam.FolderId; } }
public virtual void SetStarred(ChannelMessageHeader message, bool starred) { if (String.IsNullOrEmpty(message.SourceFolder)) { return; } IMAP_MessageFlags flags = message.IsRead ? IMAP_MessageFlags.Seen : IMAP_MessageFlags.None; if (starred) { flags = (flags | IMAP_MessageFlags.Flagged); } connection.Client.SelectFolder(message.SourceFolder); connection.Client.StoreMessageFlags(new IMAP_SequenceSet(message.MessageNumber), flags, true); }
IEnumerable <ChannelMessageHeader> FetchImapSet(IMAP_SequenceSet set) { var result = connection.Client.FetchMessages(set, IMAP_FetchItem_Flags.UID | IMAP_FetchItem_Flags.MessageFlags | IMAP_FetchItem_Flags.InternalDate | IMAP_FetchItem_Flags.Size | IMAP_FetchItem_Flags.Header | IMAP_FetchItem_Flags.Envelope, false, true); if (result != null && result.Length > 0) { foreach (var item in result) { var header = new ChannelMessageHeader(); header.MessageNumber = item.UID.ToString(); header.SourceFolder = connection.Client.SelectedFolder; header.Size = item.Size; header.Context = item.Envelope.Subject; header.DateReceived = item.Envelope.Date; header.IsRead = !item.IsNewMessage; // Message is starred if ((item.MessageFlags & IMAP_MessageFlags.Flagged) == IMAP_MessageFlags.Flagged) { header.IsStarred = true; } header.MessageIdentifier = item.Envelope.MessageID; header.InReplyTo = item.Envelope.InReplyTo; var provider = new MIME_h_Provider(); var headers = new MIME_h_Collection(provider); headers.Parse(item.HeaderData); var i2mpMessageId = headers.GetFirst("x-i2mp-messageid"); if (i2mpMessageId != null) { var unstructured = (MIME_h_Unstructured)i2mpMessageId; header.Metadata.i2mpMessageId = unstructured.Value; } yield return(header); } } }
ChannelMessageHeader ParseHeader(XElement source) { ChannelMessageHeader header = new ChannelMessageHeader(); header.Context = source.Element("title").Value; header.MessageNumber = source.Element("messageid").Value; header.MessageIdentifier = source.Element("messageid").Value; string sourceid = source.Element("userid").Value; string targetid = source.Element("target_userid").Value; var recipients = PerformRequest( String.Format("http://data.hyves-api.nl/?userid={0}&ha_responsefields=profilepicture,cityname,countryname&ha_method=users.get&ha_version=experimental&ha_format=xml&ha_fancylayout=true", String.Join(",", new[] { sourceid, targetid }))); var from = ParseContact(recipients.Elements("user").First(u => u.Element("userid").Value == sourceid)); var to = ParseContact(recipients.Elements("user").First(u => u.Element("userid").Value == targetid)); header.From = new SourceAddress(from.Profile.ChannelProfileKey, from.Profile.ScreenName); header.To.Add(new SourceAddress(to.Profile.ChannelProfileKey, to.Profile.ScreenName)); string bodyType = source.Element("format").Value; if (bodyType == "html") { // For some weird reason, hyves sometimes sends htmlBody and sometimes htmlbody (lowercase b) var elem = source.Element("htmlBody") ?? source.Element("htmlbody"); header.Body = elem.Value; } else { header.Body = source.Element("body").Value; } header.DateReceived = Int64.Parse(source.Element("created").Value).ToUnixTime(); return(header); }
public IEnumerable <ChannelMessageHeader> GetHeaders() { BuildRestClient(); var messages = client.GetMessages(FbMessageFolder.Inbox).ToList(); foreach (FbMessage fbMessage in messages) { var header = new ChannelMessageHeader(); header.MessageIdentifier = fbMessage.MessageId; header.MessageNumber = fbMessage.MessageId; header.Context = fbMessage.Subject; header.From = fbMessage.From; header.To = fbMessage.To; header.Body = fbMessage.Body; header.IsRead = fbMessage.Read; header.DateReceived = fbMessage.DateCreated; header.Metadata.i2mpRelationId = fbMessage.ThreadId; yield return(header); } }
protected virtual bool CheckReadStates(Message message, ChannelMessageHeader header) { // Channel doesn't support readstates, no point in checking if (!config.Charasteristics.SupportsReadStates) return false; // Message has been marked for channel update, ignore for now if (message.TargetMessageState.HasValue) return false; // Check readstate of message if (header.IsRead) { if (!message.IsRead) message.MarkRead(false); } else { if (message.IsRead) message.MarkUnread(false); } if (header.IsStarred) { // Star if (!message.IsStarred) message.SetStarred(false); } else { // Unstar if (message.IsStarred) message.SetUnstarred(false); } return true; }
protected override List <ChannelMessageHeader> GetHeadersToDownload(List <ChannelMessageHeader> headers) { foreach (var header in headers) { Message message; ChannelMessageHeader header1 = header; using (mailbox.Messages.ReaderLock) message = mailbox.Messages.FirstOrDefault(m => (m.SourceChannelId == config.ChannelId || m.TargetChannelId == config.ChannelId) && m.MessageIdentifier == header1.MessageIdentifier && m.Size == header1.Size); if (message != null) { // Check if the message is in the folder we are currently enumerating if (message.MessageFolder != folder.ToStorageFolder()) { message.MoveToFolder(folder.ToStorageFolder()); } } } // Return empty list return(null); }
public void MoveToFolder(ChannelMessageHeader message, ChannelFolder folder) { var client = new ExchangeClient(Hostname, CredentialsProvider); var messageId = client.GetMessageId(message.MessageNumber).ItemId; client.MoveMessageToFolder(messageId, folder.FolderId); }
public IEnumerable<ChannelMessage> GetMessage(ChannelMessageHeader header) { var client = new ExchangeClient(Hostname, CredentialsProvider); ChannelMessage message = new ChannelMessage(); MessageType inboxItem = client.GetMessage(header.MessageNumber); // Now the message Body is there. BodyType messageBody = inboxItem.Body; message.Size = header.Size; message.MessageNumber = header.MessageNumber; message.MessageIdentifier = header.MessageIdentifier; message.Context = header.Context; message.From = new SourceAddress(inboxItem.From.Item.EmailAddress, inboxItem.From.Item.Name); if (inboxItem.ToRecipients != null) foreach (var toRecipient in inboxItem.ToRecipients) message.To.Add(new SourceAddress(toRecipient.EmailAddress, toRecipient.Name)); if (inboxItem.CcRecipients != null) foreach (var ccRecipient in inboxItem.CcRecipients) message.CC.Add(new SourceAddress(ccRecipient.EmailAddress, ccRecipient.Name)); if (inboxItem.BccRecipients != null) foreach (var bccRecipient in inboxItem.BccRecipients) message.BCC.Add(new SourceAddress(bccRecipient.EmailAddress, bccRecipient.Name)); message.InReplyTo = inboxItem.InReplyTo; message.Metadata = header.Metadata; message.IsRead = inboxItem.IsRead; message.BodyHtml = messageBody.Value.ToStream(); message.DateReceived = header.DateReceived; if (inboxItem.Attachments != null) { foreach (AttachmentType exchAttachment in inboxItem.Attachments) { var fileAttachment = client.GetAttachment(exchAttachment.AttachmentId.Id); message.Attachments.Add(new ChannelAttachment { Filename = fileAttachment.Name, ContentType = ContentType.Attachment, ContentStream = new MemoryStream(fileAttachment.Content) }); } } yield return message; }
public IEnumerable<ChannelMessageHeader> GetHeaders() { var client = new ExchangeClient(Hostname, CredentialsProvider); foreach (MessageType inboxItem in client.GetHeaders(folder).Reverse().Skip((int)StartIndex).Take((int)PageSize)) { var header = new ChannelMessageHeader { MessageNumber = inboxItem.ItemId.Id, MessageIdentifier = inboxItem.InternetMessageId, Context = inboxItem.Subject, // Not sending size because exchange changes can the size due // to user actions usch as change priority. //Size = inboxItem.Size, DateReceived = inboxItem.DateTimeReceived, IsRead = inboxItem.IsRead, // Not processing IsStarred because somebody could send you an // email with importance: high and would end up getting starred; // feature just asking for abuse :-) //IsStarred = inboxItem.Importance == ImportanceChoicesType.High, }; yield return header; } }
public IEnumerable<ChannelMessage> GetMessage(ChannelMessageHeader header) { ChannelMessage message = new ChannelMessage(); message.MessageNumber = header.MessageNumber; message.MessageIdentifier = header.MessageIdentifier; message.From = header.From; message.Context = header.Context; message.BodyText = header.Body.ToStream(); message.IsRead = header.IsRead; message.DateReceived = header.DateReceived; message.ConversationId = header.Metadata.i2mpRelationId; yield return message; }
public IEnumerable<ChannelMessage> GetMessage(ChannelMessageHeader header) { ChannelMessage message = new ChannelMessage(); message.MessageNumber = header.MessageNumber; message.MessageIdentifier = header.MessageIdentifier; message.Context = header.Context; message.From = header.From; message.To.AddRange(header.To); message.InReplyTo = header.InReplyTo; message.BodyHtml = header.Body.ToStream(); message.DateReceived = header.DateReceived; yield return message; }
public IEnumerable<ChannelMessage> GetMessage(ChannelMessageHeader header) { yield break; }
public IEnumerable<ChannelMessageHeader> GetHeaders() { BuildRestClient(); var messages = client.GetMessages(FbMessageFolder.Inbox).ToList(); foreach (FbMessage fbMessage in messages) { var header = new ChannelMessageHeader(); header.MessageIdentifier = fbMessage.MessageId; header.MessageNumber = fbMessage.MessageId; header.Context = fbMessage.Subject; header.From = fbMessage.From; header.To = fbMessage.To; header.Body = fbMessage.Body; header.IsRead = fbMessage.Read; header.DateReceived = fbMessage.DateCreated; header.Metadata.i2mpRelationId = fbMessage.ThreadId; yield return header; } }
public static ChannelMessage From(Stream stream, ChannelMessageHeader header) { stream.Seek(0, SeekOrigin.Begin); var mimeMessage = Mime.Parse(stream); var message = new ChannelMessage(); if (header != null) { message.MessageNumber = header.MessageNumber; message.Size = header.Size; message.SourceFolder = header.SourceFolder; message.IsRead = header.IsRead; message.IsStarred = header.IsStarred; message.DateReceived = header.DateReceived; } else { message.SourceFolder = "INBOX"; message.DateReceived = DateTime.Now; } message.Context = mimeMessage.MainEntity.Subject; message.MessageIdentifier = mimeMessage.MainEntity.MessageID; message.InReplyTo = mimeMessage.MainEntity.InReplyTo; message.BodyHtml = mimeMessage.BodyHtml.ToStream(); if (!String.IsNullOrEmpty(mimeMessage.BodyText)) message.BodyText = mimeMessage.BodyText.ToStream(); message.From = new SourceAddress(mimeMessage.MainEntity.From.ToAddressListString()); if (mimeMessage.MainEntity.To == null) { var deliveredTo = mimeMessage.MainEntity.Header.GetFirst("Delivered-To:"); if (deliveredTo != null) message.To = new SourceAddressCollection(deliveredTo.Value); } else { message.To = new SourceAddressCollection(mimeMessage.MainEntity.To.ToAddressListString()); } if (mimeMessage.MainEntity.Cc != null) message.CC = new SourceAddressCollection(mimeMessage.MainEntity.Cc.ToAddressListString()); if (mimeMessage.MainEntity.Bcc != null) message.BCC = new SourceAddressCollection(mimeMessage.MainEntity.Bcc.ToAddressListString()); foreach (var att in mimeMessage.Attachments) { var attachment = new ChannelAttachment { Filename = att.ContentDisposition == ContentDisposition_enum.Attachment ? att.ContentDisposition_FileName : att.ContentType_Name }; if (!String.IsNullOrEmpty(att.ContentID)) attachment.ContentId = att.ContentID.Trim('<', '>'); attachment.ContentType = String.IsNullOrEmpty(att.ContentID) ? ContentType.Attachment : ContentType.Inline; attachment.ContentStream = new MemoryStream(att.Data); message.Attachments.Add(attachment); } return message; }
void ChangeLabels(ChannelMessageHeader header) { var labelsChannel = (ILabelsChannel) channel; if (!String.IsNullOrEmpty(message.SendLabels)) { var postLabels = new List<Label>( message.SendLabels.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries) .Select(l => new Label(l))); // Check if this is an add or remove for (int i = 0; i < postLabels.Count; i++) { var label = postLabels[i]; var label1 = label; if (labelsChannel.LabelsSupport == LabelsSupport.Folders) { // Make sure the given label exists as a folder since the implementation of the current // channel requires this (this is the case with for example gmail). var folder = availableFolders.FirstOrDefault( f => f.Name.Equals(label1.Labelname, StringComparison.InvariantCultureIgnoreCase)); if (folder == null) { // Folder doesn't exist, create it availableFolders.Add(channel.CreateFolder(label.Labelname)); } } // Used for error reporting string action = String.Empty; try { if (message.LabelsList.Any(l => l.Equals(label1))) { // Add action = "Add label"; labelsChannel.AddLabel(header, label1.Labelname); } else { // Remove label, sometimes labels have no Message number (for instance when // adding and deleting labels before send/receive has been executed) if (!String.IsNullOrEmpty(label1.MessageNumber)) { // If this is the last label left and the current channel has a folder based // label storage, don't remove the label but rather move the message to the inbox. // Otherwise we might end up losing the message with exchange. if (i == postLabels.Count - 1 && labelsChannel.GetType().ToString().Contains("Exchange") && message.LabelsList.Count == 0) { action = "Move to inbox"; channel.MoveToFolder(header, availableFolders.First(f => f.FolderType == ChannelFolderType.Inbox)); } else { action = "Remove label"; labelsChannel.RemoveLabel(label1.MessageNumber, label1.Labelname); } } } } catch (Exception ex) { Logger.Error("An error has occured while trying to apply label. Action = {0}, Exception = {1}", LogSource.Channel, action, ex); } } } // Clear all labels that have been processed message.SendLabels = String.Empty; }
ChannelMessageHeader ParseHeader(XElement source) { ChannelMessageHeader header = new ChannelMessageHeader(); header.Context = source.Element("title").Value; header.MessageNumber = source.Element("messageid").Value; header.MessageIdentifier = source.Element("messageid").Value; string sourceid = source.Element("userid").Value; string targetid = source.Element("target_userid").Value; var recipients = PerformRequest( String.Format("http://data.hyves-api.nl/?userid={0}&ha_responsefields=profilepicture,cityname,countryname&ha_method=users.get&ha_version=experimental&ha_format=xml&ha_fancylayout=true", String.Join(",", new[] { sourceid, targetid}))); var from = ParseContact(recipients.Elements("user").First(u => u.Element("userid").Value == sourceid)); var to = ParseContact(recipients.Elements("user").First(u => u.Element("userid").Value == targetid)); header.From = new SourceAddress(from.Profile.ChannelProfileKey, from.Profile.ScreenName); header.To.Add(new SourceAddress(to.Profile.ChannelProfileKey, to.Profile.ScreenName)); string bodyType = source.Element("format").Value; if (bodyType == "html") { // For some weird reason, hyves sometimes sends htmlBody and sometimes htmlbody (lowercase b) var elem = source.Element("htmlBody") ?? source.Element("htmlbody"); header.Body = elem.Value; } else header.Body = source.Element("body").Value; header.DateReceived = Int64.Parse(source.Element("created").Value).ToUnixTime(); return header; }