-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountManager.cs
288 lines (264 loc) · 8.57 KB
/
AccountManager.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Diagnostics;
namespace InputMaster
{
public class AccountManager : Actor
{
private Account _currentAccount;
private MyState _state;
public AccountManager() { } // For Mocking.
private AccountManager(bool dummy)
{
Env.Parser.UpdateParseAction(nameof(AccountManager), parserOutput =>
{
var mode = parserOutput.AddMode(new Mode(Env.Config.OpenAccountModeName, true));
var modifyMode = parserOutput.AddMode(new Mode(Env.Config.ModifyAccountModeName, true));
foreach (var account in GetAccounts())
{
if (account.Chord.Length == 0)
continue;
mode.AddHotkey(new ModeHotkey(account.Chord, combo =>
{
Env.ModeHook.EnterMode(Env.Config.AccountModeName);
_currentAccount = account;
}, account.Chord + " " + account.Title + " " + account.Description));
modifyMode.AddHotkey(new ModeHotkey(account.Chord, async combo =>
{
await ModifyAccountAsync(account.Id);
}, account.Chord + " " + account.Title + " " + account.Description), true);
}
});
}
private async Task<AccountManager> Initialize()
{
var stateHandler = Env.StateHandlerFactory.Create(new MyState(), nameof(AccountManager),
StateHandlerFlags.UseCipher | StateHandlerFlags.Exportable | StateHandlerFlags.SavePeriodically);
_state = await stateHandler.LoadAsync();
return this;
}
public static Task<AccountManager> GetAccountManagerAsync()
{
return new AccountManager(false).Initialize();
}
public IEnumerable<Account> GetAllAccounts()
{
return _state.Accounts.Values;
}
private static async Task<Account> ShowAndEditAccountAsync(Account account)
{
var text = Helper.JsonSerialize(account, Formatting.Indented);
while (true)
{
text = await Helper.TryGetStringAsync("Account", text, selectAll: false, containsJson: true);
if (text == null)
return account;
try
{
var newAccount = JsonConvert.DeserializeObject<Account>(text) ?? account;
if (newAccount.Id != account.Id)
{
newAccount.Id = account.Id;
Env.Notifier.Warning("Ids can only be changed by modifying the complete json. Id change is ignored.");
}
return newAccount;
}
catch (Exception ex) when (!Helper.IsFatalException(ex))
{
Env.Notifier.Info(ex.ToString());
}
}
}
[Command]
private async Task CreateRandomAccountAsync(string passwordPrefix = "")
{
var account = new Account()
{
Id = GetNewId(),
Email = Helper.GetRandomEmail(),
Password = Helper.GetRandomPassword()
};
account = await ShowAndEditAccountAsync(account);
_state.Accounts[account.Id] = account;
UpdateHook();
_currentAccount = account;
}
[Command]
public Task ModifyCurrentAccountAsync()
{
return ModifyAccountAsync(_currentAccount?.Id);
}
[Command]
public async Task ModifyAccountAsync(int? id = null)
{
if (!id.HasValue)
{
var s = await Helper.TryGetLineAsync("Id");
if (s == null)
return;
if (!int.TryParse(s, out var x))
throw new ArgumentException("Cannot parse value as int.");
id = x;
}
if (!TryGetAccount(id.Value, out var account))
{
Env.Notifier.Info($"No account with id '{id}' found.");
return;
}
var newAccount = await ShowAndEditAccountAsync(account);
_state.Accounts[newAccount.Id] = newAccount;
UpdateHook();
_currentAccount = newAccount;
}
[Command]
public void SetCurrentAccount(int id)
{
TryGetAccount(id, out _currentAccount);
}
[Command]
public Task ModifyAllAccountsAsync()
{
return ModifyAllAccounts(false);
}
public async Task ModifyAllAccounts(bool showPassword)
{
string text;
{
var strippedAccounts = GetSortedAccounts().Select(z => new Account(z)).ToList();
if (!showPassword)
strippedAccounts.ForEach(z => { z.Password = ""; z.OldPassword = ""; });
text = Helper.JsonSerialize(strippedAccounts, Formatting.Indented);
}
List<Account> newAccounts;
var first = true;
while (true)
{
text = await Helper.TryGetStringAsync("Accounts", text, selectAll: false, startWithFindDialog: first, containsJson: true);
if (text == null)
return;
first = false;
try
{
newAccounts = JsonConvert.DeserializeObject<List<Account>>(text) ?? throw new ArgumentException();
break;
}
catch (Exception ex) when (!Helper.IsFatalException(ex))
{
Env.Notifier.Info(ex.ToString());
}
}
if (!showPassword)
{
foreach (var newAccount in newAccounts)
{
if (!TryGetAccount(newAccount.Id, out var oldAccount))
continue;
newAccount.Password = string.IsNullOrWhiteSpace(newAccount.Password) ? oldAccount.Password : newAccount.Password;
newAccount.OldPassword = string.IsNullOrWhiteSpace(newAccount.OldPassword) ? oldAccount.OldPassword : newAccount.OldPassword;
}
}
_state.Accounts.Clear();
foreach (var newAccount in newAccounts)
_state.Accounts[newAccount.Id] = newAccount;
UpdateHook();
}
/// <summary>
/// c: current account
/// a: start with {Ctrl}a
/// l: login name
/// e: email
/// x: extra
/// o: old password
/// p: password
/// z: end with enter
/// </summary>
[Command]
public void PasteAccount(int slot, [ValidFlags("calexopz")]string flags = "")
{
var account = flags.Contains('c') ? _currentAccount : GetAccounts().Where(z => z.Slot == slot &&
z.HasForegroundWindowMatch()).OrderBy(z => z.Precedence).FirstOrDefault();
if (account == null)
return;
_currentAccount = account;
var injector = Env.CreateInjector();
if (flags.Contains('a'))
injector.Add(Input.A, Modifiers.Ctrl);
{
var first = true;
var str = "lexop";
var values = new string[] { account.GetLoginName(), account.GetEmail(), account.GetExtra(), account.GetOldPassword(),
account.GetPassword() };
Debug.Assert(str.Length == values.Length);
for (int i = 0; i < str.Length; i++)
{
var c = str[i];
if (!flags.Contains(c))
continue;
var value = values[i];
if (!first)
injector.Add(Input.Tab);
first = false;
injector.Add(value ?? "", Env.Config.LiteralInputReader);
}
}
if (flags.Contains('z'))
injector.Add(Input.Enter);
injector.Run();
}
[Command]
public async Task RunAsync([AllowSpaces] string filePath, [AllowSpaces]string arguments = "")
{
if (!TryGetAccount(Env.Config.LocalAccountId, out var account))
await Helper.StartProcessAsync(filePath, arguments);
var s = new SecureString();
foreach (var c in account.GetPassword())
s.AppendChar(c);
await Helper.StartProcessAsync(filePath, account.GetLoginName(), s, Environment.UserDomainName, arguments);
}
[Command]
public Task EditAsync([AllowSpaces]string filePath)
{
return RunAsync(Env.Config.DefaultTextEditor, $"\"{filePath}\"");
}
[Command]
public Task SurfAsync([AllowSpaces]string url)
{
return RunAsync(Env.Config.DefaultWebBrowser, $"\"{url}\"");
}
public IEnumerable<Account> GetAccounts()
{
return _state.Accounts.Values.AsEnumerable();
}
public bool TryGetAccount(int id, out Account account)
{
return _state.Accounts.TryGetValue(id, out account);
}
private void UpdateHook()
{
Env.Parser.Run();
}
private int GetNewId()
{
return GetAccounts().Any() ? GetAccounts().Max(z => z.Id) + 1 : Env.Config.MinAccountUploadId;
}
private IEnumerable<Account> GetSortedAccounts()
{
var list = GetAccounts().ToList();
list.Sort((x, y) => x.Id.CompareTo(y.Id));
return list;
}
public class MyState : IState
{
public Dictionary<int, Account> Accounts { get; set; }
public (bool, string message) Fix()
{
Accounts = Accounts ?? new Dictionary<int, Account>();
return (true, "");
}
}
}
}