Client loading script in correct order
[KiwiIRC.git] / server / clientcommands.js
1 var _ = require('lodash');
2
3
4
5
6 var ClientCommands = function (client) {
7 this.client = client;
8 };
9 module.exports = ClientCommands;
10
11 ClientCommands.prototype.run = function (command, args, irc_connection, callback) {
12 // Do we have a function to handle this command?
13 if (!listeners[command.toUpperCase()]) {
14 return;
15 }
16
17 return listeners[command.toUpperCase()](args, irc_connection, callback);
18 };
19
20
21
22
23 /**
24 * Truncate a string into blocks of a set size
25 */
26 function truncateString(str, block_size) {
27 block_size = block_size || 350;
28
29 var blocks = [],
30 current_pos;
31
32 for (current_pos = 0; current_pos < str.length; current_pos = current_pos + block_size) {
33 blocks.push(str.substr(current_pos, block_size));
34 }
35
36 return blocks;
37 }
38
39
40
41
42 var listeners = {
43 PRIVMSG: function (args, irc_connection, callback) {
44 // Maximum length of target + message we can send to the IRC server is 500 characters
45 // but we need to leave extra room for the sender prefix so the entire message can
46 // be sent from the IRCd to the target without being truncated.
47
48 var blocks = truncateString(args.msg, 350);
49 blocks.forEach(function (block, idx) {
50 // Apply the callback on the last message only
51 var cb = (idx === blocks.length - 1) ?
52 callback :
53 undefined;
54
55 irc_connection.write('PRIVMSG ' + args.target + ' :' + block, cb);
56 });
57 },
58
59
60 CTCP: function (args, irc_connection, callback) {
61 if ((args.target) && (args.type)) {
62 if (args.request) {
63 irc_connection.write('PRIVMSG ' + args.target + ' :' + String.fromCharCode(1) + args.type.toUpperCase() + ' ' + args.params + String.fromCharCode(1), callback);
64 } else {
65 irc_connection.write('NOTICE ' + args.target + ' :' + String.fromCharCode(1) + args.type.toUpperCase() + ' ' + args.params + String.fromCharCode(1), callback);
66 }
67 }
68 },
69
70
71 RAW: function (args, irc_connection, callback) {
72 irc_connection.write(args.data, callback);
73 },
74
75
76 JOIN: function (args, irc_connection, callback) {
77 if (args.channel) {
78 channels = args.channel.split(",");
79 keys = (args.key) ? args.key.split(",") : [];
80 _.each(channels, function (chan, index) {
81 irc_connection.write('JOIN ' + chan + ' ' + (keys[index] || ''), callback);
82 });
83 }
84 },
85
86
87 PART: function (args, irc_connection, callback) {
88 if (args.channel) {
89 _.each(args.channel.split(","), function (chan) {
90 irc_connection.write('PART ' + chan, callback);
91 });
92 }
93 },
94
95
96 TOPIC: function (args, irc_connection, callback) {
97 if (args.channel) {
98 if (args.topic) {
99 irc_connection.write('TOPIC ' + args.channel + ' :' + args.topic, callback);
100 } else {
101 irc_connection.write('TOPIC ' + args.channel, callback);
102 }
103 }
104 },
105
106
107 KICK: function (args, irc_connection, callback) {
108 if ((args.channel) && (args.nick)) {
109 irc_connection.write('KICK ' + args.channel + ' ' + args.nick + ' :' + args.reason, callback);
110 }
111 },
112
113
114 QUIT: function (args, irc_connection, callback) {
115 websocket.ircConnection.end('QUIT :' + args.message + '\r\n');
116 websocket.sentQUIT = true;
117 websocket.ircConnection.destroySoon();
118 websocket.disconnect();
119 },
120
121
122 NOTICE: function (args, irc_connection, callback) {
123 // Maximum length of target + message we can send to the IRC server is 500 characters
124 // but we need to leave extra room for the sender prefix so the entire message can
125 // be sent from the IRCd to the target without being truncated.
126
127 var blocks = truncateString(args.msg, 350);
128 blocks.forEach(function (block, idx) {
129 // Apply the callback on the last message only
130 var cb = (idx === blocks.length - 1) ?
131 callback :
132 undefined;
133
134 irc_connection.write('NOTICE ' + args.target + ' :' + block, cb);
135 });
136 },
137
138
139 MODE: function (args, irc_connection, callback) {
140 if ((args.target) && (args.mode)) {
141 irc_connection.write('MODE ' + args.target + ' ' + args.mode + ' ' + args.params, callback);
142 }
143 },
144
145
146 NICK: function (args, irc_connection, callback) {
147 if (args.nick) {
148 irc_connection.write('NICK ' + args.nick, callback);
149 }
150 },
151
152
153 KIWI: function (args, irc_connection, callback) {
154 if ((args.target) && (args.data)) {
155 irc_connection.write('PRIVMSG ' + args.target + ': ' + String.fromCharCode(1) + 'KIWI ' + args.data + String.fromCharCode(1), callback);
156 }
157 },
158
159 ENCODING: function (args, irc_connection, callback) {
160 if (args.encoding) {
161 return callback(irc_connection.setEncoding(args.encoding));
162 }
163 }
164 };