chatsharp/ChatSharp/IrcClient.Commands.cs

254 lines
9.5 KiB
C#
Raw Normal View History

2015-07-07 19:16:52 +00:00
using System;
2017-10-03 19:58:29 +00:00
using System.Collections.Generic;
2015-07-07 19:16:52 +00:00
using System.Linq;
2013-04-09 23:24:50 +00:00
namespace ChatSharp
{
public partial class IrcClient
{
2015-08-01 01:06:22 +00:00
/// <summary>
/// Changes your nick.
/// </summary>
2013-04-09 23:24:50 +00:00
public void Nick(string newNick)
{
SendRawMessage("NICK {0}", newNick);
User.Nick = newNick;
}
2013-04-09 23:31:06 +00:00
2015-08-01 01:06:22 +00:00
/// <summary>
/// Sends a message to one or more destinations (channels or users).
/// </summary>
2013-04-09 23:31:06 +00:00
public void SendMessage(string message, params string[] destinations)
{
const string illegalCharacters = "\r\n\0";
if (destinations == null || !destinations.Any()) throw new InvalidOperationException("Message must have at least one target.");
2021-09-17 20:24:13 +00:00
if (illegalCharacters.Any(message.Contains)) throw new ArgumentException("Illegal characters are present in message.", nameof(message));
2013-04-09 23:31:06 +00:00
string to = string.Join(",", destinations);
2015-03-18 16:01:19 +00:00
SendRawMessage("PRIVMSG {0} :{1}{2}", to, PrivmsgPrefix, message);
2013-04-09 23:31:06 +00:00
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Sends a CTCP action (i.e. "* SirCmpwn waves hello") to one or more destinations.
/// </summary>
2014-07-22 03:22:19 +00:00
public void SendAction(string message, params string[] destinations)
{
const string illegalCharacters = "\r\n\0";
if (destinations == null || !destinations.Any()) throw new InvalidOperationException("Message must have at least one target.");
2021-09-17 20:24:13 +00:00
if (illegalCharacters.Any(message.Contains)) throw new ArgumentException("Illegal characters are present in message.", nameof(message));
2014-07-22 03:22:19 +00:00
string to = string.Join(",", destinations);
2015-03-18 16:01:19 +00:00
SendRawMessage("PRIVMSG {0} :\x0001ACTION {1}{2}\x0001", to, PrivmsgPrefix, message);
2014-07-22 03:22:19 +00:00
}
/// <summary>
/// Sends a NOTICE to one or more destinations (channels or users).
2015-08-16 20:56:35 +00:00
/// </summary>
public void SendNotice(string message, params string[] destinations)
{
const string illegalCharacters = "\r\n\0";
if (destinations == null || !destinations.Any()) throw new InvalidOperationException("Message must have at least one target.");
2021-09-17 20:24:13 +00:00
if (illegalCharacters.Any(message.Contains)) throw new ArgumentException("Illegal characters are present in message.", nameof(message));
string to = string.Join(",", destinations);
SendRawMessage("NOTICE {0} :{1}{2}", to, PrivmsgPrefix, message);
2015-08-16 20:56:35 +00:00
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Leaves the specified channel.
/// </summary>
public void PartChannel(string channel)
{
if (!Channels.Contains(channel))
throw new InvalidOperationException("Client is not present in channel.");
SendRawMessage("PART {0}", channel);
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Leaves the specified channel, giving a reason for your departure.
/// </summary>
public void PartChannel(string channel, string reason)
{
if (!Channels.Contains(channel))
throw new InvalidOperationException("Client is not present in channel.");
SendRawMessage("PART {0} :{1}", channel, reason);
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Joins the specified channel.
/// </summary>
public void JoinChannel(string channel, string key = null)
{
2013-04-10 17:26:20 +00:00
if (Channels.Contains(channel))
throw new InvalidOperationException("Client is already present in channel.");
string joinCmd = string.Format("JOIN {0}", channel);
if (!string.IsNullOrEmpty(key))
joinCmd += string.Format(" {0}", key);
SendRawMessage(joinCmd, channel);
2017-10-13 15:56:25 +00:00
// account-notify capability
var flags = WhoxField.Nick | WhoxField.Hostname | WhoxField.AccountName | WhoxField.Username;
if (Capabilities.IsEnabled("account-notify"))
Who(channel, WhoxFlag.None, flags, (whoList) =>
{
if (whoList.Count > 0)
{
foreach (var whoQuery in whoList)
{
var user = Users.GetOrAdd(whoQuery.User.Hostmask);
user.Account = whoQuery.User.Account;
}
}
});
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Sets the topic for the specified channel.
/// </summary>
2013-04-10 17:26:20 +00:00
public void SetTopic(string channel, string topic)
{
2013-04-10 17:26:20 +00:00
if (!Channels.Contains(channel))
throw new InvalidOperationException("Client is not present in channel.");
2013-04-10 17:26:20 +00:00
SendRawMessage("TOPIC {0} :{1}", channel, topic);
}
2013-04-10 05:53:54 +00:00
2015-08-01 01:06:22 +00:00
/// <summary>
/// Retrieves the topic for the specified channel.
/// </summary>
2015-07-07 19:16:52 +00:00
public void GetTopic(string channel)
{
SendRawMessage("TOPIC {0}", channel);
2015-04-06 22:10:06 +00:00
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Kicks the specified user from the specified channel.
/// </summary>
2013-06-01 22:38:57 +00:00
public void KickUser(string channel, string user)
{
SendRawMessage("KICK {0} {1} :{1}", channel, user);
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Kicks the specified user from the specified channel.
/// </summary>
2013-06-01 22:38:57 +00:00
public void KickUser(string channel, string user, string reason)
{
SendRawMessage("KICK {0} {1} :{2}", channel, user, reason);
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Invites the specified user to the specified channel.
/// </summary>
2013-06-01 22:38:57 +00:00
public void InviteUser(string channel, string user)
{
SendRawMessage("INVITE {1} {0}", channel, user);
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Sends a WHOIS query asking for information on the given nick.
/// </summary>
2013-04-11 01:54:49 +00:00
public void WhoIs(string nick)
{
WhoIs(nick, null);
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Sends a WHOIS query asking for information on the given nick, and a callback
/// to run when we have received the response.
/// </summary>
2013-04-10 05:53:54 +00:00
public void WhoIs(string nick, Action<WhoIs> callback)
{
var whois = new WhoIs();
RequestManager.QueueOperation("WHOIS " + nick, new RequestOperation(whois, ro =>
{
2021-09-17 20:24:13 +00:00
callback?.Invoke((WhoIs)ro.State);
}));
2013-04-10 05:53:54 +00:00
SendRawMessage("WHOIS {0}", nick);
}
2013-04-10 17:26:20 +00:00
2017-10-03 19:58:29 +00:00
/// <summary>
/// Sends an extended WHO query asking for specific information about a single user
/// or the users in a channel, and runs a callback when we have received the response.
/// </summary>
public void Who(string target, WhoxFlag flags, WhoxField fields, Action<List<ExtendedWho>> callback)
2017-10-03 19:58:29 +00:00
{
if (ServerInfo.ExtendedWho)
{
var whox = new List<ExtendedWho>();
// Generate random querytype for WHO query
int queryType = RandomNumber.Next(0, 999);
// Add the querytype field if it wasn't defined
var _fields = fields;
if ((fields & WhoxField.QueryType) == 0)
_fields |= WhoxField.QueryType;
string whoQuery = string.Format("WHO {0} {1}%{2},{3}", target, flags.AsString(), _fields.AsString(), queryType);
string queryKey = string.Format("WHO {0} {1} {2:D}", target, queryType, _fields);
RequestManager.QueueOperation(queryKey, new RequestOperation(whox, ro =>
{
callback?.Invoke((List<ExtendedWho>)ro.State);
2017-10-03 19:58:29 +00:00
}));
SendRawMessage(whoQuery);
}
2017-12-20 21:11:54 +00:00
else
{
var whox = new List<ExtendedWho>();
string whoQuery = string.Format("WHO {0}", target);
RequestManager.QueueOperation(whoQuery, new RequestOperation(whox, ro =>
{
callback?.Invoke((List<ExtendedWho>)ro.State);
}));
SendRawMessage(whoQuery);
}
2017-10-03 19:58:29 +00:00
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Requests the mode of a channel from the server.
/// </summary>
2013-04-11 01:54:49 +00:00
public void GetMode(string channel)
{
GetMode(channel, null);
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Requests the mode of a channel from the server, and passes it to a callback later.
/// </summary>
2013-04-10 17:26:20 +00:00
public void GetMode(string channel, Action<IrcChannel> callback)
{
RequestManager.QueueOperation("MODE " + channel, new RequestOperation(channel, ro =>
{
var c = Channels[(string)ro.State];
2021-09-17 20:24:13 +00:00
callback?.Invoke(c);
}));
2013-04-10 17:26:20 +00:00
SendRawMessage("MODE {0}", channel);
}
2015-08-01 01:06:22 +00:00
/// <summary>
/// Sets the mode of a target.
/// </summary>
public void ChangeMode(string target, string change)
2013-05-20 01:58:02 +00:00
{
2015-08-01 01:06:22 +00:00
SendRawMessage("MODE {0} {1}", target, change);
2013-05-20 01:58:02 +00:00
}
2013-06-02 21:11:48 +00:00
2015-08-01 01:06:22 +00:00
/// <summary>
/// Gets a collection of masks from a channel by a mode. This can be used, for example,
/// to get a list of bans.
/// </summary>
2013-06-02 21:11:48 +00:00
public void GetModeList(string channel, char mode, Action<MaskCollection> callback)
{
RequestManager.QueueOperation("GETMODE " + mode + " " + channel, new RequestOperation(new MaskCollection(), ro =>
{
var c = (MaskCollection)ro.State;
2021-09-17 20:24:13 +00:00
callback?.Invoke(c);
2013-06-02 21:11:48 +00:00
}));
SendRawMessage("MODE {0} {1}", channel, mode);
}
2013-04-09 23:24:50 +00:00
}
}