UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.74 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{d as asPositiveFiniteNumber}from"./number-coercion-B3CfI447.mjs";import{g as normalizeUniqueStringEntries}from"./string-normalization-D_oBUpO3.mjs";import{b as resolveGatewayPort,n as DEFAULT_GATEWAY_PORT}from"./paths-D_FOvGon.mjs";import{i as resolveTrustedWindowsCmdExe,n as isWindowsBatchCommand,t as buildWindowsCmdExeCommandLine}from"./windows-command-CYm2xqBW.mjs";import"./config-DkZ8y8Gv.mjs"; //!src/hooks/gmail.ts const DEFAULT_GMAIL_LABEL=`INBOX`;const DEFAULT_GMAIL_TOPIC=`gog-gmail-watch`;const DEFAULT_GMAIL_SUBSCRIPTION=`gog-gmail-watch-push`;const DEFAULT_GMAIL_SERVE_BIND=`127.0.0.1`;const DEFAULT_GMAIL_SERVE_PORT=8788;const DEFAULT_GMAIL_SERVE_PATH=`/gmail-pubsub`;const DEFAULT_GMAIL_MAX_BYTES=2e4;const DEFAULT_GMAIL_RENEW_MINUTES=720;const GMAIL_WATCH_PUSH_TOKEN_ENV=`OPENCLAW_GOG_WATCH_PUSH_TOKEN`;const GMAIL_WATCH_HOOK_TOKEN_ENV=`OPENCLAW_GOG_WATCH_HOOK_TOKEN`;const GMAIL_INSECURE_TOKEN_ARGS_WARNING="Gmail is using legacy Gog token arguments. Other local users may read both the Gmail push token and the OpenClaw hook token and send authenticated requests to enabled OpenClaw hooks. Upgrade Gog to v0.48.0 or a build supporting --token-env and --hook-token-env, then restart the watcher. Run `openclaw doctor --repair` to upgrade an eligible Homebrew installation.";const DEFAULT_HOOKS_PATH=`/hooks`;const GMAIL_WATCH_EXCLUDED_LABELS=`SPAM,TRASH,DRAFT,SENT`;const GMAIL_WATCH_SENSITIVE_FLAGS=/* @__PURE__ */ new Set([`--token`,`--hook-url`,`--hook-token`]);function resolveGmailHookMaxBytes(e){return Math.floor(asPositiveFiniteNumber(e)??2e4)}function mergeHookPresets(e,t){const n=new Set(normalizeUniqueStringEntries(e));n.add(t);return Array.from(n)}function normalizeHooksPath(e){const t=e?.trim()||DEFAULT_HOOKS_PATH;if(t===`/`)return DEFAULT_HOOKS_PATH;return(t.startsWith(`/`)?t:`/${t}`).replace(/\/+$/,``)}function normalizeServePath(e){const t=e?.trim()||`/gmail-pubsub`;if(t===`/`)return`/`;return(t.startsWith(`/`)?t:`/${t}`).replace(/\/+$/,``)}function buildDefaultHookUrl(e,t=DEFAULT_GATEWAY_PORT){const n=normalizeHooksPath(e);const r=new URL(`http://127.0.0.1:${t}`);r.pathname=`${n}/gmail`;return r.toString()}function resolveGmailHookRuntimeConfig(e,t){const n=e.hooks;const r=n?.gmail;const i=t.hookToken??n?.token??``;if(!i)return{ok:false,error:`hooks.token missing (needed for gmail hook)`};const a=t.account??r?.account??``;if(!a)return{ok:false,error:`gmail account required`};const o=t.topic??r?.topic??``;if(!o)return{ok:false,error:`gmail topic required`};const s=t.subscription??r?.subscription??`gog-gmail-watch-push`;const c=t.pushToken??r?.pushToken??``;if(!c)return{ok:false,error:`gmail push token required`};const l=t.hookUrl??r?.hookUrl??buildDefaultHookUrl(n?.path,resolveGatewayPort(e));const u=t.includeBody??r?.includeBody??true;const d=resolveGmailHookMaxBytes(t.maxBytes??r?.maxBytes);const f=Math.floor(asPositiveFiniteNumber(t.renewEveryMinutes??r?.renewEveryMinutes)??720);const p=t.serveBind??r?.serve?.bind??`127.0.0.1`;const m=Math.floor(asPositiveFiniteNumber(t.servePort??r?.serve?.port)??8788);const h=normalizeServePath(normalizeOptionalString(t.servePath??r?.serve?.path));const g=t.tailscaleTarget??r?.tailscale?.target;const _=t.tailscaleMode??r?.tailscale?.mode??`off`;const v=_!==`off`?normalizeOptionalString(g):void 0;const y=normalizeServePath(_!==`off`&&!v?`/`:h);const b=t.tailscalePath??r?.tailscale?.path;const x=normalizeServePath(_!==`off`?b??h:b??y);return{ok:true,value:{account:a,label:t.label??r?.label??`INBOX`,topic:o,subscription:s,pushToken:c,hookToken:i,hookUrl:l,includeBody:u,maxBytes:d,renewEveryMinutes:f,serve:{bind:p,port:m,path:y},tailscale:{mode:_,path:x,target:v}}}}function buildGogWatchStartArgs(e){return[`gmail`,`watch`,`start`,`--account`,e.account,`--label`,e.label,`--topic`,e.topic]}function buildGogWatchServeArgs(e,t=`env`){const n=[`gmail`,`watch`,`serve`,`--account`,e.account,`--bind`,e.serve.bind,`--port`,String(e.serve.port),`--path`,e.serve.path,t===`env`?`--token-env`:`--token`,t===`env`?GMAIL_WATCH_PUSH_TOKEN_ENV:e.pushToken,`--hook-url`,e.hookUrl,t===`env`?`--hook-token-env`:`--hook-token`,t===`env`?GMAIL_WATCH_HOOK_TOKEN_ENV:e.hookToken];if(e.includeBody)n.push(`--include-body`);n.push(`--exclude-labels`,GMAIL_WATCH_EXCLUDED_LABELS);if(e.maxBytes>0)n.push(`--max-bytes`,String(e.maxBytes));return n}function buildGogWatchServeLogArgs(e,t=`env`){return buildGogWatchServeArgs(e,t).filter((e,t,n)=>!GMAIL_WATCH_SENSITIVE_FLAGS.has(e)&&!GMAIL_WATCH_SENSITIVE_FLAGS.has(n[t-1]??``))}function resolveGogServeInvocation(e,t){if(!isWindowsBatchCommand(t))return{command:t,args:e,windowsHide:process.platform===`win32`?true:void 0};return{command:resolveTrustedWindowsCmdExe(),args:[`/d`,`/s`,`/c`,buildWindowsCmdExeCommandLine(t,e)],windowsHide:true,windowsVerbatimArguments:true}}function buildTopicPath(e,t){return`projects/${e}/topics/${t}`}function parseTopicPath(e){const t=e.trim().match(/^projects\/([^/]+)\/topics\/([^/]+)$/i);if(!t)return null;return{projectId:t[1]??``,topicName:t[2]??``}}export{resolveGogServeInvocation as S,normalizeHooksPath as _,DEFAULT_GMAIL_SERVE_PORT as a,resolveGmailHookMaxBytes as b,GMAIL_INSECURE_TOKEN_ARGS_WARNING as c,buildDefaultHookUrl as d,buildGogWatchServeArgs as f,mergeHookPresets as g,buildTopicPath as h,DEFAULT_GMAIL_SERVE_PATH as i,GMAIL_WATCH_HOOK_TOKEN_ENV as l,buildGogWatchStartArgs as m,DEFAULT_GMAIL_MAX_BYTES as n,DEFAULT_GMAIL_SUBSCRIPTION as o,buildGogWatchServeLogArgs as p,DEFAULT_GMAIL_SERVE_BIND as r,DEFAULT_GMAIL_TOPIC as s,DEFAULT_GMAIL_LABEL as t,GMAIL_WATCH_PUSH_TOKEN_ENV as u,normalizeServePath as v,resolveGmailHookRuntimeConfig as x,parseTopicPath as y};