Skip to content

Commit

Permalink
Merge pull request #1 from codenoid/patch-sanitize-name
Browse files Browse the repository at this point in the history
fix(input sanitize): Sanitize telegram user's name
  • Loading branch information
codenoid committed Nov 13, 2021
2 parents 98e3dc4 + f9b750a commit 1fa31c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func onJoin(c tele.Context) error {
}

status.UserFullName = c.Sender().FirstName + " " + c.Sender().LastName
status.UserFullName = sanitizeName(status.UserFullName)

db.Set(kvID, status, minikv.DefaultExpiration)

Expand Down
20 changes: 20 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"

tele "gopkg.in/tucnak/telebot.v3"
)
Expand All @@ -24,3 +25,22 @@ func stringInSlice(a string, list []string) bool {
}
return false
}

func removeRedundantSpaces(s string) string {
return strings.Join(strings.Fields(s), " ")
}

func sanitizeName(s string) string {
var result strings.Builder
for i := 0; i < len(s); i++ {
b := s[i]
if ('a' <= b && b <= 'z') ||
('A' <= b && b <= 'Z') ||
('0' <= b && b <= '9') ||
b == ' ' {
result.WriteByte(b)
}
}
clean := removeRedundantSpaces(result.String())
return clean
}

0 comments on commit 1fa31c6

Please sign in to comment.