Compare commits
21 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a090186c54 | ||
|
|
9c948f1f3e | ||
|
|
ee0c106a2c | ||
|
|
2f095b79c5 | ||
|
|
52f9438f6c | ||
|
|
64fe0ab3d7 | ||
|
|
d33dd4539a | ||
|
|
486a3da213 | ||
|
|
34764ad6b2 | ||
|
|
7006664fc4 | ||
|
|
b4954163b2 | ||
|
|
b033031d39 | ||
|
|
6659fe5ee6 | ||
|
|
cbb26e87ce | ||
|
|
54ceae3e71 | ||
|
|
b437863f8b | ||
|
|
6eaf3b6083 | ||
|
|
a88178c2ce | ||
|
|
09f84fc493 | ||
|
|
27519378e5 | ||
|
|
2b29a4c9e8 |
19
README.md
19
README.md
|
|
@ -8,6 +8,20 @@ Mail cli for Coh@bit.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
### From [jsr](https://jsr.io)
|
||||||
|
|
||||||
|
```sh
|
||||||
|
deno install \
|
||||||
|
--allow-read \
|
||||||
|
--allow-env \
|
||||||
|
--allow-net=0.0.0.0 \
|
||||||
|
--allow-sys=osRelease,networkInterfaces \
|
||||||
|
--allow-run=/usr/sbin/sendmail,whoami \
|
||||||
|
--global --force --name=cohamail jsr:@cohabit/mailer/cli
|
||||||
|
```
|
||||||
|
|
||||||
|
### From sources
|
||||||
|
|
||||||
> [!WARNING]
|
> [!WARNING]
|
||||||
>
|
>
|
||||||
> Currently bin manually added to [releases](./releases) tab. Prefer `git clone`
|
> Currently bin manually added to [releases](./releases) tab. Prefer `git clone`
|
||||||
|
|
@ -28,3 +42,8 @@ Mail cli for Coh@bit.
|
||||||
# OR
|
# OR
|
||||||
./cohamail <subcommand> -h
|
./cohamail <subcommand> -h
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
You can override default config and templates with your own in
|
||||||
|
`/etc/cohabit/mailer/(config|templates)/`.
|
||||||
|
|
|
||||||
22
cli.ts
22
cli.ts
|
|
@ -1,14 +1,34 @@
|
||||||
import config from './deno.json' with { type: 'json' }
|
import config from './deno.json' with { type: 'json' }
|
||||||
import { Command } from '@cliffy/command/command.ts'
|
import { Command } from '@cliffy/command'
|
||||||
|
import { UpgradeCommand } from '@cliffy/command/upgrade'
|
||||||
|
import { JsrProvider } from '@cliffy/command/upgrade/provider/jsr'
|
||||||
import { cmd as send } from './cli/send.ts'
|
import { cmd as send } from './cli/send.ts'
|
||||||
import { cmd as preview } from './cli/preview.ts'
|
import { cmd as preview } from './cli/preview.ts'
|
||||||
|
|
||||||
|
const upgradeCommand = new UpgradeCommand({
|
||||||
|
args: [
|
||||||
|
'--allow-read',
|
||||||
|
'--allow-env',
|
||||||
|
'--allow-net=0.0.0.0',
|
||||||
|
'--allow-sys=osRelease,networkInterfaces',
|
||||||
|
'--allow-run=/usr/sbin/sendmail,whoami',
|
||||||
|
'--name=cohamail',
|
||||||
|
],
|
||||||
|
provider: [
|
||||||
|
new JsrProvider({
|
||||||
|
package: config.name as `@${string}/${string}`,
|
||||||
|
main: 'cli',
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
const cli = new Command()
|
const cli = new Command()
|
||||||
.name('cohamail')
|
.name('cohamail')
|
||||||
.description('Mail cli for coh@bit.')
|
.description('Mail cli for coh@bit.')
|
||||||
.version(config.version)
|
.version(config.version)
|
||||||
.command('preview', preview)
|
.command('preview', preview)
|
||||||
.command('send', send)
|
.command('send', send)
|
||||||
|
.command('upgrade', upgradeCommand)
|
||||||
|
|
||||||
if (import.meta.main) {
|
if (import.meta.main) {
|
||||||
if (Deno.args.length) {
|
if (Deno.args.length) {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { Input } from '@cliffy/prompt/input.ts'
|
import { Input } from '@cliffy/prompt'
|
||||||
import type { Template } from '../types.ts'
|
import type { Template } from '../types.ts'
|
||||||
import type { JSX } from 'preact'
|
import type { JSX } from 'preact'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import { fromFileUrl } from '@std/path'
|
import { exists } from '@std/fs/exists'
|
||||||
import type { JSX } from 'preact'
|
import type { JSX } from 'preact'
|
||||||
import { EnumType } from '@cliffy/command/mod.ts'
|
import { EnumType } from '@cliffy/command'
|
||||||
import type { Template } from '../types.ts'
|
import type { Template } from '../types.ts'
|
||||||
|
import * as defaultTemplates from '../templates/mod.ts'
|
||||||
|
|
||||||
export const templates: Map<
|
export const templates: Map<
|
||||||
string,
|
string,
|
||||||
|
|
@ -11,25 +12,38 @@ export const templates: Map<
|
||||||
>
|
>
|
||||||
> = new Map()
|
> = new Map()
|
||||||
|
|
||||||
const templatesDirUrl = import.meta.resolve('../templates')
|
//Load default templates
|
||||||
const templatesDir = fromFileUrl(templatesDirUrl)
|
for (const template of Object.values(defaultTemplates)) {
|
||||||
|
//@ts-expect-error types are checked at runtime later
|
||||||
|
templates.set(template.name, template)
|
||||||
|
}
|
||||||
|
|
||||||
//Load templates dynamicaly
|
//Load local templates
|
||||||
for await (
|
const basePath = '/etc/cohabit/mailer/templates'
|
||||||
const template of Deno.readDir(templatesDir)
|
|
||||||
) {
|
if (await exists(basePath)) {
|
||||||
if (
|
for await (const template of Deno.readDir(basePath)) {
|
||||||
template.isFile &&
|
if (!template.isFile) continue
|
||||||
template.name.endsWith('.tsx') &&
|
if (!template.name.endsWith('.tsx')) continue
|
||||||
!template.name.startsWith('_')
|
if (template.name.startsWith('_')) continue
|
||||||
) {
|
|
||||||
const modPath = new URL(
|
const mod = await import(`${basePath}/${template.name}`).catch(
|
||||||
template.name,
|
checkFsErrors,
|
||||||
`${import.meta.resolve('../templates')}/`,
|
|
||||||
)
|
)
|
||||||
const mod = await import(modPath.href)
|
|
||||||
templates.set(mod.default.name, mod.default)
|
templates.set(mod.default.name, mod.default)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkFsErrors(error: Error) {
|
||||||
|
if (error instanceof Deno.errors.PermissionDenied) {
|
||||||
|
throw new Error(
|
||||||
|
'unable to load config file due to read access permissions issues',
|
||||||
|
{
|
||||||
|
cause: error,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
export const templateType = new EnumType([...templates.keys()])
|
export const templateType = new EnumType([...templates.keys()])
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { Command } from '@cliffy/command/mod.ts'
|
import { Command } from '@cliffy/command'
|
||||||
import { renderTemplate } from '../src/template.tsx'
|
import { renderTemplate } from '../src/template.tsx'
|
||||||
import { templates, templateType } from './_templates_loader.ts'
|
import { templates, templateType } from './_templates_loader.ts'
|
||||||
import { promptProps } from './_prompt_template.ts'
|
import { promptProps } from './_prompt_template.ts'
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { Command } from '@cliffy/command/mod.ts'
|
import { Command } from '@cliffy/command'
|
||||||
import { Contact } from '../src/contact.ts'
|
import { Contact } from '../src/contact.ts'
|
||||||
import { send } from '../src/send.ts'
|
import { send } from '../src/send.ts'
|
||||||
import type { Mail } from '../types.ts'
|
import type { Mail } from '../types.ts'
|
||||||
|
|
|
||||||
49
config/loader.ts
Normal file
49
config/loader.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import type { JsonValue } from '@std/json'
|
||||||
|
import defaultAccounts from './account.json' with { type: 'json' }
|
||||||
|
import defaultDkim from './dkim.json' with { type: 'json' }
|
||||||
|
|
||||||
|
function checkFsErrors(error: Error) {
|
||||||
|
if (error instanceof Deno.errors.NotFound) {
|
||||||
|
//use default config file
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (error instanceof Deno.errors.PermissionDenied) {
|
||||||
|
throw new Error(
|
||||||
|
'unable to load config file due to read access permissions issues',
|
||||||
|
{
|
||||||
|
cause: error,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
type JsonRecord = Record<string, JsonValue>
|
||||||
|
export type DkimRecord = {
|
||||||
|
domainName: string
|
||||||
|
keySelector: string
|
||||||
|
privateKey: string
|
||||||
|
}
|
||||||
|
export type AccountRecord = Record<string, {
|
||||||
|
name: string
|
||||||
|
address: string
|
||||||
|
}>
|
||||||
|
|
||||||
|
async function readJsonFile<
|
||||||
|
T extends JsonRecord,
|
||||||
|
>(path: string, defaultValue: T): Promise<T> {
|
||||||
|
const file = await Deno.readTextFile(path).catch(checkFsErrors)
|
||||||
|
const json = JSON.parse(file ?? '{}')
|
||||||
|
return { ...defaultValue, ...json }
|
||||||
|
}
|
||||||
|
|
||||||
|
const basePath = '/ect/cohabit/mailer/config'
|
||||||
|
|
||||||
|
export const accounts = await readJsonFile<AccountRecord>(
|
||||||
|
`${basePath}/account.json`,
|
||||||
|
defaultAccounts,
|
||||||
|
)
|
||||||
|
export const dkim = await readJsonFile<DkimRecord>(
|
||||||
|
`${basePath}/dkim.json`,
|
||||||
|
defaultDkim,
|
||||||
|
)
|
||||||
19
deno.json
19
deno.json
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@cohabit/cohamail",
|
"name": "@cohabit/mailer",
|
||||||
"version": "0.2.1",
|
"version": "0.5.2",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./mod.ts",
|
".": "./mod.ts",
|
||||||
"./cli": "./cli.ts",
|
"./cli": "./cli.ts",
|
||||||
|
|
@ -8,9 +8,8 @@
|
||||||
"./templates": "./templates/mod.ts"
|
"./templates": "./templates/mod.ts"
|
||||||
},
|
},
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"compile": "deno compile --allow-read --allow-env --allow-net=0.0.0.0 --allow-sys=osRelease,networkInterfaces --allow-run=/usr/sbin/sendmail,whoami --output=bin/cohamail --target=x86_64-unknown-linux-gnu ./cli.ts",
|
"compile": "deno compile --allow-read --allow-env --allow-net=0.0.0.0,jsr.io --allow-sys=osRelease,networkInterfaces --allow-run=/usr/sbin/sendmail,whoami --output=bin/mailer --target=x86_64-unknown-linux-gnu ./cli.ts",
|
||||||
"cli": "deno run --allow-read --allow-env --allow-net=0.0.0.0 --allow-sys=osRelease,networkInterfaces --allow-run=/usr/sbin/sendmail,whoami ./cli.ts",
|
"cli": "deno run --allow-read --allow-env --allow-net=0.0.0.0,jsr.io --allow-sys=osRelease,networkInterfaces --allow-run=/usr/sbin/sendmail,whoami ./cli.ts"
|
||||||
"utils:scp": "scp -O -P 55555 -i C:/Users/Julien/.ssh/id_ed25519_cohabit -r $(pwd) julien@cohabit.fr:/home/julien/cohabit_mail"
|
|
||||||
},
|
},
|
||||||
"fmt": {
|
"fmt": {
|
||||||
"singleQuote": true,
|
"singleQuote": true,
|
||||||
|
|
@ -18,8 +17,11 @@
|
||||||
"useTabs": true
|
"useTabs": true
|
||||||
},
|
},
|
||||||
"imports": {
|
"imports": {
|
||||||
"@cliffy/": "https://deno.land/x/cliffy@v1.0.0-rc.3/",
|
"@cliffy/command": "jsr:@cliffy/command@^1.0.0-rc.5",
|
||||||
"@std/path": "jsr:@std/path@^0.221.0",
|
"@cliffy/prompt": "jsr:@cliffy/prompt@^1.0.0-rc.5",
|
||||||
|
"@std/fs": "jsr:@std/fs@^1.0.13",
|
||||||
|
"@std/json": "jsr:@std/json@^1.0.1",
|
||||||
|
"@types/nodemailer": "npm:@types/nodemailer@^6.4.15",
|
||||||
"jsx-email": "npm:jsx-email@^1.10.12",
|
"jsx-email": "npm:jsx-email@^1.10.12",
|
||||||
"nodemailer": "npm:nodemailer@^6.9.13",
|
"nodemailer": "npm:nodemailer@^6.9.13",
|
||||||
"nodemailer-smime": "npm:nodemailer-smime@^1.1.0",
|
"nodemailer-smime": "npm:nodemailer-smime@^1.1.0",
|
||||||
|
|
@ -29,5 +31,6 @@
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"jsx": "react-jsx",
|
"jsx": "react-jsx",
|
||||||
"jsxImportSource": "preact"
|
"jsxImportSource": "preact"
|
||||||
}
|
},
|
||||||
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
import custom from '../config/account.json' with { type: 'json' }
|
import { accounts, dkim } from '../config/loader.ts'
|
||||||
|
|
||||||
|
export type AddressString = `${string}@${string}.${string}`
|
||||||
|
|
||||||
export class Contact {
|
export class Contact {
|
||||||
#name: string
|
#name: string
|
||||||
#address: `${string}@${string}.${string}`
|
#address: AddressString
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
{ name, address }: {
|
{ name, address }: {
|
||||||
name: string
|
name: string
|
||||||
address: `${string}@${string}.${string}`
|
address: AddressString
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
this.#name = name
|
this.#name = name
|
||||||
|
|
@ -15,11 +17,11 @@ export class Contact {
|
||||||
}
|
}
|
||||||
|
|
||||||
static expand(shortName: string): Contact {
|
static expand(shortName: string): Contact {
|
||||||
if (!(shortName in custom)) {
|
if (!(shortName in accounts)) {
|
||||||
throw new Error('unknown short name contact')
|
throw new Error('unknown short name contact')
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name, address } = custom[shortName as keyof typeof custom]
|
const { name, address } = accounts[shortName]
|
||||||
|
|
||||||
if (typeof name !== 'string') {
|
if (typeof name !== 'string') {
|
||||||
throw new SyntaxError(
|
throw new SyntaxError(
|
||||||
|
|
@ -31,7 +33,9 @@ export class Contact {
|
||||||
`missing key "address" in contact short name config for "${shortName}"`,
|
`missing key "address" in contact short name config for "${shortName}"`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (!(/\w+@(\w+\.)?cohabit\.fr/.test(address))) {
|
|
||||||
|
const addressRegExp = new RegExp(String.raw`\w+@(\w+\.)?${dkim.domainName}`)
|
||||||
|
if (!(addressRegExp.test(address))) {
|
||||||
throw new SyntaxError(
|
throw new SyntaxError(
|
||||||
`invalid "address" in contact short name config for "${shortName}"`,
|
`invalid "address" in contact short name config for "${shortName}"`,
|
||||||
)
|
)
|
||||||
|
|
@ -57,18 +61,18 @@ export class Contact {
|
||||||
return `${this.#name} <${this.#address}>`
|
return `${this.#name} <${this.#address}>`
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON(): { name: string; address: AddressString } {
|
||||||
return {
|
return {
|
||||||
name: this.#name,
|
name: this.#name,
|
||||||
address: this.#address,
|
address: this.#address,
|
||||||
} as const
|
} as const
|
||||||
}
|
}
|
||||||
|
|
||||||
get name() {
|
get name(): string {
|
||||||
return this.#name
|
return this.#name
|
||||||
}
|
}
|
||||||
|
|
||||||
get address() {
|
get address(): AddressString {
|
||||||
return this.#address
|
return this.#address
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
|
import type SendmailTransport from 'nodemailer'
|
||||||
import type { Mail } from '../types.ts'
|
import type { Mail } from '../types.ts'
|
||||||
import { renderTemplate } from './template.tsx'
|
import { renderTemplate } from './template.tsx'
|
||||||
import { transporter } from './transporter.ts'
|
import { transporter } from './transporter.ts'
|
||||||
|
|
||||||
export async function send(mail: Mail) {
|
export async function send(
|
||||||
|
mail: Mail,
|
||||||
|
): Promise<SendmailTransport.SentMessageInfo> {
|
||||||
const { html, text } = await renderTemplate(mail.body)
|
const { html, text } = await renderTemplate(mail.body)
|
||||||
|
|
||||||
return (await transporter()).sendMail({
|
return (await transporter()).sendMail({
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
import React from 'preact/compat' //for jsx-email
|
import React from 'preact/compat' //for jsx-email
|
||||||
import { render } from 'jsx-email'
|
import { render } from 'jsx-email'
|
||||||
// @deno-types="npm:turndown"
|
// @deno-types="npm:turndown@^7.1.3"
|
||||||
import Turndown from 'turndown'
|
import Turndown from 'turndown'
|
||||||
import type { JSX } from 'preact'
|
import type { JSX } from 'preact'
|
||||||
|
|
||||||
|
console.assert(React !== undefined)
|
||||||
|
|
||||||
const htmlToMd = new Turndown({
|
const htmlToMd = new Turndown({
|
||||||
headingStyle: 'atx',
|
headingStyle: 'atx',
|
||||||
codeBlockStyle: 'fenced',
|
codeBlockStyle: 'fenced',
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
// @deno-types="npm:@types/nodemailer"
|
// @deno-types="npm:@types/nodemailer@^6.4.15"
|
||||||
import nodemailer from 'nodemailer'
|
import nodemailer from 'nodemailer'
|
||||||
import dkim from '../config/dkim.json' with { type: 'json' }
|
import { dkim } from '../config/loader.ts'
|
||||||
|
|
||||||
export async function transporter() {
|
export async function transporter() {
|
||||||
const dkimPath = dkim.privateKey
|
const dkimPath = dkim.privateKey
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ function MagicLink(
|
||||||
ip?: string
|
ip?: string
|
||||||
endpoint: string
|
endpoint: string
|
||||||
},
|
},
|
||||||
) {
|
): JSX.Element {
|
||||||
return (
|
return (
|
||||||
<Html lang='fr' style={{ fontSize: '14px' }}>
|
<Html lang='fr' style={{ fontSize: '14px' }}>
|
||||||
<BaseStyle />
|
<BaseStyle />
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,11 @@ import { Body, Container, Html, Markdown, Preview } from 'jsx-email'
|
||||||
import { Signature } from './components/Signature.tsx'
|
import { Signature } from './components/Signature.tsx'
|
||||||
import type { Template } from '../types.ts'
|
import type { Template } from '../types.ts'
|
||||||
import { BaseStyle, bodyCss, messageCss, textCss } from './styles/base.tsx'
|
import { BaseStyle, bodyCss, messageCss, textCss } from './styles/base.tsx'
|
||||||
|
import type { JSX } from 'preact'
|
||||||
|
|
||||||
function Message(
|
function Message(
|
||||||
{ summary, body }: { summary?: string; body: string },
|
{ summary, body }: { summary?: string; body: string },
|
||||||
) {
|
): JSX.Element {
|
||||||
return (
|
return (
|
||||||
<Html lang='fr' style={{ fontSize: '14px' }}>
|
<Html lang='fr' style={{ fontSize: '14px' }}>
|
||||||
<BaseStyle />
|
<BaseStyle />
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ function Welcome(
|
||||||
login: string
|
login: string
|
||||||
endpoint?: string
|
endpoint?: string
|
||||||
},
|
},
|
||||||
) {
|
): JSX.Element {
|
||||||
return (
|
return (
|
||||||
<Html lang='fr' style={{ fontSize: '14px' }}>
|
<Html lang='fr' style={{ fontSize: '14px' }}>
|
||||||
<BaseStyle />
|
<BaseStyle />
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue