/// <summary> /// Send a message to a user.</summary> /// <remarks> /// <para>If the target user status is away, the <see cref="Listener.OnAway"/> event will be /// called along with the away message if any. /// </para> /// Possible Errors /// <list type="bullet"> /// <item><description>ERR_NORECIPIENT</description></item> /// <item><description>ERR_NOTEXTTOSEND</description></item> /// <item><description>ERR_NOSUCHNICK</description></item> /// </list> /// </remarks> /// <param name="nick">The target user.</param> /// <param name="message">A message. If the message is too long it will be broken /// up into smaller piecese which will be sent sequentially.</param> /// <exception cref="ArgumentException">If the nickname is not valid or if the message is null or empty.</exception> /// <seealso cref="Listener.OnPrivate"/> public void PrivateMessage(string nick, string message) { if (IsEmpty(message)) { throw new ArgumentException("Private message cannot be null or empty."); } if (!Rfc2812Util.IsValidNick(nick)) { throw new ArgumentException(nick + " is not a valid nickname."); } lock (this) { // 11 is PRIVMSG + 2 x Spaces + : + CR + LF int max = MAX_COMMAND_SIZE - 11 - nick.Length - MAX_HOSTNAME_LEN - MAX_NICKNAME_LEN; if (message.Length > max) { string[] parts = BreakUpMessage(message, max); foreach (string part in parts) { SendMessage("PRIVMSG", nick, part); } } else { SendMessage("PRIVMSG", nick, message); } } }
/// <summary> /// Send a reply in response to a CTCP request. Replies that exceed /// the IRC max length will be truncated. /// </summary> /// <param name="nick">The target's nick name.</param> /// <param name="command">The CTCP command. Should be a string constant /// from <see cref="CtcpUtil"/>.</param> /// <param name="reply">The text of the response.</param> /// <exception cref="ArgumentException">If the nick is invalid, the command /// is empty, or the reply is empty.</exception> /// <see cref="CtcpListener.OnCtcpReply"/> public void CtcpReply(string command, string nick, string reply) { lock (this) { if (!Rfc2812Util.IsValidNick(nick)) { ClearBuffer(); throw new ArgumentException(nick + " is not a valid nick."); } if (reply == null || reply.Trim().Length == 0) { ClearBuffer(); throw new ArgumentException("Reply cannot be null or empty."); } if (command == null || command.Trim().Length == 0) { ClearBuffer(); throw new ArgumentException("The Ctcp command cannot be null or empty."); } // 14 is NOTICE + 3 x Spaces + : + CR + LF + 2xCtcpQuote int max = MAX_COMMAND_SIZE - 14 - nick.Length - command.Length; if (reply.Length > max) { reply = reply.Substring(0, max); } SendMessage("NOTICE", nick, CtcpQuote + command.ToUpper(CultureInfo.InvariantCulture) + " " + reply + CtcpQuote); } }
public void SendNick(string nick) { if (!Rfc2812Util.IsValidNick(nick)) { throw new ArgumentException(nick + " is not a valid nickname."); } SendCommand("NICK " + nick); }
/// <summary> /// Send a CTCP Ping request to another user. /// </summary> /// <remarks>The target may also respond with an error /// or nothing at all if it cannot or does not want to answer. /// </remarks> /// <param name="nick">The target's nick name.</param> /// <param name="timestamp">The timestamp to send to the target user. These /// can be generated by Thresher (<see cref="CtcpUtil.CreateTimestamp"/>) or /// by the client application.</param> /// <exception cref="ArgumentException">If the nick is invalid.</exception> /// <see cref="CtcpListener.OnCtcpPingRequest"/> public void CtcpPingRequest(string nick, string timestamp) { lock (this) { if (!Rfc2812Util.IsValidNick(nick)) { ClearBuffer(); throw new ArgumentException(nick + " is not a valid nick."); } pingList.Add(timestamp); SendMessage("PRIVMSG", nick, CtcpQuote + CtcpUtil.Ping + " " + timestamp + CtcpQuote); } }
void OnNickError(string badNick, string reason) { //If this is our initial connection attempt if (!registered && handleNickFailure) { NameGenerator generator = new NameGenerator(); string nick; do { nick = generator.MakeName(); }while(!Rfc2812Util.IsValidNick(nick) || nick.Length == 1); //Try to reconnect Sender.Register(nick); } }
public void Nick(string newNick) { if (!Rfc2812Util.IsValidNick(newNick)) { throw new ArgumentException(newNick + " is not a valid nickname."); } lock (this) { buffer.Append("NICK"); buffer.Append(SPACE); buffer.Append(newNick); connection.SendCommand(buffer); } }
/// <summary> /// Send back a timestamp so the requester can calculate his /// ping to this client. /// </summary> /// <param name="nick">The target's nick name.</param> /// <param name="timestamp">The timestamp sent by the requester.</param> /// <exception cref="ArgumentException">If the nick is invalid or the timestamp is empty.</exception> /// <see cref="CtcpListener.OnCtcpPingReply"/> public void CtcpPingReply(string nick, string timestamp) { lock (this) { if (!Rfc2812Util.IsValidNick(nick)) { ClearBuffer(); throw new ArgumentException(nick + " is not a valid nick."); } if (timestamp == null || timestamp.Trim().Length == 0) { ClearBuffer(); throw new ArgumentException("Timestamp cannot be null or empty."); } SendMessage("NOTICE", nick, CtcpQuote + CtcpUtil.Ping + " " + timestamp + CtcpQuote); } }
/// <summary> /// Send a CTCP query to another user. /// </summary> /// <remarks>The target may also respond with an error /// or nothing at all if it cannot or does not want to answer. /// </remarks> /// <param name="nick">The target's nick name.</param> /// <param name="command">The CTCP command. Should be a string constant /// from <see cref="CtcpUtil"/>.</param> /// <exception cref="ArgumentException">If the nick is invalid or the command is empty.</exception> /// <see cref="CtcpListener.OnCtcpRequest"/> public void CtcpRequest(string command, string nick) { lock (this) { if (!Rfc2812Util.IsValidNick(nick)) { ClearBuffer(); throw new ArgumentException(nick + " is not a valid nick."); } if (command == null || command.Trim().Length == 0) { ClearBuffer(); throw new ArgumentException("The Ctcp command cannot be null or empty."); } SendMessage("PRIVMSG", nick, CtcpQuote + command.ToUpper(CultureInfo.InvariantCulture) + CtcpQuote); } }
string GetNewNick() { // prefer just adding _ to end of real nick if (Nick.Length < MAX_NICKNAME_LEN) { return(Nick + "_"); } NameGenerator generator = new NameGenerator(); string name; do { name = generator.MakeName(); }while (!Rfc2812Util.IsValidNick(name) || name.Length == 1); return(name); }
/// <summary> /// Change the user's nickname. /// </summary> /// <remarks> /// Possible Errors /// <list type="bullet"> /// <item><description>ERR_NONICKNAMEGIVEN</description></item> /// <item><description>ERR_ERRONEUSNICKNAME</description></item> /// <item><description>ERR_NICKNAMEINUSE</description></item> /// <item><description>ERR_NICKCOLLISION</description></item> /// <item><description>ERR_UNAVAILRESOURCE</description></item> /// <item><description>ERR_RESTRICTED</description></item> /// </list> /// </remarks> /// <param name="newNick"> The new nickname</param> /// <example><code> /// //Make sure and verify that the nick is valid and of the right length /// string nick = GetUserInput(); /// if( Rfc2812Util.IsValidNick( connection, nick) ) { /// connection.Sender.Nick( nick ); /// } /// </code></example> /// <exception cref="ArgumentException">If the nickname is not valid.</exception> /// <seealso cref="Listener.OnNick"/> public void Nick(string newNick) { lock (this) { if (Rfc2812Util.IsValidNick(newNick)) { Buffer.Append("NICK"); Buffer.Append(SPACE); Buffer.Append(newNick); Connection.SendCommand(Buffer); } else { ClearBuffer(); throw new ArgumentException(newNick + " is not a valid nickname."); } } }