Example #1
0
        public async Task <ActionResult <List <DepartmentOptionResult> > > GetDepartmentOptions(int departmentId)
        {
            List <DepartmentOptionResult> result = new List <DepartmentOptionResult>();

            if (departmentId == 0 && IsSystem)
            {
                // Get All
                var departments = await _departmentsService.GetAllAsync();

                foreach (var department in departments)
                {
                    var emailSettings = await _departmentsService.GetDepartmentEmailSettingsAsync(department.DepartmentId);

                    var optionsResult = new DepartmentOptionResult();
                    optionsResult.DepartmentId    = department.DepartmentId;
                    optionsResult.EmailFormatType = emailSettings.FormatType;
                    result.Add(optionsResult);
                }

                return(result);
            }
            else if (departmentId >= 1)
            {
                if (departmentId == DepartmentId)
                {
                    var emailSettings = await _departmentsService.GetDepartmentEmailSettingsAsync(departmentId);

                    var optionsResult = new DepartmentOptionResult();
                    optionsResult.DepartmentId    = departmentId;
                    optionsResult.EmailFormatType = emailSettings.FormatType;
                    result.Add(optionsResult);

                    return(result);
                }
                else
                {
                    return(Unauthorized());
                }
            }
            else
            {
                return(BadRequest());
            }
        }
