Skip to content

Commit 1862cfa

Browse files
committed
fix: strip trunk prefix (0) from vCard phones, safe SQLite DSN paths
- Strip "(0)" trunk prefix before digit extraction in normalizeVCardPhone. Common in UK/European numbers: +44 (0)7700 means +447700, not +4407700. - Use file: URI for WhatsApp SQLite DSN to safely handle paths containing '?' or other special characters.
1 parent 47c7a64 commit 1862cfa

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

internal/whatsapp/contacts.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ func normalizeVCardPhone(raw string) string {
212212
// Check if it starts with + (already has country code).
213213
hasPlus := strings.HasPrefix(raw, "+")
214214

215+
// Strip trunk prefix "(0)" before digit extraction.
216+
// Common in UK/European numbers: +44 (0)7700 means +447700, not +4407700.
217+
raw = strings.ReplaceAll(raw, "(0)", "")
218+
215219
// Strip everything except digits.
216220
digits := nonDigitRe.ReplaceAllString(raw, "")
217221
if digits == "" {

internal/whatsapp/contacts_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func TestNormalizeVCardPhone(t *testing.T) {
2020
{"+44 7700 900000", "+447700900000"},
2121
{"+1-202-555-1234", "+12025551234"},
2222

23+
// Trunk prefix (0) — common in UK/European numbers
24+
{"+44 (0)7700 900000", "+447700900000"},
25+
{"+44(0)20 7123 4567", "+442071234567"},
26+
2327
// 00 prefix (international)
2428
{"003-362-4921221", "+33624921221"},
2529
{"0033624921221", "+33624921221"},

internal/whatsapp/importer.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"fmt"
99
"io"
10+
"net/url"
1011
"os"
1112
"path/filepath"
1213
"strings"
@@ -39,7 +40,14 @@ func (imp *Importer) Import(ctx context.Context, waDBPath string, opts ImportOpt
3940
summary := &ImportSummary{}
4041

4142
// Open WhatsApp DB read-only.
42-
waDB, err := sql.Open("sqlite3", waDBPath+"?mode=ro&_journal_mode=WAL&_busy_timeout=5000")
43+
// Use file: URI to safely handle paths containing '?' or other special characters.
44+
dsn := (&url.URL{
45+
Scheme: "file",
46+
OmitHost: true,
47+
Path: waDBPath,
48+
RawQuery: "mode=ro&_journal_mode=WAL&_busy_timeout=5000",
49+
}).String()
50+
waDB, err := sql.Open("sqlite3", dsn)
4351
if err != nil {
4452
return nil, fmt.Errorf("open whatsapp db: %w", err)
4553
}

0 commit comments

Comments
 (0)