-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.jsmod
More file actions
269 lines (189 loc) · 7.99 KB
/
Copy pathdefault.jsmod
File metadata and controls
269 lines (189 loc) · 7.99 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
/* ***** 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 DefaultModule($D, $A, $S) {
this.name = this.constructor.name;
var $MD = $D[this.name];
function TemporaryNick() '_'+RandomString(8);
// [TBD] autodetect max message length ( with a self sent mesage )
// [TBD] autodetect flood limit
this.moduleListener = { ircMsg:{
RPL_WELCOME: function( tmp, command, from, to ) {
setData($D.nick, to);
$S.Toggle( 'STATE_IRC_HAS_GOOD_NICK', getData($D.nick) == getData($D.wishNick) );
},
//(TBD) // RPL_YOURHOST
// ERR_NONICKNAMEGIVEN
// ERR_ERRONEUSNICKNAME
// ERR_NICKNAMEINUSE
// ERR_NICKCOLLISION
// ERR_UNAVAILRESOURCE
// ERR_RESTRICTED
NICK: function( tmp, command, from, newNick ) { // WiZ!jto@tolsun.oulu.fi NICK Kilroy
var nick = StrBefore(from, '!');
if ( nick != getData($D.nick) ) // only listen for self nickname changing.
return;
setData($D.nick, newNick);
$S.Toggle( 'STATE_IRC_HAS_GOOD_NICK', getData($D.nick) == getData($D.wishNick) );
},
// ERR_RESTRICTED: function() { // Your connection is restricted!
//
// $A.Nick( TemporaryNick() );
// },
// ERR_NICKCOLLISION: function( tmp, command, nick, nickCollisionPhrase ) { // Nickname collision KILL from <user>@<host>
//
// $A.Nick( TemporaryNick() );
// },
ERR_ERRONEUSNICKNAME: function() { // Erroneous nickname ( see. IsNickValid() )
$A.Nick( TemporaryNick() );
},
ERR_NICKNAMEINUSE: function( nick, nickInUsePhrase ) { // Nickname is already in use
// (TBD) try a random nick, then, when ready, periodically try a better nick
$A.Nick( TemporaryNick() ); // (TBD) enhance this
io.AddTimeout( getData($MD.wishNickRetryInterval), function() {
$A.Send( 'NICK '+getData($D.wishNick) );
}); // async pause
},
RPL_USERHOST: function( tmp, command, from, to, hostList ) {
if ( to == getData($D.nick) ) { // (TBD) fix it, $D.userhost seems to be buggy
var host = StrBefore(hostList, ' ');
setData( $D.userhost, host.substr(host.lastIndexOf('=')+1) ); // hostinfo[0] = '+' or '-' : AWAY message set ???
}
},
PING: function( tmp, command, prefix, arg ) {
$A.Send( 'PONG '+arg );
setData( $D.lastPingTime, Now() );
},
NOTICE: function( tmp, command, from, to, message ) {
// irc.clockworkorange.co.uk- on 1 ca 1(2) ft 10(10) tr
// where:
// on = Number of globally connected clients including yourself from your IP-number.
// ca = Connect Attempts, You have tried once, after 2 sequential connects you get throttled.
// ft = Free Targets. This is how many different people you may contact at once, also see 3.10
// tr = Targets Restored. Your targets are kept for 2 minutes or until someone else from your IP logs on.
// This stops you from refilling your free targets by reconnection.
/*
if ( message.substr(0,2) == 'on' ) {
var res = /on ([0-9]+) ca ([0-9]+)\(([0-9]+)\) ft ([0-9]+)\(([0-9]+)\) *(tr)?$/(message);
if ( res == null )
return;
// $A.RemoveModuleListener( 'NOTICE', oncafttrNotice );
}
*/
}
} };
// export module API
this.moduleApi = {
// function StripIrcText(text) text.replace(/\x03(N|(B|V|U|S|I).|F.\d?|P(P|F)|C([0-9,A-F]|#[0-9,A-F]{6}){0,2})/ig,''); // see. http://www.invlogic.com/irc/ctcp2_2.html
IrcTextStrip: function(text) text.replace(/\x02|\x1F|\x16\x0F|\x03\d{1,2}(,\d{1,2})?/g,''),
Color: function(fg, bg) {
return '0x03'+fg+(bg!=undefined?','+bg:'');
},
Bold: function() {
return '0x02';
},
ToIRCLowerString: function( str ) { // {}|^ are considered to be the lower case equivalents of the characters []\~
str = str.replace('[', '{');
str = str.replace(']', '}');
str = str.replace('\\', '|');
str = str.replace('~', '^');
return str.toLowerCase();
},
ToIRCUpperString: function( str ) {
str = str.replace('{', '[');
str = str.replace('}', ']');
str = str.replace('|', '\\');
str = str.replace('^', '~');
return str.toUpperCase();
},
// special = %x5B-60 / %x7B-7D ; "[", "]", "\", "`", "_", "^", "{", "|", "}"
// letter = %x41-5A / %x61-7A ; A-Z / a-z
// digit = %x30-39 ; 0-9
// nickname = ( letter / special ) *8( letter / digit / special / "-" )
IsNickValid: function(nick) !!/^[A-Za-z\x5B-\x60\x7B-\x7D][A-Za-z0-9\x5B-\x60\x7B-\x7D\x2D]{0,8}$/(nick),
Topic: function( channel, topic ) void $A.Send( 'TOPIC '+channel+' :'+topic ),
Nick: function( nick, OnSent ) void $A.Send( 'NICK '+nick, false, OnSent ),
Privmsg: function( to, messages, OnSent ) {
if( !(messages instanceof Array) )
messages = [messages];
$A.Send( [ 'PRIVMSG '+StrBefore(to,'!')+' :'+message for each ( message in messages ) ], false, OnSent )
},
Notice: function( to, messages, OnSent ) {
if( !(messages instanceof Array) )
messages = [messages];
$A.Send( [ 'NOTICE '+StrBefore(to,'!')+' :'+message for each ( message in messages ) ], false, OnSent )
},
// freenode network do not send a quit message if the client is connected for less than 5 mimutes
Quit: function(quitMessage) $A.Send( 'QUIT :'+quitMessage, true ) // true = force the message to be post ASAP
}
var webAccessProbe = new AsyncProcHelper( function() { // (TBD) move this module to a more standard place
for (;;) {
var [status] = yield function(callback) HttpRequest( 'http://www.google.com/wwwcheck', undefined, 5000, callback ); // or http://www.microsoft.com/wwwcheck
$S.Toggle('STATE_HAS_WEB_ACCESS', status == OK );
if ( status == OK )
yield AsyncSleep( 30*MINUTE );
yield AsyncSleep( Math.floor(RandomRange(2,7)*MINUTE) );
}
});
var lagProbe = new AsyncProcHelper( function() {
var LagProbeResponse = { ircMsg:{
PONG: function( tmp, command, from, server, data ) {
if ( data.substr(0,3) == 'LAG' )
setData( $D.ping, IntervalNow() - parseInt(data.substr(3)) );
}
}}
$A.AddModuleListener( LagProbeResponse );
try {
for (;;) {
$A.Send('PING LAG'+IntervalNow(), true);
yield AsyncSleep( getData($MD.lagProbeInterval) );
}
} finally { // called when the procedure is closed
$A.RemoveModuleListener( LagProbeResponse );
}
});
/*
var clock = new AsyncProcHelper( function() {
var count = 0;
for (;;) {
yield AsyncSleep( 1*MINUTE );
count++;
count % 1 || $A.FireModuleListener( 'clock', '1minute' );
count % 5 || $A.FireModuleListener( 'clock', '5minutes' );
count % 10 || $A.FireModuleListener( 'clock', '10minutes' );
count % 60 || $A.FireModuleListener( 'clock', '1hour' );
}
});
*/
this.stateListener = [
{
set: function(s) s.STATE_IRC_CONNECTED,
reset: False, // never resets
trigger: function(polarity) {
$A.Send( 'NICK '+getData($D.wishNick), true );
$A.Send( 'USER '+getData($D.ident.username)+' '+getData($D.hostname)+' '+getData($D.server)+' :'+getData($D.ident.realname), true );
}
},
{
set: function(s) s.STATE_IRC_INTERACTIVE,
reset: function(s) !s.STATE_IRC_INTERACTIVE,
trigger: function(polarity) {
if ( polarity )
$A.Send( 'USERHOST '+getData($D.nick) );
// clock.Toggle(polarity); // (TBD) do not depends on interactive state
lagProbe.Toggle(polarity);
webAccessProbe.Toggle(polarity);
}
}
];
})