Example #2
0
        public async Task <ActionResult> Receive(PostmarkInboundMessage message, CancellationToken cancellationToken)
        {
            if (message != null)
            {
                try
                {
                    var mailMessage = new MimeMessage();

                    if (message.FromFull != null && !String.IsNullOrWhiteSpace(message.FromFull.Email) && message.FromFull.Email.Trim() == "*****@*****.**")
                    {
                        return(CreatedAtAction(nameof(Receive), new { id = message.MessageID }, message));
                    }

                    if (message.FromFull != null && !String.IsNullOrWhiteSpace(message.FromFull.Email) && !String.IsNullOrWhiteSpace(message.FromFull.Name))
                    {
                        mailMessage.From.Add(new MailboxAddress(message.FromFull.Name.Trim(), message.FromFull.Email.Trim()));
                    }
                    else
                    {
                        mailMessage.From.Add(new MailboxAddress("Inbound Email Dispatch", "*****@*****.**"));
                    }

                    if (!String.IsNullOrWhiteSpace(message.Subject))
                    {
                        mailMessage.Subject = message.Subject;
                    }
                    else
                    {
                        mailMessage.Subject = "Dispatch Email";
                    }

                    var builder = new BodyBuilder();

                    if (!String.IsNullOrWhiteSpace(message.HtmlBody))
                    {
                        builder.HtmlBody = HttpUtility.HtmlDecode(message.HtmlBody);
                    }

                    if (!String.IsNullOrWhiteSpace(message.TextBody))
                    {
                        builder.TextBody = message.TextBody;
                    }

                    int    type         = 0;          // 1 = dispatch // 2 = email list // 3 = group dispatch // 4 = group message
                    string emailAddress = String.Empty;
                    string bounceEmail  = String.Empty;
                    string name         = String.Empty;

                    #region Trying to Find What type of email this is
                    if (message.ToFull != null)
                    {
                        foreach (var email in message.ToFull)
                        {
                            if (StringHelpers.ValidateEmail(email.Email))
                            {
                                if (email.Email.Contains($"@{Config.InboundEmailConfig.DispatchDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.DispatchTestDomain}"))
                                {
                                    type = 1;

                                    if (email.Email.Contains($"@{Config.InboundEmailConfig.DispatchDomain}"))
                                    {
                                        emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.DispatchDomain}", "");
                                    }
                                    else
                                    {
                                        emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.DispatchTestDomain}", "");
                                    }

                                    name = email.Name;
                                    mailMessage.To.Clear();
                                    mailMessage.To.Add(new MailboxAddress(email.Name, email.Email));

                                    break;
                                }
                                else if (email.Email.Contains($"@{Config.InboundEmailConfig.ListsDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.ListsTestDomain}"))
                                {
                                    type = 2;

                                    if (email.Email.Contains($"@{Config.InboundEmailConfig.ListsDomain}"))
                                    {
                                        emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.ListsDomain}", "");
                                    }
                                    else
                                    {
                                        emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.ListsTestDomain}", "");
                                    }

                                    if (emailAddress.Contains("+") && emailAddress.Contains("="))
                                    {
                                        var tempBounceEmail = emailAddress.Substring(emailAddress.IndexOf("+") + 1);
                                        bounceEmail = tempBounceEmail.Replace("=", "@");

                                        emailAddress = emailAddress.Replace(tempBounceEmail, "");
                                        emailAddress = emailAddress.Replace("+", "");
                                    }

                                    name = email.Name;
                                    mailMessage.To.Clear();
                                    mailMessage.To.Add(new MailboxAddress(email.Name, email.Email));

                                    break;
                                }
                                else if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupsDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.GroupsTestDomain}"))
                                {
                                    type = 3;

                                    if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupsDomain}"))
                                    {
                                        emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupsDomain}", "");
                                    }
                                    else
                                    {
                                        emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupsTestDomain}", "");
                                    }

                                    name = email.Name;
                                    mailMessage.To.Clear();
                                    mailMessage.To.Add(new MailboxAddress(email.Name, email.Email));

                                    break;
                                }
                                else if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupMessageDomain}") || email.Email.Contains($"@{Config.InboundEmailConfig.GroupTestMessageDomain}"))
                                {
                                    type = 4;

                                    if (email.Email.Contains($"@{Config.InboundEmailConfig.GroupMessageDomain}"))
                                    {
                                        emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupMessageDomain}", "");
                                    }
                                    else
                                    {
                                        emailAddress = email.Email.Replace($"@{Config.InboundEmailConfig.GroupTestMessageDomain}", "");
                                    }

                                    name = email.Name;
                                    mailMessage.To.Clear();
                                    mailMessage.To.Add(new MailboxAddress(email.Name, email.Email));

                                    break;
                                }
                            }
                        }
                    }

                    // Some providers aren't putting email address in the To line, process the CC line
                    if (type == 0 && message.CcFull != null)
                    {
                        foreach (var email in message.CcFull)
                        {
                            if (StringHelpers.ValidateEmail(email.Email))
                            {
                                var proccedEmailInfo = ProcessEmailAddress(email.Email);

                                if (proccedEmailInfo.Item1 > 0)
                                {
                                    type         = proccedEmailInfo.Item1;
                                    emailAddress = proccedEmailInfo.Item2;

                                    mailMessage.To.Clear();
                                    mailMessage.To.Add(new MailboxAddress(email.Name, email.Email));
                                }
                            }
                        }
                    }

                    if (type == 0 && message.BccFull != null)
                    {
                        foreach (var email in message.BccFull)
                        {
                            if (StringHelpers.ValidateEmail(email.Email))
                            {
                                var proccedEmailInfo = ProcessEmailAddress(email.Email);

                                if (proccedEmailInfo.Item1 > 0)
                                {
                                    type         = proccedEmailInfo.Item1;
                                    emailAddress = proccedEmailInfo.Item2;

                                    mailMessage.To.Clear();
                                    mailMessage.To.Add(new MailboxAddress(email.Name, email.Email));
                                }
                            }
                        }
                    }

                    // If To and CC didn't work, try the header.
                    if (type == 0)
                    {
                        try
                        {
                            if (message.Headers != null && message.Headers.Count > 0)
                            {
                                var header = message.Headers.FirstOrDefault(x => x.Name == "Received-SPF");

                                if (header != null)
                                {
                                    var lastValue = header.Value.LastIndexOf(char.Parse("="));
                                    var newEmail  = header.Value.Substring(lastValue + 1, (header.Value.Length - (lastValue + 1)));

                                    newEmail = newEmail.Trim();

                                    if (StringHelpers.ValidateEmail(newEmail))
                                    {
                                        emailAddress = newEmail;
                                        var proccedEmailInfo = ProcessEmailAddress(newEmail);

                                        type         = proccedEmailInfo.Item1;
                                        emailAddress = proccedEmailInfo.Item2;

                                        mailMessage.To.Clear();
                                        mailMessage.To.Add(new MailboxAddress("Email Importer", newEmail));
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                        }
                    }
                    #endregion Trying to Find What type of email this is

                    if (type == 1)                      // Dispatch
                    {
                        #region Dispatch Email
                        var departmentId = await _departmentSettingsService.GetDepartmentIdForDispatchEmailAsync(emailAddress);

                        if (departmentId.HasValue)
                        {
                            try
                            {
                                var emailSettings = await _departmentsService.GetDepartmentEmailSettingsAsync(departmentId.Value);

                                List <IdentityUser> departmentUsers = await _departmentsService.GetAllUsersForDepartmentAsync(departmentId.Value, true);

                                var callEmail = new CallEmail();

                                if (!String.IsNullOrWhiteSpace(message.Subject))
                                {
                                    callEmail.Subject = message.Subject;
                                }

                                else
                                {
                                    callEmail.Subject = "Dispatch Email";
                                }

                                if (!String.IsNullOrWhiteSpace(message.HtmlBody))
                                {
                                    callEmail.Body = HttpUtility.HtmlDecode(message.HtmlBody);
                                }
                                else
                                {
                                    callEmail.Body = message.TextBody;
                                }

                                callEmail.TextBody = message.TextBody;

                                foreach (var attachment in message.Attachments)
                                {
                                    try
                                    {
                                        if (Convert.ToInt32(attachment.ContentLength) > 0)
                                        {
                                            if (attachment.Name.Contains(".mp3") || attachment.Name.Contains(".amr"))
                                            {
                                                byte[] filebytes = Convert.FromBase64String(attachment.Content);

                                                callEmail.DispatchAudioFileName = attachment.Name;
                                                callEmail.DispatchAudio         = filebytes;
                                            }
                                        }
                                    }
                                    catch { }
                                }

                                if (emailSettings == null)
                                {
                                    emailSettings              = new DepartmentCallEmail();
                                    emailSettings.FormatType   = (int)CallEmailTypes.Generic;
                                    emailSettings.DepartmentId = departmentId.Value;
                                    emailSettings.Department   = await _departmentsService.GetDepartmentByIdAsync(departmentId.Value, false);
                                }
                                else if (emailSettings.Department == null)
                                {
                                    emailSettings.Department = await _departmentsService.GetDepartmentByIdAsync(departmentId.Value);
                                }

                                var activeCalls = await _callsService.GetLatest10ActiveCallsByDepartmentAsync(emailSettings.Department.DepartmentId);

                                var units = await _unitsService.GetUnitsForDepartmentAsync(emailSettings.Department.DepartmentId);

                                var priorities = await _callsService.GetActiveCallPrioritiesForDepartmentAsync(emailSettings.Department.DepartmentId);

                                int defaultPriority = (int)CallPriority.High;

                                if (priorities != null && priorities.Any())
                                {
                                    var defaultPrio = priorities.FirstOrDefault(x => x.IsDefault && x.IsDeleted == false);

                                    if (defaultPrio != null)
                                    {
                                        defaultPriority = defaultPrio.DepartmentCallPriorityId;
                                    }
                                }

                                var call = _callsService.GenerateCallFromEmail(emailSettings.FormatType, callEmail,
                                                                               emailSettings.Department.ManagingUserId,
                                                                               departmentUsers, emailSettings.Department, activeCalls, units, defaultPriority, priorities);

                                if (call != null && call.CallId <= 0)
                                {
                                    // New Call Dispatch as normal
                                    call.DepartmentId = departmentId.Value;

                                    if (!String.IsNullOrWhiteSpace(call.Address) && String.IsNullOrWhiteSpace(call.GeoLocationData))
                                    {
                                        call.GeoLocationData = await _geoLocationProvider.GetLatLonFromAddress(call.Address);
                                    }

                                    var savedCall = await _callsService.SaveCallAsync(call, cancellationToken);

                                    var cqi = new CallQueueItem();
                                    cqi.Call                 = savedCall;
                                    cqi.Profiles             = (await _userProfileService.GetAllProfilesForDepartmentAsync(call.DepartmentId)).Select(x => x.Value).ToList();
                                    cqi.DepartmentTextNumber = await _departmentSettingsService.GetTextToCallNumberForDepartmentAsync(cqi.Call.DepartmentId);

                                    await _queueService.EnqueueCallBroadcastAsync(cqi, cancellationToken);

                                    return(CreatedAtAction(nameof(Receive), new { id = savedCall.CallId }, savedCall));
                                }
                                else if (call != null && call.CallId > 0)
                                {
                                    // Existing Call, just update
                                    if (!String.IsNullOrWhiteSpace(call.Address) && String.IsNullOrWhiteSpace(call.GeoLocationData))
                                    {
                                        call.GeoLocationData = await _geoLocationProvider.GetLatLonFromAddress(call.Address);
                                    }

                                    var savedCall = await _callsService.SaveCallAsync(call, cancellationToken);

                                    // If our Dispatch Count changed, i.e. 2nd Alarm to 3rd Alarm, redispatch
                                    if (call.DidDispatchCountChange())
                                    {
                                        var cqi = new CallQueueItem();
                                        cqi.Call                 = savedCall;
                                        cqi.Profiles             = (await _userProfileService.GetAllProfilesForDepartmentAsync(call.DepartmentId)).Select(x => x.Value).ToList();
                                        cqi.DepartmentTextNumber = await _departmentSettingsService.GetTextToCallNumberForDepartmentAsync(cqi.Call.DepartmentId);

                                        await _queueService.EnqueueCallBroadcastAsync(cqi, cancellationToken);
                                    }

                                    return(CreatedAtAction(nameof(Receive), new { id = savedCall.CallId }, savedCall));
                                }
                            }
                            catch (Exception ex)
                            {
                                Logging.LogException(ex);
                                return(BadRequest(message));
                            }
                        }
                        #endregion Dispatch
                    }
                    else if (type == 2)                     // Email List
                    {
                        #region Distribution Email
                        var list = await _distributionListsService.GetDistributionListByAddressAsync(emailAddress);

                        if (list != null)
                        {
                            if (String.IsNullOrWhiteSpace(bounceEmail))
                            {
                                try
                                {
                                    List <Model.File> files = new List <Model.File>();

                                    try
                                    {
                                        if (message.Attachments != null && message.Attachments.Any())
                                        {
                                            foreach (var attachment in message.Attachments)
                                            {
                                                if (Convert.ToInt32(attachment.ContentLength) > 0)
                                                {
                                                    Model.File file = new Model.File();

                                                    byte[] filebytes = Convert.FromBase64String(attachment.Content);

                                                    file.Data         = filebytes;
                                                    file.FileName     = attachment.Name;
                                                    file.DepartmentId = list.DepartmentId;
                                                    file.ContentId    = attachment.ContentID;
                                                    file.FileType     = attachment.ContentType;
                                                    file.Timestamp    = DateTime.UtcNow;

                                                    files.Add(await _fileService.SaveFileAsync(file, cancellationToken));
                                                }
                                            }
                                        }
                                    }
                                    catch { }

                                    var dlqi = new DistributionListQueueItem();
                                    dlqi.List  = list;
                                    dlqi.Users = await _departmentsService.GetAllUsersForDepartmentAsync(list.DepartmentId);

                                    if (files != null && files.Any())
                                    {
                                        dlqi.FileIds = new List <int>();
                                        dlqi.FileIds.AddRange(files.Select(x => x.FileId).ToList());
                                    }

                                    dlqi.Message             = new InboundMessage();
                                    dlqi.Message.Attachments = new List <InboundMessageAttachment>();

                                    if (message.FromFull != null && !String.IsNullOrWhiteSpace(message.FromFull.Email) && !String.IsNullOrWhiteSpace(message.FromFull.Name))
                                    {
                                        dlqi.Message.FromEmail = message.FromFull.Email.Trim();
                                        dlqi.Message.FromName  = message.FromFull.Name.Trim();
                                    }

                                    dlqi.Message.Subject   = message.Subject;
                                    dlqi.Message.HtmlBody  = message.HtmlBody;
                                    dlqi.Message.TextBody  = message.TextBody;
                                    dlqi.Message.MessageID = message.MessageID;

                                    await _queueService.EnqueueDistributionListBroadcastAsync(dlqi, cancellationToken);
                                }
                                catch (Exception ex)
                                {
                                    Logging.LogException(ex);
                                    return(BadRequest(message));
                                }
                            }
                            else
                            {
                                return(CreatedAtAction(nameof(Receive), new { id = message.MessageID }, message));
                            }
                        }

                        return(CreatedAtAction(nameof(Receive), new { id = message.MessageID }, message));

                        #endregion Distribution Email
                    }
                    if (type == 3)                      // Group Dispatch
                    {
                        #region Group Dispatch Email
                        var departmentGroup = await _departmentGroupsService.GetGroupByDispatchEmailCodeAsync(emailAddress);

                        if (departmentGroup != null)
                        {
                            try
                            {
                                var emailSettings = await _departmentsService.GetDepartmentEmailSettingsAsync(departmentGroup.DepartmentId);

                                var departmentGroupUsers = _departmentGroupsService.GetAllMembersForGroupAndChildGroups(departmentGroup);

                                var callEmail = new CallEmail();
                                callEmail.Subject = message.Subject;

                                if (!String.IsNullOrWhiteSpace(message.HtmlBody))
                                {
                                    callEmail.Body = HttpUtility.HtmlDecode(message.HtmlBody);
                                }
                                else
                                {
                                    callEmail.Body = message.TextBody;
                                }

                                foreach (var attachment in message.Attachments)
                                {
                                    try
                                    {
                                        if (Convert.ToInt32(attachment.ContentLength) > 0)
                                        {
                                            if (attachment.Name.Contains(".mp3") || attachment.Name.Contains(".amr"))
                                            {
                                                byte[] filebytes = Convert.FromBase64String(attachment.Content);

                                                callEmail.DispatchAudioFileName = attachment.Name;
                                                callEmail.DispatchAudio         = filebytes;
                                            }
                                        }
                                    }
                                    catch { }
                                }

                                if (emailSettings == null)
                                {
                                    emailSettings              = new DepartmentCallEmail();
                                    emailSettings.FormatType   = (int)CallEmailTypes.Generic;
                                    emailSettings.DepartmentId = departmentGroup.DepartmentId;

                                    if (departmentGroup.Department != null)
                                    {
                                        emailSettings.Department = departmentGroup.Department;
                                    }
                                    else
                                    {
                                        emailSettings.Department = await _departmentsService.GetDepartmentByIdAsync(departmentGroup.DepartmentId);
                                    }
                                }

                                var activeCalls = await _callsService.GetActiveCallsByDepartmentAsync(emailSettings.Department.DepartmentId);

                                var units = await _unitsService.GetAllUnitsForGroupAsync(departmentGroup.DepartmentGroupId);

                                var priorities = await _callsService.GetActiveCallPrioritiesForDepartmentAsync(emailSettings.Department.DepartmentId);

                                int defaultPriority = (int)CallPriority.High;

                                if (priorities != null && priorities.Any())
                                {
                                    var defaultPrio = priorities.FirstOrDefault(x => x.IsDefault && x.IsDeleted == false);

                                    if (defaultPrio != null)
                                    {
                                        defaultPriority = defaultPrio.DepartmentCallPriorityId;
                                    }
                                }

                                var call = _callsService.GenerateCallFromEmail(emailSettings.FormatType, callEmail,
                                                                               emailSettings.Department.ManagingUserId,
                                                                               departmentGroupUsers.Select(x => x.User).ToList(), emailSettings.Department, activeCalls, units, defaultPriority, priorities);

                                if (call != null)
                                {
                                    call.DepartmentId = departmentGroup.DepartmentId;

                                    var savedCall = await _callsService.SaveCallAsync(call, cancellationToken);

                                    var cqi = new CallQueueItem();
                                    cqi.Call     = savedCall;
                                    cqi.Profiles = await _userProfileService.GetSelectedUserProfilesAsync(departmentGroupUsers.Select(x => x.UserId).ToList());

                                    cqi.DepartmentTextNumber = await _departmentSettingsService.GetTextToCallNumberForDepartmentAsync(cqi.Call.DepartmentId);

                                    await _queueService.EnqueueCallBroadcastAsync(cqi, cancellationToken);

                                    return(CreatedAtAction(nameof(Receive), new { id = savedCall.CallId }, savedCall));
                                }
                            }
                            catch (Exception ex)
                            {
                                Logging.LogException(ex);
                                return(BadRequest());
                            }
                        }
                        #endregion Group Dispatch Email
                    }
                    if (type == 4)                      // Group Message
                    {
                        #region Group Message
                        var departmentGroup = await _departmentGroupsService.GetGroupByMessageEmailCodeAsync(emailAddress);

                        if (departmentGroup != null)
                        {
                            try
                            {
                                var departmentGroupUsers = _departmentGroupsService.GetAllMembersForGroupAndChildGroups(departmentGroup);

                                var newMessage = new Message();
                                newMessage.SentOn          = DateTime.UtcNow;
                                newMessage.SendingUserId   = departmentGroup.Department.ManagingUserId;
                                newMessage.IsBroadcast     = true;
                                newMessage.Subject         = message.Subject;
                                newMessage.SystemGenerated = true;

                                if (!String.IsNullOrWhiteSpace(message.HtmlBody))
                                {
                                    newMessage.Body = HttpUtility.HtmlDecode(message.HtmlBody);
                                }
                                else
                                {
                                    newMessage.Body = message.TextBody;
                                }

                                foreach (var member in departmentGroupUsers)
                                {
                                    if (newMessage.GetRecipients().All(x => x != member.UserId))
                                    {
                                        newMessage.AddRecipient(member.UserId);
                                    }
                                }

                                var savedMessage = await _messageService.SaveMessageAsync(newMessage, cancellationToken);

                                await _messageService.SendMessageAsync(savedMessage, "", departmentGroup.DepartmentId, false, cancellationToken);

                                return(CreatedAtAction(nameof(Receive), new { id = savedMessage.MessageId }, savedMessage));
                            }
                            catch (Exception ex)
                            {
                                Logging.LogException(ex);
                                return(BadRequest());
                            }
                        }

                        #endregion Group Message
                    }

                    return(BadRequest());
                }
                catch (Exception ex)
                {
                    Framework.Logging.LogException(ex);
                    return(BadRequest(ex.ToString()));
                }
            }
            else
            {
                // If our message was null, we throw an exception
                return(BadRequest("Error parsing Inbound Message, message is null."));
            }
        }