-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
206 lines (150 loc) · 6.85 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using LeanCloud.Storage;
using DocoptNet;
namespace ChatStack
{
class Program
{
static async Task Main(string[] args)
{
// Parse the arguments using Docopt.
var arguments = new Docopt().Apply(Usage, args, version: "ChatStack CLI v1.0" + Environment.NewLine + "https://github.com/HackingHackers/chatstack-cli", exit: true);
var client = await CheckArgumentsAsync(arguments);
// CheckArguments() returns null if the command is executed and doesn't need a chat interface.
if (client is null) return;
#region Chat interface setup
Console.Title = "ChatStack CLI";
// Disable the beed sound if option --mute is set.
client.Beep = !arguments["--mute"].IsTrue;
await client.StartChatAsync();
#endregion
// TODO: Encrypt and save the username and password.
}
static async Task<Client> CheckArgumentsAsync(IDictionary<string, ValueObject> args)
{
// TODO: Save login state.
// string savedClientPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ChatStack", "SavedClient.bin");
if (args["logout"].IsTrue)
{
// Delete the saved client.
// File.Delete(savedClientPath);
Console.WriteLine("Logged out.");
return null;
}
#region Commands that require LeanCloud
// Log LeanCloud debug info.
// LeanCloud.LCLogger.LogDelegate += Client.DebugLogger;
// Initialize LeanCloud.
// NOTE: Add your LeanCloud credentials to 'LeanCloudCredentials.EDIT_ME'.
Client.InitApp(LeanCloudCredentials.AppID, LeanCloudCredentials.AppKey, LeanCloudCredentials.Url);
if (args["signup"].IsTrue)
{
// Check if the password and password confirmation match.
if (args["<password>"] == args["<password-confirm>"])
{
// Create a new user.
var user = new LCUser();
user.Username = args["<username>"].ToString();
user.Password = args["<password>"].ToString();
// Add an email property to the user if provided one.
if (!args["--email"].IsNullOrEmpty)
user.Email = args["--email"].ToString();
// Sign up the user.
await user.SignUp();
Console.WriteLine("User created.");
if (!args["--email"].IsNullOrEmpty)
Console.WriteLine("Verification email sent. Please check your inbox.");
}
else
{
Console.WriteLine("Passwords do not match.");
}
return null;
}
if (args["bind"].IsTrue)
{
// Get the user.
var user = await Client.LoginUserAsync(args["<username>"].ToString(), args["<password>"].ToString());
// Set the user's email.
user.Email = args["--email"].ToString();
await user.Save();
Console.WriteLine("Verification email sent. Please check your inbox.");
return null;
}
if (args["chpwd"].IsTrue)
{
// Check if the password and password confirmation match.
if (args["<new-password>"] == args["<new-password-confirm>"])
{
// Get the user.
var user = await Client.LoginUserAsync(args["<username>"].ToString(), args["<password>"].ToString());
// Set the user's password.
user.Password = args["<new-password>"].ToString();
await user.Save();
Console.WriteLine("Password changed.");
}
else
{
Console.WriteLine("New passwords do not match.");
}
return null;
}
if (args["resetpwd"].IsTrue)
{
// Request a password reset email.
await LCUser.RequestPasswordReset(args["--email"].ToString());
Console.WriteLine("Password reset email sent. Please check your inbox.");
return null;
}
#endregion
#region Commands that need a chat interface
// Create a Client object.
var client = new Client();
// Check if the login command was called.
if (args["login"].IsTrue)
{
// Log the user in.
await client.LoginClientAsync(args["<username>"].ToString(), args["<password>"].ToString());
// Query and get the chatroom.
await client.GetChatroomAsync();
}
else
{
// No command was called, so read and deserialize the saved client.
// TODO: Read from the saved login state.
// if (File.Exists(savedClientPath))
// {
// }
// else
// {
// Console.WriteLine("You are not logged in. Please use 'chatstack login'.");
// return null;
// }
Console.WriteLine("Saved login state is not implemented yet. Please use 'chatstack login'.");
Console.WriteLine();
Console.WriteLine(Usage);
return null;
}
return client;
#endregion
}
static readonly string Usage = @"ChatStack CLI by @HackingHackers
Usage:
chatstack [--mute]
chatstack login (<username> | <email-address>) <password> [--mute]
chatstack logout
chatstack signup <username> <password> <password-confirm> [--email <email-address>]
chatstack bind (<username> | <email-address>) <password> --email <email-address>
chatstack chpwd (<username> | <email-address>) <password> <new-password> <new-password-confirm>
chatstack resetpwd --email <email-address>
chatstack -h | --help
chatstack --version
Options:
-h --help Show this help message.
--version Show version.
--mute Stop playing the beep sound when you receive a message.
--email <email-address> Send a verification or password reset email to the given email address. Check your inbox.";
}
}