package main import ( "encoding/json" "errors" "os" "time" ) // Unlinked is the last number WhatsApp removed from its linked devices (a `device_removed` stream // error, or the person removing this Mac on the phone), said by `hello` until a number links // again, so the Mac can say so and ask the owner to link it again — even after a restart. type Unlinked struct { Account string `json:"account"` Reason string `json:"reason"` At int64 `json:"at"` // unix seconds } // LoadUnlinked reads the record kept from the last run. func (h *Hub) LoadUnlinked(path string) error { h.unlinkedPath = path b, err := os.ReadFile(path) if errors.Is(err, os.ErrNotExist) { return nil } if err != nil { return err } var u Unlinked if err := json.Unmarshal(b, &u); err != nil { return err } h.mu.Lock() h.unlinked = &u h.mu.Unlock() return nil } func (h *Hub) Unlinked() *Unlinked { h.mu.Lock() defer h.mu.Unlock() return h.unlinked } func (h *Hub) recordUnlinked(account, reason string, at time.Time) { u := &Unlinked{Account: account, Reason: reason, At: at.Unix()} h.mu.Lock() h.unlinked = u path := h.unlinkedPath h.mu.Unlock() if path == "" { return } b, _ := json.Marshal(u) tmp := path + ".part" err := os.WriteFile(tmp, b, 0o600) if err == nil { err = os.Rename(tmp, path) } if err != nil { h.log.Errorf("unlinked record not kept: %v", err) } } func (h *Hub) clearUnlinked() { h.mu.Lock() h.unlinked = nil path := h.unlinkedPath h.mu.Unlock() if path != "" { _ = os.Remove(path) } }