File size: 6,595 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package twilio

import (
	"bytes"
	"context"
	"errors"
	"strings"
	"text/template"
	"unicode"

	"github.com/target/goalert/config"
	"github.com/target/goalert/notification"
	"github.com/target/goalert/util"
)

// 160 GSM characters (140 bytes) is the max for a single segment message.
// Multi-segment messages include a 6-byte header limiting to 153 GSM characters
// per segment.
//
// Non-GSM will use UCS-2 encoding, using 2-bytes per character. The max would
// then be 70 or 67 characters for single or multi-segmented messages, respectively.
const maxGSMLen = 160

var alertTempl = template.Must(template.New("alertSMS").Parse(`{{.AppName}}: Alert #{{.AlertID}}: {{.Summary}}
{{- if .Link }}

{{.Link}}{{end}}
{{- if .Code}}

Reply '{{.Code}}a' to ack, '{{.Code}}e' to escalate, '{{.Code}}c' to close.{{end}}`))

var bundleTempl = template.Must(template.New("alertBundleSMS").Parse(`{{.AppName}}: Svc '{{.ServiceName}}': {{.Count}} unacked alert{{if gt .Count 1}}s{{end}}

{{- if .Link }}

	{{.Link}}
{{end}}
{{- if .Code}}
	Reply '{{.Code}}aa' to ack all, '{{.Code}}cc' to close all.{{end}}`))

var statusTempl = template.Must(template.New("alertStatusSMS").Parse(`{{.AppName}}: Alert #{{.AlertID}}{{- if .Summary }}: {{.Summary}}{{end}}

	{{.LogEntry}}`))

const gsmAlphabet = "@∆ 0¡P¿p£!1AQaq$Φ\"2BRbr¥Γ#3CScsèΛ¤4DTdtéΩ%5EUeuùΠ&6FVfvìΨ'7GWgwòΣ(8HXhxÇΘ)9IYiy\n Ξ *:JZjzØ+;KÄkäøÆ,<LÖlö\ræ-=MÑmñÅß.>NÜnüåÉ/?O§oà"

var gsmChr = make(map[rune]bool, len(gsmAlphabet))

func init() {
	for _, r := range gsmAlphabet {
		gsmChr[r] = true
	}
}

func mapGSM(r rune) rune {
	if unicode.IsSpace(r) {
		return ' '
	}

	if !unicode.IsPrint(r) {
		return -1
	}

	if gsmChr[r] {
		return r
	}

	// Map similar characters to keep as much meaning as possible.
	switch r {
	case '_', '|', '~':
		return '-'
	case '[', '{':
		return '('
	case ']', '}':
		return ')'
	case '»':
		return '>'
	case '`', '’', '‘':
		return '\''
	}

	switch {
	case unicode.Is(unicode.Dash, r):
		return '-'
	case unicode.Is(unicode.Quotation_Mark, r):
		return '"'
	}

	// If no substitute, replace with '?'
	return '?'
}

// hasAnyPrefix returns true if any of the prefixes are present in the string.
func hasAnyPrefix(s string, prefixes ...string) bool {
	for _, p := range prefixes {
		if !strings.HasPrefix(s, p) {
			continue
		}

		return true
	}

	return false
}

// canContainURL returns true if the message can contain a URL.
func canContainURL(ctx context.Context, number string) bool {
	if config.FromContext(ctx).General.DisableSMSLinks {
		return false
	}

	return !hasAnyPrefix(number,
		// Non-exhaustive list of dialing codes that forbid URLs.
		"+86", // CN - https://www.twilio.com/guidelines/cn/sms
	)
}

