123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using System.Threading;
-
- namespace Combot.IRCServices
- {
- public partial class IRC
- {
- /// <summary>
- /// Sends a private message to a nick or channel
- /// </summary>
- /// <param name="nick"></param>
- /// <param name="message"></param>
- public void SendPrivateMessage(string recipient, string message)
- {
- TimeSpan sinceLastMessage = (DateTime.Now - LastMessageSend);
- if (sinceLastMessage.TotalMilliseconds < MessageSendDelay)
- {
- Thread.Sleep((int)(MessageSendDelay - sinceLastMessage.TotalMilliseconds));
- }
- LastMessageSend = DateTime.Now;
- if (message.Length > MaxMessageLength)
- {
- List<string> splitMessage = message.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).ToList();
- string subMessage = string.Empty;
- string nextMessage = string.Empty;
- for (int i = 0; i < splitMessage.Count; i++)
- {
- int wordLength = splitMessage[i].Length + 1;
- int totalLength = subMessage.Length;
- if (totalLength + wordLength > MaxMessageLength)
- {
- nextMessage = string.Join(" ", splitMessage.GetRange(i, splitMessage.Count - i));
- break;
- }
- subMessage = string.Join(" ", subMessage, splitMessage[i]);
- }
- SendTCPMessage(string.Format("PRIVMSG {0} :{1}", recipient, subMessage));
- SendPrivateMessage(recipient, nextMessage);
- }
- else
- {
- SendTCPMessage(string.Format("PRIVMSG {0} :{1}", recipient, message));
- }
- }
-
- public void SendPrivateMessage(List<string> recipients, string message)
- {
- string recipient_list = string.Empty;
- foreach (string recipient in recipients)
- {
- recipient_list += recipient + ",";
- }
- SendPrivateMessage(recipient_list.TrimEnd(','), message);
- }
-
- /// <summary>
- /// Sends a Notice to either a nick or channel
- /// </summary>
- /// <param name="nick"></param>
- /// <param name="message"></param>
- public void SendNotice(string recipient, string message)
- {
- TimeSpan sinceLastMessage = (DateTime.Now - LastMessageSend);
- if (sinceLastMessage.TotalMilliseconds < MessageSendDelay)
- {
- Thread.Sleep((int) (MessageSendDelay - sinceLastMessage.TotalMilliseconds));
- }
- LastMessageSend = DateTime.Now;
- if (message.Length > MaxMessageLength)
- {
- List<string> splitMessage = message.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).ToList();
- string subMessage = string.Empty;
- string nextMessage = string.Empty;
- for (int i = 0; i < splitMessage.Count; i++)
- {
- int wordLength = splitMessage[i].Length + 1;
- int totalLength = subMessage.Length;
- if (totalLength + wordLength > MaxMessageLength)
- {
- nextMessage = string.Join(" ", splitMessage.GetRange(i, splitMessage.Count - i));
- break;
- }
- subMessage = string.Join(" ", subMessage, splitMessage[i]);
- }
- SendTCPMessage(string.Format("NOTICE {0} :{1}", recipient, subMessage));
- SendNotice(recipient, nextMessage);
- }
- else
- {
- SendTCPMessage(string.Format("NOTICE {0} :{1}", recipient, message));
- }
- }
-
- public void SendNotice(List<string> recipients, string message)
- {
- string recipient_list = string.Empty;
- foreach (string recipient in recipients)
- {
- recipient_list += recipient + ",";
- }
-
- SendNotice(recipient_list.TrimEnd(','), message);
- }
-
- /// <summary>
- /// Sends a CTCP command and optional message to a nick or channel
- /// </summary>
- /// <param name="recipient"></param>
- /// <param name="command"></param>
- /// <param name="message"></param>
- public void SendCTCPMessage(string recipient, string command, string message = "")
- {
- if (message != string.Empty)
- {
- message = " " + message;
- }
- SendTCPMessage(string.Format("PRIVMSG {0} :\u0001{1}{2}\u0001", recipient, command, message));
- }
-
- public void SendCTCPMessage(List<string> recipients, string command, string message)
- {
- string recipient_list = string.Empty;
- foreach (string recipient in recipients)
- {
- recipient_list += recipient + ",";
- }
- if (message != string.Empty)
- {
- message = " " + message;
- }
- SendCTCPMessage(recipient_list.TrimEnd(','), command, message);
- }
-
- /// <summary>
- /// Sends a CTCP command and optional message to a nick or channel
- /// </summary>
- /// <param name="recipient"></param>
- /// <param name="command"></param>
- /// <param name="message"></param>
- public void SendCTCPNotice(string recipient, string command, string message = "")
- {
- if (message != string.Empty)
- {
- message = " " + message;
- }
- SendTCPMessage(string.Format("NOTICE {0} :\u0001{1}{2}\u0001", recipient, command, message));
- }
-
- public void SendCTCPNotice(List<string> recipients, string command, string message)
- {
- string recipient_list = string.Empty;
- foreach (string recipient in recipients)
- {
- recipient_list += recipient + ",";
- }
- if (message != string.Empty)
- {
- message = " " + message;
- }
- SendCTCPNotice(recipient_list.TrimEnd(','), command, message);
- }
-
- /// <summary>
- /// Sends the connection password
- /// </summary>
- /// <param name="password"></param>
- public void SendPassword(string password)
- {
- SendTCPMessage(string.Format("PASSWORD {0}", password));
- }
-
- /// <summary>
- /// Sends a Nick command to set the nickname
- /// </summary>
- /// <param name="nick"></param>
- public void SendNick(string nick)
- {
- SendTCPMessage(string.Format("NICK {0}", nick));
- }
-
- /// <summary>
- /// Sends the User command to set a user
- /// </summary>
- /// <param name="user"></param>
- public void SendUser(string username, string hostname, string servername, string realname)
- {
- SendTCPMessage(string.Format("USER {0} {1} {2} :{3}", username, hostname, servername, realname));
- }
-
- /// <summary>
- /// Sends the Oper command to authorize the client as a newtork Oper
- /// </summary>
- /// <param name="username"></param>
- /// <param name="password"></param>
- public void SendOper(string username, string password)
- {
- SendTCPMessage(string.Format("OPER {0} {1}", username, password));
- }
-
- /// <summary>
- /// Sends a Quit command to end the client session
- /// </summary>
- /// <param name="message"></param>
- public void SendQuit()
- {
- SendTCPMessage("QUIT");
- }
-
- public void SendQuit(string message)
- {
- SendTCPMessage(string.Format("QUIT :{0}", message));
- }
-
- /// <summary>
- /// Sends a Join command to join a channel
- /// </summary>
- /// <param name="channel"></param>
- public void SendJoin(string channel, string key = "")
- {
- string message = string.Empty;
- message = (key != string.Empty) ? string.Format("{0} {1}", channel, key) : channel;
- SendTCPMessage(string.Format("JOIN {0}", message));
- }
-
- public void SendJoin(List<string> channels, List<string> keys)
- {
- string message = string.Empty;
- string channel_string = string.Empty;
- string key_string = string.Empty;
-
- foreach (string channel in channels)
- {
- channel_string += channel + ",";
- }
- foreach (string key in keys)
- {
- if (key != string.Empty)
- {
- key_string += key + ",";
- }
- }
- channel_string = channel_string.TrimEnd(',');
- key_string = key_string.TrimEnd(',');
-
- message = (key_string != string.Empty) ? string.Format("{0} {1}", channel_string, key_string) : channel_string;
- SendTCPMessage(string.Format("JOIN {0}", message));
- }
-
- /// <summary>
- /// Sends a Part command to leave a channel
- /// </summary>
- /// <param name="channel"></param>
- public void SendPart(string channel)
- {
- SendTCPMessage(string.Format("PART {0}", channel));
- }
-
- public void SendPart(List<string> channels)
- {
- string channel_list = string.Empty;
- foreach (string channel in channels)
- {
- channel_list += channel + ",";
- }
-
- SendPart(channel_list.TrimEnd(','));
- }
-
-
- /// <summary>
- /// Sends a Mode command for either a channel mode or user mode
- /// </summary>
- /// <param name="channel"></param>
- /// <param name="mode"></param>
- public void SendMode(string channel, ChannelModeInfo modeInfo)
- {
- string mode_set = modeInfo.Set ? "+" : "-";
- SendTCPMessage(string.Format("MODE {0} {1} {2}", channel, mode_set + modeInfo.Mode.ToString(), modeInfo.Parameter));
- }
-
- public void SendMode(string channel, List<ChannelModeInfo> modeInfos)
- {
- foreach (ChannelModeInfo modeInfo in modeInfos)
- {
- SendMode(channel, modeInfo);
- }
- }
-
- public void SendMode(string nick, UserModeInfo modeInfo)
- {
- string mode_set = modeInfo.Set ? "+" : "-";
- SendTCPMessage(string.Format("MODE {0} {1}", nick, mode_set + modeInfo.Mode.ToString()));
- }
-
- public void SendMode(string nick, List<UserModeInfo> modeInfos)
- {
- foreach (UserModeInfo modeInfo in modeInfos)
- {
- SendMode(nick, modeInfo);
- }
- }
-
- /// <summary>
- /// Sends a Topic command to change the channels topic or view the current one
- /// </summary>
- /// <param name="channel"></param>
- public void SendTopic(string channel)
- {
- SendTCPMessage(string.Format("TOPIC {0}", channel));
- }
-
- public void SendTopic(string channel, string topic)
- {
- SendTCPMessage(string.Format("TOPIC {0} :{1}", channel, topic));
- }
-
- /// <summary>
- /// Sends a Names command to get a list of visible users
- /// </summary>
- public void SendNames()
- {
- SendTCPMessage("NAMES");
- }
-
- public void SendNames(string channel)
- {
- SendTCPMessage(string.Format("NAMES {0}", channel));
- }
-
- public void SendNames(List<string> channels)
- {
- string channel_list = string.Empty;
- foreach (string channel in channels)
- {
- channel_list += channel + ",";
- }
- SendNames(channel_list.TrimEnd(','));
- }
-
- /// <summary>
- /// Sends a List command to get the topic of channels
- /// </summary>
- public void SendList()
- {
- SendTCPMessage("LIST");
- }
-
- public void SendList(string channel)
- {
- SendTCPMessage(string.Format("LIST {0}", channel));
- }
-
- public void SendList(List<string> channels)
- {
- string channel_list = string.Empty;
- foreach (string channel in channels)
- {
- channel_list += channel + ",";
- }
- SendList(channel_list.TrimEnd(','));
- }
-
- /// <summary>
- /// Sends an Invite command that invites the specified nick to the channel
- /// </summary>
- /// <param name="channel"></param>
- /// <param name="nick"></param>
- public void SendInvite(string channel, string nick)
- {
- SendTCPMessage(string.Format("INVITE {0} {1}", nick, channel));
- }
-
- /// <summary>
- /// Sends a Kick command to remove a user from a channel
- /// </summary>
- /// <param name="channel"></param>
- /// <param name="nick"></param>
- public void SendKick(string channel, string nick)
- {
- SendTCPMessage(string.Format("KICK {0} {1}", channel, nick));
- }
-
- public void SendKick(string channel, string nick, string reason)
- {
- SendTCPMessage(string.Format("KICK {0} {1} :{2}", channel, nick, reason));
- }
-
- /// <summary>
- /// Sends a Version command to the server to get a Version reply
- /// </summary>
- /// <param name="server"></param>
- public void SendVersion(string server)
- {
- SendTCPMessage(string.Format("VERSION {0}", server));
- }
-
- /// <summary>
- /// Sends a Stats command to view Server information and statistics
- /// </summary>
- /// <param name="stat"></param>
- public void SendStats(ServerStat stat)
- {
- SendTCPMessage(string.Format("STATS {0}", stat.ToString()));
- }
-
- public void SendStats(ServerStat stat, string parameter)
- {
- SendTCPMessage(string.Format("STATS {0} {1}", stat.ToString(), parameter));
- }
-
- /// <summary>
- /// Sends a Links command to list all servers matching a mask
- /// </summary>
- /// <param name="mask"></param>
- public void SendLinks(string mask)
- {
- SendTCPMessage(string.Format("LINKS {0}", mask));
- }
-
- public void SendLinks(string server, string mask)
- {
- SendTCPMessage(string.Format("LINKS {0} {1}", mask, server));
- }
-
- /// <summary>
- /// Sends a Time command to query the local server time
- /// </summary>
- public void SendTime()
- {
- SendTCPMessage("TIME");
- }
-
- public void SendTime(string server)
- {
- SendTCPMessage(string.Format("TIME {0}", server));
- }
-
- /// <summary>
- /// Senda a Connect command to have the server try to connect to another server
- /// </summary>
- /// <param name="server"></param>
- public void SendConnect(string server)
- {
- SendTCPMessage(string.Format("CONNECT {0}", server));
- }
-
- public void SendConnect(string server, string originator, int port)
- {
- SendTCPMessage(string.Format("CONNECT {0} {1} {2}", originator, port, server));
- }
-
- /// <summary>
- /// Sends a Trace command to find the route to the target (nick or server)
- /// </summary>
- /// <param name="target"></param>
- public void SendTrace(string target)
- {
- SendTCPMessage(string.Format("TRACE {0}", target));
- }
-
- /// <summary>
- /// Sends an Admin command to get the name of the server Administrator
- /// </summary>
- public void SendAdmin()
- {
- SendTCPMessage("ADMIN");
- }
-
- public void SendAdmin(string host)
- {
- SendTCPMessage(string.Format("ADMIN {0}", host));
- }
-
- /// <summary>
- /// Sends an Info command for a specific server or nick
- /// </summary>
- /// <param name="host"></param>
- public void SendInfo(string host)
- {
- SendTCPMessage(string.Format("INFO {0}", host));
- }
-
- /// <summary>
- /// Sends a Who command to list all public users or matching a mask
- /// </summary>
- public void SendWho()
- {
- SendTCPMessage("WHO");
- }
-
- public void SendWho(string host, bool ops = false)
- {
- string msg = string.Empty;
- if (ops)
- {
- msg = string.Format("WHO {0} o", host);
- }
- else
- {
- msg = string.Format("WHO {0}", host);
- }
- SendTCPMessage(msg);
- }
-
- /// <summary>
- /// Sends a Whois command to get info about a user
- /// </summary>
- /// <param name="nick"></param>
- public void SendWhois(string nick)
- {
- SendTCPMessage(string.Format("WHOIS {0}", nick));
- }
-
- public void SendWhois(string nick, string server)
- {
- SendTCPMessage(string.Format("WHOIS {0} {1}", server, nick));
- }
-
- /// <summary>
- /// Sends a Whowas command to get the nick history of a user
- /// </summary>
- /// <param name="nick"></param>
- public void SendWhowas(string nick)
- {
- SendTCPMessage(string.Format("WHOIS {0}", nick));
- }
-
- public void SendWhowas(string nick, int entries)
- {
- SendTCPMessage(string.Format("WHOIS {0} {1}", nick, entries));
- }
-
- public void SendWhowas(string nick, int entries, string server)
- {
- SendTCPMessage(string.Format("WHOIS {0} {1} {2}", nick, entries, server));
- }
-
- /// <summary>
- /// Sends a Kill command to disconnect a nick
- /// </summary>
- /// <param name="nick"></param>
- /// <param name="comment"></param>
- public void SendKill(string nick, string comment)
- {
- SendTCPMessage(string.Format("KILL {0} {1}", nick, comment));
- }
-
- /// <summary>
- /// Sends a Ping command to the recipient
- /// </summary>
- /// <param name="recipient"></param>
- public void SendPing(string recipient)
- {
- SendTCPMessage(string.Format("PING {0}", recipient));
- }
-
- /// <summary>
- /// Sends a PONG response to respond to a Ping
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="recipient"></param>
- public void SendPong()
- {
- SendTCPMessage("PONG");
- }
-
- public void SendPong(string message)
- {
- SendTCPMessage(string.Format("PONG {0}", message));
- }
-
- public void SendPong(string sender, string recipient)
- {
- SendTCPMessage(string.Format("PONG {0} {1}", sender, recipient));
- }
-
-
- /// <summary>
- /// Sends an Away command to unset away status
- /// </summary>
- public void SendAway()
- {
- SendTCPMessage("AWAY");
- }
-
- /// <summary>
- /// Sends an Away comand to set away status with auto-reply message
- /// </summary>
- /// <param name="message"></param>
- public void SendAway(string message)
- {
- SendTCPMessage(string.Format("AWAY {0}", message));
- }
-
- /// <summary>
- /// Sends a Rehash command to the server to reload it's configuration file
- /// </summary>
- public void SendRehash()
- {
- SendTCPMessage("REHASH");
- }
-
- /// <summary>
- /// Sends a Restart command to the server to restart
- /// </summary>
- public void SendRestart()
- {
- SendTCPMessage("RESTART");
- }
-
- /// <summary>
- /// Sends a Summon command to summon a nick to the server
- /// </summary>
- /// <param name="nick"></param>
- public void SendSummon()
- {
- SendTCPMessage("SUMMON");
- }
-
- public void SendSummon(string nick)
- {
- SendTCPMessage(string.Format("SUMMON {0}", nick));
- }
-
- public void SendSummon(string nick, string host)
- {
- SendTCPMessage(string.Format("SUMMON {0} {1}", nick, host));
- }
-
- /// <summary>
- /// Sends a Users command to get a list of Users from a server
- /// </summary>
- /// <param name="server"></param>
- public void SendUsers(string server)
- {
- SendTCPMessage(string.Format("USERS {0}", server));
- }
-
- /// <summary>
- /// Sends a Wallops command which sends a message to all connected ops
- /// </summary>
- /// <param name="message"></param>
- public void SendWallops(string message)
- {
- SendTCPMessage(string.Format("WALLOPS :{0}", message));
- }
-
- /// <summary>
- /// Sends an Userhost command to up to 5 nicknames to return information about each nick
- /// </summary>
- /// <param name="nicks"></param>
- public void SendUserhost(List<string> nicks)
- {
- string message = string.Empty;
- foreach (string nick in nicks)
- {
- message += " " + nick;
- }
- SendTCPMessage(string.Format("USERHOST {0}", message.Trim()));
- }
-
- /// <summary>
- /// Sends an IsOn command to get a return if the nicks specified are online
- /// </summary>
- /// <param name="nicks"></param>
- public void SendIson(List<string> nicks)
- {
- string message = string.Empty;
- foreach (string nick in nicks)
- {
- message += " " + nick;
- }
- SendTCPMessage(string.Format("ISON {0}", message.Trim()));
- }
- }
- }
|