Example #1
0
        public ISet <string> ExpandDistributionLists(
            string sender,
            IEnumerable <string> originalRecipients,
            HashSet <string> excludedFromExpansion)
        {
            var to = new HashSet <string>();

            foreach (string recipient in originalRecipients)
            {
                DomainSettings domain = null;
                if (_domainSettings.TryGetValue(
                        MailUtilities.GetDomainFromMailbox(recipient),
                        out Lazy <IVolatile <DomainSettings> > settings))
                {
                    domain = settings.Value.Value;
                }

                DistributionList distributionList = domain?.DistributionLists?.FirstOrDefault(dl => dl.Mailbox == recipient);
                if (distributionList != null)
                {
                    if (!CheckValidSender(sender, distributionList))
                    {
                        _log.Warning($"Attempt by invalid sender {sender} to send to DL {distributionList.Mailbox}");
                        continue;
                    }

                    IEnumerable <string> nonExcludedMembers = distributionList.Members.Where(m => !excludedFromExpansion.Contains(m));
                    foreach (string member in nonExcludedMembers)
                    {
                        to.Add(member);
                    }

                    continue;
                }

                {
                    // C# compiler mistake that prevents collapsing the domain.Alias null check
                    if (domain?.Aliases != null && domain.Aliases.TryGetValue(recipient, out string newRecipient))
                    {
                        if (excludedFromExpansion.Contains(newRecipient))
                        {
                            continue;
                        }

                        to.Add(newRecipient);
                        continue;
                    }
                }

                to.Add(recipient);
            }

            return(to);
        }
Example #2
0
        public static bool CheckValidSender(string sender, DistributionList distributionList)
        {
            if (!distributionList.Enabled)
            {
                return(false);
            }

            if (!distributionList.AllowExternalSenders &&
                MailUtilities.GetDomainFromMailbox(sender) != MailUtilities.GetDomainFromMailbox(distributionList.Mailbox))
            {
                return(false);
            }

            return(true);
        }