Esempio n. 1
0
        protected override void LinksAction(IChannelMessageEventArgs args, MessageFlags flags, IList <Uri> links)
        {
            bool postToChannel = false;

            if (links.Count == 0)
            {
                // no links here; never mind
                return;
            }

            // post the regular response?
            if (args.Message.StartsWith(LinkCommandPrefix))
            {
                postToChannel = true;
            }

            // broadcast to users who have opted in
            var optedInNicks = new List <string>();

            // who is in the channel?
            var channelNicks = ConnectionManager.NicknamesInChannel(args.Channel);

            if (channelNicks != null)
            {
                // what are their registered names? (consider only registered users)
                var channelUsers = channelNicks
                                   .Select(nick => new KeyValuePair <string, string>(nick, ConnectionManager.RegisteredNameForNick(nick)))
                                   .Where(n => n.Value != null)
                                   .ToList();

                // which users have opted in?
                using (var ctx = GetNewContext())
                {
                    foreach (var user in channelUsers)
                    {
                        string username = user.Value;
                        if (ctx.OptedInUsers.Any(u => u.UserName == username))
                        {
                            Logger.LogTrace("nick {Nickname} user {Username} is opted in; adding", user.Key, user.Value);
                            optedInNicks.Add(user.Key);
                        }
                        else
                        {
                            Logger.LogTrace("nick {Nickname} user {Username} is not opted in");
                        }
                    }
                }
            }

            if (!postToChannel && optedInNicks.Count == 0)
            {
                // nothing to fetch, nothing to post
                return;
            }

            foreach (var linkAndInfo in links.Select(ObtainLinkInfo))
            {
                if (postToChannel)
                {
                    PostLinkInfoToChannel(linkAndInfo, args.Channel);
                }

                if (LastBroadcastLink == null || LastBroadcastLink != linkAndInfo.Link)
                {
                    foreach (var nick in optedInNicks)
                    {
                        Logger.LogTrace("posting link info to {Nickname}", nick);
                        PostLinkInfo(linkAndInfo, message => ConnectionManager.SendQueryNotice(nick, message));
                    }
                    LastBroadcastLink = linkAndInfo.Link;
                }
                else
                {
                    Logger.LogDebug(
                        "not multicasting link info for {AbsoluteURI}; same as the last",
                        LastBroadcastLink.AbsoluteUri
                        );
                }
            }
        }