chatsharp/ChatSharp/IrcUser.cs

203 lines
6.1 KiB
C#
Raw Normal View History

2015-07-07 19:16:52 +00:00
using System;
using System.Collections.Generic;
2015-07-07 19:16:52 +00:00
2013-04-09 21:19:48 +00:00
namespace ChatSharp
{
2015-08-01 01:06:22 +00:00
/// <summary>
/// A user connected to IRC.
/// </summary>
public class IrcUser : IEquatable<IrcUser>
2013-04-09 21:19:48 +00:00
{
2013-04-10 05:53:54 +00:00
internal IrcUser()
{
Channels = new ChannelCollection();
ChannelModes = new Dictionary<IrcChannel, List<char?>>();
2017-10-13 15:56:25 +00:00
Account = "*";
2013-04-10 05:53:54 +00:00
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Constructs an IrcUser given a hostmask or nick.
/// </summary>
public IrcUser(string host) : this()
2013-04-09 21:19:48 +00:00
{
if (!host.Contains("@") && !host.Contains("!"))
Nick = host;
else
{
string[] mask = host.Split('@', '!');
Nick = mask[0];
User = mask[1];
if (mask.Length <= 2)
{
Hostname = "";
}
else
{
Hostname = mask[2];
}
}
2013-04-09 21:19:48 +00:00
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Constructs an IrcUser given a nick and user.
/// </summary>
public IrcUser(string nick, string user) : this()
2013-04-09 21:19:48 +00:00
{
Nick = nick;
User = user;
RealName = User;
Mode = string.Empty;
2013-04-09 21:19:48 +00:00
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Constructs an IRC user given a nick, user, and password.
/// </summary>
2013-04-09 21:19:48 +00:00
public IrcUser(string nick, string user, string password) : this(nick, user)
{
Password = password;
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Constructs an IRC user given a nick, user, password, and real name.
/// </summary>
2013-04-09 21:19:48 +00:00
public IrcUser(string nick, string user, string password, string realName) : this(nick, user, password)
{
RealName = realName;
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// The user's nick.
/// </summary>
2013-04-09 21:19:48 +00:00
public string Nick { get; internal set; }
2015-08-01 01:06:22 +00:00
/// <summary>
/// The user's user (an IRC construct, a string that identifies your username).
/// </summary>
2013-04-09 21:19:48 +00:00
public string User { get; internal set; }
2015-08-01 01:06:22 +00:00
/// <summary>
/// The user's password. Will not be set on anyone but your own user.
/// </summary>
2013-04-09 21:19:48 +00:00
public string Password { get; internal set; }
2015-08-01 01:06:22 +00:00
/// <summary>
/// The user's mode.
/// </summary>
/// <value>The mode.</value>
public string Mode { get; internal set; }
2015-08-01 01:06:22 +00:00
/// <summary>
/// The user's real name.
/// </summary>
/// <value>The name of the real.</value>
2013-04-09 21:19:48 +00:00
public string RealName { get; internal set; }
2015-08-01 01:06:22 +00:00
/// <summary>
/// The user's hostname.
/// </summary>
2013-04-09 21:19:48 +00:00
public string Hostname { get; internal set; }
2015-08-01 01:06:22 +00:00
/// <summary>
/// Channels this user is present in. Note that this only includes channels you are
/// also present in, even after a successful WHOIS.
/// </summary>
/// <value>The channels.</value>
public ChannelCollection Channels { get; set; }
2017-10-13 15:56:25 +00:00
/// <summary>
/// The user's account. If 0 or *, the user is not logged in.
/// Otherwise, the user is logged in with services.
/// </summary>
public string Account { get; set; }
2013-04-09 21:19:48 +00:00
internal Dictionary<IrcChannel, List<char?>> ChannelModes { get; set; }
2015-08-01 01:06:22 +00:00
/// <summary>
/// This user's hostmask (nick!user@host).
/// </summary>
2013-04-09 21:19:48 +00:00
public string Hostmask
{
get
{
return Nick + "!" + User + "@" + Hostname;
}
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Returns true if the user matches the given mask. Can be used to check if a ban applies
/// to this user, for example.
/// </summary>
public bool Match(string mask)
{
if (mask.Contains("!") && mask.Contains("@"))
{
if (mask.Contains('$'))
mask = mask.Remove(mask.IndexOf('$')); // Extra fluff on some networks
var parts = mask.Split('!', '@');
if (Match(parts[0], Nick) && Match(parts[1], User) && Match(parts[2], Hostname))
return true;
}
return false;
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Checks if the given hostmask matches the given mask.
/// </summary>
public static bool Match(string mask, string value)
{
2013-06-01 22:54:13 +00:00
if (value == null)
value = string.Empty;
int i = 0;
int j = 0;
for (; j < value.Length && i < mask.Length; j++)
{
if (mask[i] == '?')
i++;
else if (mask[i] == '*')
{
i++;
if (i >= mask.Length)
return true;
while (++j < value.Length && value[j] != mask[i]) ;
if (j-- == value.Length)
return false;
}
else
{
2014-02-18 00:57:11 +00:00
if (char.ToUpper(mask[i]) != char.ToUpper(value[j]))
return false;
i++;
}
}
2013-06-01 22:38:57 +00:00
return i == mask.Length && j == value.Length;
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// True if this user is equal to another (compares hostmasks).
/// </summary>
public bool Equals(IrcUser other)
{
return other.Hostmask == Hostmask;
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// True if this user is equal to another (compares hostmasks).
/// </summary>
public override bool Equals(object obj)
{
2021-09-17 20:24:13 +00:00
if (obj is IrcUser user)
return Equals(user);
return false;
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Returns the hash code of the user's hostmask.
/// </summary>
public override int GetHashCode()
{
return Hostmask.GetHashCode();
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Returns the user's hostmask.
/// </summary>
public override string ToString()
{
return Hostmask;
}
2013-04-09 21:19:48 +00:00
}
}