// hasTwoWaySMSSupport returns true if a number supports 2-way SMS messaging (replies).
func hasTwoWaySMSSupport(ctx context.Context, number string) bool {
	if config.FromContext(ctx).Twilio.DisableTwoWaySMS {
		return false
	}

	return !hasAnyPrefix(number,
		// Non-exhaustive list of dialing codes that do not support 2-way SMS.
		"+91",  // IN - https://www.twilio.com/guidelines/in/sms
		"+86",  // CN - https://www.twilio.com/guidelines/cn/sms
		"+502", // GT - https://www.twilio.com/guidelines/gt/sms
		"+506", // CR - https://www.twilio.com/guidelines/cr/sms
		"+507", // PA - https://www.twilio.com/guidelines/pa/sms
		"+84",  // VN - https://www.twilio.com/guidelines/vn/sms
	)
}

func normalizeGSM(str string) (s string) {
	s = strings.Map(mapGSM, str)
	s = strings.ReplaceAll(s, "  ", " ")
	s = strings.TrimSpace(s)
	return s
}

func renderMinGSMSegments(inputs []string, render func(inputs []string) (string, error)) (string, error) {
	for i, s := range inputs {
		inputs[i] = normalizeGSM(s)
	}

	size := maxGSMLen
	for {
		result, err := util.RenderSizeN(size, inputs, func(inputs []string) (string, error) {
			cpy := make([]string, len(inputs))
			for i, s := range inputs {
				cpy[i] = strings.TrimSpace(s)
			}

			return render(cpy)
		})
		if errors.Is(err, util.ErrNoSolution) {
			size += maxGSMLen
			continue
		}
		if err != nil {
			return "", err
		}

		return result, nil
	}
}

// renderAlertMessage will render a SMS message for an Alert.
//
// Non-GSM characters will be replaced with '?' and fields will be
// truncated (as needed) to use the minimum number of message segments.
func renderAlertMessage(appName string, a notification.Alert, link string, code int) (string, error) {
	var buf bytes.Buffer
	var data struct {
		AppName string
		notification.Alert
		Link string
		Code int
	}
	data.AppName = appName
	data.Alert = a
	data.Link = link
	data.Code = code

	result, err := renderMinGSMSegments([]string{a.Summary}, func(inputs []string) (string, error) {
		buf.Reset()
		data.Summary = inputs[0]
		err := alertTempl.Execute(&buf, data)
		if err != nil {
			return "", err
		}
		return buf.String(), nil
	})
	if err != nil {
		return "", err
	}

	return result, nil
}

// renderAlertStatusMessage will render a SMS message for an Alert Status.
//
// Non-GSM characters will be replaced with '?' and fields will be
// truncated (as needed) to use the minimum number of message segments.
func renderAlertStatusMessage(appName string, a notification.AlertStatus) (string, error) {
	var buf bytes.Buffer
	var data struct {
		AppName string
		notification.AlertStatus
	}
	data.AppName = appName
	data.AlertStatus = a
	result, err := renderMinGSMSegments([]string{a.Summary, a.LogEntry}, func(inputs []string) (string, error) {
		buf.Reset()
		data.Summary = inputs[0]
		data.LogEntry = inputs[1]
		err := statusTempl.Execute(&buf, data)
		if err != nil {
			return "", err
		}
		return buf.String(), nil
	})
	if err != nil {
		return "", err
	}

	return result, nil
}

// renderAlertBundleMessage will render an SMS message for an Alert Bundle.
//
// Non-GSM characters will be replaced with '?' and fields will be
// truncated (as needed) to use the minimum number of message segments.
func renderAlertBundleMessage(appName string, a notification.AlertBundle, link string, code int) (string, error) {
	var buf bytes.Buffer

	var data struct {
		AppName string
		notification.AlertBundle
		Link string
		Code int
	}
	data.AppName = appName
	data.AlertBundle = a
	data.Link = link
	data.Code = code

	result, err := renderMinGSMSegments([]string{data.ServiceName}, func(inputs []string) (string, error) {
		buf.Reset()
		data.ServiceName = inputs[0]
		err := bundleTempl.Execute(&buf, data)
		if err != nil {
			return "", err
		}
		return buf.String(), nil
	})
	if err != nil {
		return "", err
	}

	return result, nil
}