Files
reformandClaude Opus 5.5 0ee6fb843b
checks / pytest (push) Has been cancelled
checks / moderation (push) Has been cancelled
checks / node (push) Has been cancelled
IISHKO: Telegram bot, Mini App and gateway
Initial public release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 21:51:30 +03:00

144 lines
3.2 KiB
Go

package main
import (
"errors"
"strconv"
)
const (
// DefaultMuteDuration is one hour, used when /mute has a target but no duration.
DefaultMuteDuration int64 = 3600
// MinDuration is the shortest accepted mute duration.
MinDuration int64 = 30
// MaxDuration is the longest accepted mute duration: 365 days.
MaxDuration int64 = 365 * 24 * 60 * 60
)
var (
errDurationFormat = errors.New("Некорректная длительность. Используйте, например: 30s, 10m, 2h, 1d, 1h30m.")
errDurationRange = errors.New("Длительность должна быть от 30 секунд до 365 дней.")
)
// ParseDuration parses a mute duration. Unit suffixes are s, m, h, d and w;
// components may be combined (1h30m). A bare number is interpreted as minutes.
func ParseDuration(raw string) (int64, error) {
if raw == "" {
return 0, errDurationFormat
}
if isDigits(raw) {
minutes, err := strconv.ParseInt(raw, 10, 64)
if err != nil || minutes > MaxDuration/60 {
return 0, errDurationRange
}
seconds := minutes * 60
if seconds < MinDuration {
return 0, errDurationRange
}
return seconds, nil
}
if !isDurationLike(raw) {
return 0, errDurationFormat
}
var total int64
for i := 0; i < len(raw); {
j := i
for j < len(raw) && raw[j] >= '0' && raw[j] <= '9' {
j++
}
if j == i {
return 0, errDurationFormat
}
amount, err := strconv.ParseInt(raw[i:j], 10, 64)
if err != nil {
return 0, errDurationRange
}
if j >= len(raw) {
return 0, errDurationFormat
}
multiplier, ok := durationUnit(raw[j])
if !ok {
return 0, errDurationFormat
}
if amount > MaxDuration/multiplier {
return 0, errDurationRange
}
component := amount * multiplier
if component > MaxDuration-total {
return 0, errDurationRange
}
total += component
i = j + 1
}
if total < MinDuration || total > MaxDuration {
return 0, errDurationRange
}
return total, nil
}
func durationUnit(value byte) (int64, bool) {
switch value {
case 's', 'S':
return 1, true
case 'm', 'M':
return 60, true
case 'h', 'H':
return 3600, true
case 'd', 'D':
return 86400, true
case 'w', 'W':
return 604800, true
default:
return 0, false
}
}
// isDurationLike reports whether value has the lexical shape of a duration:
// either a bare number (minutes) or one or more number+unit components.
func isDurationLike(value string) bool {
if value == "" {
return false
}
if isDigits(value) {
return true
}
for i := 0; i < len(value); {
if value[i] < '0' || value[i] > '9' {
return false
}
j := i
for j < len(value) && value[j] >= '0' && value[j] <= '9' {
j++
}
if j >= len(value) {
return false
}
if _, ok := durationUnit(value[j]); !ok {
return false
}
i = j + 1
}
return true
}
func isDigits(value string) bool {
if value == "" {
return false
}
for i := range len(value) {
if value[i] < '0' || value[i] > '9' {
return false
}
}
return true
}
func isSignedDigits(value string) bool {
if value == "" {
return false
}
if value[0] == '-' || value[0] == '+' {
return len(value) > 1 && isDigits(value[1:])
}
return isDigits(value)
}