TLS connection fixes
[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 CHANNEL_INFO: function (args, irc_connection, callback) {
88 if (args.channel) {
89 irc_connection.write('MODE ' + args.channel, callback);
90 }
91 },
92
93
94 PART: function (args, irc_connection, callback) {
95 if (args.channel) {
96 _.each(args.channel.split(","), function (chan) {
97 irc_connection.write('PART ' + chan, callback);
98 });
99 }
100 },
101
102
103 TOPIC: function (args, irc_connection, callback) {
104 if (args.channel) {
105 if (args.topic) {
106 irc_connection.write('TOPIC ' + args.channel + ' :' + args.topic, callback);
107 } else {
108 irc_connection.write('TOPIC ' + args.channel, callback);
109 }
110 }
111 },
112
113
114 KICK: function (args, irc_connection, callback) {
115 if ((args.channel) && (args.nick)) {
116 irc_connection.write('KICK ' + args.channel + ' ' + args.nick + ' :' + args.reason, callback);
117 }
118 },
119
120
121 QUIT: function (args, irc_connection, callback) {
122 websocket.ircConnection.end('QUIT :' + args.message + '\r\n');
123 websocket.sentQUIT = true;
124 websocket.ircConnection.destroySoon();
125 websocket.disconnect();
126 },
127
128
129 NOTICE: function (args, irc_connection, callback) {
130 // Maximum length of target + message we can send to the IRC server is 500 characters
131 // but we need to leave extra room for the sender prefix so the entire message can
132 // be sent from the IRCd to the target without being truncated.
133
134 var blocks = truncateString(args.msg, 350);
135 blocks.forEach(function (block, idx) {
136 // Apply the callback on the last message only
137 var cb = (idx === blocks.length - 1) ?
138 callback :
139 undefined;
140
141 irc_connection.write('NOTICE ' + args.target + ' :' + block, cb);
142 });
143 },
144
145
146 MODE: function (args, irc_connection, callback) {
147 if ((args.target) && (args.mode)) {
148 irc_connection.write('MODE ' + args.target + ' ' + args.mode + ' ' + args.params, callback);
149 }
150 },
151
152
153 NICK: function (args, irc_connection, callback) {
154 if (args.nick) {
155 irc_connection.write('NICK ' + args.nick, callback);
156 }
157 },
158
159
160 KIWI: function (args, irc_connection, callback) {
161 if ((args.target) && (args.data)) {
162 irc_connection.write('PRIVMSG ' + args.target + ': ' + String.fromCharCode(1) + 'KIWI ' + args.data + String.fromCharCode(1), callback);
163 }
164 },
165
166 ENCODING: function (args, irc_connection, callback) {
167 if (args.encoding) {
168 return callback(irc_connection.setEncoding(args.encoding));
169 }
170 }
171 };