| 1 |
package signal |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"regexp" |
| 6 |
"strings" |
| 7 |
) |
| 8 |
|
| 9 |
const ( |
| 10 |
// usernamePrefix marks a Signal username recipient. |
| 11 |
usernamePrefix = "u:" |
| 12 |
) |
| 13 |
|
| 14 |
// phoneRegex validates phone number format (with or without + prefix). |
| 15 |
var phoneRegex = regexp.MustCompile(`^\+?[0-9\s)(+-]+$`) |
| 16 |
|
| 17 |
// groupRegex validates group ID format. |
| 18 |
var groupRegex = regexp.MustCompile(`^group\.[a-zA-Z0-9_+/=-]+$`) |
| 19 |
|
| 20 |
// parseRecipients parses recipient phone numbers, group IDs, and usernames from URL path segments. |
| 21 |
// It handles group IDs that may contain "/" characters by accumulating consecutive segments. |
| 22 |
// |
| 23 |
// Parameters: |
| 24 |
// - pathParts: the URL path segments to parse |
| 25 |
// |
| 26 |
// Returns: |
| 27 |
// - []string: the parsed recipients |
| 28 |
// - error: if parsing fails, nil otherwise |
| 29 |
func parseRecipients(pathParts []string) ([]string, error) { |
| 30 |
if len(pathParts) == 0 { |
| 31 |
return nil, ErrNoRecipients |
| 32 |
} |
| 33 |
|
| 34 |
var ( |
| 35 |
recipients []string |
| 36 |
currentGroupID strings.Builder |
| 37 |
) |
| 38 |
|
| 39 |
inGroupID := false |
| 40 |
|
| 41 |
for _, part := range pathParts { |
| 42 |
switch { |
| 43 |
case strings.HasPrefix(part, "group."): |
| 44 |
if inGroupID { |
| 45 |
recipients = append(recipients, currentGroupID.String()) |
| 46 |
} |
| 47 |
|
| 48 |
currentGroupID.Reset() |
| 49 |
currentGroupID.WriteString(part) |
| 50 |
|
| 51 |
inGroupID = true |
| 52 |
|
| 53 |
case inGroupID && !strings.HasPrefix(part, "+") && !isValidUsername(part): |
| 54 |
currentGroupID.WriteString("/") |
| 55 |
currentGroupID.WriteString(part) |
| 56 |
|
| 57 |
default: |
| 58 |
if inGroupID { |
| 59 |
recipients = append(recipients, currentGroupID.String()) |
| 60 |
inGroupID = false |
| 61 |
} |
| 62 |
|
| 63 |
recipients = append(recipients, part) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if inGroupID { |
| 68 |
recipients = append(recipients, currentGroupID.String()) |
| 69 |
} |
| 70 |
|
| 71 |
for _, recipient := range recipients { |
| 72 |
if !isValidRecipient(recipient) { |
| 73 |
return nil, fmt.Errorf("%w: %s", ErrInvalidRecipient, recipient) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return recipients, nil |
| 78 |
} |
| 79 |
|
| 80 |
// batchRecipients splits recipients so each /v2/send payload is a single REST API type. |
| 81 |
// Phones share one batch, usernames share one batch, and each group is its own batch. |
| 82 |
// |
| 83 |
// Parameters: |
| 84 |
// - recipients: validated phone numbers, group IDs, and u: usernames |
| 85 |
// |
| 86 |
// Returns: |
| 87 |
// - [][]string: homogeneous batches |
| 88 |
func batchRecipients(recipients []string) [][]string { |
| 89 |
var ( |
| 90 |
phones []string |
| 91 |
usernames []string |
| 92 |
batches [][]string |
| 93 |
) |
| 94 |
|
| 95 |
for _, recipient := range recipients { |
| 96 |
switch { |
| 97 |
case isValidGroupID(recipient): |
| 98 |
batches = append(batches, []string{recipient}) |
| 99 |
case isValidUsername(recipient): |
| 100 |
usernames = append(usernames, recipient) |
| 101 |
default: |
| 102 |
phones = append(phones, recipient) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
var out [][]string |
| 107 |
if len(phones) > 0 { |
| 108 |
out = append(out, phones) |
| 109 |
} |
| 110 |
|
| 111 |
if len(usernames) > 0 { |
| 112 |
out = append(out, usernames) |
| 113 |
} |
| 114 |
|
| 115 |
return append(out, batches...) |
| 116 |
} |
| 117 |
|
| 118 |
// isValidPhoneNumber checks if the string is a valid phone number. |
| 119 |
// |
| 120 |
// Parameters: |
| 121 |
// - phone: the phone number string to validate |
| 122 |
// |
| 123 |
// Returns: |
| 124 |
// - bool: true if valid, false otherwise |
| 125 |
func isValidPhoneNumber(phone string) bool { |
| 126 |
return phoneRegex.MatchString(phone) |
| 127 |
} |
| 128 |
|
| 129 |
// isValidGroupID checks if the string is a valid group ID. |
| 130 |
// |
| 131 |
// Parameters: |
| 132 |
// - groupID: the group ID string to validate |
| 133 |
// |
| 134 |
// Returns: |
| 135 |
// - bool: true if valid, false otherwise |
| 136 |
func isValidGroupID(groupID string) bool { |
| 137 |
return groupRegex.MatchString(groupID) |
| 138 |
} |
| 139 |
|
| 140 |
// isValidUsername checks if the string is a Signal username recipient. |
| 141 |
// |
| 142 |
// Parameters: |
| 143 |
// - username: the username string to validate |
| 144 |
// |
| 145 |
// Returns: |
| 146 |
// - bool: true if it has a u: prefix and a non-empty remainder |
| 147 |
func isValidUsername(username string) bool { |
| 148 |
rest, ok := strings.CutPrefix(username, usernamePrefix) |
| 149 |
|
| 150 |
return ok && rest != "" |
| 151 |
} |
| 152 |
|
| 153 |
// isValidRecipient reports whether the string is a phone number, group ID, or username. |
| 154 |
// |
| 155 |
// Parameters: |
| 156 |
// - recipient: the recipient string to validate |
| 157 |
// |
| 158 |
// Returns: |
| 159 |
// - bool: true if valid, false otherwise |
| 160 |
func isValidRecipient(recipient string) bool { |
| 161 |
return isValidPhoneNumber(recipient) || isValidGroupID(recipient) || isValidUsername(recipient) |
| 162 |
} |