<commit_before>/*
 * Copyright (c) 2014, <NAME>, https://bitbucket.org/sgt-kabukiman/
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

var
	ModeMessage     = require('./ModeMessage.js'),
	TextMessage     = require('./TextMessage.js'),
	TwitchMessage   = require('./TwitchMessage.js'),
	EventDispatcher = require('./EventDispatcher.js'),
	User            = require('./User.js');

function TwitchClient(ircClient, dispatcher, log) {
	this.irc        = ircClient;
	this.dispatcher = dispatcher;
	this.log        = log;
	this.channels   = {};

	ircClient.addListener('message', this.onMessage.bind(this));
	ircClient.addListener('+mode',   this.onModeChange.bind(this));
	ircClient.addListener('-mode',   this.onModeChange.bind(this));
}

TwitchClient.prototype = {
	getChannels: function() {
		return this.channels;
	},

	getChannel: function(chan) {
		return this.channels[chan];
	},

	connect: function() {
		var self = this, irc = this.irc;

		self.log.info('Connecting to Twitch...');

		irc.connect(function() {
			// make sure Twitch knows we're not just an ordinary IRC client, but
			// understand their special user semantics (this enables the
			// SPECIALUSER messages from Twitch);
			// using .send() on the IRC object would mess with the raw command, so
			// we have to send it directly via the TCP connection (not the trailing
			// newline).

			irc.conn.write("TWITCHCLIENT 3\r\n");
			self.log.info('Connection established.');

			// notify the rest of the app
			self.dispatcher.emit('connect', this);
		});
	},

	join: function(channel) {
		var self = this, name = channel.getName();

		if (name in this.channels) {
			return false;
		}

		this.channels[name] = channel;

		this.irc.join(channel.getIrcName(), function() {
			self.log.info('Joined #' + name);
			self.dispatcher.emit('join', channel, self);
		});

		return true;
	},

	part: function(channel) {
		var self = this, name = channel.getName();

		if (!(name in this.channels)) {
			return false;
		}

		delete this.channels[name];

		this.irc.part(channel.getIrcName(), function() {
			self.log.info('Left #' + name);
			self.dispatcher.emit('part', channel, self);
		});

		return true;
	},

	onMessage: function(username, chan, text) {
		var channel, user, msg;

		channel = this.channels[chan.substring(1)];

		// special twitch command
		if (username === 'jtv') {
			msg = TwitchMessage.fromIrcMessage(channel, username, text);
		}

		// subscriber notification
		else if (username === 'twitchnotify') {
			msg = TwitchMessage.fromIrcMessage(channel, username, text);
		}<commit_msg>make the channel detection more robust<commit_after>/*
 * Copyright (c) 2014, <NAME>, https://bitbucket.org/sgt-kabukiman/
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

var
	ModeMessage     = require('./ModeMessage.js'),
	TextMessage     = require('./TextMessage.js'),
	TwitchMessage   = require('./TwitchMessage.js'),
	EventDispatcher = require('./EventDispatcher.js'),
	User            = require('./User.js');

function TwitchClient(ircClient, dispatcher, log) {
	this.irc        = ircClient;
	this.dispatcher = dispatcher;
	this.log        = log;
	this.channels   = {};

	ircClient.addListener('message', this.onMessage.bind(this));
	ircClient.addListener('+mode',   this.onModeChange.bind(this));
	ircClient.addListener('-mode',   this.onModeChange.bind(this));
}

TwitchClient.prototype = {
	getChannels: function() {
		return this.channels;
	},

	getChannel: function(chan) {
		return this.channels[chan];
	},

	connect: function() {
		var self = this, irc = this.irc;

		self.log.info('Connecting to Twitch...');

		irc.connect(function() {
			// make sure Twitch knows we're not just an ordinary IRC client, but
			// understand their special user semantics (this enables the
			// SPECIALUSER messages from Twitch);
			// using .send() on the IRC object would mess with the raw command, so
			// we have to send it directly via the TCP connection (not the trailing
			// newline).

			irc.conn.write("TWITCHCLIENT 3\r\n");
			self.log.info('Connection established.');

			// notify the rest of the app
			self.dispatcher.emit('connect', this);
		});
	},

	join: function(channel) {
		var self = this, name = channel.getName();

		if (name in this.channels) {
			return false;
		}

		this.channels[name] = channel;

		this.irc.join(channel.getIrcName(), function() {
			self.log.info('Joined #' + name);
			self.dispatcher.emit('join', channel, self);
		});

		return true;
	},

	part: function(channel) {
		var self = this, name = channel.getName();

		if (!(name in this.channels)) {
			return false;
		}

		delete this.channels[name];

		this.irc.part(channel.getIrcName(), function() {
			self.log.info('Left #' + name);
			self.dispatcher.emit('part', channel, self);
		});

		return true;
	},

	onMessage: function(username, chan, text) {
		var channel, user, msg;

		if (chan.charAt(0) === '#') {
			chan = chan.substring(1);
		}

		channel = this.channels[chan];

		// special twitch command
		if (username === 'jtv') {
			msg = TwitchMessage.fromIrcMessage(channel, username, text);
		}

		// subscriber notification
		else if (username === 'twitchnotify') {
			msg = TwitchMessage.fromIrcMessage(channel, username, text);
		}
{'fraction_non_alphanumeric': 0.14940239043824702, 'fraction_numerical': 0.0023542194856935894, 'mean_word_length': 3.4756888168557536, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6301255', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1722, 'n_words': 451}