|
| 1 | +/** |
| 2 | + * Server-only: the Meshtastic auto-responder placeholder-DSL matcher. |
| 3 | + * |
| 4 | + * Lives under src/server/utils (NOT src/utils) because it depends on |
| 5 | + * safeRegex.js -> the native re2 module; src/utils/autoResponderUtils.ts is |
| 6 | + * imported by frontend components (TriggerItem.tsx) and must stay free of |
| 7 | + * Node-native dependencies or the Vite client build fails (UNLOADABLE_DEPENDENCY). |
| 8 | + */ |
| 9 | +import { applyHomoglyphOptimization } from '../../utils/homoglyph.js'; |
| 10 | +import { compileUserRegex } from '../../utils/safeRegex.js'; |
| 11 | + |
| 12 | +export interface AutoResponderMatch { |
| 13 | + matched: boolean; |
| 14 | + /** param name -> extracted value (from original text when possible) */ |
| 15 | + params: Record<string, string>; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Match one incoming message against one Meshtastic auto-responder trigger PATTERN |
| 20 | + * (a single comma-split pattern, e.g. "weather {city}" or "zip {z:\\d{5}}"). |
| 21 | + * Homoglyph-normalizes both sides, parses the {name}/{name:regex} placeholder DSL, |
| 22 | + * builds an anchored capture regex via compileUserRegex, and extracts params from the |
| 23 | + * ORIGINAL text where possible (Unicode preservation). |
| 24 | + */ |
| 25 | +export function matchAutoResponderPattern(patternStr: string, messageText: string): AutoResponderMatch { |
| 26 | + // Normalize trigger pattern through homoglyph mapping to match normalized message text |
| 27 | + const normalizedPatternStr = applyHomoglyphOptimization(patternStr); |
| 28 | + // Normalize message text through homoglyph mapping (Issue #2136) |
| 29 | + const normalizedText = applyHomoglyphOptimization(messageText); |
| 30 | + |
| 31 | + // Extract parameters with optional regex patterns from trigger pattern |
| 32 | + interface ParamSpec { |
| 33 | + name: string; |
| 34 | + pattern?: string; |
| 35 | + } |
| 36 | + const params: ParamSpec[] = []; |
| 37 | + let i = 0; |
| 38 | + |
| 39 | + while (i < normalizedPatternStr.length) { |
| 40 | + if (normalizedPatternStr[i] === '{') { |
| 41 | + const startPos = i + 1; |
| 42 | + let depth = 1; |
| 43 | + let colonPos = -1; |
| 44 | + let endPos = -1; |
| 45 | + |
| 46 | + // Find the matching closing brace, accounting for nested braces in regex patterns |
| 47 | + for (let j = startPos; j < normalizedPatternStr.length && depth > 0; j++) { |
| 48 | + if (normalizedPatternStr[j] === '{') { |
| 49 | + depth++; |
| 50 | + } else if (normalizedPatternStr[j] === '}') { |
| 51 | + depth--; |
| 52 | + if (depth === 0) { |
| 53 | + endPos = j; |
| 54 | + } |
| 55 | + } else if (normalizedPatternStr[j] === ':' && depth === 1 && colonPos === -1) { |
| 56 | + colonPos = j; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (endPos !== -1) { |
| 61 | + const paramName = colonPos !== -1 |
| 62 | + ? normalizedPatternStr.substring(startPos, colonPos) |
| 63 | + : normalizedPatternStr.substring(startPos, endPos); |
| 64 | + const paramPattern = colonPos !== -1 |
| 65 | + ? normalizedPatternStr.substring(colonPos + 1, endPos) |
| 66 | + : undefined; |
| 67 | + |
| 68 | + if (!params.find(p => p.name === paramName)) { |
| 69 | + params.push({ name: paramName, pattern: paramPattern }); |
| 70 | + } |
| 71 | + |
| 72 | + i = endPos + 1; |
| 73 | + } else { |
| 74 | + i++; |
| 75 | + } |
| 76 | + } else { |
| 77 | + i++; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Build regex pattern from trigger by processing it character by character |
| 82 | + let pattern = ''; |
| 83 | + const replacements: Array<{ start: number; end: number; replacement: string }> = []; |
| 84 | + i = 0; |
| 85 | + |
| 86 | + while (i < normalizedPatternStr.length) { |
| 87 | + if (normalizedPatternStr[i] === '{') { |
| 88 | + const startPos = i; |
| 89 | + let depth = 1; |
| 90 | + let endPos = -1; |
| 91 | + |
| 92 | + // Find the matching closing brace |
| 93 | + for (let j = i + 1; j < normalizedPatternStr.length && depth > 0; j++) { |
| 94 | + if (normalizedPatternStr[j] === '{') { |
| 95 | + depth++; |
| 96 | + } else if (normalizedPatternStr[j] === '}') { |
| 97 | + depth--; |
| 98 | + if (depth === 0) { |
| 99 | + endPos = j; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (endPos !== -1) { |
| 105 | + const paramIndex = replacements.length; |
| 106 | + if (paramIndex < params.length) { |
| 107 | + const paramRegex = params[paramIndex].pattern || '[^\\s]+'; |
| 108 | + replacements.push({ |
| 109 | + start: startPos, |
| 110 | + end: endPos + 1, |
| 111 | + replacement: `(${paramRegex})` |
| 112 | + }); |
| 113 | + } |
| 114 | + i = endPos + 1; |
| 115 | + } else { |
| 116 | + i++; |
| 117 | + } |
| 118 | + } else { |
| 119 | + i++; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Build the final pattern by replacing placeholders |
| 124 | + for (let i = 0; i < normalizedPatternStr.length; i++) { |
| 125 | + const replacement = replacements.find(r => r.start === i); |
| 126 | + if (replacement) { |
| 127 | + pattern += replacement.replacement; |
| 128 | + i = replacement.end - 1; // -1 because loop will increment |
| 129 | + } else { |
| 130 | + // Escape special regex characters in literal parts |
| 131 | + const char = normalizedPatternStr[i]; |
| 132 | + if (/[.*+?^${}()|[\]\\]/.test(char)) { |
| 133 | + pattern += '\\' + char; |
| 134 | + } else { |
| 135 | + pattern += char; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + const triggerRegex = compileUserRegex(`^${pattern}$`, 'i'); |
| 141 | + const triggerMatch = normalizedText.match(triggerRegex); |
| 142 | + |
| 143 | + if (triggerMatch) { |
| 144 | + // Extract parameters from original text when possible to preserve full |
| 145 | + // Unicode characters. Homoglyph normalization can mangle Cyrillic words |
| 146 | + // (e.g., "Барнаул" → "Бapнayл") which breaks geocoding APIs. |
| 147 | + // The regex usually matches original text too since param patterns like |
| 148 | + // [^\s]+ accept any non-whitespace character. |
| 149 | + const originalMatch = messageText.match(triggerRegex); |
| 150 | + |
| 151 | + const extractedParams: Record<string, string> = {}; |
| 152 | + params.forEach((param, index) => { |
| 153 | + extractedParams[param.name] = originalMatch?.[index + 1] ?? triggerMatch[index + 1]; |
| 154 | + }); |
| 155 | + return { matched: true, params: extractedParams }; |
| 156 | + } |
| 157 | + |
| 158 | + return { matched: false, params: {} }; |
| 159 | +} |
| 160 | + |
0 commit comments