@@ -23,8 +23,7 @@ internal class WtExchangeLogic : LogicBase
23
23
{
24
24
private const string Tag = nameof ( WtExchangeLogic ) ;
25
25
26
- private readonly TaskCompletionSource < bool > _qrCodeTask ;
27
- private readonly TaskCompletionSource < bool > _unusualTask ;
26
+ private readonly TaskCompletionSource < bool > _transEmpTask ;
28
27
private TaskCompletionSource < ( string , string ) > ? _captchaTask ;
29
28
30
29
private const string Interface = "https://ntlogin.qq.com/qr/getFace" ;
@@ -33,8 +32,7 @@ internal class WtExchangeLogic : LogicBase
33
32
34
33
internal WtExchangeLogic ( ContextCollection collection ) : base ( collection )
35
34
{
36
- _qrCodeTask = new TaskCompletionSource < bool > ( ) ;
37
- _unusualTask = new TaskCompletionSource < bool > ( ) ;
35
+ _transEmpTask = new TaskCompletionSource < bool > ( ) ;
38
36
}
39
37
40
38
public override async Task Incoming ( ProtocolEvent e )
@@ -80,8 +78,19 @@ public override async Task Incoming(ProtocolEvent e)
80
78
81
79
public Task LoginByQrCode ( )
82
80
{
83
- Collection . Scheduler . Interval ( QueryEvent , 2 * 1000 , async ( ) => await QueryQrCodeState ( ) ) ;
84
- return _qrCodeTask . Task ;
81
+ Collection . Scheduler . Interval ( QueryEvent , 2 * 1000 , async ( ) => await QueryTransEmpState ( async @event =>
82
+ {
83
+ if ( @event . TgtgtKey != null )
84
+ {
85
+ Collection . Keystore . Stub . TgtgtKey = @event . TgtgtKey ;
86
+ Collection . Keystore . Session . TempPassword = @event . TempPassword ;
87
+ Collection . Keystore . Session . NoPicSig = @event . NoPicSig ;
88
+ }
89
+
90
+ return await DoWtLogin ( ) ;
91
+ } ) ) ;
92
+
93
+ return _transEmpTask . Task ;
85
94
}
86
95
87
96
public async Task < bool > LoginByPassword ( )
@@ -120,22 +129,31 @@ public async Task<bool> LoginByPassword()
120
129
}
121
130
case LoginCommon . Error . UnusualVerify :
122
131
{
123
- Collection . Log . LogInfo ( Tag , "Login Success, but need to verify " ) ;
132
+ Collection . Log . LogInfo ( Tag , "Verification needed " ) ;
124
133
125
134
if ( ! await FetchUnusual ( ) )
126
135
{
127
136
Collection . Log . LogInfo ( Tag , "Fetch unusual state failed" ) ;
128
137
return false ;
129
138
}
130
139
131
- Collection . Scheduler . Interval ( QueryEvent , 2 * 1000 , async ( ) => await QueryUnusualState ( ) ) ;
132
- bool result = await _unusualTask . Task ;
140
+ Collection . Scheduler . Interval ( QueryEvent , 2 * 1000 , async ( ) => await QueryTransEmpState ( async e =>
141
+ {
142
+ if ( e . TempPassword != null )
143
+ {
144
+ Collection . Keystore . Session . TempPassword = e . TempPassword ;
145
+ return await DoUnusualEasyLogin ( ) ;
146
+ }
147
+
148
+ return false ;
149
+ } ) ) ;
150
+ bool result = await _transEmpTask . Task ;
133
151
if ( result ) await BotOnline ( ) ;
134
152
return result ;
135
153
}
136
154
default :
137
155
{
138
- Collection . Log . LogWarning ( Tag , "Fast Login Failed, trying to Login by Password..." ) ;
156
+ Collection . Log . LogWarning ( Tag , $ "Fast Login Failed with code { easyLoginResult [ 0 ] . ResultCode } , trying to Login by Password...") ;
139
157
140
158
Collection . Keystore . Session . TempPassword = null ; // clear temp password
141
159
return await LoginByPassword ( ) ; // try password login
@@ -163,10 +181,7 @@ public async Task<bool> LoginByPassword()
163
181
}
164
182
case LoginCommon . Error . UnusualVerify :
165
183
{
166
- Collection . Log . LogInfo ( Tag , "Login Success, but need to verify" ) ;
167
-
168
- await FetchUnusual ( ) ;
169
- Collection . Scheduler . Interval ( QueryEvent , 2 * 1000 , async ( ) => await QueryUnusualState ( ) ) ;
184
+ Collection . Log . LogInfo ( Tag , "Unusual Verify is not currently supported for PasswordLogin" ) ;
170
185
return true ;
171
186
}
172
187
case LoginCommon . Error . CaptchaVerify :
@@ -246,26 +261,23 @@ private async Task<bool> DoWtLogin()
246
261
return false ;
247
262
}
248
263
249
- private async Task QueryQrCodeState ( )
264
+ private async Task QueryTransEmpState ( Func < TransEmpEvent , Task < bool > > callback )
250
265
{
251
- if ( Collection . Keystore . Session . QrString = = null )
266
+ if ( Collection . Keystore . Session . QrString ! = null )
252
267
{
253
- Collection . Log . LogFatal ( Tag , "QrString is null, Please Fetch QrCode First" ) ;
254
- _qrCodeTask . SetResult ( false ) ;
255
- return ;
268
+ var request = new NTLoginHttpRequest
269
+ {
270
+ Appid = Collection . AppInfo . AppId ,
271
+ Qrsig = Collection . Keystore . Session . QrString ,
272
+ FaceUpdateTime = 0
273
+ } ;
274
+
275
+ var payload = JsonSerializer . SerializeToUtf8Bytes ( request ) ;
276
+ var response = await Http . PostAsync ( Interface , payload , "application/json" ) ;
277
+ var info = JsonSerializer . Deserialize < NTLoginHttpResponse > ( response ) ;
278
+ if ( info != null ) Collection . Keystore . Uin = info . Uin ;
256
279
}
257
280
258
- var request = new NTLoginHttpRequest
259
- {
260
- Appid = Collection . AppInfo . AppId ,
261
- Qrsig = Collection . Keystore . Session . QrString ,
262
- FaceUpdateTime = 0
263
- } ;
264
- var payload = JsonSerializer . SerializeToUtf8Bytes ( request ) ;
265
- var response = await Http . PostAsync ( Interface , payload , "application/json" ) ;
266
- var info = JsonSerializer . Deserialize < NTLoginHttpResponse > ( response ) ;
267
- if ( info != null ) Collection . Keystore . Uin = info . Uin ;
268
-
269
281
var transEmp = TransEmpEvent . Create ( TransEmpEvent . State . QueryResult ) ;
270
282
var result = await Collection . Business . SendEvent ( transEmp ) ;
271
283
@@ -281,15 +293,7 @@ private async Task QueryQrCodeState()
281
293
{
282
294
Collection . Log . LogInfo ( Tag , "QrCode Confirmed, Logging in with A1 sig..." ) ;
283
295
Collection . Scheduler . Cancel ( QueryEvent ) ; // cancel query task
284
-
285
- if ( @event . TgtgtKey != null )
286
- {
287
- Collection . Keystore . Stub . TgtgtKey = @event . TgtgtKey ;
288
- Collection . Keystore . Session . TempPassword = @event . TempPassword ;
289
- Collection . Keystore . Session . NoPicSig = @event . NoPicSig ;
290
-
291
- _qrCodeTask . SetResult ( await DoWtLogin ( ) ) ;
292
- }
296
+ _transEmpTask . SetResult ( await callback . Invoke ( @event ) ) ;
293
297
break ;
294
298
}
295
299
case TransEmp12 . State . CodeExpired :
@@ -298,7 +302,7 @@ private async Task QueryQrCodeState()
298
302
Collection . Scheduler . Cancel ( QueryEvent ) ;
299
303
Collection . Scheduler . Dispose ( ) ;
300
304
301
- _qrCodeTask . SetResult ( false ) ;
305
+ _transEmpTask . SetResult ( false ) ;
302
306
return ;
303
307
}
304
308
case TransEmp12 . State . Canceled :
@@ -307,7 +311,7 @@ private async Task QueryQrCodeState()
307
311
Collection . Scheduler . Cancel ( QueryEvent ) ;
308
312
Collection . Scheduler . Dispose ( ) ;
309
313
310
- _qrCodeTask . SetResult ( false ) ;
314
+ _transEmpTask . SetResult ( false ) ;
311
315
return ;
312
316
}
313
317
case TransEmp12 . State . WaitingForConfirm :
@@ -347,59 +351,13 @@ private async Task<bool> FetchUnusual()
347
351
return false ;
348
352
}
349
353
350
- private async Task QueryUnusualState ( )
351
- {
352
- var transEmp = TransEmpEvent . Create ( TransEmpEvent . State . QueryResult ) ;
353
- var result = await Collection . Business . SendEvent ( transEmp ) ;
354
-
355
- if ( result . Count != 0 )
356
- {
357
- var @event = ( TransEmpEvent ) result [ 0 ] ;
358
- var state = ( TransEmp12 . State ) @event . ResultCode ;
359
- Collection . Log . LogInfo ( Tag , $ "Confirmation State Queried: { state } ") ;
360
-
361
- switch ( state )
362
- {
363
- case TransEmp12 . State . Confirmed :
364
- {
365
- Collection . Log . LogInfo ( Tag , "Verification Confirmed, Logging in Unusual Login Service..." ) ;
366
- Collection . Scheduler . Cancel ( QueryEvent ) ; // cancel query task
367
-
368
- if ( @event . TempPassword != null ) Collection . Keystore . Session . TempPassword = @event . TempPassword ;
369
- _unusualTask . SetResult ( await DoUnusualEasyLogin ( ) ) ;
370
- break ;
371
- }
372
- case TransEmp12 . State . CodeExpired :
373
- {
374
- Collection . Log . LogWarning ( Tag , "Verification Expired, Please Login Again" ) ;
375
- Collection . Scheduler . Cancel ( QueryEvent ) ;
376
- Collection . Scheduler . Dispose ( ) ;
377
-
378
- _unusualTask . SetResult ( false ) ;
379
- break ;
380
- }
381
- case TransEmp12 . State . Canceled :
382
- {
383
- Collection . Log . LogWarning ( Tag , "Verification Canceled, Please Login Again" ) ;
384
- Collection . Scheduler . Cancel ( QueryEvent ) ;
385
- Collection . Scheduler . Dispose ( ) ;
386
-
387
- _unusualTask . SetResult ( false ) ;
388
- break ;
389
- }
390
- case TransEmp12 . State . WaitingForConfirm :
391
- default :
392
- break ;
393
- }
394
- }
395
- }
396
-
397
354
private async Task < bool > DoUnusualEasyLogin ( )
398
355
{
399
356
Collection . Log . LogInfo ( Tag , "Trying to Login by EasyLogin..." ) ;
400
357
var unusualEvent = UnusualEasyLoginEvent . Create ( ) ;
401
358
var result = await Collection . Business . SendEvent ( unusualEvent ) ;
402
359
return result . Count != 0 && ( ( UnusualEasyLoginEvent ) result [ 0 ] ) . Success ;
360
+ }
403
361
404
362
}
405
363
0 commit comments