Example #1
0
        public async Task SendMessage(string username, string password, string recipient, string subject, string body)
        {
            body = body.Replace("\r", "");

            var query = _downloadService.NewQuery();

            query.Add("uid", $"{await GetUserId(username, password)}");
            query.Add("to", recipient);
            query.Add("subject", subject);
            query.Add("message", body);

            await _downloadService.DownloadWithUserLogin(
                "https://www.shacknews.com/messages/send",
                username, password, query);
        }
        public async Task <ThreadLolCounts> DownloadThreadLolCounts(ChattyThread thread)
        {
            var query = _downloadService.NewQuery();

            foreach (var post in thread.Posts)
            {
                query.Add("ids[]", $"{post.Id}");
            }

            var json = await _downloadService.DownloadWithSharedLogin(
                "https://www.shacknews.com/api2/api-index.php?action2=get_all_tags_for_posts",
                verifyLoginStatus : false,
                postBody : query);

            var response = JsonSerializer.Deserialize <TagsForPosts>(json,
                                                                     new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            });

            return
                (new ThreadLolCounts
            {
                CountsByPostId = (
                    from tag in response.Data
                    group tag by int.Parse(tag.ThreadId) into post_group
                    let postLols = (
                        from x in post_group
                        select new LolModel {
                    Tag = GetTagName(x.Tag), Count = int.Parse(x.Total)
                }
                        ).ToList()
                                   select(PostId: post_group.Key, Tags: postLols)
                    ).ToDictionary(x => x.PostId, x => x.Tags)
            });
        private async Task <bool> SendMessageNotification(MessageModel newMessage)
        {
            var postBody =
                $"From: {newMessage.From}\r\n" +
                $"Date: {newMessage.Date}\r\n" +
                $"Subject: {newMessage.Subject}\r\n\r\n" +
                _stripTagsRegex.Replace(newMessage.Body, "");

            var query = _downloadService.NewQuery();

            query.Add("token", _dukeNukedOptions.SlackToken);
            query.Add("channel", "#duke-nuked");
            query.Add("text", postBody);
            query.Add("link_names", "false");
            query.Add("unfurl_links", "false");
            query.Add("unfurl_media", "false");
            query.Add("mrkdwn", "false");

            var result = await _downloadService.DownloadAnonymous(
                "https://slack.com/api/chat.postMessage", query);

            if (result.Contains("\"ok\":false"))
            {
                _logger.LogError("Slack API returned error: {Result}", result);
                return(false);
            }
            else
            {
                _logger.LogInformation("Slack result: {Result}", result);
                return(true);
            }
        }
        public async Task Post(string username, string password, int parentId, string body)
        {
            if (body.Length > 0 && body[0] == '@')
            {
                body = " " + body;
            }

            var contentTypeId = 17;
            var contentId     = 17;

            if (parentId != 0)
            {
                try
                {
                    (contentTypeId, contentId) = await _threadParser.GetContentTypeId(parentId);
                }
                catch (ParsingException)
                {
                    throw new Api400Exception(Api400Exception.Codes.INVALID_PARENT, "Invalid parent ID.");
                }
            }

            var query = _downloadService.NewQuery();

            query.Add("parent_id", parentId == 0 ? "" : $"{parentId}");
            query.Add("content_type_id", $"{contentTypeId}");
            query.Add("content_id", $"{contentId}");
            query.Add("page", "");
            query.Add("parent_url", "/chatty");
            query.Add("body", _emojiConverter.ConvertEmojisToEntities(body.Replace("&", "&amp;")));

            var lastEventId = await _eventProvider.GetLastEventId();

            var response = await _downloadService.DownloadWithUserLogin(
                "https://www.shacknews.com/post_chatty.x",
                username, password, query);

            if (response.Contains("You must be logged in to post", StringComparison.Ordinal))
            {
                throw new Api400Exception(Api400Exception.Codes.INVALID_LOGIN,
                                          $"Unable to log into user account [{username}].");
            }
            if (response.Contains("Please wait a few minutes before trying to post again", StringComparison.Ordinal))
            {
                throw new Api400Exception(Api400Exception.Codes.POST_RATE_LIMIT,
                                          "Please wait a few minutes before trying to post again.");
            }
            if (response.Contains("banned", StringComparison.Ordinal))
            {
                throw new Api400Exception(Api400Exception.Codes.BANNED,
                                          "You are banned.");
            }
            if (response.Contains("Trying to post to a nuked thread", StringComparison.Ordinal))
            {
                throw new Api400Exception(Api400Exception.Codes.NUKED,
                                          "You cannot reply to a nuked thread or subthread.");
            }
            if (!response.Contains("fixup_postbox_parent_for_remove(", StringComparison.Ordinal))
            {
                throw new Api500Exception("Unexpected response from server: " + response);
            }

            // wait for the event to appear
            var sw          = Stopwatch.StartNew();
            var maxTimeSpan = TimeSpan.FromSeconds(15);

            while (sw.Elapsed < maxTimeSpan)
            {
                foreach (var ev in await _eventProvider.GetEvents(lastEventId))
                {
                    if (ev.EventType == EventType.NewPost)
                    {
                        var eventData = (NewPostEventDataModel)ev.EventData;
                        if (eventData.Post.Author.Equals(username, StringComparison.InvariantCultureIgnoreCase))
                        {
                            return;
                        }
                    }
                    lastEventId = ev.EventId;
                }
                await Task.Delay(TimeSpan.FromSeconds(1));
            }
        }