diff options
| -rw-r--r-- | src/tagrules.cpp | 49 | ||||
| -rw-r--r-- | src/tagrules.h | 4 | ||||
| -rw-r--r-- | src/tagrulesdialog.cpp | 25 | ||||
| -rw-r--r-- | src/tagrulesdialog.h | 3 | ||||
| -rw-r--r-- | tests/test_tagrules.cpp | 133 |
5 files changed, 208 insertions, 6 deletions
diff --git a/src/tagrules.cpp b/src/tagrules.cpp index 0049278..d88a70f 100644 --- a/src/tagrules.cpp +++ b/src/tagrules.cpp @@ -41,7 +41,7 @@ bool isKnownKey(const QString &key) QStringLiteral("id"), QStringLiteral("stage"), QStringLiteral("enabled"), QStringLiteral("add"), QStringLiteral("remove"), QStringLiteral("query"), - QStringLiteral("note"), + QStringLiteral("note"), QStringLiteral("move"), }; return known.contains(key); } @@ -58,6 +58,12 @@ QStringList stringsOf(const QJsonValue &value) return out; } +/// What `move` may say. mailrules.py and qtmaildirconf.py carry the same two. +bool isMoveRole(const QString &role) +{ + return role == QStringLiteral("spam") || role == QStringLiteral("trash"); +} + /// The id pattern, in one place. mailrules.py enforces the same one; see /// "Changing the shared rule format" in CLAUDE.md before touching it. const QRegularExpression &idPattern() @@ -141,8 +147,12 @@ QStringList TagRules::validate(const QList<TagRule> &rules) if (rule.query.trimmed().isEmpty()) problems.append(QObject::tr("'%1': no query").arg(where)); - if (rule.add.isEmpty() && rule.remove.isEmpty()) - problems.append(QObject::tr("'%1': adds and removes nothing") + if (!rule.move.isEmpty() && !isMoveRole(rule.move)) { + problems.append(QObject::tr("'%1': move '%2' is not spam or trash") + .arg(where, rule.move)); + } + if (rule.add.isEmpty() && rule.remove.isEmpty() && rule.move.isEmpty()) + problems.append(QObject::tr("'%1': adds, removes and moves nothing") .arg(where)); } return problems; @@ -247,8 +257,27 @@ void TagRules::load(const QString &path) rule.add = stringsOf(object.value(QStringLiteral("add"))); rule.remove = stringsOf(object.value(QStringLiteral("remove"))); - if (rule.add.isEmpty() && rule.remove.isEmpty()) { - m_warnings.append(QObject::tr("Rule '%1': adds and removes " + // A move this version cannot act on is kept, raw, in `unknown`, so a + // save writes it back rather than deleting it; and a rule whose only + // action it was still loads, so the dialog shows it and validate() + // refuses the save until it is fixed. Same reasoning as the repaired + // id above. A non-string value (`"move": 5`) is a bad move too. + const QJsonValue moveValue = object.value(QStringLiteral("move")); + const bool badMove = !moveValue.isUndefined() + && moveValue != QJsonValue(QString()) + && !isMoveRole(moveValue.toString()); + if (badMove) { + m_warnings.append(QObject::tr("Rule '%1': move '%2' is not spam " + "or trash; ignored") + .arg(rule.id, + moveValue.toVariant().toString())); + rule.unknown.insert(QStringLiteral("move"), moveValue); + } else { + rule.move = moveValue.toString(); + } + if (!badMove && rule.add.isEmpty() && rule.remove.isEmpty() + && rule.move.isEmpty()) { + m_warnings.append(QObject::tr("Rule '%1': adds, removes and moves " "nothing; dropped").arg(rule.id)); continue; } @@ -283,10 +312,18 @@ bool TagRules::save(const QString &path) const QJsonArray::fromStringList(rule.add)); object.insert(QStringLiteral("remove"), QJsonArray::fromStringList(rule.remove)); + // Only when set: a file with no moves stays byte-identical on save. + if (!rule.move.isEmpty()) + object.insert(QStringLiteral("move"), rule.move); object.insert(QStringLiteral("query"), rule.query); object.insert(QStringLiteral("note"), rule.note); - for (auto it = rule.unknown.begin(); it != rule.unknown.end(); ++it) + for (auto it = rule.unknown.begin(); it != rule.unknown.end(); ++it) { + // A bad move kept from load must not overwrite the role the user + // has since chosen in the dialog. + if (it.key() == QStringLiteral("move") && !rule.move.isEmpty()) + continue; object.insert(it.key(), it.value()); + } array.append(object); } diff --git a/src/tagrules.h b/src/tagrules.h index 4d3f0ab..946329a 100644 --- a/src/tagrules.h +++ b/src/tagrules.h @@ -37,6 +37,10 @@ struct TagRule QString note; ///< Why the rule is shaped this way. Shown in the dialog. QStringList add; QStringList remove; + /// "spam" or "trash", or empty. An account ROLE, never a folder name: the + /// hook resolves it per message to that account's configured folder, so + /// a typo cannot create a folder on the server (item 103, item 214). + QString move; int stage = 50; ///< Ascending. Account tags 10, topic rules 50. bool enabled = true; diff --git a/src/tagrulesdialog.cpp b/src/tagrulesdialog.cpp index e037e37..2e00841 100644 --- a/src/tagrulesdialog.cpp +++ b/src/tagrulesdialog.cpp @@ -162,6 +162,16 @@ TagRulesDialog::TagRulesDialog(const TagRule &seed, QWidget *parent) m_add->setPlaceholderText(tr("comma separated")); m_remove = new QLineEdit(this); m_remove->setPlaceholderText(tr("comma separated")); + // The stored value is the item DATA, the wire-format role. The label is + // translated, and nothing may match on it ("Translating a string that + // something MATCHES on" in AGENTS.md). + m_move = new QComboBox(this); + m_move->addItem(tr("Don't move"), QString()); + m_move->addItem(tr("Spam folder"), QStringLiteral("spam")); + m_move->addItem(tr("Trash folder"), QStringLiteral("trash")); + m_move->setToolTip(tr("Moves matching new mail into the spam or trash " + "folder of the account it arrived in, and takes it " + "out of the inbox. It stays unread.")); m_query = new QLineEdit(this); // Query syntax is wire format, never translated. Only the prose is. m_query->setPlaceholderText(QStringLiteral("from:sender@example.com")); @@ -173,6 +183,7 @@ TagRulesDialog::TagRulesDialog(const TagRule &seed, QWidget *parent) form->addRow(QString(), m_enabled); form->addRow(tr("Add tags"), m_add); form->addRow(tr("Remove tags"), m_remove); + form->addRow(tr("Move to"), m_move); m_builder = new QWidget(this); auto *builderLayout = new QVBoxLayout(m_builder); builderLayout->setContentsMargins(0, 0, 0, 0); @@ -352,6 +363,8 @@ TagRulesDialog::TagRulesDialog(const TagRule &seed, QWidget *parent) this, &TagRulesDialog::applyEditsToCurrentRule); connect(m_remove, &QLineEdit::editingFinished, this, &TagRulesDialog::applyEditsToCurrentRule); + connect(m_move, &QComboBox::currentIndexChanged, + this, &TagRulesDialog::applyEditsToCurrentRule); connect(m_query, &QLineEdit::editingFinished, this, &TagRulesDialog::applyEditsToCurrentRule); connect(m_stage, &QSpinBox::editingFinished, @@ -644,6 +657,7 @@ void TagRulesDialog::onSelectionChanged() m_enabled->setChecked(rule.enabled); m_add->setText(rule.add.join(QStringLiteral(", "))); m_remove->setText(rule.remove.join(QStringLiteral(", "))); + m_move->setCurrentIndex(qMax(0, m_move->findData(rule.move))); m_query->setText(rule.query); m_note->setPlainText(rule.note); @@ -700,6 +714,7 @@ void TagRulesDialog::applyEditsToCurrentRule() rule.enabled = m_enabled->isChecked(); rule.add = splitTags(m_add->text()); rule.remove = splitTags(m_remove->text()); + rule.move = m_move->currentData().toString(); if (m_textMode->isChecked()) { rule.query = m_query->text().trimmed(); } else { @@ -922,6 +937,16 @@ void TagRulesDialog::setTagsForTest(const QString &tags) emit m_add->editingFinished(); } +void TagRulesDialog::setMoveForTest(const QString &role) +{ + m_move->setCurrentIndex(m_move->findData(role)); +} + +QString TagRulesDialog::moveForTest() const +{ + return m_move->currentData().toString(); +} + bool TagRulesDialog::textModeToggleIsReachableForTest() const { // isVisibleTo rather than isVisible: nothing is isVisible() on a dialog diff --git a/src/tagrulesdialog.h b/src/tagrulesdialog.h index 3bfb3d5..0148f38 100644 --- a/src/tagrulesdialog.h +++ b/src/tagrulesdialog.h @@ -128,6 +128,8 @@ public: bool currentRuleEnabledForTest() const; void addRuleForTest() { onAddRule(); } void setTagsForTest(const QString &tags); + void setMoveForTest(const QString &role); + QString moveForTest() const; /// Whether the text-mode toggle would be on screen. A toggle that hides /// itself when switched on is a one-way trip, and asserting only on the @@ -294,6 +296,7 @@ private: QLineEdit *m_id = nullptr; QLineEdit *m_add = nullptr; QLineEdit *m_remove = nullptr; + QComboBox *m_move = nullptr; QLineEdit *m_query = nullptr; QPlainTextEdit *m_note = nullptr; QSpinBox *m_stage = nullptr; diff --git a/tests/test_tagrules.cpp b/tests/test_tagrules.cpp index c831864..898e9eb 100644 --- a/tests/test_tagrules.cpp +++ b/tests/test_tagrules.cpp @@ -51,6 +51,9 @@ private slots: void aNameTypedWithSpacesIsSanitisedInTheField(); void aRuleAddedAndNamedInTheDialogSurvivesAReopen(); void unknownFieldsSurviveASave(); + void aMoveRoleRoundTripsAndAMoveOnlyRuleIsValid(); + void anUnknownMoveRoleIsRefused(); + void theDialogWritesTheChosenMoveRole(); void stageOrderPutsAccountsFirst(); void aQueryWithQuotesRoundTrips(); void aMissingFileIsEmptyNotAnError(); @@ -357,6 +360,136 @@ void TestTagRules::unknownFieldsSurviveASave() QStringLiteral("keeper")); } +void TestTagRules::aMoveRoleRoundTripsAndAMoveOnlyRuleIsValid() +{ + const QString path = writeRules(R"({ + "version": 1, + "rules": [ + {"id": "junk", "query": "from:spammer@example.net", "move": "spam"}, + {"id": "plain", "query": "from:a@example.com", "add": ["x"]} + ] + })"); + + TagRules rules; + rules.load(path); + QVERIFY2(rules.warnings().isEmpty(), + qPrintable(rules.warnings().join(QStringLiteral("; ")))); + QCOMPARE(rules.rules().size(), 2); + QCOMPARE(rules.rules().at(0).move, QStringLiteral("spam")); + QVERIFY(!rules.rules().at(0).unknown.contains(QStringLiteral("move"))); + QVERIFY(TagRules::validate(rules.rules()).isEmpty()); + + QVERIFY(rules.save(path)); + QFile file(path); + QVERIFY(file.open(QIODevice::ReadOnly)); + const QJsonArray saved = QJsonDocument::fromJson(file.readAll()) + .object().value(QStringLiteral("rules")) + .toArray(); + QCOMPARE(saved.at(0).toObject().value(QStringLiteral("move")).toString(), + QStringLiteral("spam")); + // Absent stays absent: an empty `move` on every rule would rewrite every + // file the dialog opens, a diff the hook's reader never asked for. + QVERIFY(!saved.at(1).toObject().contains(QStringLiteral("move"))); +} + +void TestTagRules::anUnknownMoveRoleIsRefused() +{ + TagRule rule; + rule.id = QStringLiteral("odd"); + rule.query = QStringLiteral("from:a@example.com"); + rule.move = QStringLiteral("Archive"); + QCOMPARE(TagRules::validate({ rule }).size(), 1); + + // Loaded, the move is not acted on and is not LOST either: the raw value + // rides in `unknown` so a save writes it back. Dropping it, or dropping a + // rule whose only action it was, is the writer-loses-data trap AGENTS.md + // records for rule ids: the rule vanished from the dialog and the next + // save deleted it from the file. + const QString path = writeRules(R"({ + "version": 1, + "rules": [ + {"id": "odd", "query": "from:a@example.com", + "add": ["x"], "move": "Archive"}, + {"id": "only-move", "query": "from:b@example.com", "move": "Spam"}, + {"id": "not-a-string", "query": "from:c@example.com", + "add": ["y"], "move": 5} + ] + })"); + TagRules rules; + rules.load(path); + QCOMPARE(rules.rules().size(), 3); + QCOMPARE(rules.warnings().size(), 3); + for (const TagRule &loaded : rules.rules()) + QVERIFY(loaded.move.isEmpty()); + + // Save is refused for the move-only one until the user fixes it, which is + // the dialog's existing route for a rule that would not load back. + QCOMPARE(TagRules::validate(rules.rules()).size(), 1); + + QVERIFY(rules.save(path)); + QFile file(path); + QVERIFY(file.open(QIODevice::ReadOnly)); + const QJsonArray saved = QJsonDocument::fromJson(file.readAll()) + .object().value(QStringLiteral("rules")) + .toArray(); + QCOMPARE(saved.size(), 3); + QCOMPARE(saved.at(0).toObject().value(QStringLiteral("move")).toString(), + QStringLiteral("Archive")); + QCOMPARE(saved.at(1).toObject().value(QStringLiteral("move")).toString(), + QStringLiteral("Spam")); + QCOMPARE(saved.at(2).toObject().value(QStringLiteral("move")).toInt(), 5); + + // Fixed in the dialog: the chosen role wins over the kept bad one. + QList<TagRule> fixed = rules.rules(); + fixed[1].move = QStringLiteral("spam"); + rules.setRules(fixed); + QVERIFY(TagRules::validate(rules.rules()).isEmpty()); + QVERIFY(rules.save(path)); + rules.load(path); + QVERIFY(rules.warnings().size() == 2); + QCOMPARE(rules.rules().at(1).move, QStringLiteral("spam")); +} + +void TestTagRules::theDialogWritesTheChosenMoveRole() +{ + QTemporaryDir configHome; + QVERIFY(configHome.isValid()); + qputenv("XDG_CONFIG_HOME", configHome.path().toUtf8()); + QVERIFY(QDir().mkpath(configHome.filePath(QStringLiteral("mailrules")))); + + const QString stored = configHome.filePath( + QStringLiteral("mailrules/rules.json")); + QFile out(stored); + QVERIFY(out.open(QIODevice::WriteOnly)); + out.write(R"({ + "version": 1, + "rules": [ + {"id": "vendor", "query": "from:vendor.example.org", + "add": ["vendor"], "stage": 50, "enabled": true} + ] + })"); + out.close(); + + { + TagRulesDialog dialog; + dialog.selectRuleForTest(0); + QCOMPARE(dialog.moveForTest(), QString()); + dialog.setMoveForTest(QStringLiteral("spam")); + dialog.saveForTest(); + QVERIFY(dialog.warningTextForTest().isEmpty()); + } + + TagRules reloaded; + reloaded.load(stored); + QCOMPARE(reloaded.rules().at(0).move, QStringLiteral("spam")); + + // And back the other way: the stored role shows on reopen, so the next + // save cannot silently clear it. + TagRulesDialog reopened; + reopened.selectRuleForTest(0); + QCOMPARE(reopened.moveForTest(), QStringLiteral("spam")); +} + void TestTagRules::stageOrderPutsAccountsFirst() { const QString path = writeRules(R"({ |
