-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvipInvitations.jsmod
More file actions
364 lines (257 loc) · 13 KB
/
Copy pathvipInvitations.jsmod
File metadata and controls
364 lines (257 loc) · 13 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* ***** BEGIN LICENSE BLOCK *****
* Version: GNU GPL 3.0
*
* The contents of this file are subject to the
* GNU General Public License Version 3.0; you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://www.gnu.org/licenses/gpl.html
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
* ***** END LICENSE BLOCK ***** */
(function VipInvitationModule($D, $A) {
this.disabled = false;
this.name = this.constructor.name;
var pv = Exec('_privateData.js', false);
function QueryVIPDatabase( data, OnResult ) {
HttpRequest( pv.invitationURL, data, 3*SECOND, function(status, statusCode, reasonPhrase, headers, body) {
DBG && DebugTrace( 'QueryVIPDatabase', OK, data.toSource(), body );
if ( status != OK ) {
OnResult(status);
return;
}
if ( statusCode != 200 || !body ) {
OnResult(NOTFOUND);
return;
}
var responseObject;
try {
responseObject = eval('('+body+')');
} catch(ex) {
DBG && ReportError( ExToText(ex) );
OnResult(BADRESPONSE);
return;
}
OnResult(OK, responseObject); // don't include this in the try ... catch
});
}
var _playerListToSetlevel = [];
this.moduleListener = {
botcmd: {
help: function( modCmd, cmdName, cmdData, command, from, to, msg ) {
if ( to != pv.vipChannel )
return;
var nick = StrBefore(from, '!');
var replyTo = to[0] == '#' ? to : nick;
$A.Privmsg( replyTo, 'Help: !invite [<nick>]' );
},
server: function( modCmd, cmdName, cmdData, command, from, to, msg ) {
StartAsyncProc( new function() {
if ( to != pv.vipChannel )
return;
var nick = StrBefore(from, '!');
var replyTo = to[0] == '#' ? to : nick;
var [status, result] = yield function(callback) $A.TremulousServerStatus(pv.vipTremServer, callback);
if ( status != OK ) {
$A.Privmsg( replyTo, 'Sorry, the server does not respond. ('+String(status)+')' );
AbortAsyncProc();
return;
}
var nicePlayerList = [ '\x030,1'+$A.TremulousTextToIrc(player)+'\x0F' for each ( player in result.playerList ) ];
$A.Privmsg( replyTo, $A.TremulousTextToIrc(result.info.sv_hostname) + '\x0F is running ' + result.info.mapname + ' with ' + result.playerList.length + ' player(s)' );
while ( nicePlayerList.length )
$A.Privmsg( replyTo, nicePlayerList.splice(0, 8).join(' ') );
});
}, // server
forceinvite: function( modCmd, cmdName, cmdData, command, from, to, msg ) {
StartAsyncProc( new function() {
function CancelForceinvite(message) {
var replyTo = to[0] == '#' ? to : StrBefore(from, '!');
$A.Privmsg( replyTo, message );
AbortAsyncProc();
}
!$A.IsBotOp(from) && CancelForceinvite('need bot auth.');
var replyTo = to[0] == '#' ? to : StrBefore(from, '!');
!cmdData && CancelForceinvite( 'use: !forceinvite <guest> by <host>' );
var [guestNick, byWord, hostNick] = cmdData.split(' ');
byWord != 'by' && CancelForceinvite( 'use: !forceinvite <guest> by <host>' );
var [status, hostWhois] = yield function(callback) $A.Whois(hostNick, callback);
status != OK && CancelForceinvite('Unable to get '+hostNick+' IRC status.');
!hostWhois.isIdentified && CancelForceinvite( hostWhois+' is not identified on IRC.');
var [status, guestWhois] = yield function(callback) $A.Whois(guestNick, callback);
status != OK && CancelForceinvite('Unable to get '+guestNick+' IRC status.');
!guestWhois.isIdentified && CancelForceinvite( guestNick+' is not identified on IRC.');
var [status, response] = yield function(callback) QueryVIPDatabase({ action:'invite', guest:guestNick, host:hostNick }, callback);
status != OK && CancelForceinvite('Service unavailable. Please retry later.');
response.error && CancelForceinvite(response.error);
var guestIp = GetHostByName(guestWhois.host);
if ( guestIp ) {
var [status, response] = yield function(callback) QueryVIPDatabase({ action:'setIp', nick:guestNick, ip:guestIp }, callback);
_playerListToSetlevel.push([guestIp, guestNick, Now()]);
}
});
},
invite: function( modCmd, cmdName, cmdData, command, from, to, msg ) {
StartAsyncProc( new function() {
to != pv.vipChannel && AbortAsyncProc();
function CancelInvitation(message) {
var replyTo = to[0] == '#' ? to : StrBefore(from, '!');
$A.Privmsg( replyTo, message );
DBG && log.Write(LOG_PROC, replyTo + ' ' + command + ' ' + message);
AbortAsyncProc();
}
var hostNick = StrBefore(from, '!');
var [status, hostWhois] = yield function(callback) $A.Whois(hostNick, callback);
status != OK && CancelInvitation('Unable to get your IRC status '+hostNick+'. ('+String(status)+')');
!hostWhois.isIdentified && CancelInvitation('You are not identified on IRC.');
var [status, accessList] = yield function(callback) $A.AccessInfo(pv.vipChannel, callback);
status != OK && CancelInvitation('Unable to get AccessInfo. (status: '+String(status)+')');
var hostLevel = [ user.level for each ( user in accessList ) if (user.hostmask.toLowerCase() == hostNick.toLowerCase()) ][0];
var hasBeenOpByChanServ = getData( $D.channel[pv.vipChannel].names[hostNick].operator.by ) == pv.chanServ;
!(hostLevel >= pv.vipChanLevel) && !hasBeenOpByChanServ && CancelInvitation('You must be a VIP to use invite command.');
hostLevel >= pv.vipChanLevel || hasBeenOpByChanServ && CancelInvitation('Please use your master nick before invite.');
var [status, hostDbInfo] = yield function(callback) QueryVIPDatabase({ action:'info', nick:hostNick }, callback);
status != OK && CancelInvitation('Service unavailable. Please retry later.');
hostDbInfo.error && CancelInvitation(hostNick+', you are not in the VIP database.');
if ( !cmdData ) { // just get the VIP info
var days = ( (new Date() - new Date(parseInt(hostDbInfo.time)*1000)) / DAY ).toFixed(1);
CancelInvitation(hostNick+', you\'ve been a VIP for '+days+' days and you have '+hostDbInfo.invitations_left+' invitation(s) remaining.' );
} else {
hostDbInfo.invitations_left <= 0 && CancelInvitation('Sorry '+hostNick+', you have no invitations remaining.');
var guestNick = cmdData;
var [status, guestWhois] = yield function(callback) $A.Whois(guestNick, callback);
status != OK && CancelInvitation('Unable to get '+guestNick+' IRC status.');
!guestWhois.isIdentified && CancelInvitation(guestNick +' is not identified on IRC.');
var guestLevel = [ user.level for each ( user in accessList ) if (user.hostmask.toLowerCase() == guestNick.toLowerCase()) ][0];
guestLevel >= pv.vipChanLevel && CancelInvitation(guestNick+' is already a VIP.');
var [status, response] = yield function(callback) QueryVIPDatabase({ action:'invite', guest:guestNick, host:hostNick }, callback);
status != OK && CancelInvitation('Service unavailable. Please retry later.');
response.error && CancelInvitation(response.error);
$A.SetChannelAccessLevel( pv.vipChannel, guestNick, pv.vipChanLevel );
CancelInvitation(guestNick+' has been invited. You have '+(hostDbInfo.invitations_left-1)+' invitation(s) remaining.');
$A.Invite( guestNick, pv.vipChannel );
var guestIp = GetHostByName(guestWhois.host);
if ( guestIp ) {
var [status, response] = yield function(callback) QueryVIPDatabase({ action:'setIp', nick:guestNick, ip:guestIp }, callback);
_playerListToSetlevel.push([guestIp, guestNick, Now()]); // fc. VipTremServerUpdateIpList and VipTremServerScanPlayers procedures
}
}
});
} // invite
}
}
function VipTremServerScanPlayers() {
var prevDisplayedStatusDate = 0;
for (;;) {
var [status, result] = yield function(callback) $A.TremulousServerStatus( pv.vipTremServer, callback );
if ( status != OK ) {
yield AsyncSleep( 2*MINUTE );
continue;
}
if ( result.playerList.length < 3 || result.playerList.length > 10 ) {
yield AsyncSleep( 1*MINUTE );
continue;
}
if ( Now() - 10*MINUTE > prevDisplayedStatusDate ) {
var nicePlayerList = [ '\x030,1'+$A.TremulousTextToIrc(player)+'\x0F' for each ( player in result.playerList ) ];
$A.Privmsg( pv.vipChannel, $A.TremulousTextToIrc(result.info.sv_hostname) + '\x0F is running ' + result.info.mapname + ' with ' + result.playerList.length + ' player(s)' );
while ( nicePlayerList.length )
$A.Privmsg( pv.vipChannel, nicePlayerList.splice(0, 8).join(' ') ); // avoid long lines
prevDisplayedStatusDate = Now();
}
yield AsyncSleep( 1*MINUTE );
}
}
function VipTremServerAutoSetlevel() {
for (;;) {
var [status, rconStatus] = yield function(callback) $A.TremulousServerRconStatus( pv.vipTremServer, pv.vipTremServerRcon, callback );
if ( status == OK ) { // no response or timeout or failure
loop:
for each ( let playerStatusInfo in rconStatus )
for each ( let [i, [guestIp, guestNick, date]] in Iterator(_playerListToSetlevel) )
if ( playerStatusInfo.ip == guestIp ) {
$A.TremulousServerCommand( pv.vipTremServer, 'rcon '+pv.vipTremServerRcon+' !setlevel '+playerStatusInfo.num+' '+pv.vipTremServerVipLevel, Noop );
$A.TremulousServerCommand( pv.vipTremServer, 'rcon '+pv.vipTremServerRcon+' m '+ playerStatusInfo.num +' Now your level is '+pv.vipTremServerVipLevel+', try !help command.', Noop );
guestNick && $A.TremulousServerCommand( pv.vipTremServer, 'rcon '+pv.vipTremServerRcon+' say Welcome to '+guestNick+'^7 the new VIP !', Noop );
_playerListToSetlevel.splice(i, 1); // remove processed item
QueryVIPDatabase({ action:'setlevelDone', nick:guestNick, tremName:playerStatusInfo.name }, callback);
break loop; // because the list has been splice(), we can only process one item
}
}
yield AsyncSleep( 1*MINUTE );
}
}
function VipTremServerUpdateIpList() {
for (;;) {
var [status, response] = yield function(callback) QueryVIPDatabase({ action:'getIpList' }, callback);
if ( status == OK ) {
_playerListToSetlevel.splice(0); // empty the current list
if ( !response.error ) // eg. { "error":"No ips in base" }
for each ( let ip in response )
_playerListToSetlevel.push([ip, '', 0]); // cf. end of !invite procedure
// ok, we got the list, then reload it in 2 hours
yield AsyncSleep( 2*HOUR );
} else {
// failed to get the list, then retry soon
yield AsyncSleep( 10*MINUTE );
}
}
}
this.stateListener = [
{
set: function(s) s.STATE_IRC_INTERACTIVE && s.DefaultModule,
reset: False, // state never resets
trigger: function(polarity) {
$A.Nick( pv.botNick );
$A.Privmsg( pv.nickServ, 'IDENTIFY '+pv.password );
$A.Privmsg( pv.chanServ, 'IDENTIFY '+pv.vipChannel+' '+pv.vipChannelPassword );
}
},
{
set: function(s) s.VipInvitationModule && s.STATE_IRC_INTERACTIVE && s.ChannelModule,
reset: function(s) !s.VipInvitationModule && s.STATE_IRC_INTERACTIVE && s.ChannelModule,
trigger: function(polarity) {
if ( polarity )
$A.Join( pv.vipChannel );
else
$A.Part( pv.vipChannel );
}
},
{
set: function(s) s.VipInvitationModule && s.STATE_IRC_INTERACTIVE && s.DefaultModule,
reset: function(s) !s.VipInvitationModule || !s.STATE_IRC_INTERACTIVE || !s.DefaultModule,
trigger: function(polarity) {
//ToggleAsyncProc( VipTremServerScanPlayers, polarity );
if ( polarity )
this.vipTremServerScanPlayers = StartAsyncProc( new VipTremServerScanPlayers );
else
StopAsyncProc(this.vipTremServerScanPlayers);
}
},
{
set: function(s) s.VipInvitationModule,
reset: function(s) !s.VipInvitationModule,
trigger: function(polarity) {
//ToggleAsyncProc( VipTremServerUpdateIpList, polarity );
if ( polarity )
this.vipTremServerUpdateIpList = StartAsyncProc( new VipTremServerUpdateIpList );
else
StopAsyncProc(this.vipTremServerUpdateIpList);
//ToggleAsyncProc( VipTremServerAutoSetlevel, polarity );
if ( polarity )
this.vipTremServerAutoSetlevel = StartAsyncProc( new VipTremServerAutoSetlevel );
else
StopAsyncProc(this.vipTremServerAutoSetlevel);
}
}
];
this.moduleListener = { ircMsg:{
ERR_NICKNAMEINUSE: function( nick, nickInUsePhrase ) {
$A.Privmsg( pv.nickServ, 'GHOST '+pv.botNick+' '+pv.password );
// (TBD) wait a response before changingthe nick ?
$A.Nick( pv.botNick );
}
} };
})