diff --git a/.gitignore b/.gitignore index d16ab05..718b640 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,31 @@ -# Don't commit +# Logs +logs +*.log -# Installed Node Modules. Commit the package.json instead -node_modules/ +# Runtime data +pids +*.pid +*.seed -# Bot Config: Contains highly sensitive data, NEVER Commit -newxkitbot.conf.js +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Bot storage and configuration +storage +config/local* + +# Dependency directory +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git +node_modules diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e103f30 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,283 @@ +GNU GENERAL PUBLIC LICENSE +========================== +Version 2, June 1991 + +> Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +# Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + +# GNU GENERAL PUBLIC LICENSE # +## TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION ## + +- *0\.* This License applies to any program or other work which contains + a notice placed by the copyright holder saying it may be distributed + under the terms of this General Public License. The "Program", below, + refers to any such program or work, and a "work based on the Program" + means either the Program or any derivative work under copyright law: + that is to say, a work containing the Program or a portion of it, + either verbatim or with modifications and/or translated into another + language. (Hereinafter, translation is included without limitation in + the term "modification".) Each licensee is addressed as "you". + + Activities other than copying, distribution and modification are not + covered by this License; they are outside its scope. The act of + running the Program is not restricted, and the output from the Program + is covered only if its contents constitute a work based on the + Program (independent of having been made by running the Program). + Whether that is true depends on what the Program does. + +- 1\. You may copy and distribute verbatim copies of the Program's + source code as you receive it, in any medium, provided that you + conspicuously and appropriately publish on each copy an appropriate + copyright notice and disclaimer of warranty; keep intact all the + notices that refer to this License and to the absence of any warranty; + and give any other recipients of the Program a copy of this License + along with the Program. + + You may charge a fee for the physical act of transferring a copy, and + you may at your option offer warranty protection in exchange for a fee. + +- 2\. You may modify your copy or copies of the Program or any portion + of it, thus forming a work based on the Program, and copy and + distribute such modifications or work under the terms of Section 1 + above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + + These requirements apply to the modified work as a whole. If + identifiable sections of that work are not derived from the Program, + and can be reasonably considered independent and separate works in + themselves, then this License, and its terms, do not apply to those + sections when you distribute them as separate works. But when you + distribute the same sections as part of a whole which is a work based + on the Program, the distribution of the whole must be on the terms of + this License, whose permissions for other licensees extend to the + entire whole, and thus to each and every part regardless of who wrote it. + + Thus, it is not the intent of this section to claim rights or contest + your rights to work written entirely by you; rather, the intent is to + exercise the right to control the distribution of derivative or + collective works based on the Program. + + In addition, mere aggregation of another work not based on the Program + with the Program (or with a work based on the Program) on a volume of + a storage or distribution medium does not bring the other work under + the scope of this License. + +- 3\. You may copy and distribute the Program (or a work based on it, + under Section 2) in object code or executable form under the terms of + Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + + The source code for a work means the preferred form of the work for + making modifications to it. For an executable work, complete source + code means all the source code for all modules it contains, plus any + associated interface definition files, plus the scripts used to + control compilation and installation of the executable. However, as a + special exception, the source code distributed need not include + anything that is normally distributed (in either source or binary + form) with the major components (compiler, kernel, and so on) of the + operating system on which the executable runs, unless that component + itself accompanies the executable. + + If distribution of executable or object code is made by offering + access to copy from a designated place, then offering equivalent + access to copy the source code from the same place counts as + distribution of the source code, even though third parties are not + compelled to copy the source along with the object code. + +- 4\. You may not copy, modify, sublicense, or distribute the Program + except as expressly provided under this License. Any attempt + otherwise to copy, modify, sublicense or distribute the Program is + void, and will automatically terminate your rights under this License. + However, parties who have received copies, or rights, from you under + this License will not have their licenses terminated so long as such + parties remain in full compliance. + +- 5\. You are not required to accept this License, since you have not + signed it. However, nothing else grants you permission to modify or + distribute the Program or its derivative works. These actions are + prohibited by law if you do not accept this License. Therefore, by + modifying or distributing the Program (or any work based on the + Program), you indicate your acceptance of this License to do so, and + all its terms and conditions for copying, distributing or modifying + the Program or works based on it. + +- 6\. Each time you redistribute the Program (or any work based on the + Program), the recipient automatically receives a license from the + original licensor to copy, distribute or modify the Program subject to + these terms and conditions. You may not impose any further + restrictions on the recipients' exercise of the rights granted herein. + You are not responsible for enforcing compliance by third parties to + this License. + +- 7\. If, as a consequence of a court judgment or allegation of patent + infringement or for any other reason (not limited to patent issues), + conditions are imposed on you (whether by court order, agreement or + otherwise) that contradict the conditions of this License, they do not + excuse you from the conditions of this License. If you cannot + distribute so as to satisfy simultaneously your obligations under this + License and any other pertinent obligations, then as a consequence you + may not distribute the Program at all. For example, if a patent + license would not permit royalty-free redistribution of the Program by + all those who receive copies directly or indirectly through you, then + the only way you could satisfy both it and this License would be to + refrain entirely from distribution of the Program. + + If any portion of this section is held invalid or unenforceable under + any particular circumstance, the balance of the section is intended to + apply and the section as a whole is intended to apply in other + circumstances. + + It is not the purpose of this section to induce you to infringe any + patents or other property right claims or to contest validity of any + such claims; this section has the sole purpose of protecting the + integrity of the free software distribution system, which is + implemented by public license practices. Many people have made + generous contributions to the wide range of software distributed + through that system in reliance on consistent application of that + system; it is up to the author/donor to decide if he or she is willing + to distribute software through any other system and a licensee cannot + impose that choice. + + This section is intended to make thoroughly clear what is believed to + be a consequence of the rest of this License. + +- 8\. If the distribution and/or use of the Program is restricted in + certain countries either by patents or by copyrighted interfaces, the + original copyright holder who places the Program under this License + may add an explicit geographical distribution limitation excluding + those countries, so that distribution is permitted only in or among + countries not thus excluded. In such case, this License incorporates + the limitation as if written in the body of this License. + +- 9\. The Free Software Foundation may publish revised and/or new versions + of the General Public License from time to time. Such new versions will + be similar in spirit to the present version, but may differ in detail to + address new problems or concerns. + + Each version is given a distinguishing version number. If the Program + specifies a version number of this License which applies to it and "any + later version", you have the option of following the terms and conditions + either of that version or of any later version published by the Free + Software Foundation. If the Program does not specify a version number of + this License, you may choose any version ever published by the Free Software + Foundation. + +- 10\. If you wish to incorporate parts of the Program into other free + programs whose distribution conditions are different, write to the author + to ask for permission. For software which is copyrighted by the Free + Software Foundation, write to the Free Software Foundation; we sometimes + make exceptions for this. Our decision will be guided by the two goals + of preserving the free status of all derivatives of our free software and + of promoting the sharing and reuse of software generally. + + +## NO WARRANTY + +- 11\. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY + FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN + OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES + PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED + OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS + TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE + PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, + REPAIR OR CORRECTION. + +- 12\. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING + WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR + REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, + INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING + OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED + TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY + YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER + PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE + POSSIBILITY OF SUCH DAMAGES. + +## END OF TERMS AND CONDITIONS ## diff --git a/README.md b/README.md index 86f56fc..19f4a94 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,24 @@ # NewXKitBot +[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/new-xkit/NewXKitBot?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/new-xkit/NewXKitBot?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) \ No newline at end of file +### What is NewXKitBot? +NewXKitBot is [New XKit](https://github.com/new-xkit/XKit) Team's little assistant, written in JavaScript for [Node.js](https://nodejs.org/). It's here to make the lives of the New-XKit team just a tiny bit easier! + +### What does it do? +For now, the bot has 1 primary feature, which is bridging the support IRC channel on freenode to the gitter support channel. +More functionality is might be added in the future, depending on our needs. + +### How do I run it myself? +1. Install [Node.js](https://nodejs.org/) +2. Clone this repository into a folder +3. Copy the `default.js` file in the `config` subfolder and rename it to `local.js` +4. Change whatever you want in the `local.js` and remove whatever values you left as default +5. Run the bot with `node main.js` + +### How do I contribute? +1. Fork the repository +2. Make your changes in your forked repository +3. Open a Pull Request with your changes + +### License +This software is licensed under GPL-2.0. See LICENSE.md diff --git a/config/default.js b/config/default.js new file mode 100644 index 0000000..8e2cb24 --- /dev/null +++ b/config/default.js @@ -0,0 +1,189 @@ +/* + A multi-functional bot for the NXKit Team + Copyright (C) 2015 Wolvan + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +module.exports = { + GitterIRCBridges: [ + { + gitter: { + token: process.env.NXB_GITTER_TOKEN, + channel: process.env.NXB_GITTER_CHANNEL + }, + irc: { + host: "chat.freenode.net", + port: 7000, + nick: process.env.NXB_IRC_NICK, + password: "None", + channel: process.env.NXB_IRC_CHANNEL, + welcome_message: + "Welcome to the Live Support for New XKit. If you did not yet set a username, please use /nick to do so." + + "\nJust go ahead and ask whatever questions you have! If we do not respond immediately, please be patient. We might be away/busy/asleep. " + + "Once we have time, we'll get back to you.", + ssl: true + }, + github: { + org: process.env.NXB_GITHUB_ORG, + repo: process.env.NXB_GITHUB_REPO, + auth: process.env.NXB_GITHUB_TOKEN + }, + cmd_prefix: "!", + commands_gitter: { + list: { + not_elevated: true, + run: function(user, args, gitter, irc) { + var users = irc.getUsers(); + var msg = ""; + if (users.length <= 1) { + msg = "[GitterBridge] There are no users online."; + } else { + users.map(function(value, index, array) { + if (value === irc.getUsername()) { + users.splice(index, 1); + } + }); + msg = "[GitterBridge] There are " + users.length + " User(s) online: " + users.join(", "); + } + gitter.say(msg); + } + }, + whois: { + not_elevated: true, + run: function(user, args, gitter, irc) { + irc.whois(args[0], function(username, whoisinfo) { + gitter.say("[GitterBridge] Whois for " + username + ":\n```\n" + JSON.stringify(whoisinfo, null, "\t") + "\n```"); + }.bind(this, user)); + } + }, + xcloud: { + not_elevated: true, + run: function(user, args, gitter, irc) { + var msg = "XCloud Migration Guide: http://portal.new-xkit.com/assets/migration/xcloud-migration.html"; + if (!args[0]) { + irc.send(msg); + } else { + irc.notice(args[0], msg); + } + gitter.say("[GitterBridge] Migration guide linked."); + } + }, + chrome: { + not_elevated: true, + run: function(user, args, gitter, irc) { + var msg = "New XKit Download for Google Chrome: http://new-xkit-extension.tumblr.com/chrome"; + if (!args[0]) { + irc.send(msg); + } else { + irc.notice(args[0], msg); + } + gitter.say("[GitterBridge] Chrome download linked."); + } + }, + firefox: { + not_elevated: true, + run: function(user, args, gitter, irc) { + var msg = "New XKit Download for Mozilla Firefox: http://new-xkit-extension.tumblr.com/firefox"; + if (!args[0]) { + irc.send(msg); + } else { + irc.notice(args[0], msg); + } + gitter.say("[GitterBridge] Firefox download linked."); + } + }, + edump: { + not_elevated: true, + run: function(user, args, gitter, irc) { + var msg = "How to create an extension dump:\n" + + "1.) Go to your XKit Control Panel\n" + + "2.) Go to the 'Other' tab\n" + + "3.) Click on 'Export Configuration'\n" + + "4.) Depending on what we ask for, click either 'Extension Info Export' (this is what we need most of the time) or 'Full Configuration Export'\n" + + "5.) Copy the https://gist.github.com/ link it generates and give it to us\n"; + if (!args[0]) { + irc.send(msg); + } else { + irc.notice(args[0], msg); + } + gitter.say("[GitterBridge] Extension dump guide sent."); + } + }, + help: { + not_elevated: true, + run: function(user, args, gitter, irc) { + gitter.say( + "#Commands\n" + + "**!list** - List all online users\n" + + "**!kick** [reason] - Kicks the user from the channel\n" + + "**!ban** - Ban the Hostname of the user\n" + + "**!whois** - Retrieve whois information\n" + + "**!topic** [topic] - Get or set the IRC Channel topic\n" + + "**!xcloud** [nick] - Link the XCloud migration guide [to a user]\n" + + "**!chrome** [nick] - Link the Chrome download link [to a user]\n" + + "**!firefox** [nick] - Link the Firefox download link [to a user]\n" + + "**!edump** [nick] - Print a step by step guide to dump extensions [to a user]\n" + + "**!help** - Print this help message\n" + + "\nCredits:\nGitterBridge v2.0 written by **Wolvan**" + ); + } + }, + + kick: { + run: function(user, args, gitter, irc) { + if (args.length < 1) { + gitter.say("[GitterBridge] Usage: `!kick [reason]`") + } else { + irc.kick(args.splice(0,1)[0], args.join(" ")); + } + }, + not_allowed: function(user, gitter, irc) { + gitter.say("[GitterBridge] You do not have access to the `kick` command, " + user); + } + }, + ban: { + run: function(user, args, gitter, irc) { + if (args.length < 1) { + gitter.say("[GitterBridge] Usage: `!ban `") + } else { + irc.ban(args.splice(0,1)[0], args.join(" "), function(username, gitter) { + gitter.say("**" + username + " has been banned**"); + }.bind(this, user, gitter)); + } + }, + not_allowed: function(user, gitter, irc) { + gitter.say("[GitterBridge] You do not have access to the `ban` command, " + user); + } + }, + topic: { + run: function(user, args, gitter, irc) { + if (args.length < 1) { + gitter.say("[GitterBridge] Current topic: " + irc.getTopic()); + } else { + irc.setTopic(args.join(" "), function(new_topic, gitter) { + gitter.say("[GitterBridge] Channel Topic changed to '" + new_topic + "'"); + }.bind(this, args.join(" "), gitter)); + } + }, + not_allowed: function(user, gitter, irc) { + gitter.say("[GitterBridge] You do not have access to the `topic` command, " + user); + } + } + }, + commands_irc: {} + } + ] +} diff --git a/main.js b/main.js index 4b049f6..834f07e 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,46 @@ -// Load Config -Config = require("./newxkitbot.conf.js"); +/* + A multi-functional bot for the NXKit Team + Copyright (C) 2015 Wolvan -// Start GitterBridge -GitterBridge = new require("./modules/GitterBridge.js")(Config.GitterBridge); + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +console.log("NewXKitBot version 2, Copyright (C) 2015 Wolvan\nNewXKitBot comes with ABSOLUTELY NO WARRANTY\n\n"); +console.log("Starting up NewXKitBot v2.0 by Wolvan...\n"); + +var GitterIRCBridge = require("./modules/GitterIRCBridge.js"); + +function loadConfig() { + console.log("Loading configuration...\n"); + var extend = require("extend"); + var default_config = require("./config/default.js"); + var config = require("./config/local.js"); + for (x in config.GitterIRCBridges) { + extend(true, config.GitterIRCBridges[x], default_config.GitterIRCBridges[x] || default_config.GitterIRCBridges[0]); + } + return config; +} + +var config = loadConfig(); + + +console.log("Starting Gitter-IRC Bridges..."); +var girc_bridges = []; +for (x in config.GitterIRCBridges) { + setTimeout(function(x) { + console.log("Starting bridge " + x + ": " + config.GitterIRCBridges[x].gitter.channel + " <-> " + config.GitterIRCBridges[x].irc.channel); + girc_bridges[x] = new GitterIRCBridge(config.GitterIRCBridges[x]); + }.bind(this, x), x * 1000); +} diff --git a/modules/GitterBridge.js b/modules/GitterBridge.js deleted file mode 100644 index b759238..0000000 --- a/modules/GitterBridge.js +++ /dev/null @@ -1,38 +0,0 @@ -var irc = require('irc'); - -module.exports = function(Config) { - if (Config.ENABLED) { - this.gitter = new irc.Client('irc.gitter.im', Config.Gitter_Username, { - channels: [Config.Gitter_Channel], - secure: true, - password: Config.Gitter_Password, - autoRejoin: true, - }); - this.freenode = new irc.Client("chat.freenode.net", Config.IRC_Username, { - channels: [Config.IRC_Channel], - autoRejoin: true, - }); - - this.gitter.addListener('message' + Config.Gitter_Channel, function (from, message) { - freenode.say(Config.Gitter_Channel, "<" + from + "> " + message); - }); - - this.freenode.addListener('message' + Config.IRC_Channel, function (from, message) { - gitter.say(Config.IRC_Channel, "`" + from + "` " + message); - }); - - this.freenode.addListener('registered', function(message) { - freenode.say("nickserv", "identify " + Config.IRC_Password); - }) - - this.gitter.addListener('error', function(message) { - console.log('Gitter Error: ', message); - }); - - this.freenode.addListener('error', function(message) { - console.log('Freenode Error: ', message); - }); - } - - return this; -} diff --git a/modules/GitterChat.js b/modules/GitterChat.js new file mode 100644 index 0000000..d8dc638 --- /dev/null +++ b/modules/GitterChat.js @@ -0,0 +1,80 @@ +/* + A multi-functional bot for the NXKit Team + Copyright (C) 2015 Wolvan + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +var Gitter = require('node-gitter'); + +function GitterChat(gitter_token, channel, cmd_prefix) { + + if (!gitter_token || !channel) { throw new Error("Gitter Token or Room not specified!") } + if (!cmd_prefix) { cmd_prefix = "!"; } + + var self = this; + var username = ""; + + self.client = new Gitter(gitter_token); + + self.client.currentUser().then(function(user) { + username = user.username; + }); + + self.client.rooms.join(channel) + .then(function (room) { + var events = room.streaming().chatMessages(); + events.on("chatMessages", function(message) { + self.emit("chatMessages", message); + if (message.operation === "patch") { return; } + if (message.model.fromUser.username === username) { return; } + if (message.model.text.startsWith(cmd_prefix)) { + var cmd_string = message.model.text.substring(1); + var cmd_array = cmd_string.split(" "); + var command = cmd_array.splice(0,1)[0].toLowerCase(); + + self.emit("command", command, message.model.fromUser.username, cmd_array, message); + self.emit("command#" + command, message.model.fromUser.username, cmd_array, message); + } else { + var event_name; + if (message.operation === "create") { + event_name = "message" + } else if (message.operation === "update") { + event_name = "message_update" + } + self.emit(event_name, message.model.fromUser.username, message.model.text, message); + } + }); + }).fail(function(error) { + self.emit("error", error); + }); + + self.say = function(message) { + self.client.rooms.join(channel) + .then(function(room) { + room.send(message); + }).fail(function(err) { + self.emit("error", err); + }); + } + + self.getUsername = function() { + return username; + } +} + +require("util").inherits(GitterChat, require('events').EventEmitter); + +module.exports = GitterChat; diff --git a/modules/GitterIRCBridge.js b/modules/GitterIRCBridge.js new file mode 100644 index 0000000..5be4350 --- /dev/null +++ b/modules/GitterIRCBridge.js @@ -0,0 +1,125 @@ +/* + A multi-functional bot for the NXKit Team + Copyright (C) 2015 Wolvan + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +var GitterChat = require("./GitterChat"); +var IRCChat = require("./IRCChat"); +var octonode = require("octonode"); + +function transform_message(msg, github_org, github_repo) { + // In gitter we can do org_or_username/repo#IssueNr., the following replace turns all these into proper links for the irc + msg = msg.replace(new RegExp(github_org + "/" + github_repo + "#", "gi"), "https://github.com/" + github_org + "/" + github_repo + "/issues/"); + + // This regex checks if the last message was an image by parsing the markdown in the following format + // [![FILENAME](IMAGE_URL)](IMAGE_URL) + if (msg.match(/\[!\[(.*?)]/gi)) { // First we check if [![*] exists, which indicates an image being shared + var img_name = msg.match(/\[!\[(.*?)]/gi)[0]; // We grab the filename here + img_name = img_name.substring(3, img_name.length - 1); // and then we remove the [![ in the beginning and the ] in the end to get our filename + var img_url = msg.match(/]\((.*?)\)]/gi)[0]; // Same thing happening here, we get the image url by grabbing everything between ]( and )] + img_url = img_url.substring(2, img_url.length - 2); // and remove the ]( and )] + msg = "shared '" + img_name + "': " + img_url; // using that information we put together the message + } + return msg; +} + +function Bridge(bridge_config) { + var self = this; + + if (!bridge_config || typeof bridge_config !== "object") { throw new Error("A bridge config must be specified in form of an object"); } + + var github = octonode.client(bridge_config.github.auth); + + var gitter = new GitterChat( + bridge_config.gitter.token, + bridge_config.gitter.channel, + bridge_config.cmd_prefix + ); + var irc = new IRCChat( + bridge_config.irc.host, + bridge_config.irc.port, + bridge_config.irc.nick, + bridge_config.irc.password, + bridge_config.irc.channel, + bridge_config.irc.welcome_message, + bridge_config.irc.ssl || false, + bridge_config.cmd_prefix + ); + + gitter.on("message", function(from, msg) { + msg = transform_message(msg); + irc.say(from, msg); + }).on("message_update", function(from, msg) { + msg = transform_message(msg); + irc.say(from, "[EDIT] " + msg); + }); + irc.on("message", function(from, msg) { + gitter.say("`" + from + "` " + msg); + }); + + irc.on("nick_changed", function(old_nick, new_nick) { + gitter.say("*" + old_nick + "* is now known as *" + new_nick + "*.") + }).on("join", function(nick) { + gitter.say("*" + nick + " has joined.*"); + }).on("kick", function(nick, by, reason) { + gitter.say("*" + nick + " has been kicked by " + by + " for '" + reason + "'.*"); + }).on("part", function(nick, reason) { + gitter.say("*" + nick + " has left.*"); + }).on("quit", function(nick, reason, channels) { + gitter.say("*" + nick + " has quit.*"); + }).on("kill", function(nick, reason, channels) { + gitter.say("*" + nick + " was killed by the IRC network for '" + reason + "'.*"); + }); + + irc.on("error", function(err) { + console.log(err); + }); + gitter.on("error", function(err) { + console.log(err); + }); + + for (var x in bridge_config.commands_gitter) { + gitter.on("command#" + x, function(command_data, from, args) { + if (command_data.not_elevated) { + command_data.run(from, args, gitter, irc); + } else { + github.org(bridge_config.github.org).member(from, function(user, args, command, error, isOrgMember, headers) { + if(isOrgMember) { + command.run(user, args, gitter, irc); + } else { + command.not_allowed(user, gitter, irc); + } + }.bind(this, from, args, command_data)); + } + }.bind(this, bridge_config.commands_gitter[x])); + } + for (var y in bridge_config.commands_irc) { + irc.on("command#" + y, function(command_data, from, args) { + if (command_data.not_elevated) { + command_data.run(from, args, irc, gitter); + } else { + if(irc.getUserRank(from) === "@") { + command_data.run(from, args, irc, gitter); + } else { + command_data.not_allowed(from, irc, gitter); + } + } + }.bind(this, bridge_config.commands_irc[y])); + } +} + +module.exports = Bridge; diff --git a/modules/IRCChat.js b/modules/IRCChat.js new file mode 100644 index 0000000..d7c9077 --- /dev/null +++ b/modules/IRCChat.js @@ -0,0 +1,156 @@ +/* + A multi-functional bot for the NXKit Team + Copyright (C) 2015 Wolvan + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +var irc = require('irc'); + +var IRCConnections = {}; + +function IRCChat(host, server_port, name, pass, channel, WelcomeMsg, isSecure, cmd_prefix) { + + var self = this; + var users = []; + var userRanks = {}; + var topic = ""; + + if (!host || !server_port || !name || !pass || !channel || !WelcomeMsg) { throw new Error("Missing argument! You need to specify host, port, name, channel and welcome message!"); } + if (!cmd_prefix) { cmd_prefix = "!"; } + if (typeof isSecure === "undefined") { isSecure = false; } + if (!channel.startsWith("#")) { channel = "#" + channel; } + + + if (typeof IRCConnections[host + ":" + server_port + "_" + name] === "undefined") { + IRCConnections[host + ":" + server_port + "_" + name] = new irc.Client(host, name, { + port: server_port, + userName: "NewXKitBot", + realName: "New-XKit Bot", + secure: isSecure, + autoRejoin: true + }).on('registered', function(message) { + this.say("nickserv", "identify " + pass); + irc_con.join(channel); + }).on("error", function(err) { + if (err.rawCommand !== "451") { + self.emit("error", err); + } + }); + } else { + IRCConnections[host + ":" + server_port + "_" + name].join(channel); + } + var irc_con = IRCConnections[host + ":" + server_port + "_" + name]; + + irc_con.on('names' + channel, function(nicks) { + users = Object.keys(nicks); + userRanks = nicks; + }).on('topic', function(chan, chan_topic, nick, message) { + if(channel === chan) { + self.emit("topic_changed", topic, chan_topic); + topic = chan_topic; + } + }).on('nick', function(old, newnick, channels, message) { + if (name === newnick) { return; } + self.emit("nick_changed", old, newnick, channels); + }).on('message' + channel, function (from, message) { + self.emit("irc_message", from, message); + if (message.startsWith(cmd_prefix)) { + var cmd_args = message.substring(0,1).split(" "); + var command = cmd_args.splice(0,1)[0].toLowerCase(); + self.emit("command", command, from, cmd_args); + self.emit("command#" + command, from, cmd_args); + } else { + self.emit("message", from, message); + } + }).on('join' + channel, function(nick, message) { + if (name === nick) { return; } + irc_con.send("NAMES", channel); + irc_con.notice(nick, WelcomeMsg); + self.emit("join", nick); + }).on('kick' + channel, function(nick, by, reason, message) { + if (name === nick) { return; } + irc_con.send("NAMES", channel); + self.emit("kick", nick, by, reason); + }).on('part' + channel, function(nick, reason, message) { + if (name === nick) { return; } + irc_con.send("NAMES", channel); + self.emit("part", nick, reason); + }).on('quit', function(nick, reason, channels, message) { + if (name === nick) { return; } + irc_con.send("NAMES", channel); + self.emit("quit", nick, reason, channels); + }).on("kill", function(nick, reason, channels, message) { + if (name === nick) { return; } + irc_con.send("NAMES", channel); + self.emit("kill", nick, reason, channels); + }); + + self.say = function(from, message) { + irc_con.say(channel, "<" + from.substring(0,1) + '\u0081' + from.substring(1) + "> " + message); + } + self.notice = function(to, message) { + irc_con.notice(to, message); + } + self.send = function(message) { + irc_con.say(channel, message); + } + + self.getUsers = function() { + return users; + } + self.getUserRank = function(nick) { + return userRanks[nick] || ""; + } + + self.getUsername = function() { + return irc_con.nick; + } + + self.getTopic = function() { + return topic; + } + self.setTopic = function(new_topic, cb) { + if (!cb) { cb = function() {}; } + irc_con.send("TOPIC", channel, new_topic); + cb(); + } + + self.kick = function(user, reason, cb) { + if (!cb) { cb = function() {}; } + irc_con.send("KICK", channel, user, reason || "You have been kicked by an admin!"); + cb(); + } + + self.ban = function(user, cb) { + if (!cb) { cb = function() {}; } + irc_con.whois(user, function(whoisinfo) { + irc_con.send("MODE", channel, "+b", "*!*@" + whoisinfo.host); + cb(); + }); + } + + self.whois = function(user, cb) { + if (!cb) { cb = function() {}; } + irc_con.whois(user, function(whoisinfo) { + cb(whoisinfo); + }); + } + +} + +require("util").inherits(IRCChat, require('events').EventEmitter); + +module.exports = IRCChat; diff --git a/newxkitbot.conf.js b/newxkitbot.conf.js deleted file mode 100644 index c061bc7..0000000 --- a/newxkitbot.conf.js +++ /dev/null @@ -1,13 +0,0 @@ -module.exports = { - GitterBridge: { - ENABLED: true, - - Gitter_Channel: "", - Gitter_Username: "", - Gitter_Password: "", - - IRC_Channel: "", - IRC_Username: "", - IRC_Password: "", - }, -} diff --git a/package.json b/package.json index 79a655e..9d1e7c3 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,32 @@ { "name": "newxkitbot", - "version": "1.0.0", - "description": "A bot that acomplishes multiple tasks for the new-xkit team", + "version": "2.0.0", + "description": "The little helping hand for the New-XKit Team", "main": "main.js", "dependencies": { - "irc": "^0.3.12" + "extend": "^3.0.0", + "irc": "^0.4.0", + "node-gitter": "^1.2.8", + "octonode": "^0.7.4" }, "devDependencies": {}, "scripts": { - "test": "node main.js" + "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "git+https://github.com/new-xkit/NewXKitBot.git" }, "keywords": [ - "XKit", - "new-xkit", - "bot" + "bot", + "new", + "xkit", + "gitter", + "irc", + "bridge" ], - "author": "Wolvan", - "license": "GPL-3.0", + "author": "Wolvan (http://www.github.com/Wolvan)", + "license": "GPL-2.0", "bugs": { "url": "https://github.com/new-xkit/NewXKitBot/issues" },