mirror of
https://github.com/thelounge/thelounge
synced 2024-12-24 11:23:07 +00:00
83 lines
1.6 KiB
JavaScript
83 lines
1.6 KiB
JavaScript
var _ = require("lodash");
|
|
var config = require("../../config") || {};
|
|
var fs = require('fs');
|
|
var moment = require("moment");
|
|
|
|
module.exports = Chan;
|
|
|
|
function Chan(attr) {
|
|
_.merge(this, _.extend({
|
|
id: global.id = ++global.id || 1,
|
|
name: "",
|
|
type: "channel",
|
|
network: "",
|
|
count: 0,
|
|
messages: [],
|
|
users: [],
|
|
}, attr));
|
|
};
|
|
|
|
Chan.prototype.addMsg = function(msg) {
|
|
this.messages.push(msg);
|
|
if (config.log != true || this.type == "lobby") {
|
|
return;
|
|
}
|
|
|
|
var dir = "logs/";
|
|
dir += this.network + "/";
|
|
if (!fs.existsSync(dir)) {
|
|
fs.mkdir(dir);
|
|
}
|
|
|
|
var date = moment().format("YYYY-MM-DD");
|
|
var file = dir + this.name + ".log";
|
|
|
|
var line = "[" + msg.time + "] ";
|
|
if (msg.type == "normal") {
|
|
// Format:
|
|
// [00:00] <Arnold> Put that cookie down.. Now!!
|
|
line += "<" + msg.from + "> " + msg.text;
|
|
} else {
|
|
// Format:
|
|
// [00:00] * Arnold quit
|
|
line += "* " + msg.from + " " + msg.type;
|
|
if (msg.text) {
|
|
line += " " + msg.text;
|
|
}
|
|
}
|
|
|
|
fs.appendFile(
|
|
file,
|
|
line + "\n"
|
|
);
|
|
};
|
|
|
|
Chan.prototype.addUser = function(user) {
|
|
this.users.push(user);
|
|
};
|
|
|
|
Chan.prototype.sortUsers = function() {
|
|
this.users = _.sortBy(
|
|
this.users,
|
|
function(u) { return u.name.toLowerCase(); }
|
|
);
|
|
var modes = [
|
|
"~",
|
|
"%",
|
|
"@",
|
|
"+",
|
|
].reverse();
|
|
modes.forEach(function(mode) {
|
|
this.users = _.remove(
|
|
this.users,
|
|
function(u) { return u.mode == mode; }
|
|
).concat(this.users);
|
|
}, this);
|
|
};
|
|
|
|
Chan.prototype.toJSON = function() {
|
|
var clone = _.clone(this);
|
|
clone.count = clone.messages.length;
|
|
clone.messages = clone.messages.slice(-100);
|
|
return clone;
|
|
};
|