forked from KockaAdmiralac/WikiaActivityLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
243 lines (239 loc) · 7.19 KB
/
main.js
File metadata and controls
243 lines (239 loc) · 7.19 KB
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
243
/**
* main.js
* Module containing the main program class
*/
'use strict';
/**
* Importing modules
*/
const Wiki = require('./includes/wiki.js'),
io = require('./includes/io.js'),
fs = require('fs'),
util = require('./includes/util.js'),
packageJSON = require('./package.json');
/**
* URL from which to fetch the master branch package.json
* for checking the version in update process
*/
const UPDATE_URL = 'https://raw.githubusercontent.com/KockaAdmiralac/WikiaActivityLogger/master/package.json';
/**
* Main class
* @class Logger
*/
class Logger {
/**
* Initializer
* @method initialize
*/
initialize() {
this._initConfig();
this._initCache();
io.makeJar();
this._checkForUpdates();
}
/**
* Initializes the program configuration
* @method _initConfig
* @private
*/
_initConfig() {
try {
this._config = require(`./config.json`);
} catch(e) {
main.hook('error', 'An error occurred while loading the configuration! Please make sure config.json file in the main directory exists.', 'Logger', '_initConfig');
}
}
/**
* Initializes the program cache
* @method _initCache
* @private
*/
_initCache() {
try {
this._cache = require('./cache.json');
} catch(e) {
main.hook('error', 'An error occurred while initializing cache. If this is your first time starting WikiaActivityLogger, ignore this error.', 'Logger', '_initCache');
this._cache = { wikis: {} };
}
this._cacheInterval = setInterval(this._saveCache.bind(this), 5000); // TODO: Make interval configurable?
}
/**
* Saves cache to cache.json
* @method _saveCache
* @private
*/
_saveCache() {
try {
if(!(this._wikis instanceof Array)) {
return;
}
this._cache.wikis = {};
this._wikis.forEach(el => this._cache.wikis[el.name] = el.cache);
if(this._cache && this._cache.wikis) {
fs.writeFileSync('./cache.json', JSON.stringify(this._cache));
}
} catch(e) {
main.hook('throw', e);
}
}
/**
* Checks for updates to WikiaActivityLogger
* @method _checkForUpdates
* @private
*/
_checkForUpdates() {
if(this._config.no_updates) {
this._initAccount();
} else {
io.get(UPDATE_URL)
.then((function(d) {
if(d.version !== packageJSON.version) {
main.hook('updateAvailable', d.version);
}
this._initAccount();
}).bind(this))
.catch(error => main.hook('throw', error));
}
}
/**
* Initializes information about wikis
* @method _initWikis
* @private
*/
_initWikis() {
this._wikis = [];
const wikis = this._config.wikis;
if(typeof wikis !== 'object') {
main.hook('noWikis');
}
util.each(wikis, (k, v) => this._wikis.push(new Wiki(k, v, (this._config.language || 'en'), this._cookieJar, this._cache.wikis[k])), this);
}
/**
* Adds a wiki to array of wikis
* @method addWiki
* @param {Wiki} wiki Wiki object to add
*/
addWiki(wiki) {
this._wikis.push(wiki);
}
/**
* Removes a wiki from the array of wikis
* @method removeWiki
* @param {Wiki} wiki The wiki to remove
*/
removeWiki(wiki) {
this._wikis.splice(this._wikis.indexOf(wiki), 1);
}
/**
* Initializes the account used to access the API
* @method _initAccount
* @private
*/
_initAccount() {
if(this._config.account && this._config.account.username) {
const acc = this._config.account;
if(acc.password) {
this._login(acc);
} else {
main.hook('enterPassword', acc.username, function(password) {
this._login({
username: acc.username,
password: password,
domain: acc.domain
});
});
}
} else {
this._initWikis();
}
}
/**
* Logs in through the API
* @method _login
* @private
*/
_login(info) {
const options = {
lgname: info.username,
lgpassword: info.password
};
if(info.token) {
options.lgtoken = info.token;
}
info.domain = info.domain || 'community';
io.api(info.domain, 'login', options, undefined, 'POST').then((function(data) {
switch(data.result) {
case 'NeedToken':
info.token = data.token;
this._login(info);
break;
case 'WrongToken':
main.hook('error', 'The supplied token was not a valid token!', 'Logger', '_login');
break;
case 'Illegal':
main.hook('error', 'Supplied user name is invalid!', 'Logger', '_login');
break;
case 'NotExists':
main.hook('error', 'Supplied user name does not exist!', 'Logger', '_login');
break;
case 'EmptyPass':
main.hook('error', 'Supplied password is empty!', 'Logger', '_login');
break;
case 'WrongPass':
case 'WrongPluginPass':
main.hook('error', 'Supplied password is incorrect!', 'Logger', '_login');
break;
case 'CreateBlocked':
main.hook('error', 'Auto-creation of the account is required but not possible!', 'Logger', '_login');
break;
case 'Throttled':
main.hook('error', 'Login attempts from your IP have been throttled!', 'Logger', '_login');
break;
case 'Blocked':
main.hook('error', `The user account you attempted to log into is blocked on ${info.domain}.wikia.com! You can change the login domain with the 'domain' parameter.`, 'Logger', '_login');
break;
case 'Aborted':
main.hook('error', `Logging in was aborted by an extension hook! Reason: ${data.reason ? data.reason : 'unknown'}`);
break;
case 'Success':
main.hook('login');
this._account = info.username;
this._cookieJar = io.jar;
this._initWikis();
}
}).bind(this)).catch(error => main.hook('throw', error));
}
/**
* Destroys all wikis
* Not really
* @method destroy
* @todo Make this method name less scary
*/
destroy() {
this._wikis.forEach(el => el.destroy());
clearInterval(this._cacheInterval);
this._saveCache();
}
/**
* Get all avaialable wikis
* @return {Array<Wiki>} All available wikis
*/
get wikis() {
return this._wikis;
}
/**
* Get project configuration
* @return {Object} Project configuration
*/
get config() {
return this._config;
}
/**
* Get logged in account name
* @return {String} Account name
*/
get account() {
return this._account;
}
}
module.exports = Logger;