]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/irc/commandparser.js
12ef4bd540fa79ea35bc9d9d236dcb94171e73ab
[irc/quakenet/qwebirc.git] / js / irc / commandparser.js
1 qwebirc.irc.BaseCommandParser = new Class({
2 initialize: function(parentObject) {
3 this.send = parentObject.send;
4 this.parentObject = parentObject;
5 },
6 buildExtra: function(extra, target, message) {
7 if(!extra)
8 extra = {}
9
10 extra["n"] = this.parentObject.getNickname();
11 extra["m"] = message;
12 extra["t"] = target;
13 return extra;
14 },
15 newTargetLine: function(target, type, message, extra) {
16 extra = this.buildExtra(extra, target, message);
17 var window = this.parentObject.getWindow(target);
18 var channel;
19 if(!window) {
20 type = "TARGETED" + type;
21 target = false;
22 this.parentObject.newActiveLine("OUR" + type, extra);
23 return;
24 } else if(window.type == qwebirc.ui.WINDOW_CHANNEL) {
25 type = "CHAN" + type;
26 } else {
27 type = "PRIV" + type;
28 }
29
30 this.parentObject.newLine(target, "OUR" + type, extra);
31 },
32 newQueryLine: function(target, type, message, extra) {
33 extra = this.buildExtra(extra, target, message);
34
35 if(this.parentObject.ui.uiOptions.DEDICATED_MSG_WINDOW) {
36 var window = this.parentObject.getWindow(target);
37 if(!window) {
38 var w = this.parentObject.ui.newWindow(this.parentObject, qwebirc.ui.WINDOW_MESSAGES, "Messages");
39 w.addLine("OURTARGETED" + type, extra);
40 return;
41 }
42 }
43 return this.newTargetLine(target, type, message, extra);
44 },
45 dispatch: function(line) {
46 if(line.length == 0)
47 return;
48
49 if(line.charAt(0) != "/")
50 line = "/SAY " + line;
51
52 var line = line.substr(1);
53 var allargs = line.splitMax(" ", 2);
54 var command = allargs[0].toUpperCase();
55 var args = allargs[1];
56
57 var aliascmd = this.aliases[command];
58 if(aliascmd)
59 command = aliascmd;
60
61 for(;;) {
62 var cmdopts = this["cmd_" + command];
63 if(!cmdopts) {
64 if(args) {
65 this.send(command + " " + args);
66 } else {
67 this.send(command);
68 }
69 return;
70 }
71
72 var activewin = cmdopts[0];
73 var splitargs = cmdopts[1];
74 var minargs = cmdopts[2];
75 var fn = cmdopts[3];
76
77 var w = this.getActiveWindow();
78 if(activewin && (w.type != qwebirc.ui.WINDOW_CHANNEL && w.type != qwebirc.ui.WINDOW_QUERY)) {
79 w.errorMessage("Can't use this command in this window");
80 return;
81 }
82
83 if((splitargs != undefined) && (args != undefined))
84 args = args.splitMax(" ", splitargs);
85
86 if((minargs != undefined) && (
87 ((args != undefined) && (minargs > args.length)) ||
88 ((args == undefined) && (minargs > 0))
89 )) {
90 w.errorMessage("Insufficient arguments for command.");
91 return;
92 }
93
94 var ret = fn.run([args], this);
95 if(ret == undefined)
96 return;
97
98 command = ret[0];
99 args = ret[1];
100 }
101 },
102 getActiveWindow: function() {
103 return this.parentObject.getActiveWindow();
104 }
105 });
106
107 qwebirc.irc.CommandParser = new Class({
108 Extends: qwebirc.irc.BaseCommandParser,
109 initialize: function(parentObject) {
110 this.parent(parentObject);
111
112 this.aliases = {
113 "J": "JOIN",
114 "K": "KICK",
115 "MSG": "PRIVMSG",
116 "Q": "QUERY",
117 "BACK": "AWAY",
118 "HOP": "CYCLE"
119 };
120 },
121
122 newUIWindow: function(property) {
123 var p = this.parentObject.ui[property];
124 if(!$defined(p)) {
125 this.getActiveWindow().errorMessage("Current UI does not support that command.");
126 } else {
127 p.bind(this.parentObject.ui)();
128 }
129 },
130
131 /* [require_active_window, splitintoXargs, minargs, function] */
132 cmd_ME: [true, undefined, undefined, function(args) {
133 if(args == undefined)
134 args = "";
135
136 var target = this.getActiveWindow().name;
137 if(!this.send("PRIVMSG " + target + " :\x01ACTION " + args + "\x01"))
138 return;
139
140 this.newQueryLine(target, "ACTION", args, {"@": this.parentObject.getNickStatus(target, this.parentObject.nickname)});
141 }],
142 cmd_CTCP: [false, 3, 2, function(args) {
143 var target = args[0];
144 var type = args[1].toUpperCase();
145 var message = args[2];
146
147 if(message == undefined)
148 message = "";
149
150 if(message == "") {
151 if(!this.send("PRIVMSG " + target + " :\x01" + type + "\x01"))
152 return;
153 } else {
154 if(!this.send("PRIVMSG " + target + " :\x01" + type + " " + message + "\x01"))
155 return;
156 }
157
158 this.newTargetLine(target, "CTCP", message, {"x": type});
159 }],
160 cmd_PRIVMSG: [false, 2, 2, function(args) {
161 var target = args[0];
162 var message = args[1];
163
164 if(!this.parentObject.isChannel(target))
165 this.parentObject.pushLastNick(target);
166 if(this.send("PRIVMSG " + target + " :" + message))
167 this.newQueryLine(target, "MSG", message, {"@": this.parentObject.getNickStatus(target, this.parentObject.nickname)});
168 }],
169 cmd_NOTICE: [false, 2, 2, function(args) {
170 var target = args[0];
171 var message = args[1];
172
173 if(this.send("NOTICE " + target + " :" + message)) {
174 if(this.parentObject.isChannel(target)) {
175 this.newTargetLine(target, "NOTICE", message, {"@": this.parentObject.getNickStatus(target, this.parentObject.nickname)});
176 } else {
177 this.newTargetLine(target, "NOTICE", message);
178 }
179 }
180 }],
181 cmd_QUERY: [false, 2, 1, function(args) {
182 if(this.parentObject.isChannel(args[0])) {
183 this.getActiveWindow().errorMessage("Can't target a channel with this command.");
184 return;
185 }
186
187 this.parentObject.newWindow(args[0], qwebirc.ui.WINDOW_QUERY, true);
188
189 if((args.length > 1) && (args[1] != ""))
190 return ["SAY", args[1]];
191 }],
192 cmd_SAY: [true, undefined, undefined, function(args) {
193 if(args == undefined)
194 args = "";
195
196 return ["PRIVMSG", this.getActiveWindow().name + " " + args]
197 }],
198 cmd_ABOUT: [false, undefined, undefined, function(args) {
199 var lines = [
200 "",
201 "qwebirc v" + qwebirc.VERSION,
202 "Copyright (C) 2008 Chris Porter. All rights reserved.",
203 "http://webchat.quakenet.org/",
204 "",
205 "For licensing questions please contact slug@quakenet.org.",
206 "",
207 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.",
208 "",
209 "This software contains portions by the following third parties:",
210 "- MooTools v1.2 -- http://mootools.net/",
211 " Copyright (C) 2006-2008 Valerio Proietti, MIT license.",
212 "- qwebirc icon -- http://meeb.org/",
213 "- SoundManager 2 -- http://www.schillmania.com/projects/soundmanager2/",
214 " Copyright (C) 2007, Scott Schiller (schillmania.com), BSD license.",
215 "",
216 "Thank you for flying QuakeNet!",
217 "",
218 ];
219
220 var aw = this.getActiveWindow();
221 lines.forEach(function(x) {
222 this.parentObject.newActiveLine("", x);
223 }.bind(this));
224 }],
225 cmd_LOGOUT: [false, undefined, undefined, function(args) {
226 this.parentObject.ui.logout();
227 }],
228 cmd_OPTIONS: [false, undefined, undefined, function(args) {
229 this.newUIWindow("optionsWindow");
230 }],
231 cmd_EMBED: [false, undefined, undefined, function(args) {
232 this.newUIWindow("embeddedWindow");
233 }],
234 cmd_QUOTE: [false, 1, 1, function(args) {
235 this.send(args[0]);
236 }],
237 cmd_KICK: [true, 2, 1, function(args) {
238 var channel = this.getActiveWindow().name;
239
240 var message = "";
241 var target = args[0];
242
243 if(args.length == 2)
244 message = args[1];
245
246 this.send("KICK " + channel + " " + target + " :" + message);
247 }],
248 automode: function(direction, mode, args) {
249 var channel = this.getActiveWindow().name;
250
251 var modes = direction;
252 for(var i=0;i<args.length;i++)
253 modes = modes + mode;
254
255 this.send("MODE " + channel + " " + modes + " " + args.join(" "));
256 },
257 cmd_OP: [true, 6, 1, function(args) {
258 this.automode("+", "o", args);
259 }],
260 cmd_DEOP: [true, 6, 1, function(args) {
261 this.automode("-", "o", args);
262 }],
263 cmd_VOICE: [true, 6, 1, function(args) {
264 this.automode("+", "v", args);
265 }],
266 cmd_DEVOICE: [true, 6, 1, function(args) {
267 this.automode("-", "v", args);
268 }],
269 cmd_TOPIC: [true, 1, 1, function(args) {
270 this.send("TOPIC " + this.getActiveWindow().name + " :" + args[0]);
271 }],
272 cmd_AWAY: [false, 1, 0, function(args) {
273 this.send("AWAY :" + (args?args[0]:""));
274 }],
275 cmd_QUIT: [false, 1, 0, function(args) {
276 this.send("QUIT :" + (args?args[0]:""));
277 }],
278 cmd_CYCLE: [true, 1, 0, function(args) {
279 var c = this.getActiveWindow().name;
280
281 this.send("PART " + c + " :" + (args?args[0]:"rejoining. . ."));
282 this.send("JOIN " + c);
283 }],
284 cmd_UMODE: [false, 1, 0, function(args) {
285 this.send("MODE " + this.parentObject.getNickname() + (args?(" " + args[0]):""));
286 }],
287 cmd_CLEAR: [false, undefined, undefined, function(args) {
288 var w = this.getActiveWindow().lines;
289 while(w.childNodes.length > 0)
290 w.removeChild(w.firstChild);
291 }],
292 cmd_PART: [false, 2, 0, function(args) {
293 var w = this.getActiveWindow();
294 var message = "";
295 var channel;
296
297 if(w.type != qwebirc.ui.WINDOW_CHANNEL) {
298 if(!args || args.length == 0) {
299 w.errorMessage("Insufficient arguments for command.");
300 return;
301 }
302 channel = args[0];
303 if(args.length > 1)
304 message = args[1];
305 } else {
306 if(!args || args.length == 0) {
307 channel = w.name;
308 } else {
309 var isChan = this.parentObject.isChannel(args[0]);
310 if(isChan) {
311 channel = args[0];
312 if(args.length > 1)
313 message = args[1];
314 } else {
315 channel = w.name;
316 message = args.join(" ");
317 }
318 }
319 }
320
321 this.send("PART " + channel + " :" + message);
322 }]
323 });