Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var util = require('util');
- var Channels = {};
- var Users = {};
- var AUTH = {
- "+": "v",
- "%": "h",
- "@": "o",
- "&": "a",
- "~": "q"
- }
- // Object types
- var Channel = function Channel(name) {
- var channel = Object.create(Channel.prototype);
- channel.name = name.toLowerCase() || "#";
- channel.users = {};
- channel.topic = "";
- channel.topicHistory = []; // [Current, Previous, ... , First Known Topic]
- channel.mode = "";
- channel.modes = {};
- return channel;
- }
- Channel.prototype = {
- addUser: function (nick) {
- var auth = nick.charAt(0) in AUTH ? nick.charAt(0) : "";
- nick = auth ? nick.substr(1) : nick;
- this.users[nick] = nick;
- if (auth) {
- this.updateModes({mode: AUTH[auth], parameter: nick});
- }
- // if (!Users[nick]) Users[nick] = new User(nick);
- },
- removeUser: function (nick) {
- delete this.users[nick];
- for (var symbol in AUTH) {
- if (this.modes[AUTH[symbol]][nick]) this.updateModes({mode: AUTH[auth], parameter: nick, remove: true});
- }
- },
- renameUser: function (nick, newNick) {
- this.users[newNick] = this.users[nick];
- for (var symbol in AUTH) {
- if (this.modes[AUTH[symbol]][nick]) this.updateModes({mode: AUTH[auth], parameter: newNick}, this.modes[AUTH[symbol]][nick]);
- }
- this.removeUser(nick);
- },
- updateTopic: function (topic) {
- this.topic = topic;
- this.topicHistory.unshift({topic:topic});
- },
- updateTopicInfo: function (editedBy, lastEdited) {
- this.topicHistory[0].editedBy = editedBy;
- this.topicHistory[0].lastEdited = lastEdited;
- },
- updateModes: function (modeObj, sender) {
- var mode = modeObj.mode, remove = !!modeObj.remove, parameter = modeObj.parameter || false;
- var listModes = {"v":"+","h":"%","o":"@","a":"&","q":"~","b":"bans","e":"excepts","I":"invited"};
- if (mode in listModes) {
- if (!this.modes[mode]) this.modes[mode] = {};
- if (remove) delete this.modes[mode][parameter]
- else this.modes[mode][parameter] = sender || "Not Specified";
- } else {
- if (remove) delete this.modes[mode];
- else this.modes[mode] = parameter || true;
- }
- },
- getAuth: function (nick, auth) { // Returns the auth level (v,h,o,a,q) or if auth is specified a boolean if they are at or above that auth level.
- nick = nick || "";
- if (!this.users[nick]) return null;
- var authModes = {"v":"+","h":"%","o":"@","a":"&","q":"~"}
- auth = auth in authModes ? auth : false;
- var authLevels = ['q', 'a', 'o', 'h', 'v'];
- var nickAuth = "";
- for (var i = 0; i < authLevels.length; i++) {
- var mode = authLevels[i];
- if (this.modes[mode][nick]) {
- if (auth && authLevels.indexOf(auth) <= authLevels.indexOf(mode)) return true;
- nickAuth += mode;
- }
- }
- if (auth && !nickAuth) return false;
- return nickAuth;
- },
- banList: {},
- exceptList: {},
- addExcept: {},
- inviteList: {},
- addInvite: {},
- modes: {
- "v": {},
- "h": {},
- "o": {},
- "a": {},
- "q": {}
- }
- };
- function addChannel(name) {
- if (name.charAt(0) !== "#") return;
- Channels[name] = Channel(name);
- }
- function removeChannel(name) {
- if (name.charAt(0) !== "#") return;
- delete Channels[name];
- }
- var channel_plugin = {
- name: "channels",
- init: function (client, imports) {
- client.Channels = Channels;
- client.Users = Users;
- var handles = {
- 'join': function (msg) { // client.on('join') Add a user or channel
- // if (true) return console.log('my name: ' + client.receiver.nickname() + ' nickname: ' + msg.nickname);
- var isSelf = client.nickname() === msg.nickname;
- if (isSelf) Channels[msg.channel] = Channel(msg.channel);
- else {
- Channels[msg.channel].addUser(msg.nickname);
- }
- },
- 'quit': function (msg) { // client.on('quit') Remove user from all channels and from user list
- var nick = msg.nickname;
- var reason = msg.reason;
- for (var channel in Channels) {
- var chan = Channels[channel];
- if (nick in chan.users) chan.removeUser(nick);
- }
- },
- 'part': function (msg) { // client.on('part') Remove user from parted channel
- Channels[msg.channel].removeUser(msg.nickname);
- },
- 'nick': function (msg) { // client.on('nick') Update nick across channels
- var nick = msg.old;
- var newNick = msg.new;
- for (var channel in Channels) {
- var chan = Channels[channel];
- if (nick in chan.users) chan.renameUser(nick, newNick);
- }
- },
- '332' : function (msg) { // client.on('332') Update Topic
- var channel = msg.channel || msg.chanenl;
- Channels[channel].updateTopic(msg.topic);
- },
- '333': function (msg) { // client.on('333') Update Topic Info
- Channels[msg.channel].updateTopicInfo(msg.who, msg.timestamp);
- },
- '353' : function (msg) { // client.on('353') Update channel with users
- var channel = Channels[msg.channel];
- msg.nicknames.forEach(channel.addUser.bind(channel));
- },
- 'mode': function (msg) {
- // console.log('mode event');
- // console.log(util.inspect(msg));
- client.ModeMsg = msg;
- if (msg.channel.charAt(0) !== '#') return;
- var handleMode = (function (modeObj) {
- Channels[msg.channel].updateModes(modeObj, msg.nickname);
- }).bind(msg.channel);
- msg.modes.forEach(handleMode);
- }
- };
- return {
- handlers: handles
- };
- }
- }
- module.exports = channel_plugin;
Advertisement
Add Comment
Please, Sign In to add comment