@@ -3,11 +3,11 @@ import { setTimeout as delay } from "node:timers/promises";
33import type { Logger } from "pino" ;
44
55import type { Config , ChannelName } from "./config.js" ;
6+ import { normalizeWhatsAppId } from "./config.js" ;
67import { BridgeStore } from "./db.js" ;
78import { normalizeEvent } from "./events.js" ;
89import { startHealthServer , type HealthSnapshot } from "./health.js" ;
910import { buildPermissionRules , createClient } from "./opencode.js" ;
10- import { resolvePairingCode } from "./pairing.js" ;
1111import { chunkText , formatInputSummary , truncateText } from "./text.js" ;
1212import { createTelegramAdapter } from "./telegram.js" ;
1313import { createWhatsAppAdapter } from "./whatsapp.js" ;
@@ -25,6 +25,7 @@ type InboundMessage = {
2525 peerId : string ;
2626 text : string ;
2727 raw : unknown ;
28+ fromMe ?: boolean ;
2829} ;
2930
3031type RunState = {
@@ -52,10 +53,11 @@ export async function startBridge(config: Config, logger: Logger) {
5253 const client = createClient ( config ) ;
5354 const store = new BridgeStore ( config . dbPath ) ;
5455 store . seedAllowlist ( "telegram" , config . allowlist . telegram ) ;
55- store . seedAllowlist ( "whatsapp" , config . allowlist . whatsapp ) ;
56-
57- const pairingCode = resolvePairingCode ( store , config . pairingCode ) ;
58- logger . info ( { pairingCode } , "pairing code ready" ) ;
56+ store . seedAllowlist (
57+ "whatsapp" ,
58+ [ ...config . whatsappAllowFrom ] . filter ( ( entry ) => entry !== "*" ) ,
59+ ) ;
60+ store . prunePairingRequests ( ) ;
5961
6062 const adapters = new Map < ChannelName , Adapter > ( ) ;
6163 if ( config . telegramEnabled && config . telegramToken ) {
@@ -182,32 +184,57 @@ export async function startBridge(config: Config, logger: Logger) {
182184 { channel : inbound . channel , peerId : inbound . peerId , length : inbound . text . length } ,
183185 "received message" ,
184186 ) ;
187+ const peerKey = inbound . channel === "whatsapp" ? normalizeWhatsAppId ( inbound . peerId ) : inbound . peerId ;
188+ if ( inbound . channel === "whatsapp" ) {
189+ if ( config . whatsappDmPolicy === "disabled" ) {
190+ return ;
191+ }
185192
186- const allowed = store . isAllowed ( inbound . channel , inbound . peerId ) ;
187- if ( ! allowed ) {
188- const trimmed = inbound . text . trim ( ) ;
189- if ( trimmed . includes ( pairingCode ) ) {
190- store . allowPeer ( inbound . channel , inbound . peerId ) ;
191- const remaining = trimmed . replace ( pairingCode , "" ) . trim ( ) ;
192- if ( remaining ) {
193- await sendText ( inbound . channel , inbound . peerId , "Paired. Processing your message." ) ;
194- } else {
195- await sendText ( inbound . channel , inbound . peerId , "Paired. Send your message again." ) ;
193+ const allowAll = config . whatsappDmPolicy === "open" || config . whatsappAllowFrom . has ( "*" ) ;
194+ const isSelf = Boolean ( inbound . fromMe && config . whatsappSelfChatMode ) ;
195+ const allowed = allowAll || isSelf || store . isAllowed ( "whatsapp" , peerKey ) ;
196+ if ( ! allowed ) {
197+ if ( config . whatsappDmPolicy === "allowlist" ) {
198+ await sendText (
199+ inbound . channel ,
200+ inbound . peerId ,
201+ "Access denied. Ask the owner to allowlist your number." ,
202+ ) ;
196203 return ;
197204 }
198- inbound = { ...inbound , text : remaining } ;
199- } else {
205+
206+ store . prunePairingRequests ( ) ;
207+ const active = store . getPairingRequest ( "whatsapp" , peerKey ) ;
208+ const pending = store . listPairingRequests ( "whatsapp" ) ;
209+ if ( ! active && pending . length >= 3 ) {
210+ await sendText (
211+ inbound . channel ,
212+ inbound . peerId ,
213+ "Pairing queue full. Ask the owner to approve pending requests." ,
214+ ) ;
215+ return ;
216+ }
217+
218+ const code = active ?. code ?? String ( Math . floor ( 100000 + Math . random ( ) * 900000 ) ) ;
219+ if ( ! active ) {
220+ store . createPairingRequest ( "whatsapp" , peerKey , code , 60 * 60_000 ) ;
221+ }
200222 await sendText (
201223 inbound . channel ,
202224 inbound . peerId ,
203- `Pairing required. Reply with code: ${ pairingCode } ` ,
225+ `Pairing required. Ask the owner to approve code: ${ code } ` ,
204226 ) ;
205227 return ;
206228 }
229+ } else if ( config . allowlist [ inbound . channel ] . size > 0 ) {
230+ if ( ! store . isAllowed ( inbound . channel , peerKey ) ) {
231+ await sendText ( inbound . channel , inbound . peerId , "Access denied." ) ;
232+ return ;
233+ }
207234 }
208235
209- const session = store . getSession ( inbound . channel , inbound . peerId ) ;
210- const sessionID = session ?. session_id ?? ( await createSession ( inbound ) ) ;
236+ const session = store . getSession ( inbound . channel , peerKey ) ;
237+ const sessionID = session ?. session_id ?? ( await createSession ( { ... inbound , peerId : peerKey } ) ) ;
211238
212239 enqueue ( sessionID , async ( ) => {
213240 const runState : RunState = {
0 commit comments