Skip to content

Commit

Permalink
Added commentary.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzarnal committed Aug 10, 2017
1 parent 1123630 commit 3829419
Showing 1 changed file with 48 additions and 16 deletions.
64 changes: 48 additions & 16 deletions StripSystem/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

namespace JoinPartFilter
{
public class Filter : IPlugin // Reference this is a plugin
//Inerit form IPlugin to be an AdiIRC plugin.
public class Filter : IPlugin
{
private IPluginHost _host;

//Mandatory information fields.
public string PluginName => "Join-Part Filter";

public string PluginDescription => "Filters join/part messages.";

public string PluginAuthor => "Xesyto";
public string PluginVersion => "4";
public string PluginEmail => "s.oudenaarden@gmail.com";
Expand All @@ -32,11 +32,10 @@ public Filter()

public void Initialize(IPluginHost host)
{
// This is called when the plugin is loaded
// Suscribe to delegates here

//Store the host in a private field, we want to be able to access it later
_host = host;

//Register Delegates
_host.OnChannelJoin += OnChannelJoin;
_host.OnChannelPart += OnChannelPart;
_host.OnQuit += OnQuit;
Expand All @@ -45,21 +44,26 @@ public void Initialize(IPluginHost host)
_host.OnChannelRawMode += OnChannelRawMode;
}

//Append time to messages
private void OnChannelNormalMessage(ChannelNormalMessageArgs argument)
{
//sometimes ( mostly twitch ) user ident or host is not yet known even though a message was received
if (argument.User == null || argument.User.Ident == null || argument.User.Host == null)
return;

var userKey = argument.Server.Network + argument.Channel.Name + argument.User.Host;

var userKey = argument.Server.Network + argument.Channel.Name + argument.User.Host;

if (!_userDatabase.ContainsKey(userKey))
{
{
//If the user is new to us add it to the data
//We did not see them join so they must have been in channel before us.
//So'set AnnounceJoin to true so we don't announce them later.
var newData = new UserData { AnnouncedJoin = true, LastMessage = DateTime.Now };
_userDatabase.Add(userKey, newData);
}
else
{
//if the user was in the database and has not been announced yet, do so.
var userData = _userDatabase[userKey];

if (!userData.AnnouncedJoin)
Expand All @@ -69,10 +73,12 @@ private void OnChannelNormalMessage(ChannelNormalMessageArgs argument)
userData.AnnouncedJoin = true;
}

//Update last message property.
userData.LastMessage = DateTime.Now;
}
}

//Hide join messages
private void OnChannelJoin(ChannelJoinArgs argument)
{
argument.EatData = EatData.EatText;
Expand All @@ -81,6 +87,7 @@ private void OnChannelJoin(ChannelJoinArgs argument)

if (_userDatabase.ContainsKey(userKey))
{
//If the user was already in the data and has talked recently, announce they joined the channel
var userData = _userDatabase[userKey];
if (userData.TalkedRecently())
{
Expand All @@ -90,70 +97,94 @@ private void OnChannelJoin(ChannelJoinArgs argument)
}
else
{
//If the user is new to us add it to the data
var userData = new UserData { Joined = DateTime.Now };

_userDatabase.Add(userKey, userData);
}
}

//Hide Part Messages
private void OnChannelPart(ChannelPartArgs argument)
{
argument.EatData = EatData.EatText;

var userKey = argument.Server.Network + argument.Channel.Name + argument.User.Host;

//if the user is leaving but we don't know about the user yet don't show the PART message and do nothing else
if (!_userDatabase.ContainsKey(userKey)) return;


var userData = _userDatabase[userKey];
if (userData.TalkedRecently())
{
//Of they talked recently show the PART message
argument.EatData = EatData.EatNone;
}
}

//Hide Quite messages
private void OnQuit(QuitArgs argument)
{
argument.EatData = EatData.EatText;

// QUIT messages are server wide not channel specific.
// But user Identities are stored on a channel basis.
// So iterate through all channels on the server
// generating a userkey for them all and checking each individually.

foreach (IChannel channel in argument.Server.GetChannels)
{
var userKey = argument.Server.Network + channel.Name + argument.User.Host;

//This key isn't in the data so skip ahead to the next iteration
if (!_userDatabase.ContainsKey(userKey)) continue;

var userData = _userDatabase[userKey];
if (userData.TalkedRecently())
{
{
//The quitting user talked recently in one of the channels we know about, announce they are quitting.
argument.EatData = EatData.EatNone;
return;
}
}
}

//Hide Nick changes
private void OnNick(NickArgs argument)
{
{
argument.EatData = EatData.EatText;


// NICK messages are server wide not channel specific.
// But user Identities are stored on a channel basis.
// So iterate through all channels on the server
// generating a userkey for them all and checking each individually.

foreach (IChannel channel in argument.Server.GetChannels)
{
var userKey = argument.Server.Network + channel.Name + argument.User.Host;

//This key isn't in the data so skip ahead to the next iteration
if (!_userDatabase.ContainsKey(userKey)) continue;

var userData = _userDatabase[userKey];
if (userData.TalkedRecently())
{
{
//The user talked recently in one of the channels we know about, announce they are changing name.
argument.EatData = EatData.EatNone;
return;
}
}
}

//Hide mode changes ( op, voice, hop, etc )
private void OnChannelRawMode(ChannelRawModeArgs argument)
{
var user = argument.User;
var mode = argument.Modes;

//Attempt to find a user ident in case its empty, without it we can't build a userKey
//This is mostly a twitch issue
if (string.IsNullOrEmpty(user?.Ident))
{
var modeRegex = @"[+-]. (\w+)";
Expand All @@ -167,7 +198,7 @@ private void OnChannelRawMode(ChannelRawModeArgs argument)
}
else
{
argument.EatData = EatData.EatNone;
argument.EatData = EatData.EatText;
return;
}

Expand All @@ -184,7 +215,7 @@ private void OnChannelRawMode(ChannelRawModeArgs argument)
//Twitch has a nasty habit of showing mode removal after a user has left the channel.
if (!argument.Server.Network.ToLower().Contains("twitch"))
{
argument.EatData = EatData.EatNone;
argument.EatData = EatData.EatText;
return;
}
}
Expand All @@ -197,7 +228,8 @@ private void OnChannelRawMode(ChannelRawModeArgs argument)

var userData = _userDatabase[userKey];
if (userData.TalkedRecently())
{
{
//The user talked recently, announce they are changing mode
argument.EatData = EatData.EatNone;
}

Expand Down

0 comments on commit 3829419

Please sign in to comment.