tina4ruby 3.0.0 → 3.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +120 -32
- data/lib/tina4/auth.rb +137 -27
- data/lib/tina4/auto_crud.rb +55 -3
- data/lib/tina4/cli.rb +228 -28
- data/lib/tina4/cors.rb +1 -1
- data/lib/tina4/database.rb +230 -26
- data/lib/tina4/database_result.rb +122 -8
- data/lib/tina4/dev_mailbox.rb +1 -1
- data/lib/tina4/env.rb +1 -1
- data/lib/tina4/frond.rb +314 -7
- data/lib/tina4/gallery/queue/meta.json +1 -1
- data/lib/tina4/gallery/queue/src/routes/api/gallery_queue.rb +314 -16
- data/lib/tina4/localization.rb +1 -1
- data/lib/tina4/messenger.rb +111 -33
- data/lib/tina4/middleware.rb +349 -1
- data/lib/tina4/migration.rb +132 -11
- data/lib/tina4/orm.rb +149 -18
- data/lib/tina4/public/js/tina4-dev-admin.min.js +1 -1
- data/lib/tina4/public/js/tina4js.min.js +47 -0
- data/lib/tina4/query_builder.rb +374 -0
- data/lib/tina4/queue.rb +219 -61
- data/lib/tina4/queue_backends/lite_backend.rb +42 -7
- data/lib/tina4/queue_backends/mongo_backend.rb +126 -0
- data/lib/tina4/rack_app.rb +200 -11
- data/lib/tina4/request.rb +14 -1
- data/lib/tina4/response.rb +26 -0
- data/lib/tina4/response_cache.rb +446 -29
- data/lib/tina4/router.rb +127 -0
- data/lib/tina4/service_runner.rb +1 -1
- data/lib/tina4/session.rb +6 -1
- data/lib/tina4/session_handlers/database_handler.rb +66 -0
- data/lib/tina4/swagger.rb +1 -1
- data/lib/tina4/templates/errors/404.twig +2 -2
- data/lib/tina4/templates/errors/500.twig +1 -1
- data/lib/tina4/validator.rb +174 -0
- data/lib/tina4/version.rb +1 -1
- data/lib/tina4/websocket.rb +23 -4
- data/lib/tina4/websocket_backplane.rb +118 -0
- data/lib/tina4.rb +126 -5
- metadata +40 -3
|
@@ -1,27 +1,325 @@
|
|
|
1
|
-
# Gallery: Queue —
|
|
1
|
+
# Gallery: Queue — interactive queue demo with visual web UI.
|
|
2
|
+
#
|
|
3
|
+
# Uses a SQLite database directly for the demo queue table,
|
|
4
|
+
# matching the Python gallery demo's database-backed approach.
|
|
5
|
+
|
|
6
|
+
require "json"
|
|
7
|
+
|
|
8
|
+
def _gallery_queue_db
|
|
9
|
+
@_gallery_queue_db ||= begin
|
|
10
|
+
db = Tina4::Database.new("sqlite://data/gallery_queue.db")
|
|
11
|
+
unless db.table_exists?("tina4_queue")
|
|
12
|
+
db.execute("CREATE TABLE tina4_queue (
|
|
13
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
14
|
+
topic TEXT NOT NULL,
|
|
15
|
+
data TEXT NOT NULL,
|
|
16
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
17
|
+
priority INTEGER NOT NULL DEFAULT 0,
|
|
18
|
+
attempts INTEGER NOT NULL DEFAULT 0,
|
|
19
|
+
error TEXT,
|
|
20
|
+
available_at TEXT NOT NULL,
|
|
21
|
+
created_at TEXT NOT NULL,
|
|
22
|
+
completed_at TEXT,
|
|
23
|
+
reserved_at TEXT
|
|
24
|
+
)")
|
|
25
|
+
end
|
|
26
|
+
db
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def _gallery_queue_now
|
|
31
|
+
Time.now.utc.iso8601
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
GALLERY_QUEUE_MAX_RETRIES = 3
|
|
35
|
+
|
|
36
|
+
GALLERY_QUEUE_HTML = <<~'HTML'
|
|
37
|
+
<!DOCTYPE html>
|
|
38
|
+
<html lang="en">
|
|
39
|
+
<head>
|
|
40
|
+
<meta charset="UTF-8">
|
|
41
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
42
|
+
<title>Queue Gallery — Tina4 Ruby</title>
|
|
43
|
+
<link rel="stylesheet" href="/css/tina4.min.css">
|
|
44
|
+
</head>
|
|
45
|
+
<body>
|
|
46
|
+
<div class="container mt-4 mb-4">
|
|
47
|
+
<h1>Queue Gallery</h1>
|
|
48
|
+
<p class="text-muted">Interactive demo of Tina4's database-backed job queue. Produce messages, consume them, simulate failures, and inspect dead letters.</p>
|
|
49
|
+
|
|
50
|
+
<div class="row mt-3">
|
|
51
|
+
<div class="col-md-6">
|
|
52
|
+
<div class="card">
|
|
53
|
+
<div class="card-header">Produce a Message</div>
|
|
54
|
+
<div class="card-body">
|
|
55
|
+
<div class="d-flex gap-2">
|
|
56
|
+
<input type="text" id="msgInput" class="form-control" placeholder="Enter a task message, e.g. send-email">
|
|
57
|
+
<button class="btn btn-primary" onclick="produce()">Produce</button>
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
60
|
+
</div>
|
|
61
|
+
</div>
|
|
62
|
+
<div class="col-md-6">
|
|
63
|
+
<div class="card">
|
|
64
|
+
<div class="card-header">Actions</div>
|
|
65
|
+
<div class="card-body d-flex gap-2 flex-wrap">
|
|
66
|
+
<button class="btn btn-success" onclick="consume()">Consume Next</button>
|
|
67
|
+
<button class="btn btn-danger" onclick="failNext()">Fail Next</button>
|
|
68
|
+
<button class="btn btn-warning" onclick="retryFailed()">Retry Failed</button>
|
|
69
|
+
<button class="btn btn-secondary" onclick="refresh()">Refresh</button>
|
|
70
|
+
</div>
|
|
71
|
+
</div>
|
|
72
|
+
</div>
|
|
73
|
+
</div>
|
|
74
|
+
|
|
75
|
+
<div id="alertArea" class="mt-3"></div>
|
|
76
|
+
|
|
77
|
+
<div class="card mt-3">
|
|
78
|
+
<div class="card-header d-flex justify-content-between align-items-center">
|
|
79
|
+
<span>Queue Messages</span>
|
|
80
|
+
<small class="text-muted" id="lastRefresh"></small>
|
|
81
|
+
</div>
|
|
82
|
+
<div class="card-body p-0">
|
|
83
|
+
<table class="table table-striped mb-0">
|
|
84
|
+
<thead>
|
|
85
|
+
<tr>
|
|
86
|
+
<th>ID</th>
|
|
87
|
+
<th>Data</th>
|
|
88
|
+
<th>Status</th>
|
|
89
|
+
<th>Attempts</th>
|
|
90
|
+
<th>Error</th>
|
|
91
|
+
<th>Created</th>
|
|
92
|
+
</tr>
|
|
93
|
+
</thead>
|
|
94
|
+
<tbody id="queueBody">
|
|
95
|
+
<tr><td colspan="6" class="text-center text-muted">Loading...</td></tr>
|
|
96
|
+
</tbody>
|
|
97
|
+
</table>
|
|
98
|
+
</div>
|
|
99
|
+
</div>
|
|
100
|
+
</div>
|
|
101
|
+
|
|
102
|
+
<script>
|
|
103
|
+
function statusBadge(status) {
|
|
104
|
+
var colors = {pending:"primary", reserved:"warning", completed:"success", failed:"danger", dead:"secondary"};
|
|
105
|
+
var color = colors[status] || "secondary";
|
|
106
|
+
return '<span class="badge bg-' + color + '">' + status + '</span>';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function showAlert(msg, type) {
|
|
110
|
+
var area = document.getElementById("alertArea");
|
|
111
|
+
area.innerHTML = '<div class="alert alert-' + type + ' alert-dismissible">' + msg +
|
|
112
|
+
'<button type="button" class="btn-close" onclick="this.parentElement.remove()"></button></div>';
|
|
113
|
+
setTimeout(function(){ area.innerHTML = ""; }, 3000);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function truncate(s, n) {
|
|
117
|
+
if (!s) return "";
|
|
118
|
+
return s.length > n ? s.substring(0, n) + "..." : s;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function refresh() {
|
|
122
|
+
try {
|
|
123
|
+
var r = await fetch("/api/gallery/queue/status");
|
|
124
|
+
var data = await r.json();
|
|
125
|
+
var tbody = document.getElementById("queueBody");
|
|
126
|
+
if (!data.messages || data.messages.length === 0) {
|
|
127
|
+
tbody.innerHTML = '<tr><td colspan="6" class="text-center text-muted">No messages in queue. Produce one above.</td></tr>';
|
|
128
|
+
} else {
|
|
129
|
+
var html = "";
|
|
130
|
+
for (var i = 0; i < data.messages.length; i++) {
|
|
131
|
+
var m = data.messages[i];
|
|
132
|
+
html += "<tr><td>" + m.id + "</td><td><code>" + truncate(m.data, 60) + "</code></td><td>" +
|
|
133
|
+
statusBadge(m.status) + "</td><td>" + m.attempts + "</td><td>" +
|
|
134
|
+
truncate(m.error || "", 40) + "</td><td><small>" + (m.created_at || "") + "</small></td></tr>";
|
|
135
|
+
}
|
|
136
|
+
tbody.innerHTML = html;
|
|
137
|
+
}
|
|
138
|
+
document.getElementById("lastRefresh").textContent = "Updated " + new Date().toLocaleTimeString();
|
|
139
|
+
} catch (e) {
|
|
140
|
+
console.error(e);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function produce() {
|
|
145
|
+
var input = document.getElementById("msgInput");
|
|
146
|
+
var task = input.value.trim() || "demo-task";
|
|
147
|
+
var r = await fetch("/api/gallery/queue/produce", {
|
|
148
|
+
method: "POST", headers: {"Content-Type":"application/json"},
|
|
149
|
+
body: JSON.stringify({task: task, data: {message: task}})
|
|
150
|
+
});
|
|
151
|
+
var d = await r.json();
|
|
152
|
+
showAlert("Produced message: " + task, "success");
|
|
153
|
+
input.value = "";
|
|
154
|
+
refresh();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function consume() {
|
|
158
|
+
var r = await fetch("/api/gallery/queue/consume", {method:"POST"});
|
|
159
|
+
var d = await r.json();
|
|
160
|
+
if (d.consumed) {
|
|
161
|
+
showAlert("Consumed job #" + d.job_id + " successfully", "success");
|
|
162
|
+
} else {
|
|
163
|
+
showAlert(d.message || "Nothing to consume", "info");
|
|
164
|
+
}
|
|
165
|
+
refresh();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async function failNext() {
|
|
169
|
+
var r = await fetch("/api/gallery/queue/fail", {method:"POST"});
|
|
170
|
+
var d = await r.json();
|
|
171
|
+
if (d.failed) {
|
|
172
|
+
showAlert("Deliberately failed job #" + d.job_id, "danger");
|
|
173
|
+
} else {
|
|
174
|
+
showAlert(d.message || "Nothing to fail", "info");
|
|
175
|
+
}
|
|
176
|
+
refresh();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function retryFailed() {
|
|
180
|
+
var r = await fetch("/api/gallery/queue/retry", {method:"POST"});
|
|
181
|
+
var d = await r.json();
|
|
182
|
+
showAlert("Retried " + (d.retried || 0) + " failed message(s)", "warning");
|
|
183
|
+
refresh();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
refresh();
|
|
187
|
+
setInterval(refresh, 2000);
|
|
188
|
+
</script>
|
|
189
|
+
</body>
|
|
190
|
+
</html>
|
|
191
|
+
HTML
|
|
192
|
+
|
|
193
|
+
# ── Render the interactive HTML page ──────────────────────────
|
|
194
|
+
|
|
195
|
+
Tina4::Router.get("/gallery/queue") do |request, response|
|
|
196
|
+
response.html(GALLERY_QUEUE_HTML)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# ── Produce — add a message to the queue ──────────────────────
|
|
2
200
|
|
|
3
201
|
Tina4::Router.post("/api/gallery/queue/produce") do |request, response|
|
|
4
202
|
body = request.body || {}
|
|
5
203
|
task = body["task"] || "default-task"
|
|
6
204
|
data = body["data"] || {}
|
|
7
205
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
206
|
+
db = _gallery_queue_db
|
|
207
|
+
now = _gallery_queue_now
|
|
208
|
+
payload = JSON.generate({ task: task, data: data })
|
|
209
|
+
|
|
210
|
+
db.execute(
|
|
211
|
+
"INSERT INTO tina4_queue (topic, data, status, priority, attempts, available_at, created_at) VALUES (?, ?, 'pending', 0, 0, ?, ?)",
|
|
212
|
+
["gallery-tasks", payload, now, now]
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
row = db.fetch_one("SELECT last_insert_rowid() as last_id")
|
|
216
|
+
job_id = row ? row["last_id"] : 0
|
|
217
|
+
|
|
218
|
+
response.json({ queued: true, task: task, job_id: job_id }, 201)
|
|
17
219
|
end
|
|
18
220
|
|
|
221
|
+
# ── Status — list all messages with statuses ──────────────────
|
|
222
|
+
|
|
19
223
|
Tina4::Router.get("/api/gallery/queue/status") do |request, response|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
224
|
+
db = _gallery_queue_db
|
|
225
|
+
|
|
226
|
+
result = db.fetch(
|
|
227
|
+
"SELECT * FROM tina4_queue WHERE topic = ? ORDER BY id DESC",
|
|
228
|
+
["gallery-tasks"],
|
|
229
|
+
limit: 100
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
messages = []
|
|
233
|
+
counts = { pending: 0, reserved: 0, completed: 0, failed: 0 }
|
|
234
|
+
|
|
235
|
+
(result.respond_to?(:records) ? result.records : (result || [])).each do |row|
|
|
236
|
+
status = row["status"] || "pending"
|
|
237
|
+
attempts = (row["attempts"] || 0).to_i
|
|
238
|
+
|
|
239
|
+
display_status = status
|
|
240
|
+
if status == "failed" && attempts >= GALLERY_QUEUE_MAX_RETRIES
|
|
241
|
+
display_status = "dead"
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
counts[status.to_sym] = (counts[status.to_sym] || 0) + 1 if counts.key?(status.to_sym)
|
|
245
|
+
|
|
246
|
+
messages << {
|
|
247
|
+
id: row["id"],
|
|
248
|
+
data: row["data"] || "",
|
|
249
|
+
status: display_status,
|
|
250
|
+
attempts: attempts,
|
|
251
|
+
error: row["error"] || "",
|
|
252
|
+
created_at: row["created_at"] || ""
|
|
253
|
+
}
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
response.json({
|
|
257
|
+
topic: "gallery-tasks",
|
|
258
|
+
messages: messages,
|
|
259
|
+
counts: counts
|
|
260
|
+
})
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# ── Consume — process the next pending message ────────────────
|
|
264
|
+
|
|
265
|
+
Tina4::Router.post("/api/gallery/queue/consume") do |request, response|
|
|
266
|
+
db = _gallery_queue_db
|
|
267
|
+
now = _gallery_queue_now
|
|
268
|
+
|
|
269
|
+
row = db.fetch_one(
|
|
270
|
+
"SELECT * FROM tina4_queue WHERE topic = ? AND status = 'pending' AND available_at <= ? ORDER BY priority DESC, id ASC",
|
|
271
|
+
["gallery-tasks", now]
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
if row.nil?
|
|
275
|
+
response.json({ consumed: false, message: "No pending messages to consume" })
|
|
276
|
+
else
|
|
277
|
+
db.execute(
|
|
278
|
+
"UPDATE tina4_queue SET status = 'completed', completed_at = ? WHERE id = ? AND status = 'pending'",
|
|
279
|
+
[now, row["id"]]
|
|
280
|
+
)
|
|
281
|
+
response.json({ consumed: true, job_id: row["id"], data: row["data"] })
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# ── Fail — deliberately fail the next pending message ─────────
|
|
286
|
+
|
|
287
|
+
Tina4::Router.post("/api/gallery/queue/fail") do |request, response|
|
|
288
|
+
db = _gallery_queue_db
|
|
289
|
+
now = _gallery_queue_now
|
|
290
|
+
|
|
291
|
+
row = db.fetch_one(
|
|
292
|
+
"SELECT * FROM tina4_queue WHERE topic = ? AND status = 'pending' AND available_at <= ? ORDER BY priority DESC, id ASC",
|
|
293
|
+
["gallery-tasks", now]
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
if row.nil?
|
|
297
|
+
response.json({ failed: false, message: "No pending messages to fail" })
|
|
298
|
+
else
|
|
299
|
+
db.execute(
|
|
300
|
+
"UPDATE tina4_queue SET status = 'failed', error = ?, attempts = attempts + 1 WHERE id = ?",
|
|
301
|
+
["Deliberately failed via gallery demo", row["id"]]
|
|
302
|
+
)
|
|
303
|
+
response.json({ failed: true, job_id: row["id"], data: row["data"] })
|
|
26
304
|
end
|
|
27
305
|
end
|
|
306
|
+
|
|
307
|
+
# ── Retry — re-queue failed messages ──────────────────────────
|
|
308
|
+
|
|
309
|
+
Tina4::Router.post("/api/gallery/queue/retry") do |request, response|
|
|
310
|
+
db = _gallery_queue_db
|
|
311
|
+
now = _gallery_queue_now
|
|
312
|
+
|
|
313
|
+
db.execute(
|
|
314
|
+
"UPDATE tina4_queue SET status = 'pending', available_at = ? WHERE topic = ? AND status = 'failed' AND attempts < ?",
|
|
315
|
+
[now, "gallery-tasks", GALLERY_QUEUE_MAX_RETRIES]
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
row = db.fetch_one(
|
|
319
|
+
"SELECT COUNT(*) as cnt FROM tina4_queue WHERE topic = ? AND status = 'pending'",
|
|
320
|
+
["gallery-tasks"]
|
|
321
|
+
)
|
|
322
|
+
retried = row ? row["cnt"].to_i : 0
|
|
323
|
+
|
|
324
|
+
response.json({ retried: retried })
|
|
325
|
+
end
|
data/lib/tina4/localization.rb
CHANGED
data/lib/tina4/messenger.rb
CHANGED
|
@@ -1,38 +1,80 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
require "net/
|
|
3
|
+
begin
|
|
4
|
+
require "net/smtp"
|
|
5
|
+
rescue LoadError
|
|
6
|
+
# net-smtp gem needed on Ruby >= 4.0: gem install net-smtp
|
|
7
|
+
end
|
|
8
|
+
begin
|
|
9
|
+
require "net/imap"
|
|
10
|
+
rescue LoadError
|
|
11
|
+
# net-imap gem needed on Ruby >= 4.0: gem install net-imap
|
|
12
|
+
end
|
|
5
13
|
require "base64"
|
|
6
14
|
require "securerandom"
|
|
7
15
|
require "time"
|
|
8
16
|
|
|
9
17
|
module Tina4
|
|
18
|
+
# Tina4 Messenger — Email sending (SMTP) and reading (IMAP).
|
|
19
|
+
#
|
|
20
|
+
# Unified .env-driven configuration with constructor override.
|
|
21
|
+
# Priority: constructor params > .env (TINA4_MAIL_* with SMTP_* fallback) > sensible defaults
|
|
22
|
+
#
|
|
23
|
+
# # .env
|
|
24
|
+
# TINA4_MAIL_HOST=smtp.gmail.com
|
|
25
|
+
# TINA4_MAIL_PORT=587
|
|
26
|
+
# TINA4_MAIL_USERNAME=user@gmail.com
|
|
27
|
+
# TINA4_MAIL_PASSWORD=app-password
|
|
28
|
+
# TINA4_MAIL_FROM=noreply@myapp.com
|
|
29
|
+
# TINA4_MAIL_ENCRYPTION=tls
|
|
30
|
+
# TINA4_MAIL_IMAP_HOST=imap.gmail.com
|
|
31
|
+
# TINA4_MAIL_IMAP_PORT=993
|
|
32
|
+
#
|
|
33
|
+
# mail = Messenger.new # reads from .env
|
|
34
|
+
# mail = Messenger.new(host: "smtp.office365.com", port: 587) # override
|
|
35
|
+
# mail.send(to: "user@test.com", subject: "Welcome", body: "<h1>Hello!</h1>", html: true, text: "Hello!")
|
|
36
|
+
#
|
|
10
37
|
class Messenger
|
|
11
38
|
attr_reader :host, :port, :username, :from_address, :from_name,
|
|
12
|
-
:imap_host, :imap_port, :use_tls
|
|
39
|
+
:imap_host, :imap_port, :use_tls, :encryption
|
|
13
40
|
|
|
14
|
-
# Initialize with SMTP config
|
|
41
|
+
# Initialize with SMTP config.
|
|
42
|
+
# Priority: constructor params > ENV (TINA4_MAIL_* with SMTP_* fallback) > sensible defaults
|
|
15
43
|
def initialize(host: nil, port: nil, username: nil, password: nil,
|
|
16
|
-
from_address: nil, from_name: nil, use_tls:
|
|
44
|
+
from_address: nil, from_name: nil, encryption: nil, use_tls: nil,
|
|
17
45
|
imap_host: nil, imap_port: nil)
|
|
18
|
-
@host = host || ENV["SMTP_HOST"]
|
|
19
|
-
@port = (port || ENV["SMTP_PORT"]
|
|
20
|
-
@username = username || ENV["SMTP_USERNAME"]
|
|
21
|
-
@password = password || ENV["SMTP_PASSWORD"]
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@
|
|
25
|
-
|
|
26
|
-
@
|
|
46
|
+
@host = host || ENV["TINA4_MAIL_HOST"] || ENV["SMTP_HOST"] || "localhost"
|
|
47
|
+
@port = (port || ENV["TINA4_MAIL_PORT"] || ENV["SMTP_PORT"] || 587).to_i
|
|
48
|
+
@username = username || ENV["TINA4_MAIL_USERNAME"] || ENV["SMTP_USERNAME"]
|
|
49
|
+
@password = password || ENV["TINA4_MAIL_PASSWORD"] || ENV["SMTP_PASSWORD"]
|
|
50
|
+
|
|
51
|
+
resolved_from = from_address || ENV["TINA4_MAIL_FROM"] || ENV["SMTP_FROM"]
|
|
52
|
+
@from_address = resolved_from || @username || "noreply@localhost"
|
|
53
|
+
|
|
54
|
+
@from_name = from_name || ENV["TINA4_MAIL_FROM_NAME"] || ENV["SMTP_FROM_NAME"] || ""
|
|
55
|
+
|
|
56
|
+
# Encryption: constructor > .env > backward-compat use_tls > default "tls"
|
|
57
|
+
env_encryption = encryption || ENV["TINA4_MAIL_ENCRYPTION"]
|
|
58
|
+
if env_encryption
|
|
59
|
+
@encryption = env_encryption.downcase
|
|
60
|
+
elsif !use_tls.nil?
|
|
61
|
+
@encryption = use_tls ? "tls" : "none"
|
|
62
|
+
else
|
|
63
|
+
@encryption = "tls"
|
|
64
|
+
end
|
|
65
|
+
@use_tls = %w[tls starttls].include?(@encryption)
|
|
66
|
+
|
|
67
|
+
@imap_host = imap_host || ENV["TINA4_MAIL_IMAP_HOST"] || ENV["IMAP_HOST"] || @host
|
|
68
|
+
@imap_port = (imap_port || ENV["TINA4_MAIL_IMAP_PORT"] || ENV["IMAP_PORT"] || 993).to_i
|
|
27
69
|
end
|
|
28
70
|
|
|
29
71
|
# Send email using Ruby's Net::SMTP
|
|
30
72
|
# Returns { success: true/false, message: "...", id: "..." }
|
|
31
|
-
def send(to:, subject:, body:, html: false, cc: [], bcc: [],
|
|
73
|
+
def send(to:, subject:, body:, html: false, text: nil, cc: [], bcc: [],
|
|
32
74
|
reply_to: nil, attachments: [], headers: {})
|
|
33
75
|
message_id = "<#{SecureRandom.uuid}@#{@host}>"
|
|
34
76
|
raw = build_message(
|
|
35
|
-
to: to, subject: subject, body: body, html: html,
|
|
77
|
+
to: to, subject: subject, body: body, html: html, text: text,
|
|
36
78
|
cc: cc, bcc: bcc, reply_to: reply_to,
|
|
37
79
|
attachments: attachments, headers: headers,
|
|
38
80
|
message_id: message_id
|
|
@@ -179,10 +221,12 @@ module Tina4
|
|
|
179
221
|
end
|
|
180
222
|
end
|
|
181
223
|
|
|
182
|
-
def build_message(to:, subject:, body:, html:, cc:, bcc:, reply_to:,
|
|
224
|
+
def build_message(to:, subject:, body:, html:, text: nil, cc:, bcc:, reply_to:,
|
|
183
225
|
attachments:, headers:, message_id:)
|
|
184
226
|
boundary = "----=_Tina4_#{SecureRandom.hex(16)}"
|
|
227
|
+
alt_boundary = "----=_Tina4Alt_#{SecureRandom.hex(16)}"
|
|
185
228
|
date = Time.now.strftime("%a, %d %b %Y %H:%M:%S %z")
|
|
229
|
+
has_text_alt = !text.nil? && html
|
|
186
230
|
|
|
187
231
|
parts = []
|
|
188
232
|
parts << "From: #{format_address(@from_address, @from_name)}"
|
|
@@ -196,28 +240,61 @@ module Tina4
|
|
|
196
240
|
|
|
197
241
|
headers.each { |k, v| parts << "#{k}: #{v}" }
|
|
198
242
|
|
|
199
|
-
if attachments.empty?
|
|
200
|
-
content_type = html ? "text/html" : "text/plain"
|
|
201
|
-
parts << "Content-Type: #{content_type}; charset=UTF-8"
|
|
202
|
-
parts << "Content-Transfer-Encoding: base64"
|
|
203
|
-
parts << ""
|
|
204
|
-
parts << Base64.encode64(body)
|
|
205
|
-
else
|
|
243
|
+
if !attachments.empty?
|
|
206
244
|
parts << "Content-Type: multipart/mixed; boundary=\"#{boundary}\""
|
|
207
245
|
parts << ""
|
|
208
|
-
# Body part
|
|
209
|
-
content_type = html ? "text/html" : "text/plain"
|
|
210
246
|
parts << "--#{boundary}"
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
247
|
+
|
|
248
|
+
# Body part (with optional text alternative)
|
|
249
|
+
if has_text_alt
|
|
250
|
+
parts << "Content-Type: multipart/alternative; boundary=\"#{alt_boundary}\""
|
|
251
|
+
parts << ""
|
|
252
|
+
parts << "--#{alt_boundary}"
|
|
253
|
+
parts << "Content-Type: text/plain; charset=UTF-8"
|
|
254
|
+
parts << "Content-Transfer-Encoding: base64"
|
|
255
|
+
parts << ""
|
|
256
|
+
parts << Base64.encode64(text)
|
|
257
|
+
parts << "--#{alt_boundary}"
|
|
258
|
+
parts << "Content-Type: text/html; charset=UTF-8"
|
|
259
|
+
parts << "Content-Transfer-Encoding: base64"
|
|
260
|
+
parts << ""
|
|
261
|
+
parts << Base64.encode64(body)
|
|
262
|
+
parts << "--#{alt_boundary}--"
|
|
263
|
+
else
|
|
264
|
+
content_type = html ? "text/html" : "text/plain"
|
|
265
|
+
parts << "Content-Type: #{content_type}; charset=UTF-8"
|
|
266
|
+
parts << "Content-Transfer-Encoding: base64"
|
|
267
|
+
parts << ""
|
|
268
|
+
parts << Base64.encode64(body)
|
|
269
|
+
end
|
|
270
|
+
|
|
215
271
|
# Attachment parts
|
|
216
272
|
attachments.each do |attachment|
|
|
217
273
|
parts << "--#{boundary}"
|
|
218
274
|
parts.concat(build_attachment_part(attachment))
|
|
219
275
|
end
|
|
220
276
|
parts << "--#{boundary}--"
|
|
277
|
+
elsif has_text_alt
|
|
278
|
+
# Text alternative without attachments
|
|
279
|
+
parts << "Content-Type: multipart/alternative; boundary=\"#{alt_boundary}\""
|
|
280
|
+
parts << ""
|
|
281
|
+
parts << "--#{alt_boundary}"
|
|
282
|
+
parts << "Content-Type: text/plain; charset=UTF-8"
|
|
283
|
+
parts << "Content-Transfer-Encoding: base64"
|
|
284
|
+
parts << ""
|
|
285
|
+
parts << Base64.encode64(text)
|
|
286
|
+
parts << "--#{alt_boundary}"
|
|
287
|
+
parts << "Content-Type: text/html; charset=UTF-8"
|
|
288
|
+
parts << "Content-Transfer-Encoding: base64"
|
|
289
|
+
parts << ""
|
|
290
|
+
parts << Base64.encode64(body)
|
|
291
|
+
parts << "--#{alt_boundary}--"
|
|
292
|
+
else
|
|
293
|
+
content_type = html ? "text/html" : "text/plain"
|
|
294
|
+
parts << "Content-Type: #{content_type}; charset=UTF-8"
|
|
295
|
+
parts << "Content-Transfer-Encoding: base64"
|
|
296
|
+
parts << ""
|
|
297
|
+
parts << Base64.encode64(body)
|
|
221
298
|
end
|
|
222
299
|
|
|
223
300
|
parts.join("\r\n")
|
|
@@ -440,7 +517,8 @@ module Tina4
|
|
|
440
517
|
def self.create_messenger(**options)
|
|
441
518
|
dev_mode = Tina4::Env.truthy?(ENV["TINA4_DEBUG"])
|
|
442
519
|
|
|
443
|
-
smtp_configured = ENV["
|
|
520
|
+
smtp_configured = (ENV["TINA4_MAIL_HOST"] && !ENV["TINA4_MAIL_HOST"].empty?) ||
|
|
521
|
+
(ENV["SMTP_HOST"] && !ENV["SMTP_HOST"].empty?)
|
|
444
522
|
|
|
445
523
|
if dev_mode && !smtp_configured
|
|
446
524
|
mailbox_dir = options.delete(:mailbox_dir) || ENV["TINA4_MAILBOX_DIR"]
|
|
@@ -457,8 +535,8 @@ module Tina4
|
|
|
457
535
|
|
|
458
536
|
def initialize(mailbox, **options)
|
|
459
537
|
@mailbox = mailbox
|
|
460
|
-
@from_address = options[:from_address] || ENV["SMTP_FROM"] || "dev@localhost"
|
|
461
|
-
@from_name = options[:from_name] || ENV["SMTP_FROM_NAME"] || "Dev Mailer"
|
|
538
|
+
@from_address = options[:from_address] || ENV["TINA4_MAIL_FROM"] || ENV["SMTP_FROM"] || "dev@localhost"
|
|
539
|
+
@from_name = options[:from_name] || ENV["TINA4_MAIL_FROM_NAME"] || ENV["SMTP_FROM_NAME"] || "Dev Mailer"
|
|
462
540
|
end
|
|
463
541
|
|
|
464
542
|
def send(to:, subject:, body:, html: false, cc: [], bcc: [],
|