Files
harbour-grumpydove/engine/patch_engine.py
T
Carlo Baratto 367df942c8 Comandi: esito leggibile e log del segno sottoscrizione (0.23.7)
Dopo un comando al server il messaggio diceva 'Nessun messaggio nuovo', che
faceva credere a un fallimento. Ora dice 'Fatto: SUBSCRIBE'. E il motore scrive
nel log se il segno .grumpydove-unsubscribed e' stato tolto e dove.
2026-11-20 22:05:33 +01:00

327 lines
14 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"),
# Anche i contatti della rubrica finiscono sotto HOME, che nella sandbox
# non e' scrivibile: senza la variabile la sincronizzazione non salva
# niente e riporta zero contatti, senza errore.
("mail_carddav.adb",
''' (Ada.Environment_Variables.Value ("HOME", "")
& "/.local/share/GrumpyDove/books");''',
''' (if Ada.Environment_Variables.Exists ("GRUMPYDOVE_BOOKS")
then Ada.Environment_Variables.Value ("GRUMPYDOVE_BOOKS")
else Ada.Environment_Variables.Value ("HOME", "")
& "/.local/share/GrumpyDove/books");''',
"rubrica in GRUMPYDOVE_BOOKS"),
# E le liste dei filtri che imparano: stesso motivo, stessa cura.
("mail_filing.adb",
''' (Ada.Environment_Variables.Value ("HOME", "")
& "/.local/share/GrumpyDove/filing");''',
''' (if Ada.Environment_Variables.Exists ("GRUMPYDOVE_FILING")
then Ada.Environment_Variables.Value ("GRUMPYDOVE_FILING")
else Ada.Environment_Variables.Value ("HOME", "")
& "/.local/share/GrumpyDove/filing");''',
"liste dei filtri in GRUMPYDOVE_FILING"),
# Ogni richiesta DAV nel log: senza, un 404 arrivava senza dire ne' quale
# indirizzo ne' quale metodo, e non c'era modo di capirlo dall'esterno.
("mail_carddav.adb",
"with Mail_Folders;",
'''with Mail_Folders;
with Mail_Log;''',
"Mail_Log fra i with della rubrica"),
("mail_carddav.adb",
''' R.Status := Natural (Code);
R.Etag := To_Unbounded_String (To_Ada (Tag));''',
''' R.Status := Natural (Code);
-- Metodo, indirizzo e stato: la traccia che mancava.
Mail_Log.Write ("dav: " & Method & " " & Url & " ->"
& Natural'Image (R.Status));
R.Etag := To_Unbounded_String (To_Ada (Tag));''',
"log delle richieste DAV"),
# 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"),
# Creare una cartella sul server. Make_Folder sa creare solo sotto Local
# (Can_Hold = 1); per le cartelle di un account la via e' IMAP CREATE, che
# nel motore esiste (Mail_Imap.I_Create) ma non e' mai accodata da nessuno.
# La nota sta nel pending della INBOX dell'account e la esegue il Flush.
("mail_pending.ads",
''' -- File was written in Goal by us: the server gets it at the next check.
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);
-- A mailbox to make on the server. Box is the account's INBOX, Name what
-- the user called it: the next check runs IMAP CREATE.
procedure Note_Make (Box, Name : String);''',
"Note_Make in Mail_Pending (specifica)"),
("mail_pending.adb",
''' procedure Note_Copy (Goal, File : String) is
begin''',
''' procedure Note_Make (Box, Name : String) is
begin
if In_Account (Box) and then Name /= "" then
Write (Box, Name & ".mkdir", Name);
Mail_Log.Write ("pending: mkdir " & Name & " in " & Box);
end if;
exception
when Error : others =>
Mail_Log.Write ("ERR pending: " & Ada.Exceptions.Exception_Message (Error));
end Note_Make;
procedure Note_Copy (Goal, File : String) is
begin''',
"Note_Make in Mail_Pending (corpo)"),
# Il Flush esegue la creazione. "Esiste gia'" non e' un guasto: la
# cartella c'e', che e' quello che l'utente voleva.
("mail_imap.adb",
''' elsif Kind = "delete" then''',
''' elsif Kind = "mkdir" then
declare
Wanted : constant String := Read_File (Path);
New_Box : chars_ptr := New_String (Wanted);
begin
Ok := C_Create (S, New_Box, Why\'Address, Why_Size) /= 0;
if not Ok then
declare
Said : constant String := To_Ada (Why);
begin
Ok := Ada.Strings.Fixed.Index (Said, "ALREADYEXISTS") > 0
or else Ada.Strings.Fixed.Index (Said, "already exists") > 0;
end;
end if;
if not Ok then
Channel.Put ("!" & Tab & Clean (Name) & ": create "
& Clean (Wanted) & ": " & To_Ada (Why));
end if;
-- Una cartella appena creata non risulta sottoscritta: senza
-- SUBSCRIBE resta "(unsubscribed)" e fuori elenco.
if Ok
and then C_Subscribe (S, New_Box, 1, Why'Address, Why_Size) = 0
then
Channel.Put ("!" & Tab & Clean (Name) & ": subscribe "
& Clean (Wanted) & ": " & To_Ada (Why));
end if;
Free (New_Box);
end;
elsif Kind = "delete" then''',
"ramo mkdir nel Flush"),
# Dopo SUBSCRIBE/UNSUBSCRIBE il segno locale va aggiornato subito: il
# motore marca le cartelle non sottoscritte con un file nella loro
# directory, e senza questo la cartella resta "(unsubscribed)" finche' un
# controllo completo non riscarica la lista dal server.
("mail_imap.adb",
''' begin
Check (Done, Name);
Free (Box);
end;
end if;
end Server_Op;''',
''' begin
Check (Done, Name);
Free (Box);
if Done /= 0 and then Ada.Directories.Exists (Dir) then
declare
use GNAT.OS_Lib;
Mark : constant String := Dir & "/.grumpydove-unsubscribed";
Fd : File_Descriptor;
Gone : Boolean;
begin
if Op = "SUBSCRIBE" then
if Ada.Directories.Exists (Mark) then
GNAT.OS_Lib.Delete_File (Mark, Gone);
Mail_Log.Write ("subscribe: segno tolto in " & Dir);
else
Mail_Log.Write ("subscribe: nessun segno in " & Dir);
end if;
elsif not Ada.Directories.Exists (Mark) then
Fd := GNAT.OS_Lib.Create_File (Mark, GNAT.OS_Lib.Binary);
if Fd /= GNAT.OS_Lib.Invalid_FD then
GNAT.OS_Lib.Close (Fd);
end if;
end if;
end;
end if;
end;
end if;
end Server_Op;''',
"segno non sottoscritta aggiornato dopo SUBSCRIBE"),
]
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())