-
Notifications
You must be signed in to change notification settings - Fork 3
/
AuthenticatorBot.cs
155 lines (129 loc) · 5.15 KB
/
AuthenticatorBot.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
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ConsoleUtilities;
using Microsoft.Extensions.Logging;
using SDroid;
using SDroid.Interfaces;
using SDroid.SteamMobile;
namespace SDroidTest
{
internal class AuthenticatorBot : SteamBot, IAuthenticatorBot
{
// ReSharper disable once SuggestBaseTypeForParameter
public AuthenticatorBot(AuthenticatorBotSettings settings, ILogger logger) : base(settings, logger)
{
}
public new AuthenticatorBotSettings BotSettings
{
get => base.BotSettings as AuthenticatorBotSettings;
}
/// <inheritdoc />
public IAuthenticatorSettings BotAuthenticatorSettings
{
get => BotSettings;
}
/// <inheritdoc />
public Task OnAuthenticatorConfirmationAvailable(Confirmation confirmation)
{
ConsoleWriter.Default.PrintMessage("New confirmation available.");
ConsoleWriter.Default.WriteObject(confirmation);
return Task.CompletedTask;
}
/// <inheritdoc />
public Task OnAuthenticatorMissing()
{
ConsoleWriter.Default.PrintWarning(
"This account has authenticator linked to it. Provide us with the authentication file associated with this account. (*.*, *.maFile, *.maFile2)");
while (true)
{
var maFileAddress = ConsoleWriter.Default.PrintQuestion("Authentication file address");
if (File.Exists(maFileAddress))
{
try
{
var authenticator = Authenticator.DeSerializeFromFile(maFileAddress);
if (authenticator != null && authenticator.HasEnoughInfo())
{
BotSettings.Authenticator = authenticator;
BotSettings.Username = authenticator.AuthenticatorData.AccountName;
BotSettings.SaveSettings();
break;
}
}
catch (Exception e)
{
ConsoleWriter.Default.WriteException(e);
}
}
else
{
ConsoleWriter.Default.PrintError("File not found.");
}
}
return Task.CompletedTask;
}
public override async Task StartBot()
{
await base.StartBot().ConfigureAwait(false);
if (BotStatus != SteamBotStatus.Running)
{
await BotLogin().ConfigureAwait(false);
}
}
public async Task ConfirmByTradeOfferId(long tradeOfferId)
{
try
{
var authenticator = (this as IAuthenticatorBot).BotAuthenticatorSettings?.Authenticator;
if (authenticator != null)
{
var confirmations = await authenticator.FetchConfirmations().ConfigureAwait(false);
var confirmation = confirmations?.FirstOrDefault(c =>
c.Type == ConfirmationType.Trade && c.Creator == (ulong) tradeOfferId);
if (confirmation != null)
{
await authenticator.AcceptConfirmation(confirmation).ConfigureAwait(false);
BotLogger.LogInformation("Requested to confirm trade offer #{0}", tradeOfferId);
return;
}
}
BotLogger.LogInformation("Confirmation for trade offer #{0} not found.", tradeOfferId);
}
catch (Exception e)
{
BotLogger.LogError(e, e.Message);
}
}
public async Task DenyByTradeOfferId(long tradeOfferId)
{
try
{
var authenticator = (this as IAuthenticatorBot).BotAuthenticatorSettings?.Authenticator;
if (authenticator != null)
{
var confirmations = await authenticator.FetchConfirmations().ConfigureAwait(false);
var confirmation = confirmations?.FirstOrDefault(c =>
c.Type == ConfirmationType.Trade && c.Creator == (ulong) tradeOfferId);
if (confirmation != null)
{
await authenticator.DenyConfirmation(confirmation).ConfigureAwait(false);
BotLogger.LogInformation(nameof(DenyByTradeOfferId), "Requested to deny trade offer #{0}", tradeOfferId);
return;
}
}
BotLogger.LogInformation("Confirmation for trade offer #{0} not found.", tradeOfferId);
}
catch (Exception e)
{
BotLogger.LogError(e, e.Message);
}
}
/// <inheritdoc />
protected override Task<string> OnPasswordRequired()
{
return Task.FromResult(ConsoleWriter.Default.PrintQuestion("Password"));
}
}
}