Example #1
0
        public void Cleanup()
        {
            _running = false;

            _tox1.Dispose();
            _tox2.Dispose();
        }
Example #2
0
        public Basic()
        {
            ToxOptions options = new ToxOptions(true, false);

            tox = new Tox(options);
            tox.OnFriendRequest += tox_OnFriendRequest;
            tox.OnFriendMessage += tox_OnFriendMessage;

            foreach (ToxNode node in Nodes)
            {
                tox.BootstrapFromNode(node);
            }

            tox.Name          = "SharpTox";
            tox.StatusMessage = "Testing SharpTox";

            tox.Start();

            string id = tox.Id.ToString();

            Console.WriteLine("ID: {0}", id);

            Console.ReadKey();
            tox.Dispose();
        }
Example #3
0
        public void TestToxEncryptionLoad()
        {
            var tox1 = new Tox(ToxOptions.Default);

            tox1.Name          = "Test";
            tox1.StatusMessage = "Hey";

            string password = "******";
            var    data     = tox1.GetData(password);

            Assert.IsNotNull(data, "Failed to encrypt the Tox data");
            Assert.IsTrue(data.IsEncrypted, "We encrypted the data, but toxencryptsave thinks we didn't");

            var tox2 = new Tox(ToxOptions.Default, ToxData.FromBytes(data.Bytes), password);

            if (tox2.Id != tox1.Id)
            {
                Assert.Fail("Failed to load tox data correctly, tox id's don't match");
            }

            if (tox2.Name != tox1.Name)
            {
                Assert.Fail("Failed to load tox data correctly, names don't match");
            }

            if (tox2.StatusMessage != tox1.StatusMessage)
            {
                Assert.Fail("Failed to load tox data correctly, status messages don't match");
            }

            tox1.Dispose();
            tox2.Dispose();
        }
Example #4
0
        public void TestToxLoadData()
        {
            var tox1 = new Tox(ToxOptions.Default);

            tox1.Name          = "Test";
            tox1.StatusMessage = "Hey";

            var data = tox1.GetData();
            var tox2 = new Tox(ToxOptions.Default, ToxData.FromBytes(data.Bytes));

            if (tox2.Id != tox1.Id)
            {
                Assert.Fail("Failed to load tox data correctly, tox id's don't match");
            }

            if (tox2.Name != tox1.Name)
            {
                Assert.Fail("Failed to load tox data correctly, names don't match");
            }

            if (tox2.StatusMessage != tox1.StatusMessage)
            {
                Assert.Fail("Failed to load tox data correctly, status messages don't match");
            }

            tox1.Dispose();
            tox2.Dispose();
        }
Example #5
0
        public ProfileInfo CreateNew(string profileName)
        {
            string path = Path.Combine(ProfileDataPath, profileName + ".tox");

            if (File.Exists(path))
            {
                return(null);
            }

            var tox = new Tox(ToxOptions.Default);

            tox.Name          = profileName;
            tox.StatusMessage = "Toxing on Toxy";

            try
            {
                if (!Directory.Exists(ProfileDataPath))
                {
                    Directory.CreateDirectory(ProfileDataPath);
                }
            }
            catch { return(null); }

            if (!tox.GetData().Save(path))
            {
                return(null);
            }

            tox.Dispose();
            return(new ProfileInfo(path));
        }
Example #6
0
        public void TestToxAvCallAndAnswer()
        {
            var options = new ToxOptions(true, true);
            var tox1    = new Tox(options);
            var tox2    = new Tox(options);

            var toxAv1 = new ToxAv(tox1);
            var toxAv2 = new ToxAv(tox2);

            bool testFinished = false;

            Task.Run(async() =>
            {
                while (!testFinished)
                {
                    int time1 = tox1.Iterate();
                    int time2 = tox2.Iterate();

                    await Task.Delay(Math.Min(time1, time2));
                }
            });

            tox1.AddFriend(tox2.Id, "hey");
            tox2.AddFriend(tox1.Id, "hey");

            while (tox1.GetFriendConnectionStatus(0) == ToxConnectionStatus.None)
            {
                Thread.Sleep(10);
            }

            bool answered = false;

            toxAv1.Call(0, 48, 30000);

            toxAv2.OnCallRequestReceived += (sender, e) =>
            {
                var  error2  = ToxAvErrorAnswer.Ok;
                bool result2 = toxAv2.Answer(e.FriendNumber, 48, 30000, out error2);
            };

            toxAv1.OnCallStateChanged += (sender, e) =>
            {
                answered = true;
            };

            while (!answered)
            {
                Thread.Sleep(10);
            }

            testFinished = true;
            toxAv1.Dispose();
            toxAv2.Dispose();
            tox1.Dispose();
            tox2.Dispose();
        }
Example #7
0
        //should only be used when the application closes
        public void Dispose()
        {
            if (ToxAv != null)
            {
                ToxAv.Dispose();
            }

            if (Tox != null)
            {
                Tox.Dispose();
            }
        }
Example #8
0
        public void TestToxSelfStatusMessage()
        {
            var    tox           = new Tox(ToxOptions.Default);
            string statusMessage = "Test status message";

            tox.StatusMessage = statusMessage;

            if (tox.StatusMessage != statusMessage)
            {
                Assert.Fail("Failed to set/retrieve status message");
            }

            tox.Dispose();
        }
Example #9
0
        public void TestToxSelfName()
        {
            var    tox  = new Tox(ToxOptions.Default);
            string name = "Test name";

            tox.Name = name;

            if (tox.Name != name)
            {
                Assert.Fail("Failed to set/retrieve name");
            }

            tox.Dispose();
        }
Example #10
0
        public void TestToxSelfStatus()
        {
            var tox    = new Tox(ToxOptions.Default);
            var status = ToxUserStatus.Away;

            tox.Status = status;

            if (tox.Status != status)
            {
                Assert.Fail("Failed to set/retrieve status");
            }

            tox.Dispose();
        }
Example #11
0
        public void Logout()
        {
            if (Tox != null)
            {
                Tox.Dispose();
            }

            if (ToxAv != null)
            {
                ToxAv.Dispose();
            }

            Config.Instance.ProfilePath = null;
            Config.Instance.Save();
        }
Example #12
0
        public void TestToxFriendRequest()
        {
            var    options      = new ToxOptions(true, true);
            var    tox1         = new Tox(options);
            var    tox2         = new Tox(options);
            var    error        = ToxErrorFriendAdd.Ok;
            string message      = "Hey, this is a test friend request.";
            bool   testFinished = false;

            tox1.AddFriend(tox2.Id, message, out error);
            if (error != ToxErrorFriendAdd.Ok)
            {
                Assert.Fail("Failed to add friend: {0}", error);
            }

            tox2.OnFriendRequestReceived += (sender, args) =>
            {
                if (args.Message != message)
                {
                    Assert.Fail("Message received in the friend request is not the same as the one that was sent");
                }

                tox2.AddFriendNoRequest(args.PublicKey, out error);
                if (error != ToxErrorFriendAdd.Ok)
                {
                    Assert.Fail("Failed to add friend (no request): {0}", error);
                }

                if (!tox2.FriendExists(0))
                {
                    Assert.Fail("Friend doesn't exist according to core");
                }

                testFinished = true;
            };

            while (!testFinished && tox1.GetFriendConnectionStatus(0) == ToxConnectionStatus.None)
            {
                int time1 = tox1.Iterate();
                int time2 = tox2.Iterate();

                Thread.Sleep(Math.Min(time1, time2));
            }

            tox1.Dispose();
            tox2.Dispose();
        }
