dtchat: draw WhatsApp pairing QRs as a bitmap (T9.1)
The Go adapter encodes every whatsmeow QR code with github.com/skip2/go-qrcode and sends both the text (qrText) and the module bitmap the UI's request dialog already understands: qr {width,height,data}, one byte per module, 1 = dark, quiet zone included, base64. The fake pairing goes through the same request function, so tests/whatsapp_fake.py now checks a square bitmap, one byte per module, both module values and the empty quiet-zone corner; all 17 checks pass with the stand-alone binary. go.mod/go.sum carry the new module. In-tree build and harness follow.
This commit is contained in:
1 parent
4892aa9c60
commit
3107ff3490
4 files changed
+50
-7
No files matched your search
@@ -3,6 +3,7 @@ module dtchat/whatsapp_adapter
|
||||
go 1.27
|
||||
|
||||
require (
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||
go.mau.fi/whatsmeow v0.0.0-20261116124925-2ecca2c913b9
|
||||
google.golang.org/protobuf v1.36.12
|
||||
modernc.org/sqlite v1.37.0
|
||||
|
||||
@@ -20,6 +20,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/rs/zerolog v1.35.1 h1:m7xQeoiLIiV0BCEY4Hs+j2NG4Gp2o2KPKmhnnLiazKI=
|
||||
github.com/rs/zerolog v1.35.1/go.mod h1:EjML9kdfa/RMA7h/6z6pYmq1ykOuA8/mjWaEvGI+jcw=
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
|
||||
github.com/vektah/gqlparser/v2 v2.5.27 h1:RHPD3JOplpk5mP5JGX8RKZkt2/Vwj/PZv0HxTdwFp0s=
|
||||
github.com/vektah/gqlparser/v2 v2.5.27/go.mod h1:D1/VCZtV3LPnQrcPBeR/q5jkSQIPti0uYCP/RI0gIeo=
|
||||
go.mau.fi/libsignal v0.2.3 h1:kSN5CEcW0DhKWqAo/bsBcQvlv0+MRz/x1tYbA+kuA8w=
|
||||
|
||||
@@ -11,6 +11,7 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -18,22 +19,23 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/skip2/go-qrcode"
|
||||
"go.mau.fi/whatsmeow"
|
||||
waE2E "go.mau.fi/whatsmeow/proto/waE2E"
|
||||
"go.mau.fi/whatsmeow/store/sqlstore"
|
||||
"go.mau.fi/whatsmeow/types"
|
||||
"go.mau.fi/whatsmeow/types/events"
|
||||
waE2E "go.mau.fi/whatsmeow/proto/waE2E"
|
||||
"google.golang.org/protobuf/proto"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
var (
|
||||
outMu sync.Mutex
|
||||
out = bufio.NewWriter(os.Stdout)
|
||||
seq int
|
||||
online bool
|
||||
client *whatsmeow.Client
|
||||
fake = os.Getenv("DTCHAT_WHATSAPP_FAKE") != ""
|
||||
outMu sync.Mutex
|
||||
out = bufio.NewWriter(os.Stdout)
|
||||
seq int
|
||||
online bool
|
||||
client *whatsmeow.Client
|
||||
fake = os.Getenv("DTCHAT_WHATSAPP_FAKE") != ""
|
||||
database string
|
||||
)
|
||||
|
||||
@@ -73,12 +75,39 @@ func hello() {
|
||||
})
|
||||
}
|
||||
|
||||
// qrBitmap renders CODE as one byte per module (1 = dark, quiet zone
|
||||
// included), the shape the UI's QR pixmap decoder expects.
|
||||
func qrBitmap(code string) (int, int, string, bool) {
|
||||
q, err := qrcode.New(code, qrcode.Medium)
|
||||
if err != nil {
|
||||
return 0, 0, "", false
|
||||
}
|
||||
b := q.Bitmap()
|
||||
if len(b) == 0 || len(b[0]) == 0 {
|
||||
return 0, 0, "", false
|
||||
}
|
||||
h, w := len(b), len(b[0])
|
||||
data := make([]byte, w*h)
|
||||
for y := 0; y < h; y++ {
|
||||
for x := 0; x < w; x++ {
|
||||
if b[y][x] {
|
||||
data[y*w+x] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return w, h, base64.StdEncoding.EncodeToString(data), true
|
||||
}
|
||||
|
||||
func request(kind, title, hint, qrText string, secret bool) int {
|
||||
seq++
|
||||
m := map[string]any{"type": "request", "id": seq, "kind": kind,
|
||||
"title": title, "hint": hint, "secret": secret}
|
||||
if qrText != "" {
|
||||
m["qrText"] = qrText
|
||||
if w, h, data, ok := qrBitmap(qrText); ok {
|
||||
m["qr"] = map[string]any{"width": w, "height": h,
|
||||
"data": data}
|
||||
}
|
||||
}
|
||||
emit(m)
|
||||
return seq
|
||||
|
||||
@@ -7,6 +7,7 @@ DTCHAT_WHATSAPP_FAKE=1 replaces whatsmeow with a QR request and an echo, so
|
||||
the IPC path (hello, configure, connect, QR request, online, send,
|
||||
disconnect) is tested without an account.
|
||||
"""
|
||||
import base64
|
||||
import json
|
||||
import queue
|
||||
import subprocess
|
||||
@@ -82,6 +83,16 @@ check(request is not None and request.get("kind") == "qr",
|
||||
"a QR request arrives")
|
||||
if request is not None:
|
||||
check(bool(request.get("qrText")), "the request carries the QR text")
|
||||
qr = request.get("qr")
|
||||
check(qr is not None, "the request carries a QR bitmap")
|
||||
if qr is not None:
|
||||
w = int(qr.get("width", 0))
|
||||
h = int(qr.get("height", 0))
|
||||
data = base64.b64decode(qr.get("data", ""))
|
||||
check(w > 10 and h == w, "QR bitmap has a square size")
|
||||
check(len(data) == w * h, "QR bitmap has one byte per module")
|
||||
check(0 in data and 1 in data, "QR bitmap has light and dark modules")
|
||||
check(data[0] == 0, "QR bitmap starts in the quiet zone")
|
||||
send({"type": "answer", "id": request.get("id"), "value": ""})
|
||||
|
||||
online = False
|
||||
|
||||
Reference in new issue
Block a user