| 1 | var _ = require('lodash');\r |
| 2 | \r |
| 3 | \r |
| 4 | \r |
| 5 | \r |
| 6 | var ClientCommands = function (client) {\r |
| 7 | this.client = client;\r |
| 8 | };\r |
| 9 | module.exports = ClientCommands;\r |
| 10 | \r |
| 11 | ClientCommands.prototype.run = function (command, args, irc_connection, callback) {\r |
| 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 |
| 23 | /**\r |
| 24 | * Truncate a string into blocks of a set size\r |
| 25 | */\r |
| 26 | function 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 |
| 42 | var listeners = {\r |
| 43 | PRIVMSG: function (args, irc_connection, callback) {\r |
| 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 |
| 47 | \r |
| 48 | var blocks = truncateString(args.msg, 350);\r |
| 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 |
| 56 | });\r |
| 57 | },\r |
| 58 | \r |
| 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 |
| 87 | PART: function (args, irc_connection, callback) {\r |
| 88 | if (args.channel) {\r |
| 89 | _.each(args.channel.split(","), function (chan) {\r |
| 90 | irc_connection.write('PART ' + chan, callback);\r |
| 91 | });\r |
| 92 | }\r |
| 93 | },\r |
| 94 | \r |
| 95 | \r |
| 96 | TOPIC: function (args, irc_connection, callback) {\r |
| 97 | if (args.channel) {\r |
| 98 | if (args.topic) {\r |
| 99 | irc_connection.write('TOPIC ' + args.channel + ' :' + args.topic, callback);\r |
| 100 | } else {\r |
| 101 | irc_connection.write('TOPIC ' + args.channel, callback);\r |
| 102 | }\r |
| 103 | }\r |
| 104 | },\r |
| 105 | \r |
| 106 | \r |
| 107 | KICK: function (args, irc_connection, callback) {\r |
| 108 | if ((args.channel) && (args.nick)) {\r |
| 109 | irc_connection.write('KICK ' + args.channel + ' ' + args.nick + ' :' + args.reason, callback);\r |
| 110 | }\r |
| 111 | },\r |
| 112 | \r |
| 113 | \r |
| 114 | QUIT: function (args, irc_connection, callback) {\r |
| 115 | websocket.ircConnection.end('QUIT :' + args.message + '\r\n');\r |
| 116 | websocket.sentQUIT = true;\r |
| 117 | websocket.ircConnection.destroySoon();\r |
| 118 | websocket.disconnect();\r |
| 119 | },\r |
| 120 | \r |
| 121 | \r |
| 122 | NOTICE: function (args, irc_connection, callback) {\r |
| 123 | // Maximum length of target + message we can send to the IRC server is 500 characters\r |
| 124 | // but we need to leave extra room for the sender prefix so the entire message can\r |
| 125 | // be sent from the IRCd to the target without being truncated.\r |
| 126 | \r |
| 127 | var blocks = truncateString(args.msg, 350);\r |
| 128 | blocks.forEach(function (block, idx) {\r |
| 129 | // Apply the callback on the last message only\r |
| 130 | var cb = (idx === blocks.length - 1) ?\r |
| 131 | callback :\r |
| 132 | undefined;\r |
| 133 | \r |
| 134 | irc_connection.write('NOTICE ' + args.target + ' :' + block, cb);\r |
| 135 | });\r |
| 136 | },\r |
| 137 | \r |
| 138 | \r |
| 139 | MODE: function (args, irc_connection, callback) {\r |
| 140 | if ((args.target) && (args.mode)) {\r |
| 141 | irc_connection.write('MODE ' + args.target + ' ' + args.mode + ' ' + args.params, callback);\r |
| 142 | }\r |
| 143 | },\r |
| 144 | \r |
| 145 | \r |
| 146 | NICK: function (args, irc_connection, callback) {\r |
| 147 | if (args.nick) {\r |
| 148 | irc_connection.write('NICK ' + args.nick, callback);\r |
| 149 | }\r |
| 150 | },\r |
| 151 | \r |
| 152 | \r |
| 153 | KIWI: function (args, irc_connection, callback) {\r |
| 154 | if ((args.target) && (args.data)) {\r |
| 155 | irc_connection.write('PRIVMSG ' + args.target + ': ' + String.fromCharCode(1) + 'KIWI ' + args.data + String.fromCharCode(1), callback);\r |
| 156 | }\r |
| 157 | },\r |
| 158 | \r |
| 159 | ENCODING: function (args, irc_connection, callback) {\r |
| 160 | if (args.encoding) {\r |
| 161 | return callback(irc_connection.setEncoding(args.encoding));\r |
| 162 | }\r |
| 163 | }\r |
| 164 | };\r |