Example #13
0
        public NewUserProfileViewModel(string directory)
        {
            this.WhenAnyValue(x => x.Password)
            .Select(x => !string.IsNullOrEmpty(x))
            .ToPropertyEx(this, x => x.Encrypted);

            this.Login = ReactiveCommand.Create(() =>
            {
                var filePath = Path.Combine(directory, this.Name + ".tox");
                var tox      = new Tox(ToxOptions.Default());
                if (this.Encrypted)
                {
                    var data = tox.GetData().Bytes;
                    ToxEncryption.Encrypt(data, this.Password, out _);
                    var toxdata = ToxData.FromBytes(data);
                    tox.Dispose();
                    toxdata.Save(filePath);

                    tox = new Tox(ToxOptions.Default(), toxdata, this.Password);
                }

                return(new ToxSession(tox, filePath));
            }, this.WhenAnyValue(x => x.Name)
                                                .Select(fileName =>
            {
                if (string.IsNullOrWhiteSpace(fileName))
                {
                    return(false);
                }

                var filePath = Path.Combine(directory, fileName + ".tox");
                if (File.Exists(filePath))
                {
                    return(false);
                }

                if ((from i in Path.GetInvalidFileNameChars()
                     from c in fileName
                     select i == c).Any(x => x))
                {
                    return(false);
                }

                return(true);
            }));
        }
Example #14
0
        public void TestToxNospam()
        {
            var tox = new Tox(ToxOptions.Default);

            byte[] randomBytes = new byte[sizeof(uint)];
            new Random().NextBytes(randomBytes);

            int nospam = BitConverter.ToInt32(randomBytes, 0);

            tox.SetNospam(nospam);

            if (nospam != tox.GetNospam())
            {
                Assert.Fail("Failed to set/get nospam correctly, values don't match");
            }

            tox.Dispose();
        }
Example #15
0
        public void TestToxPortBind()
        {
            var tox1 = new Tox(new ToxOptions(true, false));
            var tox2 = new Tox(new ToxOptions(true, true));

            var error = ToxErrorGetPort.Ok;
            int port  = tox1.GetUdpPort(out error);

            if (error != ToxErrorGetPort.NotBound)
            {
                Assert.Fail("Tox bound to an udp port while it's not supposed to, port: {0}", port);
            }

            port = tox2.GetUdpPort(out error);
            if (error != ToxErrorGetPort.Ok)
            {
                Assert.Fail("Failed to bind to an udp port");
            }

            tox1.Dispose();
            tox2.Dispose();
        }
Example #16
0
        public void TestToxDataParsing()
        {
            var tox = new Tox(ToxOptions.Default);

            tox.Name          = "Test";
            tox.StatusMessage = "Status";
            tox.Status        = ToxUserStatus.Away;

            var         data = tox.GetData();
            ToxDataInfo info = null;

            if (data == null || !data.TryParse(out info))
            {
                Assert.Fail("Parsing the data file failed");
            }

            if (info.Id != tox.Id || info.Name != tox.Name || info.SecretKey != tox.GetPrivateKey() || info.Status != tox.Status || info.StatusMessage != tox.StatusMessage)
            {
                Assert.Fail("Parsing the data file failed");
            }

            tox.Dispose();
        }
Example #17
0
        public void TestToxBootstrapAndConnectTcp()
        {
            var tox   = new Tox(new ToxOptions(true, false));
            var error = ToxErrorBootstrap.Ok;

            foreach (var node in Globals.TcpRelays)
            {
                bool result = tox.AddTcpRelay(node, out error);
                if (!result || error != ToxErrorBootstrap.Ok)
                {
                    Assert.Fail("Failed to bootstrap error: {0}, result: {1}", error, result);
                }
            }

            tox.Start();
            while (!tox.IsConnected)
            {
                Thread.Sleep(10);
            }

            Console.WriteLine("Tox connected!");
            tox.Dispose();
        }
Example #18
0
        public void TestToxProxySocks5()
        {
            var options = new ToxOptions(true, ToxProxyType.Socks5, "127.0.0.1", 9050);
            var tox     = new Tox(options);
            var error   = ToxErrorBootstrap.Ok;

            foreach (var node in Globals.TcpRelays)
            {
                bool result = tox.AddTcpRelay(node, out error);
                if (!result || error != ToxErrorBootstrap.Ok)
                {
                    Assert.Fail("Failed to bootstrap, error: {0}, result: {1}", error, result);
                }
            }

            tox.Start();
            while (!tox.IsConnected)
            {
                Thread.Sleep(10);
            }

            Console.WriteLine("Tox connected!");
            tox.Dispose();
        }
