/
PeecsWebService.cs
242 lines (182 loc) · 7.13 KB
/
PeecsWebService.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
using Styx.Plugins;
using Styx.WoWInternals;
using Styx.CommonBot;
using Styx.Common;
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using Styx.MemoryManagement;
using System.Web;
using System.Net;
using Styx.Common.Helpers;
using System.Collections.Specialized;
using System.Text;
using System.Net.NetworkInformation;
using System.Runtime.Serialization;
using Styx.WoWInternals.WoWObjects;
namespace com.peec.webservice
{
public partial class PeecsWebService : HBPlugin
{
// Metas
public override string Author { get { return "Peec"; } }
public override string Name { get { return "Peecs WebService"; } }
public override System.Version Version { get { return new Version(0, 0, 2); } }
public override bool WantButton { get { return true; } }
private LocalPlayer Me { get { return Styx.StyxWoW.Me; } }
public override void OnButtonPress()
{
new FormSettings().ShowDialog();
}
private Server server;
private List<ChatLog> chatLogs;
private string apiKey;
private String WoWPath;
public override void Initialize()
{
chatLogs = new List<ChatLog>();
Chat.Say += QueueChat;
Chat.Yell += QueueChat;
Chat.Whisper += QueueChat;
Chat.Party += QueueChat;
Chat.PartyLeader += QueueChat;
Chat.Battleground += QueueChat;
Chat.BattlegroundLeader += QueueChat;
Chat.Raid += QueueChat;
Chat.RaidLeader += QueueChat;
Styx.CommonBot.BotEvents.OnBotStopped += OnStop;
Styx.CommonBot.BotEvents.OnBotStarted += OnStart;
Logging.Write("Init : WebService");
this.apiKey = WSSettings.Instance.apikey;
WoWPath = Path.GetDirectoryName(Styx.StyxWoW.Memory.Process.MainModule.FileName);
server = new Server(WSSettings.Instance.webport, WoWPath + "\\Screenshots\\", checkRequestAccess, parseResult);
}
public override void Dispose()
{
Chat.Say -= QueueChat;
Chat.Yell -= QueueChat;
Chat.Whisper -= QueueChat;
Chat.Party -= QueueChat;
Chat.PartyLeader -= QueueChat;
Chat.Battleground -= QueueChat;
Chat.BattlegroundLeader -= QueueChat;
Chat.Raid -= QueueChat;
Chat.RaidLeader -= QueueChat;
Styx.CommonBot.BotEvents.OnBotStopped -= OnStop;
Styx.CommonBot.BotEvents.OnBotStarted -= OnStart;
chatLogs.Clear();
server.Dispose();
server = null;
}
public void OnStart(EventArgs args)
{
chatLogs.Clear();
}
public void OnStop(EventArgs args)
{
chatLogs.Clear();
}
private DirectoryInfo getScreenshotDir(){
return new DirectoryInfo(WoWPath + "\\Screenshots\\");
}
public override void Pulse(){
if (chatLogs.Count >= 5000)
{
chatLogs.RemoveAll(delegate(ChatLog l) {
return !l.IsWhisper();
});
}
}
public Hashtable parseResult(NameValueCollection res)
{
Hashtable reqData = new Hashtable();
reqData["secretKey"] = this.apiKey;
reqData["apiVersion"] = this.Version.ToString();
JSONAPI reqHandler = new JSONAPI(this.apiKey, this.Version);
Hashtable result = new Hashtable();
switch ((string)res["cmd"])
{
default:
throw new Exception(string.Format("{0} is not a valid cmd.", res["cmd"]));
case "me:playerInfo":
result = reqHandler.Me.getPlayerInfo(Me);
break;
case "me:gameStats":
result = reqHandler.Me.getGameStats(Me);
break;
case "me:items":
result = reqHandler.Me.getItems(Me);
break;
case "me:allStats":
result = reqHandler.Me.getAllStats(Me);
break;
case "chat:send":
LuaAPI.SendChatMessage(
LuaAPI.cs(res["msg"]),
LuaAPI.cs(res["chatType"]),
LuaAPI.cs(res["language"]),
LuaAPI.cs(res["channel"])
);
break;
case "game:getScreenshots":
result = reqHandler.Game.getScreenshots(getScreenshotDir());
break;
case "game:takeScreenshot":
result = reqHandler.Game.takeScreenshot(getScreenshotDir());
break;
case "bot:start":
result = reqHandler.Bot.start();
break;
case "chat:logs":
result = reqHandler.Chat.logs(chatLogs, res["EventName"]);
break;
case "bot:isRunning":
result = reqHandler.Bot.isRunning();
break;
case "bot:stop":
result = reqHandler.Bot.stop();
break;
}
reqData["result"] = result;
return reqData;
}
public void checkRequestAccess(NameValueCollection res)
{
if ((string)res["secretKey"] == "" || (string)res["secretKey"] != this.apiKey)
{
Logging.Write("Response is invalid. Must be of type JSON and include \"secretKey\" with the corresponding secret key.");
throw new Exception("Could not authenticate this key.");
}
if ((string)res["apiVersion"] == "" || (string)res["apiVersion"] != this.Version.ToString())
{
Logging.Write("Response is invalid. Must be of type JSON and include correct \"apiVersion\". Current API VERSION is " + Version.ToString());
throw new Exception(string.Format("Wrong API version of client ({0}), current server API version is: {1}.", res["apiVersion"], Version.ToString()));
}
}
private void QueueChat(Styx.CommonBot.Chat.ChatLanguageSpecificEventArgs e)
{
chatLogs.Add(new ChatLog(e.EventName, e.Author, e.Message, e.FireTimeStamp));
}
public class ChatLog
{
public string EventName, Author, Message;
public uint FireTimeStamp;
public ChatLog(string EventName, string Author, string Message, uint FireTimeStamp)
{
this.EventName = EventName;
this.Author = Author;
this.Message = Message;
this.FireTimeStamp = FireTimeStamp;
}
public bool IsWhisper()
{
return EventName == "CHAT_MSG_WHISPER";
}
}
}
}