Parcourir la source

Set own and users' umodes

k4be il y a 5 ans
Parent
commit
7ca7c7aa01
3 fichiers modifiés avec 55 ajouts et 18 suppressions
  1. 13 12
      irc.js
  2. 37 6
      protocol/unrealircd.js
  3. 5 0
      services.js

+ 13 - 12
irc.js

@@ -4,7 +4,6 @@ var IRCserver = require('./server');
 var IRCuser = require('./user');
 var IRCchannel = require('./channel');
 
-var uplink = null;
 var connection = null;
 var recvData = '';
 var users = [];
@@ -18,11 +17,11 @@ function addInternalEvents(){
 		for(var i=0; i<users.length; i++){
 			var user = users[i];
 			if(user.uplink == me){
-				uplink.introduceUser(user);
+				irc.uplink.introduceUser(user);
 			}
 		}
 		for(var i=0; i<channels.length; i++){
-			uplink.syncChannel(channels[i]);
+			irc.uplink.syncChannel(channels[i]);
 		}
 	});
 }
@@ -176,7 +175,7 @@ function makeTagsString(tags){
 
 function ircMessage(data){
 	var msg = irc.parseLine(data);
-	uplink.processMessage(msg);
+	irc.uplink.processMessage(msg);
 }
 
 function ircConnectionClosed(e){
@@ -228,13 +227,14 @@ var irc = {
 	'channels': channels,
 	'servers': servers,
 	'getChannel': getChannel,
+	'uplink': null,
 	'setConnection': function(host, port, protocol){
-		uplink = require('./protocol/' + protocol);
+		irc.uplink = require('./protocol/' + protocol);
 		me = new IRCserver;
 		me.setEvents(events);
 		me.introduce('serwisy.pirc.pl', '11K', 'Serwisy', 0, null);
 		servers.push(me);
-		uplink.setHandlers({
+		irc.uplink.setHandlers({
 			send: ircSendData,
 			findServer: findServer,
 			findUser: findUser,
@@ -246,7 +246,7 @@ var irc = {
 			removeServer: removeServer,
 			findChannel: findChannel
 		});
-		uplink.setSettings({
+		irc.uplink.setSettings({
 			ID: me.sid,
 			password: 'myservicespassword',
 			name: me.name,
@@ -256,9 +256,9 @@ var irc = {
 			maxUsers: 0
 		});
 		addInternalEvents();
-		uplink.setEvents(events);
+		irc.uplink.setEvents(events);
 		connection = new net.Socket();
-		connection.connect(port, host, uplink.connected.bind(this));
+		connection.connect(port, host, irc.uplink.connected.bind(this));
 		connection.on('data', ircDataReceived);
 		connection.on('close', ircConnectionClosed);
 	},
@@ -268,8 +268,9 @@ var irc = {
 	'makeUser': function(nick, ident, host, realname){
 		var user = new IRCuser;
 		user.setEvents(events);
-		var uid = uplink.makeUid();
-		user.introduce(nick, 0, Math.floor(new Date() / 1000).toString(10), ident, host, uid, null, '+SoB'/*TODO*/, null, null, null, realname, me);
+		var uid = irc.uplink.makeUid();
+		var umodes = { 'SERVICEUSER': true, 'BOT': true, 'OPER': true }; // TODO make it configurable
+		user.introduce(nick, 0, Math.floor(new Date() / 1000).toString(10), ident, host, uid, null, umodes, null, null, null, realname, me);
 		users.push(user);
 		return user;
 	},
@@ -438,7 +439,7 @@ var irc = {
 			ircmsg.args.push(currArg);
 		}
 		
-		ircmsg.sender = uplink.processSender(ircmsg.sender);
+		ircmsg.sender = irc.uplink.processSender(ircmsg.sender);
 		if(!ircmsg.sender){
 			return false;
 		}

+ 37 - 6
protocol/unrealircd.js

@@ -53,7 +53,7 @@ var protocol = {
 		if(user.account){
 			account = user.account;
 		}
-		var umodes = '+oSB'; // TODO
+		var umodes = makeUmodeString(user.umodes, true);
 		var vhost = '*';
 		if(user.vhost){
 			vhost = user.vhost;
@@ -68,7 +68,6 @@ var protocol = {
 		}
 		var realname = user.realname;
 		var args = [nick, distance, TS, ident, host, uid, account, umodes, vhost, cloakedHost, ip, realname];
-		console.log(args);
 		ircSend(null, settings.ID, 'UID', args);
 	},
 	syncChannel: function(channel){
@@ -100,7 +99,7 @@ var protocol = {
 						if(chmodeMap[c].name == statusMode){
 							for(sm in connection.statusmodes){
 								if(connection.statusmodes[sm] == c){
-									status += sm;
+									status += sm; // assign status character to user
 									break;
 								}
 							}
@@ -136,13 +135,19 @@ var protocol = {
 			if(modes) args.push(modes); else args.push('+');
 			if(modeArgs) args.push(modeArgs);
 			args.push(text);
-			console.log(args);
 			ircSend(null, settings.ID, 'SJOIN', args);
-			
 		} while(true);
 	},
 	'makeUid': function(){
 		return settings.me.sid + randomID();
+	},
+	'changeUmodes': function(user, umodes){
+		if(user.uplink == settings.me){ // services bot
+			ircSend(null, user.uid, 'UMODE2', [makeUmodeString(umodes, true)]);
+		} else { // normal user
+			ircSend(null, settings.ID, 'SVS2MODE', [user.uid, makeUmodeString(umodes, false)]);
+		}
+		user.changeUmodes(umodes);
 	}
 }
 //>>> @time=2020-03-15T12:17:07.355Z;msgid=FhjgUp20YBKQ32TGPM28tS :143 SJOIN 1580651090 #help +nt :0931Y7O8W 093375CFR 123VTY20R
@@ -253,7 +258,7 @@ var umodeMap = {
 	'W': { 'name': 'WHOISNOTICE', 'user': false, 'services': false },
 	'G': { 'name': 'CENSORMESSAGES', 'user': true, 'services': true },
 	'T': { 'name': 'NOCTCPMESSAGES', 'user': true, 'services': true },
-	'S': { 'name': 'SERVICEUSER', 'user': false, 'services': true },
+	'S': { 'name': 'SERVICEUSER', 'user': false, 'services': false },
 	'B': { 'name': 'BOT', 'user': true, 'services': true }
 };
 
@@ -324,6 +329,32 @@ function parseUmodes(modeString){
 	return modes;
 }
 
+function makeUmodeString(umodes, own){
+	var out = '';
+	var plus = false;
+	var addPM = true;
+	for(char in umodeMap){
+		var name = umodeMap[char].name;
+		if(name in umodes){
+			if(own || umodeMap[char].services){
+				if(umodes[name] && (addPM || !plus)){
+					addPM = false;
+					plus = true;
+					out += '+';
+				} else if(!umodes[name] && (addPM || plus)){
+					addPM = false;
+					plus = false;
+					out += '-';
+				}
+				out += char;
+			} else {
+				console.log('Attempted to set umode ' + char + ' which is not settable by services');
+			}
+		}
+	}
+	return out;
+}
+
 function parseModePrefix(text){
 	var modes = {'status': [], 'list': [] };
 	var isUser = false;

+ 5 - 0
services.js

@@ -47,3 +47,8 @@ irc.setConnection('localhost', 6667, 'unrealircd');
 var myUser = irc.makeUser('TestServ', 'pirc', 'serwisy.pirc.pl', 'Użytkownik testowy');
 var myChannel = irc.getChannel('#test');
 myChannel.joinUser(myUser);
+/*
+setTimeout(function(){
+	irc.uplink.changeUmodes(myUser, { 'NOCTCPMESSAGES': true, 'BOT': false }); // setting umodes test
+}, 10000);
+*/