Exemplo n.º 1
0
 public void LoginUser(IEnumerable <FlurlCookie> cookies)
 {
     Cookies = new CookieJar();
     foreach (var cookie in cookies)
     {
         Cookies.AddOrReplace(cookie);
     }
 }
Exemplo n.º 2
0
        public static async Task <bool> PingAsync(string room)
        {
            int       index;
            string    me;
            CookieJar cookies   = new CookieJar();
            string    StintoUrl = "https://stin.to/api/chat/" + room + "/";

            while (true)
            {
                me = RandomString(6);
                IFlurlResponse response;
                try {
                    Trace.WriteLine("Logujem se u sobu za ping " + room);
                    response = await StintoUrl.AppendPathSegment("login").WithTimeout(999).PostUrlEncodedAsync(new { nick = me, termsOfUse = "true" });

                    if (response.ResponseMessage.IsSuccessStatusCode)
                    {
                        cookies.AddOrReplace(response.Cookies[1]);
                        break;
                    }
                }
                catch (FlurlHttpException ex) when(ex.StatusCode == 429)
                {
                    Trace.WriteLine("Previše se logujem se u sobu za ping " + room);
                    await Task.Delay(500);

                    continue;
                }
            }
            Trace.WriteLine("Gledam ko sam ja u pingu u sobi " + room);
            string result = await StintoUrl.AppendPathSegment("poll").SetQueryParam("seq", -2).WithCookies(cookies).GetStringAsync();

            string[] lines = result.Substring(0, result.IndexOf("\n0")).Split('\n');
            lines = lines.Take(lines.Length - 1).Where(line => line.Split('\t')[6] == me && line.Split('\t')[8] == "false").ToArray();
            me    = lines[0].Split('\t')[4];

            Trace.WriteLine("Čitam index poslednje poruke za ping u sobi " + room);
            result = DecodeUri((await StintoUrl.AppendPathSegment("poll").SetQueryParam("seq", 0).WithCookies(cookies).GetStringAsync()).Trim());
            index  = Convert.ToInt32(result.Split('\n').Last().Split('\t')[0], 16);

            Trace.WriteLine("Zovem domaćina za ping u sobi " + room);
            await StintoUrl.AppendPathSegment("post").WithCookies(cookies).PostUrlEncodedAsync(new { type = "TXT", text = "private" + delimeter + areyoufree + delimeter + me });

            var cancellationToken = new CancellationTokenSource();
            var task = WaitPingAsync(cookies, StintoUrl, me, index, cancellationToken.Token);

            Trace.WriteLine("Čekam domaćinov odgovr za ping u sobi " + room);
            if (await Task.WhenAny(task, Task.Delay(waitTime, cancellationToken.Token)) == task)
            {
                return(true);
            }
            Trace.WriteLine("Domaćin nije odgovrio u sobi " + room);
            cancellationToken.Cancel();
            return(false);
        }