Files
harbour-grumpydove/engine/patch_engine.py
T
Carlo Baratto c0f973cf6e Cancellazione sul server e messaggi HTML (0.8.0)
- engine/patch_engine.py applica le modifiche al motore a ogni build: log
  redirigibile, .delete in Mail_Pending, gd_imap_remove nello shim C, Empty
  che propaga la cancellazione
- body() ripiega su plain e poi su html; htmltext.js riduce l'HTML a testo
  (10 controlli node)
2026-11-11 17:07:02 +01:00

178 lines
7.2 KiB
Python

#!/usr/bin/env python3
"""Modifiche al motore di GrumpyDove necessarie al porting Sailfish.
Il motore si compila dai sorgenti originali, non da una copia modificata a mano:
questo script applica le poche modifiche che servono e si ferma con un errore
chiaro se un punto non si trova (sorgenti diversi da quelli attesi).
Uso: patch_engine.py <cartella_src>
"""
import sys
from pathlib import Path
# (file, testo_da_trovare, testo_nuovo, descrizione)
PATCHES = [
# Il log deve poter finire dove la sandbox di Sailfish lascia scrivere:
# ~/.local/state non esiste, e il motore fallirebbe in silenzio.
("mail_log.adb",
''' Dir : constant String :=
Ada.Environment_Variables.Value ("HOME", "")
& "/.local/state/GrumpyDove";''',
''' Dir : constant String :=
(if Ada.Environment_Variables.Exists ("GRUMPYDOVE_LOG")
then Ada.Environment_Variables.Value ("GRUMPYDOVE_LOG")
else Ada.Environment_Variables.Value ("HOME", "")
& "/.local/state/GrumpyDove");''',
"log redirigibile con GRUMPYDOVE_LOG"),
# Cancellare un messaggio sul server: \\Deleted su quel solo UID e,
# quando il server ha UIDPLUS, un expunge di quello soltanto.
("imap_shim.c",
"int gd_imap_append(gd_imap *s, const char *mailbox, int seen, const char *data,",
'''/* Deletes one message for good. \\Deleted on that UID only, then an expunge
of it alone when the server has UIDPLUS: never of what someone else marked. */
int gd_imap_remove(gd_imap *s, unsigned uid, char *why, size_t cap) {
struct mailimap_set *set = mailimap_set_new_single(uid);
int result;
if (set == NULL) {
say(why, cap, "out of memory");
return 0;
}
if (!store_flag(s, uid, mailimap_flag_new_deleted(), 1, why, cap)) {
mailimap_set_free(set);
return 0;
}
if (has_capability(s, "UIDPLUS"))
result = mailimap_uid_expunge(s->imap, set);
else
result = mailimap_expunge(s->imap);
mailimap_set_free(set);
if (result != MAILIMAP_NO_ERROR) {
say_imap(s, "EXPUNGE", result, why, cap);
return 0;
}
return 1;
}
int gd_imap_append(gd_imap *s, const char *mailbox, int seen, const char *data,''',
"gd_imap_remove nello shim C"),
("imap_shim.h",
"int gd_imap_move(gd_imap *session, unsigned uid, const char *mailbox, char *why, size_t cap);",
'''int gd_imap_move(gd_imap *session, unsigned uid, const char *mailbox, char *why, size_t cap);
int gd_imap_remove(gd_imap *session, unsigned uid, char *why, size_t cap);''',
"dichiarazione di gd_imap_remove"),
# Un'operazione in sospeso nuova: la cancellazione.
("mail_pending.ads",
" -- File was written in Goal by us: the server gets it at the next check.\n procedure Note_Copy (Goal, File : String);",
''' -- File was written in Goal by us: the server gets it at the next check.
procedure Note_Copy (Goal, File : String);
-- The user emptied the message out of its mailbox for good: the server
-- drops it at the next check. Does nothing outside accounts.
procedure Note_Delete (Path : String);''',
"Note_Delete in Mail_Pending (specifica)"),
("mail_pending.adb",
" procedure Note_Copy (Goal, File : String) is",
''' procedure Note_Delete (Path : String) is
Uid : constant String := Uid_Of (File_Part (Path));
Box : constant String := Box_Of (Path);
begin
if Uid /= "" and then In_Account (Path) then
-- A flag still waiting would be about a message that is going.
Remove (Box, Uid & ".seen");
Remove (Box, Uid & ".unseen");
Remove (Box, Uid & ".move");
Remove (Box, Uid & ".delete");
Write (Box, Uid & ".delete", "");
Mail_Log.Write ("pending: delete " & Uid & " in " & Box);
end if;
exception
when Error : others =>
Mail_Log.Write ("ERR pending: " & Ada.Exceptions.Exception_Message (Error));
end Note_Delete;
procedure Note_Copy (Goal, File : String) is''',
"Note_Delete in Mail_Pending (corpo)"),
# Il flush delle operazioni: esegue la cancellazione sul server.
("mail_imap.adb",
' with Import, Convention => C, External_Name => "gd_imap_move";',
''' with Import, Convention => C, External_Name => "gd_imap_move";
function I_Remove
(S : System.Address; Uid : unsigned; Why : System.Address; Cap : size_t)
return int
with Import, Convention => C, External_Name => "gd_imap_remove";''',
"import di gd_imap_remove"),
("mail_imap.adb",
' elsif Kind = "move" then',
''' elsif Kind = "delete" then
if Jmap then
-- Lo shim JMAP non copre la cancellazione: meglio dirlo che
-- dichiarare fatto quello che non si e' fatto.
Channel.Put ("!" & Tab & "cannot delete a message through JMAP");
else
Ok := I_Remove (S, unsigned'Value (Stem), Why'Address,
Why_Size) /= 0;
end if;
elsif Kind = "move" then''',
"esecuzione della cancellazione nel Flush"),
# Un messaggio che aspetta di essere cancellato non va rinominato.
("mail_imap.adb",
''' (Is_Regular_File (Dir & Pending & "/" & Uid & ".seen")
or else Is_Regular_File (Dir & Pending & "/" & Uid & ".unseen")
or else Is_Regular_File (Dir & Pending & "/" & Uid & ".move"));''',
''' (Is_Regular_File (Dir & Pending & "/" & Uid & ".seen")
or else Is_Regular_File (Dir & Pending & "/" & Uid & ".unseen")
or else Is_Regular_File (Dir & Pending & "/" & Uid & ".delete")
or else Is_Regular_File (Dir & Pending & "/" & Uid & ".move"));''',
"Waiting comprende .delete"),
# Svuotare il cestino: oltre a togliere il file, dirlo al server.
("mail_boxes.adb",
''' if not M.Gone then
GNAT.OS_Lib.Delete_File (To_String (M.Path), Done);''',
''' if not M.Gone then
-- Prima di toglierlo: la cancellazione va anche sul server,
-- altrimenti il messaggio tornerebbe al controllo dopo.
Mail_Pending.Note_Delete (To_String (M.Path));
GNAT.OS_Lib.Delete_File (To_String (M.Path), Done);''',
"Empty propaga la cancellazione"),
]
def main() -> int:
if len(sys.argv) != 2:
print("uso: patch_engine.py <cartella_src>", file=sys.stderr)
return 2
src = Path(sys.argv[1])
if not (src / "mail_boxes.adb").exists():
print(f"{src}: non sembrano i sorgenti di GrumpyDove", file=sys.stderr)
return 1
for nome, vecchio, nuovo, descrizione in PATCHES:
f = src / nome
testo = f.read_text()
# Il nuovo testo contiene spesso quello vecchio: la presenza del nuovo
# basta a dire che la patch c'e' gia' (senza, si applicava due volte e
# finiva in "conflicts with declaration").
if nuovo in testo:
print(f"già applicata: {descrizione}")
continue
if vecchio not in testo:
print(f"NON TROVATO in {nome}: {descrizione}", file=sys.stderr)
return 1
f.write_text(testo.replace(vecchio, nuovo, 1))
print(f"applicata: {descrizione}")
return 0
if __name__ == "__main__":
sys.exit(main())