Exemple #1
0
        public static void Chat(string[] args, AppData appData)
        {
            int  p    = -1;
            bool room = false;

            string[] opts = new OptionSet()
            {
                { "p=|prev=", _ => p = Convert.ToInt32(_) },
                { "R|room", _ => room = true },
            }.Parse(args).ToArray();

            long id = appData.GetId(opts.GetArg(1));

            bool?roomAbbr = appData.IsRoom(opts.GetArg(1));

            if (roomAbbr.HasValue && roomAbbr.Value != room)
            {
                Console.WriteLine($"Warning: setting -R|--room flag to {roomAbbr.Value} according to abbreviations data.");
                room = roomAbbr.Value;
            }

            var vk = new VkApi();

            vk.Authorize(appData.AccessToken);

            var msgs = MiscUtils.RecvMessages(vk, id, p >= 0 ? (int?)p : null, false, false);

            Console.WriteLine("Entering chat mode. Press enter at any time to begin typing.");
            if (!room)
            {
                CliUtils.PresentField("Buddy", appData.GetAbbr(id));
            }
            else
            {
                CliUtils.PresentField("Room", appData.GetAbbr(id));
            }

            if (p != 0)
            {
                foreach (var m in msgs)
                {
                    Console.WriteLine();
                    CliUtils.PresentMessage(m, appData);
                }
            }

            CliUtils.LaunchChatMode(vk, appData, id, room);
        }
Exemple #2
0
        public static void Friend(string[] args, AppData appData)
        {
            string[] opts = new OptionSet().Parse(args).ToArray();
            long     id   = appData.GetId(opts.GetArg(1));

            var vk = new VkApi();

            vk.Authorize(appData.AccessToken);

            User user;

            ProfileFields fields =
                ProfileFields.Nickname
                | ProfileFields.FirstName
                | ProfileFields.LastName
                | ProfileFields.Sex
                | ProfileFields.Status
                | ProfileFields.BirthDate
                | ProfileFields.Relation
                | ProfileFields.RelationPartner;

            try {
                user = vk.Users.Get(id, fields | ProfileFields.Online);
            } catch (Exception e) {
                if (e.Message == "Can not convert String to Int64.")
                {
                    Thread.Sleep(400);
                    user = vk.Users.Get(id, fields);
                }
                else
                {
                    throw;
                }
            }

            CliUtils.PresentField("User name", $"{user.FirstName} {user.LastName}");
            CliUtils.PresentField("Online",
                                  user.Online.HasValue ? (user.Online.Value ? "online" : "offline") : "unknown",
                                  user.Online.HasValue ? (user.Online.Value ? ConsoleColor.DarkGreen : ConsoleColor.DarkBlue) : ConsoleColor.Gray);
            CliUtils.PresentField("VK ID", user.ScreenName);
            CliUtils.PresentField("Status", user.Status);
            CliUtils.PresentField("Sex", user.Sex);
            CliUtils.PresentField("Birth date", MiscUtils.FormatDate(user.BirthDate));
            CliUtils.PresentField("Home town", user.HomeTown);
            CliUtils.PresentField("Relation", user.Relation);
            CliUtils.PresentField("Relation partner",
                                  user.RelationPartner != null ? $"{user.RelationPartner.FirstName} {user.RelationPartner.LastName}" :  null);
        }
Exemple #3
0
        public static void Send(string[] args, AppData appData)
        {
            bool edit = false;
            bool room = false;

            string[] opts = new OptionSet()
            {
                { "e|edit", _ => edit = true },
                { "R|room", _ => room = true },
            }.Parse(args).ToArray();

            long id = appData.GetId(opts.GetArg(1));

            bool?roomAbbr = appData.IsRoom(opts.GetArg(1));

            if (roomAbbr.HasValue && roomAbbr.Value != room)
            {
                Console.WriteLine($"Warning: setting -R|--room flag to {roomAbbr.Value} according to abbreviations data.");
                room = roomAbbr.Value;
            }

            string text = String.Join(" ", from i in Enumerable.Range(2, opts.Length - 2) select opts[i]);

            if (edit)
            {
                CliUtils.Validate(String.IsNullOrWhiteSpace(text), AppError.ErrorCode.ArgumentParseError,
                                  "both -e|--edit mode and message body arguments are present");

                text = CliUtils.ReadText(ConsoleColor.DarkGreen);

                CliUtils.Validate(!String.IsNullOrWhiteSpace(text), AppError.ErrorCode.ArgumentParseError,
                                  $"empty message");
            }
            else
            {
                CliUtils.Validate(!String.IsNullOrWhiteSpace(text), AppError.ErrorCode.ArgumentParseError,
                                  "no message body is passed and -e|--edit mode is not enabled");
            }

            var vk = new VkApi();

            vk.Authorize(appData.AccessToken);

            MiscUtils.Send(vk, id, text, room);
        }
