chatsharp/ChatSharp/IrcClient.Commands.cs

230 lines
8.6 KiB
C#
Raw Permalink 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>
2022-04-12 20:14:48 +00:00
/// Changes your nick.
2015-08-01 01:06:22 +00:00
/// </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>
2022-04-12 20:14:48 +00:00
/// Sends a message to one or more destinations (channels or users).
2015-08-01 01:06:22 +00:00
/// </summary>
2013-04-09 23:31:06 +00:00
public void SendMessage(string message, params string[] destinations)
{
2024-04-19 19:06:28 +00:00
IllegalCharacters(message, destinations);
2022-04-12 20:14:48 +00:00
var 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>
2022-04-12 20:14:48 +00:00
/// Sends a CTCP action (i.e. "* SirCmpwn waves hello") to one or more destinations.
2015-08-01 01:06:22 +00:00
/// </summary>
2014-07-22 03:22:19 +00:00
public void SendAction(string message, params string[] destinations)
{
2024-04-19 19:06:28 +00:00
IllegalCharacters(message, destinations);
2022-04-12 20:14:48 +00:00
var 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>
2022-04-12 20:14:48 +00:00
/// 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)
2024-04-19 19:06:28 +00:00
{
IllegalCharacters(message, destinations);
var to = string.Join(",", destinations);
SendRawMessage("NOTICE {0} :{1}{2}", to, PrivmsgPrefix, message);
}
private static void IllegalCharacters(string message, string[] destinations)
{
const string illegalCharacters = "\r\n\0";
2022-04-12 20:14:48 +00:00
if (destinations == null || !destinations.Any())
throw new InvalidOperationException("Message must have at least one target.");
if (illegalCharacters.Any(message.Contains))
throw new ArgumentException("Illegal characters are present in message.", nameof(message));
2015-08-16 20:56:35 +00:00
}
2015-08-01 01:06:22 +00:00
/// <summary>
2022-04-12 20:14:48 +00:00
/// Leaves the specified channel.
2015-08-01 01:06:22 +00:00
/// </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>
2022-04-12 20:14:48 +00:00
/// Leaves the specified channel, giving a reason for your departure.
2015-08-01 01:06:22 +00:00
/// </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>
2022-04-12 20:14:48 +00:00
/// Joins the specified channel.
2015-08-01 01:06:22 +00:00
/// </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.");
2024-04-19 18:52:01 +00:00
var joinCmd = $"JOIN {channel}";
if (!string.IsNullOrEmpty(key))
2024-04-19 18:52:01 +00:00
joinCmd += $" {key}";
SendRawMessage(joinCmd, channel);
2017-10-13 15:56:25 +00:00
// account-notify capability
2024-04-19 19:06:28 +00:00
const WhoxField flags = WhoxField.Nick | WhoxField.Hostname | WhoxField.AccountName | WhoxField.Username;
2017-10-13 15:56:25 +00:00
if (Capabilities.IsEnabled("account-notify"))
2022-04-12 20:14:48 +00:00
Who(channel, WhoxFlag.None, flags, whoList =>
2017-10-13 15:56:25 +00:00
{
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>
2022-04-12 20:14:48 +00:00
/// Sets the topic for the specified channel.
2015-08-01 01:06:22 +00:00
/// </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>
2022-04-12 20:14:48 +00:00
/// Retrieves the topic for the specified channel.
2015-08-01 01:06:22 +00:00
/// </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>
2022-04-12 20:14:48 +00:00
/// Kicks the specified user from the specified channel.
2015-08-01 01:06:22 +00:00
/// </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>
2022-04-12 20:14:48 +00:00
/// Kicks the specified user from the specified channel.
2015-08-01 01:06:22 +00:00
/// </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>
2022-04-12 20:14:48 +00:00
/// Invites the specified user to the specified channel.
2015-08-01 01:06:22 +00:00
/// </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>
2022-04-12 20:14:48 +00:00
/// Sends a WHOIS query asking for information on the given nick, and a callback
/// to run when we have received the response.
2015-08-01 01:06:22 +00:00
/// </summary>
2024-04-19 19:06:28 +00:00
public void WhoIs(string nick, Action<WhoIs> callback = null)
2013-04-10 05:53:54 +00:00
{
var whois = new WhoIs();
2024-04-19 19:06:28 +00:00
var message = $"WHOIS {nick}";
RequestManager.QueueOperation(message,
new RequestOperation(whois, ro => { callback?.Invoke((WhoIs)ro.State); }));
SendRawMessage(message);
2013-04-10 05:53:54 +00:00
}
2013-04-10 17:26:20 +00:00
2017-10-03 19:58:29 +00:00
/// <summary>
2022-04-12 20:14:48 +00:00
/// 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.
2017-10-03 19:58:29 +00:00
/// </summary>
2024-04-19 19:06:28 +00:00
public void Who(string target, WhoxFlag flags, WhoxField whoxField, 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
2022-04-12 20:14:48 +00:00
var queryType = RandomNumber.Next(0, 999);
2017-10-03 19:58:29 +00:00
// Add the querytype field if it wasn't defined
2024-04-19 19:06:28 +00:00
var fields = whoxField;
if ((whoxField & WhoxField.QueryType) == 0)
fields |= WhoxField.QueryType;
2017-10-03 19:58:29 +00:00
2024-04-19 19:06:28 +00:00
var whoQuery = $"WHO {target} {flags.AsString()}%{fields.AsString()},{queryType}";
var queryKey = $"WHO {target} {queryType} {fields:D}";
2017-10-03 19:58:29 +00:00
2022-04-12 20:14:48 +00:00
RequestManager.QueueOperation(queryKey,
2024-04-19 04:24:48 +00:00
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>();
2024-04-19 18:52:01 +00:00
var whoQuery = $"WHO {target}";
2017-12-20 21:11:54 +00:00
2022-04-12 20:14:48 +00:00
RequestManager.QueueOperation(whoQuery,
2024-04-19 04:24:48 +00:00
new RequestOperation(whox, ro => { callback?.Invoke((List<ExtendedWho>)ro.State); }));
2017-12-20 21:11:54 +00:00
SendRawMessage(whoQuery);
}
2017-10-03 19:58:29 +00:00
}
2015-08-01 01:06:22 +00:00
/// <summary>
2022-04-12 20:14:48 +00:00
/// Requests the mode of a channel from the server, and passes it to a callback later.
2015-08-01 01:06:22 +00:00
/// </summary>
2024-04-19 19:06:28 +00:00
public void GetMode(string channel, Action<IrcChannel> callback = null)
2013-04-10 17:26:20 +00:00
{
2024-04-19 19:06:28 +00:00
var message = $"MODE {channel}";
RequestManager.QueueOperation(message, new RequestOperation(channel, ro =>
2022-04-12 20:14:48 +00:00
{
var c = Channels[(string)ro.State];
callback?.Invoke(c);
}));
2024-04-19 19:06:28 +00:00
SendRawMessage(message);
2013-04-10 17:26:20 +00:00
}
2015-08-01 01:06:22 +00:00
/// <summary>
2022-04-12 20:14:48 +00:00
/// Sets the mode of a target.
2015-08-01 01:06:22 +00:00
/// </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>
2022-04-12 20:14:48 +00:00
/// Gets a collection of masks from a channel by a mode. This can be used, for example,
/// to get a list of bans.
2015-08-01 01:06:22 +00:00
/// </summary>
2013-06-02 21:11:48 +00:00
public void GetModeList(string channel, char mode, Action<MaskCollection> callback)
{
2024-04-19 19:06:28 +00:00
RequestManager.QueueOperation($"MODE {mode} {channel}",
new RequestOperation(new MaskCollection(), ro => callback?.Invoke((MaskCollection)ro.State)));
2013-06-02 21:11:48 +00:00
SendRawMessage("MODE {0} {1}", channel, mode);
}
2013-04-09 23:24:50 +00:00
}
2022-04-12 20:14:48 +00:00
}