Esempio n. 1
0
        public static void Login(string[] args, AppData appData)
        {
            CliUtils.Validate(!appData.Authorized, AppError.ErrorCode.ApplicationConfigError,
                              "already authorized");

            Console.WriteLine("Authorizing in the VK api..");

            Console.WriteLine();
            Console.WriteLine("Step 1. OAuth authorization.");
            Console.WriteLine("Visit the following page in your web browser.");
            Console.WriteLine("Authorize if necessary and copy the code (should appear after #code= in your address bar).");
            Console.WriteLine($"https://oauth.vk.com/authorize?client_id={AppId}&redirect_uri=https:%2F%2Foauth.vk.com%2Fblank.html&scope={AccessMagicNumber}");

            string code = CliUtils.ReadString("Code: ");

            Console.WriteLine();
            Console.WriteLine("Step 2. Receiving access token.");
            Console.WriteLine("Sending authorization request..");

            AccessTokenResponse resp = JsonModelUtils.ReceiveJson <AccessTokenResponse>(
                $"https://oauth.vk.com/access_token?client_id={AppId}&client_secret={AppSecret}&code={code}&redirect_uri=https:%2F%2Foauth.vk.com%2Fblank.html");

            CliUtils.Validate(String.IsNullOrEmpty(resp.error), AppError.ErrorCode.AuthorizationError,
                              $"{resp.error}, {resp.error_description}");

            string accessToken = resp.access_token;

            Console.WriteLine();
            Console.WriteLine("Step 3. VK authorization.");

            var vk = new VkApi();

            vk.Authorize(accessToken);

            var    profileInfo = vk.Account.GetProfileInfo();
            string userName    = $"{profileInfo.FirstName} {profileInfo.LastName}";

            appData = new AppData()
            {
                Authorized  = true,
                AccessToken = accessToken,
                FullName    = userName,
                UserId      = Convert.ToInt64(resp.user_id),
            };

            appData.AddAbbr("self", appData.UserId);

            AppConfig.SaveAppData(appData);

            Console.WriteLine();
            Console.WriteLine($"Welcome, {userName}!");
        }
Esempio n. 2
0
        public static void Abbr(string[] args, AppData appData)
        {
            bool delete = false;
            bool room   = false;

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

            string abbr = opts.GetArg(1);
            string ids  = opts.GetArg(2);

            if (!delete)
            {
                CliUtils.Validate(!String.IsNullOrWhiteSpace(abbr), AppError.ErrorCode.ArgumentParseError,
                                  "passed abbreviation is empty");

                CliUtils.Validate(!String.IsNullOrWhiteSpace(ids), AppError.ErrorCode.ArgumentParseError,
                                  "passed id is empty");

                long id;

                try {
                    id = Convert.ToInt64(ids);
                } catch {
                    throw new AppError(AppError.ErrorCode.ArgumentParseError, $"unable to convert id '{ids}' to integer");
                }

                appData.AddAbbr(abbr, id, room);
                appData.SaveChanges();
            }
            else
            {
                CliUtils.Validate(ids == null, AppError.ErrorCode.ArgumentParseError,
                                  "passed id, but requested abbreviation deletion");

                appData.DeleteAbbr(abbr);
                appData.SaveChanges();
            }
        }
Esempio n. 3
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");
            }
        }