Skip to content

Commit 7e5edac

Browse files
committed
Escape CJK characters with uconv
1 parent c6f451d commit 7e5edac

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ WORKDIR /app/
99
ADD . .
1010
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="-w -s" -o /palog .
1111

12-
FROM --platform=${TARGETPLATFORM:-linux/amd64} scratch
12+
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.19
1313
COPY --from=builder /palog /palog
14+
RUN apk add --no-cache icu
1415

1516
ENTRYPOINT ["/palog"]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ Broadcast messages that someone joined/left the game.
1212

1313
## Known issues
1414
* If the message is split via whitespaces, `Broadcast` sends only the first segment
15+
* CKJ characters are corrupted
16+
* https://github.com/miscord-dev/palog/issues/6

main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"log/slog"
66
"os"
7+
"os/exec"
8+
"strings"
79
"time"
810

911
"github.com/miscord-dev/palog/pkg/palrcon"
@@ -18,6 +20,8 @@ var (
1820

1921
timeoutRaw = os.Getenv("TIMEOUT")
2022
timeout time.Duration
23+
24+
uconvLatin = os.Getenv("UCONV_LATIN") != "false"
2125
)
2226

2327
func init() {
@@ -45,6 +49,26 @@ func init() {
4549
}
4650
}
4751

52+
func escapeString(s string) string {
53+
if !uconvLatin {
54+
return s
55+
}
56+
57+
var out strings.Builder
58+
cmd := exec.Command("uconv", "-x", "latin")
59+
cmd.Stdin = strings.NewReader(s)
60+
cmd.Stderr = os.Stderr
61+
cmd.Stdout = &out
62+
63+
err := cmd.Run()
64+
if err != nil {
65+
slog.Error("failed to run uconv", "error", err)
66+
return s
67+
}
68+
69+
return out.String()
70+
}
71+
4872
func main() {
4973
palRCON := palrcon.NewPalRCON(rconEndpoint, rconPassword)
5074
palRCON.SetTimeout(timeout)
@@ -66,6 +90,8 @@ func main() {
6690
}
6791

6892
retriedBoarcast := func(message string) error {
93+
message = escapeString(message)
94+
6995
var err error
7096
for i := 0; i < 10; i++ {
7197
err = palRCON.Broadcast(message)

0 commit comments

Comments
 (0)