(() => { 'use strict'; const app = document.getElementById('tasks-app') || document.getElementById('calendar-app') || document.getElementById('contacts-app'); if (!app) return; const calendar = app.id === 'calendar-app'; const contacts = app.id === 'contacts-app'; if (calendar && !app.dataset.actor) return; const ja = document.documentElement.lang.startsWith('ja'); const t = (en, jp) => ja ? jp : en; const api = '/_/api/v1/caldav-credentials'; const el = (tag, text) => { const n = document.createElement(tag); if (text !== undefined) n.textContent = text; return n; }; const button = el('button', '⇄'); button.type = 'button'; button.id = 'tasks-dav-open'; button.title = button.ariaLabel = contacts ? t('CardDAV access', 'CardDAV連携') : t('CalDAV / CLI access', 'CalDAV / CLI連携'); app.querySelector(contacts ? '#contacts-controls' : calendar ? '.calendar-toolbar' : '.tasks-toolbar .tasks-actions').append(button); const dialog = el('dialog'); dialog.id = 'tasks-dav-dialog'; dialog.setAttribute('aria-labelledby', 'tasks-dav-title'); const header = el('header'); header.className = 'tasks-toolbar'; const title = el('h2', t('CalDAV access', '予定・タスクのCalDAV連携')); title.id = 'tasks-dav-title'; if (contacts) title.textContent = t('CardDAV access', '連絡先のCardDAV連携'); const close = el('button', '×'); close.type = 'button'; close.ariaLabel = t('Close', '閉じる'); header.append(title, close); const help = el('p', t('Choose calendars, tasks, or both. Calendar permissions still apply. Existing task passwords remain task-only. Wiki access is not granted. Use HTTPS.', '予定・タスクの対象を選んで発行します。予定には既存のカレンダー権限が適用されます。従来のパスワードはタスク専用のままです。Wikiへのアクセス権は付与されません。HTTPSで接続してください。')); const url = el('input'); url.readOnly = true; url.value = location.origin + '/_/api/v1/caldav/'; url.ariaLabel = 'CalDAV URL'; url.onclick = () => url.select(); if (contacts) { url.value = location.origin + '/_/api/v1/carddav/'; url.ariaLabel = 'CardDAV URL'; help.textContent = t('Private address book access only. Issue a separate password for each device. Existing CalDAV passwords cannot access contacts. Use HTTPS.', '個人アドレス帳専用です。端末ごとにパスワードを発行してください。既存のCalDAVパスワードでは連絡先へアクセスできません。HTTPSで接続してください。'); } const form = el('form'); const label = el('input'); label.required = true; label.maxLength = 100; label.placeholder = t('Device / client name', '端末・クライアント名'); label.ariaLabel = label.placeholder; const access = el('select'); access.ariaLabel = t('Access', 'アクセス権限'); for (const [value, text] of [['read', t('Read only', '読み取り専用')], ['write', t('Read / write', '読み書き')]]) { const option = el('option', text); option.value = value; if (value === 'write' && !calendar && app.dataset.writable !== 'true') option.disabled = true; access.append(option); } const scope = el('select'); scope.ariaLabel = t('Scope', '対象'); const scopes = { tasks: t('Tasks only', 'タスクのみ'), calendar: t('Calendars only', '予定のみ'), all: t('Calendars and tasks', '予定とタスク'), contacts: t('Contacts only', '連絡先のみ') }; for (const [value, text] of Object.entries(scopes)) { if (contacts && value !== 'contacts') continue; const option = el('option', text); option.value = value; option.selected = value === (contacts ? 'contacts' : calendar ? 'calendar' : 'tasks'); scope.append(option); } const days = el('select'); days.ariaLabel = t('Validity', '有効期間'); for (const n of [30, 90, 365]) { const option = el('option', n + t(' days', '日間')); option.value = String(n); option.selected = n === 90; days.append(option); } const issue = el('button', t('Issue password', 'パスワードを発行')); issue.type = 'submit'; form.append(label, scope, access, days, issue); const compatLabel = el('label'); compatLabel.className = 'dav-compat-toggle'; const compat = el('input'); compat.type = 'checkbox'; compat.id = 'tasks-dav-compat'; compat.setAttribute('aria-describedby', 'tasks-dav-compat-help'); compatLabel.append(compat, document.createTextNode(t('OneTask compatibility (calendar-access advertisement)', 'OneTask互換モード(calendar-accessを宣言)'))); const compatHelp = el('p', t('Opt in only for clients that require calendar-access. This is a limited implementation, not full RFC compliance. Task dates are date-only; recurrence, timezone definitions and alarms remain unsupported. Enabling this does not grant additional access.', 'calendar-accessを要求するクライアント向けの互換設定です。完全なRFC準拠ではありません。タスクの日付は日単位のみで、繰り返し・タイムゾーン定義・アラームは引き続き未対応です。アクセス権限は増えません。')); compatHelp.id = 'tasks-dav-compat-help'; compatHelp.className = 'dav-compat-help'; form.append(compatLabel, compatHelp); const updateScope = () => { const card = scope.value === 'contacts'; compatLabel.hidden = compatHelp.hidden = card; if (card) compat.checked = false; url.value = location.origin + (card ? '/_/api/v1/carddav/' : '/_/api/v1/caldav/'); }; scope.onchange = updateScope; updateScope(); const secret = el('section'); secret.hidden = true; const status = el('p'); status.role = 'status'; const tableWrap = el('div'); tableWrap.className = 'task-dav-table'; const table = el('table'); tableWrap.append(table); const note = el('p', t('Clients: chroncal, vdirsyncer + Todoman. Task start/due accept dates only. Recurring events and alarms cannot be created via DAV. Calendar events are deleted; tasks are archived.', '連携例:chroncal、vdirsyncer + Todoman。タスクの開始・期限は日付のみ対応。DAV経由の繰り返し予定・アラームの新規作成は未対応です。削除操作では予定は削除、タスクはアーカイブされます。')); if (contacts) note.textContent = t('Use the credential ID as the username, not your account name. Deletes go to trash. Changes use ETags; stale edits are rejected. Shared books and remote-source connectors are not available.', 'ユーザー名にはアカウント名ではなく認証情報IDを使います。削除した連絡先は削除済み一覧から復元できます。ETagで競合を検知し、古い編集内容は上書きしません。共有アドレス帳・外部サービス取り込みは未対応です。'); dialog.append(header, help, url, form, secret, status, tableWrap, note); app.append(dialog); let busy = false, generation = 0; async function request(path = '', options = {}) { const res = await fetch(api + path, { credentials: 'same-origin', ...options, headers: { 'Content-Type': 'application/json', ...options.headers } }); const data = await res.json().catch(() => ({})); if (!res.ok || res.redirected) throw new Error(t('Request failed: ', '処理に失敗しました:') + (data.error || res.status)); return data; } async function load() { const ticket = ++generation; const data = await request(); if (ticket !== generation || !dialog.open) return; table.replaceChildren(); const head = el('tr'); [t('Name', '名称'), t('Scope', '対象'), t('Access', '権限'), t('Expires', '有効期限'), ''].forEach(v => head.append(el('th', v))); const thead = el('thead'); thead.append(head); table.append(thead); const body = el('tbody'); table.append(body); for (const c of data.items) { if (contacts && c.scope !== 'contacts') continue; const row = el('tr'); row.append(el('td', c.label + (c.calendar_access_compat ? t(' · OneTask compatibility', ' · OneTask互換') : '')), el('td', scopes[c.scope || 'tasks']), el('td', c.write ? t('Read / write', '読み書き') : t('Read only', '読み取り専用')), el('td', new Date(c.expires_ms).toLocaleString())); const cell = el('td'), revoke = el('button', t('Revoke', '失効')); revoke.type = 'button'; revoke.onclick = async () => { if (busy || !confirm(t('Revoke this password?', 'このパスワードを失効させますか?'))) return; busy = true; revoke.disabled = true; status.textContent = ''; try { await request('/' + encodeURIComponent(c.id), { method: 'DELETE' }); await load(); } catch (e) { status.textContent = e.message; } finally { busy = false; revoke.disabled = false; } }; cell.append(revoke); row.append(cell); body.append(row); } } function hide() { if (busy) return; ++generation; dialog.close(); secret.replaceChildren(); secret.hidden = true; } close.onclick = hide; dialog.addEventListener('cancel', e => { e.preventDefault(); hide(); }); button.onclick = async () => { if (dialog.open) return; status.textContent = ''; dialog.showModal(); try { await load(); } catch (e) { status.textContent = e.message; } }; form.onsubmit = async e => { e.preventDefault(); if (busy) return; busy = true; issue.disabled = true; status.textContent = ''; try { const data = await request('', { method: 'POST', body: JSON.stringify({ label: label.value, scope: scope.value, write: access.value === 'write', days: Number(days.value), calendar_access_compat: compat.checked }) }); url.value = location.origin + data.url; secret.replaceChildren(el('p', t('Shown only once. Save both values in your password manager before closing.', 'この画面を閉じると再表示できません。両方をパスワードマネージャーなどに保存してください。'))); for (const [name, value] of [[t('Username (credential ID)', 'ユーザー名(認証情報ID)'), data.username], [t('Password', 'パスワード'), data.password]]) { const wrapper = el('label', name), field = el('input'); field.readOnly = true; field.value = value; field.autocomplete = 'off'; field.spellcheck = false; field.onclick = () => field.select(); wrapper.append(field); secret.append(wrapper); } secret.hidden = false; label.value = ''; await load(); } catch (e) { status.textContent = e.message; } finally { busy = false; issue.disabled = false; } }; })();