<commit_before>﻿// -----------------------------------------------------------------------------
//  <copyright file="IrcClient.Callbacks.cs" company="<NAME>">
//      Copyright (c) <NAME>.  All rights reserved.
//  </copyright>
// -----------------------------------------------------------------------------

namespace Lantea.Core.Net.Irc
{
	using System;
	using System.Linq;
	using System.Text.RegularExpressions;
	using System.Threading.Tasks;
	using System.Timers;
	using Common.Linq;
	using Data;

	public partial class IrcClient
	{
		private bool registered;
		private DateTime lastMessage;
		private Timer timeoutTimer;

		internal const string IrcRawRegex = @"^(:(?<prefix>\S+) )?(?<command>\S+)( (?!:)(?<params>.+?))?( :(?<trail>.+))?$";

		private void StartTimeoutTimer()
		{
			timeoutTimer          = new Timer(Timeout.TotalMilliseconds);
			timeoutTimer.Elapsed += TimeoutTimerElapsed;
			timeoutTimer.Start();
		}

		#region Handlers

		private void CancellationNoticeHandler()
		{
			if (!tokenSource.IsCancellationRequested) return;

			Send("QUIT :Exiting.");
			client.Close();
		}

		private void OnDataReceived(string input)
		{
			RawMessageEvent.Raise(this, new RawMessageEventArgs(input));

			lastMessage = DateTime.Now;
		}

		private void TimeoutTimerElapsed(object sender, ElapsedEventArgs args)
		{
			if ((args.SignalTime - lastMessage) < Timeout)
			{
				TimeoutEvent.Raise(this, EventArgs.Empty);

				tokenSource.Cancel();
			}
		}

		protected virtual void JoinPartHandler(object sender, RawMessageEventArgs args)
		{
			var message = args.Message;
			Match m;

			// expression credit
			// web: http://calebdelnay.com/blog/2010/11/parsing-the-irc-message-format-as-a-client

			// :Lantea!<EMAIL>.jhi.145.98.IP JOIN :#UnifiedTech
			if (message.TryMatch(@"^(:(?<prefix>\S+) )?(?<command>JOIN|PART)( (?!:)(?<params>.+?))?( ?:(?<trail>.+))?$", out m))
			{
				var user = "";
				var channel = m.Groups["trail"].Value;

				if (m.Groups["command"].Value.EqualsIgnoreCase("join"))
				{
					ChannelJoinEvent.Raise(this, new JoinPartEventArgs(user, channel));
				}
				else if (m.Groups["command"].Value.EqualsIgnoreCase("part"))
				{
					ChannelPartEvent.Raise(this, new JoinPartEventArgs(user, channel));
				}
			}
		}
		
		protected virtual void RfcNumericHandler(object sender, RawMessageEventArgs args)
		{
			var toks = args.Message.Split(' ');

			int num;
			if (Int32.TryParse(toks[1], out num))
			{
				var message = string.Join(" ", toks.Skip(2));<commit_msg>misc.<commit_after>﻿// -----------------------------------------------------------------------------
//  <copyright file="IrcClient.Callbacks.cs" company="<NAME>">
//      Copyright (c) <NAME>.  All rights reserved.
//  </copyright>
// -----------------------------------------------------------------------------

namespace Lantea.Core.Net.Irc
{
	using System;
	using System.Linq;
	using System.Text.RegularExpressions;
	using System.Threading.Tasks;
	using System.Timers;
	using Common.Linq;
	using Data;

	public partial class IrcClient
	{
		private bool registered;
		private DateTime lastMessage;
		private Timer timeoutTimer;

		internal const string IrcRawRegex = @"^(:(?<prefix>\S+) )?(?<command>\S+)( (?!:)(?<params>.+?))?( :(?<trail>.+))?$";

		private void StartTimeoutTimer()
		{
			timeoutTimer          = new Timer(Timeout.TotalMilliseconds);
			timeoutTimer.Elapsed += TimeoutTimerElapsed;
			timeoutTimer.Start();
		}

		#region Handlers

		private void CancellationNoticeHandler()
		{
			if (!tokenSource.IsCancellationRequested) return;

			Send("QUIT :Exiting.");
			client.Close();
		}

		private void OnDataReceived(string input)
		{
			RawMessageEvent.Raise(this, new RawMessageEventArgs(input));

			lastMessage = DateTime.Now;
		}

		private void TimeoutTimerElapsed(object sender, ElapsedEventArgs args)
		{
			if ((args.SignalTime - lastMessage) < Timeout)
			{
				TimeoutEvent.Raise(this, EventArgs.Empty);

				tokenSource.Cancel();
			}
		}

		protected virtual void JoinPartHandler(object sender, RawMessageEventArgs args)
		{
			var message = args.Message;
			Match m;

			// expression credit
			// web: http://calebdelnay.com/blog/2010/11/parsing-the-irc-message-format-as-a-client

			// :Lantea!<EMAIL>.145.98.IP JOIN :#UnifiedTech
			if (message.TryMatch(@":?([^!]+)!([^@]+@\S+) (JOIN|PART) (?:#?[^!]+) :(.+)", out m))
			{
				var user = "";
				var channel = m.Groups["trail"].Value;

				if (m.Groups["command"].Value.EqualsIgnoreCase("join"))
				{
					ChannelJoinEvent.Raise(this, new JoinPartEventArgs(user, channel));
				}
				else if (m.Groups["command"].Value.EqualsIgnoreCase("part"))
				{
					ChannelPartEvent.Raise(this, new JoinPartEventArgs(user, channel));
				}
			}
		}
		
		protected virtual void RfcNumericHandler(object sender, RawMessageEventArgs args)
		{
			var toks = args.Message.Split(' ');

			int num;
			if (Int32.TryParse(toks[1], out num))
			{
				var message = string.Join(" ", toks.Skip(2));
{'fraction_non_alphanumeric': 0.2110128617363344, 'fraction_numerical': 0.006028938906752411, 'mean_word_length': 2.8284615384615384, 'pattern_counts': {'":': 1, '<': 27, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5312440', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1542, 'n_words': 305}