private async void OnEmailRecv(EmailRecvEvent ev) { var email = ev.email; var match = Regex.Match(email.To, "(.*) <(?<name>.+)@.+>"); var targetChannels = new List <string?>(); var targetChannelIds = targetChannels.Select <string?, Ulid?>(str => { if (Ulid.TryParse(str, out Ulid res)) { return(res); } else { return(null); } }).Where(id => id != null).ToList(); while (match.Success) { targetChannels.Add(match.Groups["name"].Value); match = match.NextMatch(); } var origin = Regex.Match(email.From, "^(?<nick>.*) <(?<email>.+)>$").Groups; var senderEmail = origin["email"].Value; var senderNickname = origin["nick"].Value; var allTargetChannels = await this.db.db.Channels .AsQueryable().Where(ch => targetChannelIds.Contains(ch.ChannelId) || targetChannels.Contains(ch.ChannelUsername)).ToListAsync(); var allTargetChannelIds = allTargetChannels.Select(ch => ch.ChannelId).ToList(); var msg = new HatoMessage() { MsgId = Ulid.Empty, // WARN: All timestamps represent the time when this email ARRIVES at the server // Timestamp = email.Date, SenderEmail = senderEmail, SenderNickname = senderNickname, Title = email.Subject, BodyHtml = email.BodyHtml, BodyPlain = email.BodyPlain, }; var attachments = await UploadAndSaveAttachments(email); msg.Attachments = attachments; foreach (var channel in allTargetChannels) { msg._Channel = channel; msg.ChannelId = channel.ChannelId; await SaveEmail(msg); } List <IGrouping <string, string> > allTargetEmails = await GetEmailsFromChannelIds(allTargetChannelIds); await SendEmailAndFailure(senderEmail, senderNickname, msg, allTargetEmails); await db.db.SaveChangesAsync(); }
public async void dealWithChannel(EmailRecvEvent ev) { var email = ev.email; var data = new EmailData(); if (email.BodyHtml != null) { data.Body = email.BodyHtml; data.IsHtml = true; } else { data.Body = email.BodyPlain; data.IsHtml = false; } data.PlaintextAlternativeBody = email.BodyPlain; data.Subject = email.Subject; var captures = Regex.Match(email.To, "^(.*) <(?<name>.+)@.+>$").Groups; var channelName = captures["name"].Value; logger.LogInformation("Got email from {0} to {1}; channelName = {2}", email.From, email.To, channelName); var origin = Regex.Match(email.From, "^(?<nick>.*) <(?<email>.+)>$").Groups; var originEmail = origin["email"].Value; var originNick = origin["nick"].Value; try { foreach (var attachment in email.Attachments) { var attachmentStream = await recvService.GetAttachment(attachment.Url); var att = new Attachment(); att.Data = attachmentStream; att.Filename = attachment.Name; att.ContentType = attachment.ContentType; data.Attachments.Add(att); } await this.SendEmailToChannel(channelName, data); } catch (ChannelSendFailedException e) { // const int maxBodyLength = 200; // string strippedBody; // if (email.BodyPlain.Length > maxBodyLength) // { // strippedBody = email.BodyPlain.Substring(0, 200) + "..."; // } // else // { // strippedBody = email.BodyPlain; // } // await this.SendFailureEmailBack( // originEmail, originNick, channelName, strippedBody, e.ToString(), email.Date); // logger.LogWarning(e, "Failed to send to channel"); } }