Add a feature of accepting steam gifts, closes #18

This commit is contained in:
JustArchi
2016-03-27 23:07:00 +02:00
parent 2554794daa
commit 8048d4a7aa
5 changed files with 82 additions and 3 deletions

View File

@@ -32,6 +32,7 @@ using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Threading;
namespace ArchiSteamFarm {
internal sealed class ArchiWebHandler {
@@ -44,6 +45,8 @@ namespace ArchiSteamFarm {
private readonly Bot Bot;
private readonly Dictionary<string, string> Cookie = new Dictionary<string, string>(4);
internal bool IsInitialized { get; private set; }
private ulong SteamID;
internal static void Init() {
@@ -59,8 +62,13 @@ namespace ArchiSteamFarm {
Bot = bot;
}
internal void OnDisconnected() {
IsInitialized = false;
Cookie.Clear();
}
internal async Task<bool> Init(SteamClient steamClient, string webAPIUserNonce, string parentalPin) {
if (steamClient == null || steamClient.SteamID == null || string.IsNullOrEmpty(webAPIUserNonce)) {
if (steamClient == null || steamClient.SteamID == null || string.IsNullOrEmpty(webAPIUserNonce) || IsInitialized) {
return false;
}
@@ -122,6 +130,8 @@ namespace ArchiSteamFarm {
Cookie["webTradeEligibility"] = "{\"allowed\":0,\"reason\":0,\"allowed_at_time\":0,\"steamguard_required_days\":0,\"sales_this_year\":0,\"max_sales_per_year\":0,\"forms_requested\":0}";
await UnlockParentalAccount(parentalPin).ConfigureAwait(false);
IsInitialized = true;
return true;
}
@@ -494,6 +504,34 @@ namespace ArchiSteamFarm {
return true;
}
internal async Task<bool> AcceptGift(ulong gid) {
if (gid == 0) {
return false;
}
string sessionID;
if (!Cookie.TryGetValue("sessionid", out sessionID)) {
return false;
}
string request = SteamCommunityURL + "/gifts/" + gid + "/acceptunpack";
Dictionary<string, string> data = new Dictionary<string, string>(1) {
{ "sessionid", sessionID }
};
HttpResponseMessage response = null;
for (byte i = 0; i < WebBrowser.MaxRetries && response == null; i++) {
response = await WebBrowser.UrlPost(request, data, Cookie).ConfigureAwait(false);
}
if (response == null) {
Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName);
return false;
}
return true;
}
private async Task UnlockParentalAccount(string parentalPin) {
if (string.IsNullOrEmpty(parentalPin) || parentalPin.Equals("0")) {
return;

View File

@@ -172,6 +172,7 @@ namespace ArchiSteamFarm {
SteamApps = SteamClient.GetHandler<SteamApps>();
CallbackManager.Subscribe<SteamApps.FreeLicenseCallback>(OnFreeLicense);
CallbackManager.Subscribe<SteamApps.GuestPassListCallback>(OnGuestPassList);
SteamFriends = SteamClient.GetHandler<SteamFriends>();
CallbackManager.Subscribe<SteamFriends.ChatInviteCallback>(OnChatInvite);
@@ -1299,6 +1300,7 @@ namespace ArchiSteamFarm {
}
Logging.LogGenericInfo("Disconnected from Steam!", BotName);
ArchiWebHandler.OnDisconnected();
CardsFarmer.StopFarming().Forget();
// If we initiated disconnect, do not attempt to reconnect
@@ -1347,6 +1349,41 @@ namespace ArchiSteamFarm {
}
}
private async void OnGuestPassList(SteamApps.GuestPassListCallback callback) {
if (callback == null || callback.Result != EResult.OK || callback.CountGuestPassesToRedeem <= 0 || !BotConfig.AcceptGifts) {
return;
}
for (byte i = 0; i < WebBrowser.MaxRetries && !ArchiWebHandler.IsInitialized; i++) {
await Utilities.SleepAsync(1000).ConfigureAwait(false);
}
if (!ArchiWebHandler.IsInitialized) {
Logging.LogGenericWarning("Reached timeout while waiting for ArchiWebHandler to initialize!");
return;
}
bool acceptedSomething = false;
foreach (KeyValue guestPass in callback.GuestPasses) {
ulong gid = guestPass["gid"].AsUnsignedLong();
if (gid == 0) {
continue;
}
Logging.LogGenericInfo("Accepting gift: " + gid + "...", BotName);
if (await ArchiWebHandler.AcceptGift(gid).ConfigureAwait(false)) {
acceptedSomething = true;
Logging.LogGenericInfo("Success!", BotName);
} else {
Logging.LogGenericInfo("Failed!", BotName);
}
}
if (acceptedSomething) {
CardsFarmer.RestartFarming().Forget();
}
}
private void OnChatInvite(SteamFriends.ChatInviteCallback callback) {
if (callback == null || !IsMaster(callback.PatronID)) {
return;

View File

@@ -65,6 +65,9 @@ namespace ArchiSteamFarm {
[JsonProperty(Required = Required.DisallowNull)]
internal bool HandleOfflineMessages { get; private set; } = false;
[JsonProperty(Required = Required.DisallowNull)]
internal bool AcceptGifts { get; private set; } = false;
[JsonProperty(Required = Required.DisallowNull)]
internal bool ForwardKeysToOtherBots { get; private set; } = false;

View File

@@ -219,10 +219,10 @@ namespace ArchiSteamFarm {
}
if (!responseMessage.IsSuccessStatusCode) {
if (Program.GlobalConfig.Debug) {
if (Debugging.IsDebugBuild || Program.GlobalConfig.Debug) {
Logging.LogGenericError("Request: " + request + "failed!");
Logging.LogGenericError("Status code: " + responseMessage.StatusCode);
Logging.LogGenericError("Content: " + await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false));
Logging.LogGenericError("Content: " + Environment.NewLine + await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false));
}
return null;
}

View File

@@ -11,6 +11,7 @@
"DismissInventoryNotifications": true,
"FarmOffline": false,
"HandleOfflineMessages": false,
"RedeemGifts": false,
"ForwardKeysToOtherBots": false,
"DistributeKeys": false,
"UseAsfAsMobileAuthenticator": false,