Example #19
0
        public Skynet(string filename = "")
        {
            // init tox client
            ToxOptions options = new ToxOptions(true, true);

            if (filename != "")
            {
                tox = new Tox(options, ToxData.FromDisk(filename));
            }
            else
            {
                tox = new Tox(options);
            }



            tox.OnFriendRequestReceived         += tox_OnFriendRequestReceived;
            tox.OnFriendLosslessPacketReceived  += tox_OnFriendLosslessPacketReceived;
            tox.OnFriendConnectionStatusChanged += tox_OnFriendConnectionStatusChanged;

            foreach (ToxNode node in Nodes)
            {
                tox.Bootstrap(node);
            }

            tox.Name          = "Skynet";
            tox.StatusMessage = "Running Skynet";
            tox.Start();

            string id = tox.Id.ToString();

            Console.WriteLine("ID: {0}", id);
            Utils.Utils.LogUtils("ID: " + id);

            // Log tox online status
            Task.Factory.StartNew(async() => {
                var offLineCount = 0;
                while (true)
                {
                    Thread.Sleep(2000);
                    if (tox.IsConnected)
                    {
                        Console.WriteLine("From Server " + httpPort + ":" + "tox is connected.");
                        Utils.Utils.LogUtils("From Server " + httpPort + ":" + "tox is connected.");
                        offLineCount = 0;
                        // send a online message to server
                        using (var client = new HttpClient()){
                            await client.PostAsJsonAsync("http://xiaoqiang.bwbot.org/online", tox.Id.ToString());
                        }
                        break;
                    }
                    else
                    {
                        Utils.Utils.LogUtils("Event: tox is offline");
                        offLineCount++;
                    }
                    if (offLineCount > 10)
                    {
                        // start a new tox node
                        offLineCount = 0;
                        tox.Dispose();
                        options = new ToxOptions(true, true);
                        if (filename != "")
                        {
                            tox = new Tox(options, ToxData.FromDisk(filename));
                        }
                        else
                        {
                            tox = new Tox(options);
                        }

                        tox.OnFriendRequestReceived         += tox_OnFriendRequestReceived;
                        tox.OnFriendLosslessPacketReceived  += tox_OnFriendLosslessPacketReceived;
                        tox.OnFriendConnectionStatusChanged += tox_OnFriendConnectionStatusChanged;

                        foreach (ToxNode node in Nodes)
                        {
                            tox.Bootstrap(node);
                        }

                        tox.Name          = "Skynet";
                        tox.StatusMessage = "Running Skynet";
                        tox.Start();

                        id = tox.Id.ToString();
                        Console.WriteLine("ID: {0}", id);
                        Utils.Utils.LogUtils("ID: " + id);
                    }
                }

                while (true)
                {
                    // start queue process
                    while (tox.IsConnected)
                    {
                        Package processPack = null;
                        lock (reqQueueLock) {
                            if (reqQueue.Count > 0)
                            {
                                processPack = reqQueue.Dequeue();
                            }
                        }
                        if (processPack != null)
                        {
                            newReqReceived(processPack);
                        }
                        else
                        {
                            Thread.Sleep(1);
                        }
                    }
                    Utils.Utils.LogUtils("Event: tox is offline");
                    Thread.Sleep(1000);
                }
            }, TaskCreationOptions.LongRunning).ForgetOrThrow();



            // start http server
            httpPort = Utils.Utils.FreeTcpPort();
            string baseUrl = "http://localhost:" + httpPort + "/";

            WebApp.Start <StartUp> (url: baseUrl);
            Console.WriteLine("Server listening on " + httpPort);
            Utils.Utils.LogUtils("Server listening on " + httpPort);
            allInstance.Add(this);
        }
