Example #1
0
        private bool CheckChatIsAllowed(Message message, ITelegramClientAndServerLogger log)
        {
            if (this.allowedChats.IsNullOrEmpty() || this.allowedChats.Contains(message.Chat.Id))
            {
                return(true);
            }

            log.ReplyBack("You are not allowed to use this Bot instance. Run your own!");
            log.Log($"Bot is not allowed for the user {message.From?.Username} {message.Chat.Id}.");
            return(false);
        }
Example #2
0
        public void Handle(string queryData, ITelegramClientAndServerLogger log)
        {
            var actions = new Dictionary <Regex, Action <Match> >
            {
                { BotCommands.PickTorrent.Regex, PickTorrent },
                { BotCommands.StartTorrent.Regex, StartTorrent },
                { BotCommands.GoToPage.Regex, GoToPage },
                { BotCommands.SortResults.Regex, SortResults },
                { BotCommands.DownloadTorrentFile.Regex, DownloadTorrentFile },
            };

            bool TryFindTorrentInLocalResults(Match match, out string hashedUri, out TrackerCacheResult torrentCandidate)
            {
                torrentCandidate = default;

                hashedUri = match.Groups[BotCommands.PickTorrent.Groups.HashUrl].Value;
                if (!this.hashToUrl.TryGetValue(hashedUri, out var uri))
                {
                    log.Text($"Search results are expired, repeat your search");
                    log.Log($"Uri not found by hash '{hashedUri}'");
                    return(false);
                }

                var torrentCandidates = this.torrents.Results.Where(t => t.Guid == uri).ToArray();

                if (!torrentCandidates.Any())
                {
                    log.Text("Found no torrents, probably Search results are expired, repeat your search");
                    log.Log(
                        $"Torrent could not be found with URL={uri.LocalPath}, "
                        + $"last search has {this.torrents?.Results?.Count} results");
                    return(false);
                }

                if (torrentCandidates.Length > 1)
                {
                    log.Text(
                        "Strange, found more than one torrent with the same characteristics, "
                        + "probably search results are corrupted, repeat your search");
                    log.Log(
                        $"Found {torrentCandidates.Length} torrents with URL={uri.AbsoluteUri}, "
                        + $"last search has {this.torrents?.Results?.Count} results");
                    return(false);
                }

                torrentCandidate = torrentCandidates.Single();
                return(true);
            }

            void DownloadTorrentFile(Match match)
            {
                if (!TryFindTorrentInLocalResults(match, out var hashedUri, out this.torrent))
                {
                    return;
                }

                log.TrySendDocumentBackAsync(this.torrent.Link);
            }

            void PickTorrent(Match match)
            {
                if (!TryFindTorrentInLocalResults(match, out var hashedUri, out this.torrent))
                {
                    return;
                }

                var downloadLocations = (this.bitTorrentClient.IsSetUp
                    ? this.bitTorrentClient.ListDownloadLocations()
                    : default) ?? new string[0];