Exemple #4
0
        public static void Room(string[] args, AppData appData)
        {
            string[] opts = new OptionSet()
            {
            }.Parse(args).ToArray();

            long id = appData.GetId(opts.GetArg(1));

            var vk = new VkApi();

            vk.Authorize(appData.AccessToken);

            var res = vk.Messages.GetChat(id);

            CliUtils.PresentField("Title", res.Title);
            CliUtils.PresentField("Admin", appData.GetAbbr(res.AdminId ?? 0));
            CliUtils.PresentField("Users", String.Join(" ", from i in res.Users select appData.GetAbbr(i)));
        }
Exemple #5
0
        public static void Recv(string[] args, AppData appData)
        {
            bool quiet   = false;
            bool all     = false;
            uint n       = 200;
            bool reverse = false;
            bool room    = false;

            string[] opts = new OptionSet()
            {
                { "q|quiet", _ => quiet = true },
                { "a=|all=", _ => { all = true; n = Convert.ToUInt32(_); } },
                { "r|reverse", _ => reverse = true },
                { "R|room", _ => room = true },
            }.Parse(args).ToArray();

            long id = appData.GetId(opts.GetArg(1));

            bool?roomAbbr = appData.IsRoom(opts.GetArg(1));

            if (roomAbbr.HasValue && roomAbbr.Value != room)
            {
                Console.WriteLine($"Warning: setting -R|--room flag to {roomAbbr.Value} according to abbreviations data.");
                room = roomAbbr.Value;
            }

            var vk = new VkApi();

            vk.Authorize(appData.AccessToken);

            var msgs = MiscUtils.RecvMessages(vk, id, all ? (int?)n : null, reverse, quiet);

            if (msgs.Count > 0)
            {
                CliUtils.PresentField("Messages", msgs.Count, ConsoleColor.Magenta);
                CliUtils.PresentField("Quiet", quiet);

                foreach (var m in msgs)
                {
                    Console.WriteLine();
                    CliUtils.PresentMessage(m, appData);
                }
            }
        }
Exemple #6
0
        public static void Abbrs(string[] args, AppData appData)
        {
            bool   erase = false;
            string save  = null;
            string load  = null;

            new OptionSet()
            {
                { "e|erase", _ => erase = true },
                { "s=|save=", _ => save = _ },
                { "l=|load=", _ => load = _ },
            }.Parse(args);

            if (erase && save == null && load == null)
            {
                string[] abbrs = appData.GetAbbrs().ToArray();
                foreach (string abbr in abbrs)
                {
                    appData.DeleteAbbr(abbr);
                }

                appData.SaveChanges();
            }
            else if (!erase && save != null && load == null)
            {
                var table = new Table();

                foreach (string abbr in appData.GetAbbrs())
                {
                    table.Add(appData.GetId(abbr), abbr, (appData.IsRoom(abbr) ?? false) ? "room" : "");
                }

                using (var w = new StreamWriter(save)) {
                    table.Display(w);
                }
            }
            else if (!erase && save == null && load != null)
            {
                using (var r = new StreamReader(load)) {
                    while (!r.EndOfStream)
                    {
                        string   line   = r.ReadLine();
                        string[] tokens = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (tokens.Length >= 2)
                        {
                            long   id   = Convert.ToInt64(tokens[0]);
                            string abbr = tokens[1];
                            bool   room = tokens.Length >= 3 && tokens[2] == "room";

                            appData.AddAbbr(abbr, id, room);
                        }
                    }
                }

                appData.SaveChanges();
            }
            else if (!erase && save == null && load == null)
            {
                var table = new Table();

                foreach (string abbr in appData.GetAbbrs())
                {
                    table.Add(appData.GetId(abbr), abbr, (appData.IsRoom(abbr) ?? false) ? "room" : "");
                }

                table.Display();
            }
            else
            {
                throw new AppError(AppError.ErrorCode.ArgumentParseError, "multiple modes (erase/save/load) are given");
            }
        }