Example #20
0
        public void SwitchTo(ProfileInfo profile)
        {
            if (profile == null)
            {
                throw new ArgumentNullException("profile");
            }

            ToxOptions options = ToxOptions.Default;

            options.Ipv6Enabled = Config.Instance.EnableIpv6;
            options.UdpEnabled  = Config.Instance.EnableUdp;

            ToxData data = ToxData.FromDisk(profile.Path);

            if (data == null)
            {
                throw new Exception("Could not load profile.");
            }

            Tox   tox   = new Tox(options, data);
            ToxAv toxAv = new ToxAv(tox);

            try
            {
                this.InitManagers(tox, toxAv);
            }
            catch
            {
                toxAv.Dispose();
                tox.Dispose();
                throw;
            }

            if (this.Tox != null)
            {
                this.Tox.Dispose();
                this.Tox = null;
            }

            if (this.ToxAv != null)
            {
                this.ToxAv.Dispose();
                this.ToxAv = null;
            }

            this.Tox   = tox;
            this.ToxAv = toxAv;

            this.Tox.OnFriendRequestReceived         += this.OnToxFriendRequestReceived;
            this.Tox.OnFriendMessageReceived         += this.OnToxFriendMessageReceived;
            this.Tox.OnFriendNameChanged             += this.OnToxFriendNameChanged;
            this.Tox.OnFriendStatusMessageChanged    += this.OnToxFriendStatusMessageChanged;
            this.Tox.OnFriendStatusChanged           += this.OnToxFriendStatusChanged;
            this.Tox.OnFriendTypingChanged           += this.OnToxFriendTypingChanged;
            this.Tox.OnFriendConnectionStatusChanged += this.OnToxFriendConnectionStatusChanged;
            this.Tox.OnReadReceiptReceived           += this.OnToxReadReceiptReceived;

            this.Tox.OnFriendLossyPacketReceived    += this.OnToxFriendLossyPacketReceived;
            this.Tox.OnFriendLosslessPacketReceived += this.OnToxFriendLosslessPacketReceived;

            this.Tox.OnGroupInvite         += this.OnToxGroupInvite;
            this.Tox.OnGroupAction         += this.OnToxGroupAction;
            this.Tox.OnGroupMessage        += this.OnToxGroupMessage;
            this.Tox.OnGroupNamelistChange += this.OnToxGroupNamelistChange;
            this.Tox.OnGroupTitleChanged   += this.OnToxGroupTitleChanged;

            this.Tox.OnFileSendRequestReceived += this.OnToxFileSendRequestReceived;
            this.Tox.OnFileControlReceived     += this.OnToxFileControlReceived;
            this.Tox.OnFileChunkReceived       += this.OnToxFileChunkReceived;
            this.Tox.OnFileChunkRequested      += this.OnToxFileChunkRequested;

            this.CurrentProfile = profile;
        }
Example #21
0
        public void SwitchTo(ProfileInfo profile)
        {
            var options = ToxOptions.Default;

            options.Ipv6Enabled = Config.Instance.EnableIpv6;

            if (Config.Instance.ProxyType != ToxProxyType.None)
            {
                options.UdpEnabled = false;
                options.ProxyType  = Config.Instance.ProxyType;
                options.ProxyHost  = Config.Instance.ProxyAddress;
                options.ProxyPort  = Config.Instance.ProxyPort;
            }
            else
            {
                options.UdpEnabled = Config.Instance.EnableUdp;
            }

            Tox newTox;

            if (profile != null)
            {
                var data = ToxData.FromDisk(profile.Path);
                if (data == null)
                {
                    throw new Exception("Could not load profile.");
                }

                if (data.IsEncrypted)
                {
                    throw new Exception("Data is encrypted, Toxy does not support encrypted profiles yet.");
                }

                newTox = new Tox(options, data);
            }
            else
            {
                newTox = new Tox(options);
            }

            var newToxAv = new ToxAv(newTox);

            InitManagers(newTox, newToxAv);

            if (Tox != null)
            {
                Tox.Dispose();
            }

            if (ToxAv != null)
            {
                ToxAv.Dispose();
            }

            Tox   = newTox;
            ToxAv = newToxAv;

            AvatarManager.Rehash();
            ConnectionManager.DoBootstrap();

            //TODO: move this someplace else and make it configurable
            if (string.IsNullOrEmpty(Tox.Name))
            {
                Tox.Name = "Tox User";
            }
            if (string.IsNullOrEmpty(Tox.StatusMessage))
            {
                Tox.StatusMessage = "Toxing on Toxy";
            }

            Tox.Start();
            ToxAv.Start();

            CurrentProfile = profile;
            MainWindow.Instance.Reload();
        }