Sending correct message length when truncated (privmsg/notice)
[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 var listeners = {
24 PRIVMSG: function (args, irc_connection, callback) {
25 // Maximum length of target + message we can send to the IRC server is 500 characters
26 // but we need to leave extra room for the sender prefix so the entire message can
27 // be sent from the IRCd to the target without being truncated.
28 var wrap_length = 350,
29 trunc_msg,
30 trunc_length,
31 message = args.msg;
32
33 if (args.target && (args.msg)) {
34 trunc_length = wrap_length - args.target.length;
35 // If the message is longer than wrap_length, send the message in chunks
36 while (message.length > trunc_length) {
37 trunc_msg = message.substr(0, trunc_length);
38 message = message.substr(trunc_length);
39 irc_connection.write('PRIVMSG ' + args.target + ' :' + trunc_msg);
40 }
41 // Send the remaining text
42 irc_connection.write('PRIVMSG ' + args.target + ' :' + message, callback);
43 }
44 },
45
46
47 CTCP: function (args, irc_connection, callback) {
48 if ((args.target) && (args.type)) {
49 if (args.request) {
50 irc_connection.write('PRIVMSG ' + args.target + ' :' + String.fromCharCode(1) + args.type.toUpperCase() + ' ' + args.params + String.fromCharCode(1), callback);
51 } else {
52 irc_connection.write('NOTICE ' + args.target + ' :' + String.fromCharCode(1) + args.type.toUpperCase() + ' ' + args.params + String.fromCharCode(1), callback);
53 }
54 }
55 },
56
57
58 RAW: function (args, irc_connection, callback) {
59 irc_connection.write(args.data, callback);
60 },
61
62
63 JOIN: function (args, irc_connection, callback) {
64 if (args.channel) {
65 channels = args.channel.split(",");
66 keys = (args.key) ? args.key.split(",") : [];
67 _.each(channels, function (chan, index) {
68 irc_connection.write('JOIN ' + chan + ' ' + (keys[index] || ''), callback);
69 });
70 }
71 },
72
73
74 PART: function (args, irc_connection, callback) {
75 if (args.channel) {
76 _.each(args.channel.split(","), function (chan) {
77 irc_connection.write('PART ' + chan, callback);
78 });
79 }
80 },
81
82
83 TOPIC: function (args, irc_connection, callback) {
84 if (args.channel) {
85 if (args.topic) {
86 irc_connection.write('TOPIC ' + args.channel + ' :' + args.topic, callback);
87 } else {
88 irc_connection.write('TOPIC ' + args.channel, callback);
89 }
90 }
91 },
92
93
94 KICK: function (args, irc_connection, callback) {
95 if ((args.channel) && (args.nick)) {
96 irc_connection.write('KICK ' + args.channel + ' ' + args.nick + ':' + args.reason, callback);
97 }
98 },
99
100
101 QUIT: function (args, irc_connection, callback) {
102 websocket.ircConnection.end('QUIT :' + args.message + '\r\n');
103 websocket.sentQUIT = true;
104 websocket.ircConnection.destroySoon();
105 websocket.disconnect();
106 },
107
108
109 NOTICE: function (args, irc_connection, callback) {
110 // Maximum length of target + message we can send to the IRC server is 500 characters
111 // but we need to leave extra room for the sender prefix so the entire message can
112 // be sent from the IRCd to the target without being truncated.
113 var wrap_length = 350,
114 trunc_msg,
115 trunc_length,
116 message = args.msg;
117
118 if (args.target && (args.msg)) {
119 trunc_length = wrap_length - args.target.length;
120 // If the message is longer than wrap_length, send the message in chunks
121 while (message.length > trunc_length) {
122 trunc_msg = message.substr(0, trunc_length);
123 message = message.substr(trunc_length);
124 irc_connection.write('NOTICE ' + args.target + ' :' + trunc_msg);
125 }
126 // Send the remaining text
127 irc_connection.write('NOTICE ' + args.target + ' :' + message, callback);
128 }
129 },
130
131
132 MODE: function (args, irc_connection, callback) {
133 if ((args.target) && (args.mode)) {
134 irc_connection.write('MODE ' + args.target + ' ' + args.mode + ' ' + args.params, callback);
135 }
136 },
137
138
139 NICK: function (args, irc_connection, callback) {
140 if (args.nick) {
141 irc_connection.write('NICK ' + args.nick, callback);
142 }
143 },
144
145
146 KIWI: function (args, irc_connection, callback) {
147 if ((args.target) && (args.data)) {
148 irc_connection.write('PRIVMSG ' + args.target + ': ' + String.fromCharCode(1) + 'KIWI ' + args.data + String.fromCharCode(1), callback);
149 }
150 }
151 };