Filtri che imparano, con bogofilter compilato per aarch64 (0.10.0)
Il motore ha il modulo Mail_Filing dalla beta10: impara cosa c'e' nelle cartelle e archivia i messaggi simili. Due giri: prima l'apprendimento, poi le righe 'C' con id e percorso e il motore risponde '= cartella id destinazione', che il ponte esegue con move. - bogofilter 1.2.5 compilato con SQLite (dipende solo da libsqlite3 e libc), installato in /usr/bin: il motore lo cerca nel PATH - liste in GRUMPYDOVE_FILING, non sotto HOME - Filing_Keep esiste perche' Mail_Filing, senza un riferimento, non entrava nella chiusura della libreria: gd_filing_start non c'era e il link falliva
This commit is contained in:
1 parent
1b5ca66785
commit
29a9b6ffb5
14 files changed
+273
-7
No files matched your search
+143
-1
@@ -93,13 +93,14 @@ void EngineBridge::keepCallbacks(gd_folder_fn folder, gd_open_fn open, gd_cell_f
|
||||
gd_row_of_fn row_of,
|
||||
gd_find_start_fn book_start,
|
||||
gd_find_poll_fn book_poll,
|
||||
gd_place_fn book_list)
|
||||
gd_place_fn book_list, gd_text_fn path)
|
||||
{
|
||||
s_folder = folder;
|
||||
s_open = open;
|
||||
s_cell = cell;
|
||||
s_body = body;
|
||||
s_unread = unread;
|
||||
s_path = path;
|
||||
s_read = mark_read;
|
||||
s_id_of = id_of;
|
||||
s_move = move;
|
||||
@@ -807,6 +808,147 @@ QVariantList EngineBridge::suggerisci(const QString &testo, int quanti)
|
||||
return out;
|
||||
}
|
||||
|
||||
// I filtri stanno in un modulo che si esporta da sé (Mail_Filing), non
|
||||
// nell'aggregato di gd_run: si chiamano come le altre funzioni C del motore.
|
||||
extern "C" {
|
||||
int gd_filing_start(void *config, int len);
|
||||
int gd_filing_poll(char *buf, int cap, int *running);
|
||||
void gd_filing_cancel(void);
|
||||
}
|
||||
|
||||
bool EngineBridge::filtriAttivi() const
|
||||
{
|
||||
QSettings s;
|
||||
return s.value(QStringLiteral("filtri/attivi"), false).toBool();
|
||||
}
|
||||
|
||||
void EngineBridge::setFiltriAttivi(bool attivi)
|
||||
{
|
||||
QSettings s;
|
||||
s.setValue(QStringLiteral("filtri/attivi"), attivi);
|
||||
logInvio(QString("filtri: %1").arg(attivi ? "attivi" : "spenti"));
|
||||
}
|
||||
|
||||
// Due giri, come l'originale: prima il motore impara cosa c'e' nelle cartelle,
|
||||
// poi gli si danno i messaggi da archiviare e lui risponde dove metterli.
|
||||
QString EngineBridge::filtra(int folder)
|
||||
{
|
||||
if (m_filtriTimer && m_filtriTimer->isActive())
|
||||
return tr("I filtri stanno già lavorando");
|
||||
if (!s_folder || !s_id_of || !s_path)
|
||||
return tr("Il motore non offre i filtri");
|
||||
|
||||
// Si archivia solo nelle cartelle normali: non negli account, non in In,
|
||||
// Out, Sent, Junk e Trash (che il motore rifiuta comunque).
|
||||
QString elenco;
|
||||
int inbox = -1;
|
||||
const QVariantList tutte = folders();
|
||||
for (int i = 0; i < tutte.size(); i++) {
|
||||
const QVariantMap m = tutte.at(i).toMap();
|
||||
const QString tipo = m.value("tipo").toString();
|
||||
const int idx = m.value("folder").toInt();
|
||||
if (tipo == QLatin1String("in")) { inbox = idx; continue; }
|
||||
if (tipo != QLatin1String("cartella")) continue;
|
||||
elenco += QStringLiteral("F\t%1\n").arg(idx);
|
||||
}
|
||||
if (elenco.isEmpty())
|
||||
return tr("Nessuna cartella in cui archiviare");
|
||||
if (inbox < 0) inbox = folder;
|
||||
|
||||
// I messaggi della cartella, con il percorso del file: il motore li legge
|
||||
// da lì per imparare e per decidere.
|
||||
const QVariantList messaggi = messages(folder);
|
||||
m_filtriArchivia.clear();
|
||||
int quanti = 0;
|
||||
for (int i = 0; i < messaggi.size() && quanti < 300; i++) {
|
||||
const int riga = messaggi.at(i).toMap().value(QStringLiteral("riga")).toInt();
|
||||
const int id = s_id_of(folder, riga);
|
||||
if (id < 0) continue;
|
||||
char buf[4096];
|
||||
const int n = s_path(folder, id, buf, int(sizeof buf) - 1);
|
||||
if (n <= 0) continue;
|
||||
const QString percorso = QString::fromUtf8(buf, qMin(n, int(sizeof buf) - 1));
|
||||
if (percorso.isEmpty()) continue;
|
||||
m_filtriArchivia += QStringLiteral("C\t%1\t%2\t%3\n")
|
||||
.arg(folder).arg(id).arg(percorso);
|
||||
quanti++;
|
||||
}
|
||||
if (m_filtriArchivia.isEmpty())
|
||||
return tr("Nessun messaggio da archiviare qui");
|
||||
|
||||
m_filtriImpara = QStringLiteral("A\n") + elenco
|
||||
+ QStringLiteral("I\t%1\n").arg(inbox);
|
||||
m_filtriSpostati = 0;
|
||||
m_filtriRestano = 0;
|
||||
m_filtriErrore.clear();
|
||||
|
||||
if (!m_filtriTimer) {
|
||||
m_filtriTimer = new QTimer(this);
|
||||
m_filtriTimer->setInterval(300);
|
||||
connect(m_filtriTimer, &QTimer::timeout, this, &EngineBridge::pollFiltri);
|
||||
}
|
||||
logInvio(QString("filtri: %1 messaggi da archiviare, In %2")
|
||||
.arg(quanti).arg(inbox));
|
||||
avviaFiltri(true);
|
||||
return QString();
|
||||
}
|
||||
|
||||
void EngineBridge::avviaFiltri(bool apprendimento)
|
||||
{
|
||||
m_faseFiltri = apprendimento ? 1 : 2;
|
||||
const QByteArray config =
|
||||
(apprendimento ? m_filtriImpara : m_filtriArchivia).toUtf8();
|
||||
gd_filing_start(config.data(), config.size());
|
||||
if (m_filtriTimer) m_filtriTimer->start();
|
||||
}
|
||||
|
||||
void EngineBridge::pollFiltri()
|
||||
{
|
||||
QByteArray buffer(64 * 1024, '\0');
|
||||
int ancora = 0;
|
||||
const int letti = gd_filing_poll(buffer.data(), buffer.size(), &ancora);
|
||||
const QList<QByteArray> righe = buffer.left(letti).split('\n');
|
||||
for (int i = 0; i < righe.size(); i++) {
|
||||
const QByteArray riga = righe.at(i);
|
||||
if (riga.isEmpty()) continue;
|
||||
if (riga.at(0) == '!') {
|
||||
m_filtriErrore = QString::fromUtf8(riga.mid(2)).trimmed();
|
||||
continue;
|
||||
}
|
||||
if (riga.at(0) == '+') { // "+ quanti" imparati
|
||||
m_filtriRestano = riga.mid(2).trimmed().toInt();
|
||||
continue;
|
||||
}
|
||||
if (riga.at(0) != '=') continue; // "? folder id" = resta dov'e'
|
||||
const QList<QByteArray> celle = riga.split('\t');
|
||||
if (celle.size() < 4) continue;
|
||||
const int da = celle.at(1).toInt();
|
||||
const int id = celle.at(2).toInt();
|
||||
const int a = celle.at(3).toInt();
|
||||
if (s_move && s_move(da, id, a) != 0) {
|
||||
m_filtriSpostati++;
|
||||
dropRows(da);
|
||||
dropRows(a);
|
||||
}
|
||||
}
|
||||
if (ancora) return;
|
||||
|
||||
if (m_faseFiltri == 1) { avviaFiltri(false); return; }
|
||||
|
||||
if (m_filtriTimer) m_filtriTimer->stop();
|
||||
QString esito;
|
||||
if (!m_filtriErrore.isEmpty())
|
||||
esito = tr("Filtri: %1").arg(m_filtriErrore);
|
||||
else if (m_filtriSpostati == 0)
|
||||
esito = tr("Filtri: nessun messaggio archiviato (imparati %1)")
|
||||
.arg(m_filtriRestano);
|
||||
else
|
||||
esito = tr("Filtri: %1 messaggi archiviati").arg(m_filtriSpostati);
|
||||
logInvio("filtri: " + esito);
|
||||
emit filtrazione(esito);
|
||||
emit ready();
|
||||
}
|
||||
|
||||
// Il giro dell'invio, come nell'originale: compose scrive il messaggio in Out
|
||||
// con il flag di coda, claim estrae quelli da mandare, send_start parla con il
|
||||
// server SMTP e send_poll riporta un messaggio per volta.
|
||||
|
||||
+21
-1
@@ -54,7 +54,7 @@ public:
|
||||
gd_row_of_fn row_of,
|
||||
gd_find_start_fn book_start,
|
||||
gd_find_poll_fn book_poll,
|
||||
gd_place_fn book_list);
|
||||
gd_place_fn book_list, gd_text_fn path);
|
||||
|
||||
bool checking() const { return m_checking; }
|
||||
QString lastResult() const { return m_lastResult; }
|
||||
@@ -86,6 +86,11 @@ public:
|
||||
// Rubrica CardDAV: sincronizza e restituisce i contatti.
|
||||
Q_INVOKABLE QString sincronizzaRubrica();
|
||||
Q_INVOKABLE QVariantList contatti();
|
||||
// Filtri che imparano (bogofilter): si imparano le cartelle, poi il motore
|
||||
// dice dove va ogni messaggio della cartella indicata.
|
||||
Q_INVOKABLE QString filtra(int folder);
|
||||
Q_INVOKABLE bool filtriAttivi() const;
|
||||
Q_INVOKABLE void setFiltriAttivi(bool attivi);
|
||||
// Suggerimenti per il campo "A": nome o indirizzo che contengono il testo.
|
||||
Q_INVOKABLE QVariantList suggerisci(const QString &testo, int quanti);
|
||||
// Una riga dalla QML nel log del motore: serve a sapere se un tocco e'
|
||||
@@ -100,6 +105,8 @@ signals:
|
||||
void ready();
|
||||
void searchFinished(int quanti, const QString &errore);
|
||||
void rubricaPronta(int quanti, const QString &errore);
|
||||
// Esito della filtrazione: quanti messaggi spostati, o perché no.
|
||||
void filtrazione(const QString &esito);
|
||||
void sendFinished(int inviati, int falliti, const QString &esito);
|
||||
void checkingChanged();
|
||||
void mailChecked();
|
||||
@@ -116,6 +123,8 @@ private:
|
||||
static gd_cell_fn s_cell;
|
||||
static gd_body_fn s_body;
|
||||
static gd_can_hold_fn s_unread;
|
||||
// Il percorso del file di un messaggio: serve ai filtri che imparano.
|
||||
static gd_text_fn s_path;
|
||||
static gd_read_fn s_read;
|
||||
static gd_id_of_fn s_id_of;
|
||||
static gd_move_fn s_move;
|
||||
@@ -160,6 +169,17 @@ private:
|
||||
void pollRubrica();
|
||||
void leggiContattiLocali();
|
||||
|
||||
// Filtri: fase 1 impara dalle cartelle, fase 2 archivia i messaggi.
|
||||
void avviaFiltri(bool apprendimento);
|
||||
void pollFiltri();
|
||||
QTimer *m_filtriTimer = nullptr;
|
||||
QString m_filtriImpara;
|
||||
QString m_filtriArchivia;
|
||||
QString m_filtriErrore;
|
||||
int m_faseFiltri = 0;
|
||||
int m_filtriSpostati = 0;
|
||||
int m_filtriRestano = 0;
|
||||
|
||||
// Un risultato di ricerca, con la riga gia' risolta: la ricerca da' il
|
||||
// nome del file, non la posizione nella cartella.
|
||||
struct SearchHit {
|
||||
|
||||
@@ -62,7 +62,7 @@ int gd_run(gd_folder_fn folder, gd_open_fn open, gd_cell_fn cell,
|
||||
send_start, send_poll, send_done, queue_sent,
|
||||
place, log, empty, plain, html,
|
||||
find_start, find_poll, find_cancel, id_named,
|
||||
row_of, book_start, book_poll, book_list);
|
||||
row_of, book_start, book_poll, book_list, path);
|
||||
return QCoreApplication::exec();
|
||||
}
|
||||
|
||||
|
||||
+8
-2
@@ -24,9 +24,9 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
|
||||
|
||||
app->setOrganizationName(QStringLiteral("harbour"));
|
||||
app->setApplicationName(QStringLiteral("grumpydove"));
|
||||
app->setApplicationVersion(QStringLiteral("0.9.8"));
|
||||
app->setApplicationVersion(QStringLiteral("0.10.0"));
|
||||
|
||||
qDebug() << "harbour-grumpydove v0.9.8 build" << __DATE__ << __TIME__;
|
||||
qDebug() << "harbour-grumpydove v0.10.0 build" << __DATE__ << __TIME__;
|
||||
|
||||
// La sandbox di Sailfish (SailJail) non lascia vedere ~/Mail, ma il motore
|
||||
// legge il percorso del Maildir da GRUMPYDOVE_MAIL: si punta alla cartella
|
||||
@@ -51,6 +51,12 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
|
||||
+ QStringLiteral("/books");
|
||||
QDir().mkpath(booksDir);
|
||||
qputenv("GRUMPYDOVE_BOOKS", QFile::encodeName(booksDir));
|
||||
// E le liste dei filtri che imparano.
|
||||
const QString filingDir = QStandardPaths::writableLocation(
|
||||
QStandardPaths::AppDataLocation)
|
||||
+ QStringLiteral("/filing");
|
||||
QDir().mkpath(filingDir);
|
||||
qputenv("GRUMPYDOVE_FILING", QFile::encodeName(filingDir));
|
||||
qDebug() << "Maildir del motore:" << mailDir;
|
||||
|
||||
QTranslator translator;
|
||||
|
||||
Reference in new issue
Block a user