This repository has been archived by the owner on Apr 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBootstrap.cs
220 lines (175 loc) Β· 7.18 KB
/
Bootstrap.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
using System;
using System.IO;
using System.Text.Json;
using System.Threading;
using SteamKit2;
namespace SteamToTwitter
{
internal static class Bootstrap
{
private static readonly SteamClient Client = new SteamClient();
private static readonly SteamUser User = Client.GetHandler<SteamUser>();
private static readonly SteamFriends Friends = Client.GetHandler<SteamFriends>();
private static Timer ReconnectTimer;
private static bool IsRunning = true;
private static TinyTwitter.TinyTwitter Twitter;
private static Configuration Configuration;
private static string authCode, twoFactorAuth;
public static void Main()
{
Console.Title = "SteamToTwitter";
Log("Starting...");
Console.CancelKeyPress += delegate
{
Log("Exiting...");
try
{
User.LogOff();
Client.Disconnect();
}
catch
{
Log("Failed to disconnect from Steam");
}
IsRunning = false;
};
Configuration = JsonSerializer.Deserialize<Configuration>(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "settings.json")));
Twitter = new TinyTwitter.TinyTwitter(Configuration.Twitter);
var callbackManager = new CallbackManager(Client);
callbackManager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
callbackManager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);
callbackManager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
callbackManager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOff);
callbackManager.Subscribe<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth);
callbackManager.Subscribe<SteamUser.AccountInfoCallback>(OnAccountInfo);
callbackManager.Subscribe<SteamFriends.ClanStateCallback>(OnClanState);
Client.Connect();
var reconnectTime = TimeSpan.FromHours(6);
ReconnectTimer = new Timer(_ => Client.Disconnect(), null, reconnectTime, reconnectTime);
while (IsRunning)
{
callbackManager.RunWaitCallbacks(TimeSpan.FromSeconds(5));
}
}
private static void OnConnected(SteamClient.ConnectedCallback callback)
{
Log("Connected to Steam, logging in...");
byte[] sentryHash = null;
if (File.Exists("sentry.bin"))
{
var sentryFile = File.ReadAllBytes("sentry.bin");
sentryHash = CryptoHelper.SHAHash(sentryFile);
}
User.LogOn(new SteamUser.LogOnDetails
{
AuthCode = authCode,
TwoFactorCode = twoFactorAuth,
SentryFileHash = sentryHash,
Username = Configuration.SteamUsername,
Password = Configuration.SteamPassword
});
}
private static void OnDisconnected(SteamClient.DisconnectedCallback callback)
{
if (!IsRunning)
{
Log("Shutting down...");
return;
}
Log("Disconnected from Steam. Retrying...");
Thread.Sleep(TimeSpan.FromSeconds(15));
Client.Connect();
}
private static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
{
if (callback.Result == EResult.AccountLoginDeniedNeedTwoFactor)
{
Console.Write("Please enter your 2 factor auth code from your authenticator app: ");
twoFactorAuth = Console.ReadLine();
return;
}
if (callback.Result == EResult.AccountLogonDenied)
{
Console.Write("Please enter the auth code sent to the email at {0}: ", callback.EmailDomain);
authCode = Console.ReadLine();
return;
}
if (callback.Result != EResult.OK)
{
Log($"Failed to login: {callback.Result}");
Thread.Sleep(TimeSpan.FromSeconds(2));
return;
}
Log($"Logged in, current valve time is {callback.ServerTime} UTC");
}
private static void OnLoggedOff(SteamUser.LoggedOffCallback callback)
{
Log($"Logged off from Steam: {callback.Result}");
}
private static void OnAccountInfo(SteamUser.AccountInfoCallback callback)
{
Friends.SetPersonaState(EPersonaState.Busy);
}
private static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback)
{
Log("Updating sentryfile so that you don't need to authenticate with SteamGuard next time.");
var sentryHash = CryptoHelper.SHAHash(callback.Data);
File.WriteAllBytes("sentry.bin", callback.Data);
User.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
{
JobID = callback.JobID,
FileName = callback.FileName,
BytesWritten = callback.BytesToWrite,
FileSize = callback.Data.Length,
Offset = callback.Offset,
Result = EResult.OK,
LastError = 0,
OneTimePassword = callback.OneTimePassword,
SentryFileHash = sentryHash,
});
}
public static void OnClanState(SteamFriends.ClanStateCallback callback)
{
if (callback.Announcements.Count == 0)
{
return;
}
var groupName = callback.ClanName;
if (string.IsNullOrEmpty(groupName))
{
groupName = Friends.GetClanName(callback.ClanID);
}
foreach (var announcement in callback.Announcements)
{
var message = announcement.Headline.Trim();
if (!string.IsNullOrEmpty(groupName) && !announcement.Headline.Contains(groupName.Replace("Steam", string.Empty).Trim()))
{
message = $"{announcement.Headline} ({groupName})";
}
// 240 max tweet length, minus 23 characters for the t.co link
if (message.Length > 217)
{
message = $"{message.Substring(0, 216)}β¦";
}
var url = $"https://steamcommunity.com/gid/{callback.ClanID.ConvertToUInt64()}/announcements/detail/{announcement.ID}";
for (var i = 0; i < 2; i++)
{
try
{
Log($"Tweeting \"{message}\" - {url}");
Twitter.UpdateStatus($"{message} {url}");
break;
}
catch (Exception e)
{
Log($"Exception: {e.Message}");
}
}
}
}
public static void Log(string format)
{
Console.WriteLine($"[{DateTime.Now:R}] {format}");
}
}
}