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); }
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); }
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); } } }
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"); } }