
/**
 * This module is responsible for sending messages directly between users.
 */

var
	extend = req('/lib/utilities/extend');

var
	Commands     = req('/lib/constants/commands'),
	ErrorReasons = req('/lib/constants/error-reasons'),
	ModuleTypes  = req('/lib/constants/module-types');

var
	Module              = req('/lib/server/module'),
	InvalidCommandError = req('/lib/errors/invalid-command');

var
	PrivateMessage = req('/lib/messages/commands/private'),
	NoticeMessage  = req('/lib/messages/commands/notice');


class PMModule extends Module {

	coupleToClient() {
		// Deliberately a noop
	}

	decoupleFromClient() {
		// Deliberately a noop
	}

	handleClientMessage(client, message) {
		client.getUserDetails().bumpIdleStartTimestamp();

		var user_targets = message.getUserMessageTargets();

		user_targets.forEach(function each(user_target) {
			this.handleClientMessageToUserTarget(
				client,
				message,
				user_target
			);
		}, this);
	}

	handleClientMessageToUserTarget(client, message, user_target) {
		var target_client = this.getClientForNickname(user_target);

		if (!target_client) {
			return this.sendNoSuchNickMessageToClientForNickname(
				client,
				user_target
			);
		}

		return this.handleClientMessageToClient(
			client,
			message,
			target_client
		);
	}

	handleClientMessageToClient(source_client, message, target_client) {
		// TODO: Permissions etc
		var
			command = message.getCommand(),
			outbound_message;

		switch (command) {
			case Commands.PRIVMSG:
				outbound_message = PrivateMessage.fromInboundMessage(message);
				break;

			case Commands.NOTICE:
				outbound_message = NoticeMessage.fromInboundMessage(message);
				break;

			default:
				throw new InvalidCommandError(command, ErrorReasons.UNSUPPORTED);
		}

		target_client.sendMessage(outbound_message);
	}


}

extend(PMModule.prototype, {

	type: ModuleTypes.PM

});

module.exports = PMModule;

@@ -1522,16 +1522,20 @@
 essage =
+ new
  Private
@@ -1545,35 +1545,9 @@
 sage
-.fromInboundMessage(message
+(
 );%0A%09
@@ -1604,16 +1604,20 @@
 essage =
+ new
  NoticeM
@@ -1626,35 +1626,9 @@
 sage
-.fromInboundMessage(message
+(
 );%0A%09
@@ -1725,16 +1725,141 @@
 );%0A%09%09%7D%0A%0A
+%09%09outbound_message.setOriginUserId(source_client.getUserId());%0A%09%09outbound_message.setMessageBody(message.getMessageBody());%0A%0A
 %09%09target

Fix forward propagation of PRIVMSG / NOTICE messages between clients