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