-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandEvent.jsmod
More file actions
233 lines (166 loc) · 7.06 KB
/
commandEvent.jsmod
File metadata and controls
233 lines (166 loc) · 7.06 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
/* ***** 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 CommandEventModule($D, $A) {
this.name = this.constructor.name;
var $MD = $D[this.name];
var _cs = new CallScheduler;
this.moduleApi = {
Whois: function( nick, OnReply ) _cs.Add(function() {
var pingData = RandomNumber(16);
var timeoutId, events = {ircMsg:{}}, result = NewDataObj();
$A.AddModuleListener(events);
function End(status, reply) {
$A.RemoveModuleListener(events);
io.RemoveTimeout(timeoutId);
_cs.Next();
OnReply(status, reply);
}
events.ircMsg.PONG = function( tmp, command, from, server, data ) {
if ( data != pingData )
return;
delete events.ircMsg.PONG;
events.ircMsg.RPL_WHOISUSER = function( tmp, command, from, to, nick, user, host, star, realName ) {
result.nick = nick
result.user = user
result.host = host;
result.realName = realName;
}
events.ircMsg.RPL_WHOISCHANNELS = function( tmp, command, from, to, nick, channelList ) {
result.channelList = RTrim(channelList).split(' ');
}
events.ircMsg.RPL_WHOISSERVER = function( tmp, command, from, to, nick, server, serverInfo ) {
result.server = server;
result.serverInfo = serverInfo;
}
events.ircMsg[320] = function( tmp, command, from, to, nick ) { // is identified to services (freenode only ?)
result.isIdentified = true;
}
events.ircMsg.ERR_NOSUCHSERVER = function() End(NOTFOUND);
events.ircMsg.ERR_NONICKNAMEGIVEN = function() End(BADREQUEST);
events.ircMsg.ERR_NOSUCHNICK = function() End(NOTFOUND);
events.ircMsg.RPL_ENDOFWHOIS = function() End(OK, result);
}
$A.Send( ['PING '+pingData, 'WHOIS '+nick], false, function() { // PING to sync the request
timeoutId = io.AddTimeout(getData($MD.maxServerReplyInterval), function() End(TIMEOUT) );
});
}),
UserHost: function( nickList, OnReply ) _cs.Add(function() {
var pingData = RandomNumber(16);
var timeoutId;
var events = {ircMsg:{}};
function End(status, reply) {
$A.RemoveModuleListener(events);
io.RemoveTimeout(timeoutId);
_cs.Next();
OnReply(status, reply);
}
events.ircMsg.PONG = function( tmp, command, from, server, data ) {
if ( data != pingData )
return;
delete events.ircMsg.PONG;
events.ircMsg.RPL_USERHOST = function( tmp, command, from, to, hostList ) {
var result = NewDataObj();
for each ( var userhost in hostList.split(' ') ) {
var match = /(.+?)(\*?)=(\+?)(?:.*?)@(.*)/(userhost);
if ( userhost && match ) {
var [, nick, ircOp, hasAwayMsg, host] = match;
result[nick] = host;
}
}
End(OK, result);
};
events.ircMsg.ERR_NEEDMOREPARAMS = function( command, from, to, erroneousCommand, reasonPhrase ) erroneousCommand == 'USERHOST' && End(BADREQUEST);
};
$A.AddModuleListener(status, events);
$A.Send( ['PING '+pingData, 'USERHOST '+nickList.join(' ')], false, function() {
timeoutId = io.AddTimeout(getData($MD.maxServerReplyInterval), function() End(TIMEOUT) ); // start the timeout ONLY vhen the message has been sent
});
}),
Prompt: function( nick, message, OnReply ) _cs.Add(function() {
var timeoutId, events = {ircMsg:{}};
function End(status, message) {
$A.RemoveModuleListener(events);
io.RemoveTimeout(timeoutId);
_cs.Next();
OnReply(status, message);
}
events.ircMsg.ERR_NORECIPIENT = function() End(BADREQUEST);
events.ircMsg.ERR_NOTEXTTOSEND = function() End(BADREQUEST);
events.ircMsg.ERR_CANNOTSENDTOCHAN = function() End(ERROR);
events.ircMsg.ERR_NOTOPLEVEL = function() End(ERROR);
events.ircMsg.ERR_WILDTOPLEVEL = function() End(ERROR);
events.ircMsg.ERR_TOOMANYTARGETS = function() End(ERROR);
events.ircMsg.ERR_NOSUCHNICK = function() End(NOTFOUND);
events.ircMsg.RPL_AWAY = function() End(UNAVAILABLE);
events.ircMsg.PRIVMSG = function( tmp, command, from, to, message ) {
if ( to == getData($D.nick) && nick == StrBefore(from, '!') )
End(OK, message);
};
$A.AddModuleListener(events);
$A.Privmsg( nick, message, function() {
timeoutId = io.AddTimeout(getData($MD.maxUserReplyInterval), function() End(TIMEOUT) ); // start the timeout ONLY vhen the message has been sent
});
}),
Sync: function( OnReply ) _cs.Add(function() {
var timeoutId, events, pingData = IntervalNow();
function End(status) {
$A.RemoveModuleListener(events);
io.RemoveTimeout(timeoutId);
_cs.Next();
OnReply(status);
}
function Reply( command, from, server, data ) data == pingData && End(OK);
events = { ircMsg:{PONG:Reply} };
$A.AddModuleListener(events);
$A.Send( 'PING '+pingData, false, function() {
timeoutId = io.AddTimeout(getData($MD.maxServerReplyInterval), function() End(TIMEOUT) ); // start the timeout ONLY vhen the message has been sent
});
}),
Response: function( commandName, OnReply ) _cs.Add(function() {
var timeoutId, events = { ircMsg:{} };
function End() {
$A.RemoveModuleListener(events);
io.RemoveTimeout(timeoutId);
_cs.Next();
OnReply.apply(null, arguments);
}
$A.AddModuleListener(events);
events.ircMsg[commandName] = function( tmp, command, from, server, data ) End( OK, command, from, server, data );
timeoutId = io.AddTimeout(getData($MD.maxServerReplyInterval), function() End(TIMEOUT) );
})
};
})
/*
302 RPL_USERHOST
":*1<reply> *( " " <reply> )"
- Reply format used by USERHOST to list replies to
the query list. The reply string is composed as
follows:
reply = nickname [ "*" ] "=" ( "+" / "-" ) hostname
The '*' indicates whether the client has registered
as an Operator. The '-' or '+' characters represent
whether the client has set an AWAY message or not
respectively.
eg:
USERHOST Wiz Michael syrk
:ircd.stealth.net 302 yournick :syrk=+syrk@millennium.stealth.net
*/
// WHOIS
// doc: http://www.irchelp.org/irchelp/rfc/rfc2812.txt
// WHOIS CY`TV
// :zelazny.freenode.net 311 soubok CY`TV n=tv ns301208.ovh.net * :tv
// :zelazny.freenode.net 319 soubok CY`TV :@#CY @#trem-vips
// :zelazny.freenode.net 312 soubok CY`TV irc.freenode.net :http://freenode.net/
// :zelazny.freenode.net 320 soubok CY`TV :is identified to services
// :zelazny.freenode.net 318 soubok CY`TV :End of /WHOIS list.