forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
143 lines (117 loc) · 2.96 KB
/
request.go
File metadata and controls
143 lines (117 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package requests
import (
"net/url"
"strconv"
"strings"
"unicode"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/nyaruka/phonenumbers"
)
type request struct{}
func (input *request) sanitizeAddresses(value []string) []string {
var result []string
for _, address := range value {
result = append(result, input.sanitizeAddress(address))
}
return result
}
func (input *request) sanitizeAddress(value string) string {
value = strings.TrimSpace(value)
if !strings.HasPrefix(value, "+") && input.isDigits(value) && len(value) > 9 {
value = "+" + value
}
if number, err := phonenumbers.Parse(value, phonenumbers.UNKNOWN_REGION); err == nil {
value = phonenumbers.Format(number, phonenumbers.E164)
}
return value
}
func (input *request) sanitizeContact(owner string, contact string) string {
contact = strings.TrimSpace(contact)
if len(contact) < 8 || !input.isDigits(contact) {
return contact
}
regionPhoneNumber, err := phonenumbers.Parse(owner, phonenumbers.UNKNOWN_REGION)
if err != nil {
return contact
}
if number, err := phonenumbers.Parse(contact, phonenumbers.GetRegionCodeForNumber(regionPhoneNumber)); err == nil {
contact = phonenumbers.Format(number, phonenumbers.E164)
}
return contact
}
// sanitizeBool sanitizes a boolean string
func (input *request) sanitizeBool(value string) string {
value = strings.TrimSpace(value)
if value == "1" || strings.ToLower(value) == "true" {
value = "true"
}
if value == "0" || strings.ToLower(value) == "false" {
value = "false"
}
return value
}
func (input *request) sanitizeSIM(value string) string {
if value == entities.SIM1.String() || value == entities.SIM2.String() {
return value
}
return entities.SIM1.String()
}
func (input *request) sanitizeURL(value string) string {
value = strings.TrimSpace(value)
website, err := url.Parse(value)
if err != nil {
return value
}
if website.Scheme == "" {
return "https://" + value
}
return value
}
func (input *request) sanitizeStringPointer(value string) *string {
value = strings.TrimSpace(value)
if value == "" {
return nil
}
return &value
}
func (input *request) removeStringDuplicates(values []string) []string {
cache := map[string]struct{}{}
for _, value := range values {
cache[value] = struct{}{}
}
var result []string
for key := range cache {
result = append(result, key)
}
return result
}
func (input *request) sanitizeMessageID(value string) string {
id := strings.Builder{}
for _, char := range value {
if char == '.' {
return id.String()
}
id.WriteRune(char)
}
return id.String()
}
// getLimit gets the take as a string
func (input *request) getBool(value string) bool {
if value == "true" {
return true
}
return false
}
// getLimit gets the take as a string
func (input *request) getInt(value string) int {
val, _ := strconv.Atoi(value)
return val
}
func (input *request) isDigits(value string) bool {
for _, c := range value {
if !unicode.IsDigit(c) {
return false
}
}
return true
}