Skip to content

Commit

Permalink
Add client-nonce to outgoing private message (#4)
Browse files Browse the repository at this point in the history
Add client-nonce to outgoing private message
  • Loading branch information
MahdiBM authored Apr 6, 2023
2 parents c85709e + 0126a6e commit cecc760
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Sources/TwitchIRC/IncomingMessage/Notice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public struct Notice {
case msgBadCharacters = "msgbadcharacters"
/// Your message was not sent because your email address is banned from this channel.
case msgBannedEmailAlias = "msgbannedemailalias"
/// Your message was not sent because your phone number is banned from this channel.
case msgBannedPhoneNumberAlias = "msgbannedphonenumberalias"
/// Your message was not sent because your account is not in good standing in this channel.
case msgChannelBlocked = "msgchannelblocked"
/// This channel has been suspended.
Expand Down
5 changes: 4 additions & 1 deletion Sources/TwitchIRC/IncomingMessage/UserState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public struct UserState: MessageWithBadges {
public var emoteSets = [String]()
/// The ID of this user state message.
public var id = String()
/// The nonce sent by client in the PRIVMSG.
public var clientNonce: String? = nil
/// Contains info about unused info and parsing problems.
public var parsingLeftOvers = ParsingLeftOvers()

Expand All @@ -34,10 +36,11 @@ public struct UserState: MessageWithBadges {
self.color = parser.string(for: "color")
self.displayName = parser.string(for: "display-name")
self.emoteSets = parser.array(for: "emote-sets")
self.clientNonce = parser.optionalString(for: "client-nonce")
self.id = parser.string(for: "id")

let deprecatedKeys = ["turbo", "mod", "subscriber", "user-type"]
let sometimesUnavailableKeys = ["id"]
let sometimesUnavailableKeys = ["id", "client-nonce"]
self.parsingLeftOvers = parser.getLeftOvers(
excludedUnusedKeys: deprecatedKeys,
excludedUnavailableKeys: sometimesUnavailableKeys
Expand Down
20 changes: 14 additions & 6 deletions Sources/TwitchIRC/OutgoingMessage/1- OutgoingMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
/// An IRC message to be sent to Twitch.
public enum OutgoingMessage {
/// Sends a message to a channel. Channel name must be lowercased.
case privateMessage(to: String, message: String, messageIdToReply: String? = nil)
/// `clientNonce` if provided, will be available in the next `userState` you receive.
case privateMessage(
to: String,
message: String,
messageIdToReply: String? = nil,
clientNonce: String? = nil
)
/// Joins a channel's chat. Channel name must be lowercased.
case join(to: String)
/// Parts from a channel's chat. Channel name must be lowercased.
Expand All @@ -22,13 +28,15 @@ public enum OutgoingMessage {
/// Serializes this message into a string that can be sent to Twitch over IRC.
public func serialize() -> String {
switch self {
case let .privateMessage(channel, message, messageIdToReply):
let prefix: String
case let .privateMessage(channel, message, messageIdToReply, clientNonce):
var prefixes = [String]()
if let messageId = messageIdToReply, !messageId.isEmpty {
prefix = "@reply-parent-msg-id=" + messageId + " "
} else {
prefix = ""
prefixes.append("reply-parent-msg-id=\(messageId)")
}
if let clientNonce = clientNonce, !clientNonce.isEmpty {
prefixes.append("client-nonce=\(clientNonce)")
}
let prefix = prefixes.isEmpty ? "" : "@\(prefixes.joined(separator: ";")) "
return prefix + "PRIVMSG #\(channel) :\(message)"
case let .join(channel):
return "JOIN #\(channel)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ final class OutgoingMessageTests: XCTestCase {
)
}

do {
let message = OutgoingMessage.privateMessage(
to: "ronni",
message: "HeyGuys HeyGuys",
messageIdToReply: "b34ccfc7-4977-403a-8a94-33c6bac34fb8",
clientNonce: "nonci"
)
let serialized = message.serialize()

XCTAssertEqual(
serialized,
"@reply-parent-msg-id=b34ccfc7-4977-403a-8a94-33c6bac34fb8;client-nonce=nonci PRIVMSG #ronni :HeyGuys HeyGuys"
)
}

do {
let message = OutgoingMessage.privateMessage(
to: "ronni",
Expand Down

0 comments on commit cecc760

Please sign in to comment.