Ejemplo n.º 1
0
 private void PerformCheckinIfNecessary(ref TimeSpan uptime, ref TimeSpan lastCheckin)
 {
     if (Uptime - LastCheckin > CheckinInterval)
     {
         bool newTicket = GetNewApiTicket();
         Utils.AddToLog("performing checkin, newticket? " + newTicket, AccountSettings);
         Console.WriteLine("performing checkin, newticket? " + newTicket);
         if (newTicket)
         {
             MessageQueue.AddMessage(GetNewIDNRequest());
         }
         LastCheckin += (Uptime - LastCheckin);
     }
 }
Ejemplo n.º 2
0
        private BotMessage GetNewIDNRequest()
        {
            var idn = new IDNclient()
            {
                account   = AccountSettings.AccountName,
                ticket    = CurrentApiKey,
                character = AccountSettings.CharacterName,
                cname     = AccountSettings.CName,
                cversion  = Version,
                method    = "ticket"
            };

            Utils.AddToLog("idn request created", idn);
            Console.WriteLine("idn request created: " + JsonConvert.SerializeObject(idn));

            return(new BotMessage()
            {
                MessageDataFormat = idn, messageType = "IDN"
            });
        }
Ejemplo n.º 3
0
        public void RunLoop()
        {
            if (_debug)
            {
                Console.WriteLine("Runloop started");
            }

            Uptime      = new TimeSpan();
            LastCheckin = new TimeSpan();

            int LastMessageSent = 0;

            using (var ws = new WebSocket(FListChatUri))
            {
                BotMessage firstIdnRequest = GetIdentificationTicket();

                //ws.OnOpen += (sender, e) =>
                //ws.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.None;

                ws.OnMessage += (sender, e) =>
                                OnMessage(e.Data);

                ws.OnError += (sender, e) =>
                {
                    Console.WriteLine("Websocket Error: " + e.Message);
                    Utils.AddToLog("Websocket Error: " + e.Message, null);
                };

                ws.OnClose += (sender, e) =>
                {
                    Console.WriteLine("WebSocket Close ({0}) ({1})", e.Code, e.Reason);
                    Utils.AddToLog(string.Format("WebSocket Close ({0}) ({1})", e.Code, e.Reason), null);

                    var sslProtocolHack = (System.Security.Authentication.SslProtocols)(SslProtocolsHack.Tls12 | SslProtocolsHack.Tls11 | SslProtocolsHack.Tls);
                    //TlsHandshakeFailure
                    if (e.Code == 1015 && ws.SslConfiguration.EnabledSslProtocols != sslProtocolHack)
                    {
                        Console.WriteLine("activating ssl protocol change");
                        //Utils.AddToLog("activating ssl protocol change", null);
                        ws.SslConfiguration.EnabledSslProtocols = sslProtocolHack;
                        Thread.Sleep(1000);
                        ws.Connect();
                    }
                };


                ws.Origin = "http://localhost:4649";

                ws.EnableRedirection = true;

                PerformStartupClientCommands(ws, firstIdnRequest, false);

                while (true)
                {
                    if (ws.IsAlive)
                    {
                        LastMessageSent += TickTimeMiliseconds;

                        if (MessageQueue.HasMessages())
                        {
                            BotMessage nextMessagePeek = MessageQueue.Peek();

                            if (!Utils.BotMessageIsChatMessage(nextMessagePeek) || LastMessageSent >= MinimumTimeBetweenMessages)
                            {
                                BotMessage nextMessageToSend = MessageQueue.GetNextMessage();

                                if (nextMessageToSend != null)
                                {
                                    if (nextMessageToSend.messageType == "PIN")
                                    {
                                        PinMessageSent++;
                                        if (PinMessageSent % 5 == 0)
                                        {
                                            Console.WriteLine("sending: " + nextMessageToSend.PrintedCommand() + " (#" + PinMessageSent + ")");
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("sending: " + nextMessageToSend.PrintedCommand());
                                        Utils.AddToLog("sending: " + nextMessageToSend.PrintedCommand(), nextMessageToSend);
                                    }

                                    ws.Send(nextMessageToSend.PrintedCommand());
                                }
                                if (Utils.BotMessageIsChatMessage(nextMessageToSend))
                                {
                                    LastMessageSent = 0;
                                }
                            }
                        }
                        if (WaitingChannelOpRequests.Count > 0)
                        {
                            if (_testVersion)
                            {
                                Console.WriteLine("Channeloprequest found in queue");
                            }

                            List <UserGeneratedCommand> finished = new List <UserGeneratedCommand>();
                            foreach (UserGeneratedCommand req in WaitingChannelOpRequests)
                            {
                                if (req.ops != null)
                                {
                                    BotCommandController.RunChatBotCommand(req);
                                    finished.Add(req);
                                }
                            }
                            if (finished.Count > 0)
                            {
                                foreach (UserGeneratedCommand rFin in finished)
                                {
                                    WaitingChannelOpRequests.Remove(rFin);
                                }
                            }
                        }

                        Uptime.Add(new TimeSpan(0, 0, 0, 0, TickTimeMiliseconds));

                        PerformCheckinIfNecessary(ref Uptime, ref LastCheckin);
                    }
                    else
                    {
                        if (ReconnectTimer == 0)
                        {
                            Console.WriteLine("Connection was lost.");
                            Console.WriteLine("Waiting for " + (ReconnectTimeMs / 1000) + " seconds to reconnect.");
                            Utils.AddToLog("Waiting for " + (ReconnectTimeMs / 1000) + " seconds to reconnect.", null);
                        }

                        ReconnectTimer += TickTimeMiliseconds;

                        if (ReconnectTimer > ReconnectTimeMs)
                        {
                            BotMessage reconnectIdnRequest = GetIdentificationTicket();
                            PerformStartupClientCommands(ws, reconnectIdnRequest, true);
                            ReconnectTimer = 0;
                        }
                    }
                    Thread.Sleep(TickTimeMiliseconds);
                }
            }
        }
Ejemplo n.º 4
0
        private void LoginToServer(AccountSettings accountSettings, bool friendsList, bool bookmarksList)
        {

            GetApiTicketRequest requestObject = new GetApiTicketRequest()
            {
                password = accountSettings.AccountPassword,
                account = accountSettings.AccountName,
                no_bookmarks = bookmarksList? "false" : "true",
                no_characters = "true",
                no_friends = friendsList ? "false" : "true"
            };

            string webserver = GetApiTicketUrl;
            //parameters are what is actually used to get the api ticket rather than the post body content
            string parameters = "?account=" + requestObject.account + "&password="******"&no_characters=" + 
                requestObject.no_characters + "&no_friends=" + requestObject.no_friends + "&no_bookmarks=" + 
                requestObject.no_bookmarks;

            WebRequest request = Utils.CreateWebRequest(webserver + parameters, requestObject);
            WebResponse webResponse = null;

            try
            {
                webResponse = (HttpWebResponse)request.GetResponse();

                if (webResponse != null)
                {
                    System.IO.StreamReader sr = new System.IO.StreamReader(webResponse.GetResponseStream());
                    string rtnValue = sr.ReadToEnd();

                    if (BotMain._debug)
                    {
                        Console.WriteLine("LoginToServer return message: " + rtnValue);
                        Utils.AddToLog("LoginToServer return message: " + rtnValue, null);
                    }

                    GetApiTicketResponse rpt = JsonConvert.DeserializeObject<GetApiTicketResponse>(rtnValue);
                    awaitingReturn = false;

                    if (rpt != null)
                    {
                        ApiTicketResult = rpt;
                    }
                    else
                    {
                        Console.WriteLine("Error: failed to parse result into GetApiTicketResponse");
                        Utils.AddToLog("Error: failed to parse result into GetApiTicketResponse", null);
                    }

                    string saaaaaa = rtnValue + " ";
                }
            }
            catch (WebException ex)
            {
                Console.WriteLine("WebException on LoginToServer Call: " + ex.Message);
                Console.WriteLine("Retrying...");

                Utils.AddToLog("WebException on LoginToServer Call: " + ex.Message, accountSettings);
                LoginToServer(accountSettings, friendsList, bookmarksList);
            }
        }