WIP twilio

This commit is contained in:
binwiederhier 2023-05-13 12:26:14 -04:00
parent cea434a57c
commit 539ba43cd1
6 changed files with 33 additions and 9 deletions

View file

@ -129,7 +129,6 @@ type Config struct {
VisitorMessageDailyLimit int
VisitorEmailLimitBurst int
VisitorEmailLimitReplenish time.Duration
VisitorCallDailyLimit int
VisitorAccountCreationLimitBurst int
VisitorAccountCreationLimitReplenish time.Duration
VisitorAuthFailureLimitBurst int

View file

@ -691,12 +691,18 @@ func (s *Server) handlePublishInternal(r *http.Request, v *visitor) (*message, e
return nil, errHTTPTooManyRequestsLimitMessages.With(t)
} else if email != "" && !vrate.EmailAllowed() {
return nil, errHTTPTooManyRequestsLimitEmails.With(t)
} else if call != "" && !vrate.CallAllowed() {
return nil, errHTTPTooManyRequestsLimitCalls.With(t)
} else if call != "" {
call, err = s.convertPhoneNumber(v.User(), call)
if err != nil {
return nil, errHTTPBadRequestInvalidPhoneNumber.With(t)
}
if !vrate.CallAllowed() {
return nil, errHTTPTooManyRequestsLimitCalls.With(t)
}
}
// FIXME check allowed phone numbers
if m.PollID != "" {
m = newPollRequestMessage(t.ID, m.PollID)
}
@ -893,7 +899,7 @@ func (s *Server) parsePublishParams(r *http.Request, m *message) (cache bool, fi
call = readParam(r, "x-call", "call")
if call != "" && s.config.TwilioAccount == "" {
return false, false, "", "", false, errHTTPBadRequestTwilioDisabled
} else if call != "" && !phoneNumberRegex.MatchString(call) {
} else if call != "" && !isBoolValue(call) && !phoneNumberRegex.MatchString(call) {
return false, false, "", "", false, errHTTPBadRequestPhoneNumberInvalid
}
messageStr := strings.ReplaceAll(readParam(r, "x-message", "message", "m"), "\\n", "\n")

View file

@ -31,6 +31,16 @@ const (
</Response>`
)
func (s *Server) convertPhoneNumber(u *user.User, phoneNumber string) (string, error) {
if u == nil {
return "", fmt.Errorf("user is nil")
}
if s.config.TwilioPhoneNumberConverter == nil {
return phoneNumber, nil
}
return s.config.TwilioPhoneNumberConverter(u, phoneNumber)
}
func (s *Server) callPhone(v *visitor, r *http.Request, m *message, to string) {
body := fmt.Sprintf(twilioCallFormat, xmlEscapeText(m.Topic), xmlEscapeText(m.Message), xmlEscapeText(s.messageFooter(v.User(), m)))
data := url.Values{}

View file

@ -18,6 +18,14 @@ func readBoolParam(r *http.Request, defaultValue bool, names ...string) bool {
if value == "" {
return defaultValue
}
return toBool(value)
}
func isBoolValue(value string) bool {
return value == "1" || value == "yes" || value == "true" || value == "0" || value == "no" || value == "false"
}
func toBool(value string) bool {
return value == "1" || value == "yes" || value == "true"
}

View file

@ -24,6 +24,10 @@ const (
// visitorDefaultReservationsLimit is the amount of topic names a user without a tier is allowed to reserve.
// This number is zero, and changing it may have unintended consequences in the web app, or otherwise
visitorDefaultReservationsLimit = int64(0)
// visitorDefaultCallsLimit is the amount of calls a user without a tier is allowed to make.
// This number is zero, because phone numbers have to be verified first.
visitorDefaultCallsLimit = int64(0)
)
// Constants used to convert a tier-user's MessageLimit (see user.Tier) into adequate request limiter
@ -444,7 +448,7 @@ func configBasedVisitorLimits(conf *Config) *visitorLimits {
EmailLimit: replenishDurationToDailyLimit(conf.VisitorEmailLimitReplenish), // Approximation!
EmailLimitBurst: conf.VisitorEmailLimitBurst,
EmailLimitReplenish: rate.Every(conf.VisitorEmailLimitReplenish),
CallLimit: int64(conf.VisitorCallDailyLimit),
CallLimit: visitorDefaultCallsLimit,
ReservationsLimit: visitorDefaultReservationsLimit,
AttachmentTotalSizeLimit: conf.VisitorAttachmentTotalSizeLimit,
AttachmentFileSizeLimit: conf.AttachmentFileSizeLimit,