19 "go.mau.fi/whatsmeow/store/sqlstore"
20 waLog "go.mau.fi/whatsmeow/util/log"
24 // whatsappImportMark marks history in the first line of a batch.
25 whatsappImportMark = " (imported)"
27 // whatsappImportBytes limits one export's uncompressed chat text.
28 whatsappImportBytes = 64 << 20
32 whatsappImportStart = regexp.MustCompile(`^\[(\d{2}/\d{2}/\d{4}, \d{2}:\d{2}:\d{2})\] ([^\r\n]+?): ?(.*)$`)
33 whatsappImportHeader = regexp.MustCompile(`^\[WhatsApp\] (.*), (?:group|private chat) ([^ ,]+), [A-Za-z]+ (\d{4}-\d{2}-\d{2})$`)
34 whatsappImportUnknown = regexp.MustCompile(`^\[WhatsApp\] (.*), [A-Za-z]+ (\d{4}-\d{2}-\d{2})$`)
37 // whatsappImport runs the offline import command. It holds the same token lock
38 // as the daemon and probe, even for a plan. The plan reads no credentials for a
39 // model and makes no network call. Only a completed rebuild replaces the agent
40 // directory, and unfinished subagents prevent either mode.
41 func whatsappImport(ctx context.Context, args []string, out io.Writer) (err error) {
42 planOnly := len(args) > 0 && args[0] == "-n"
48 return errors.New("usage: onechat import [-n] ZIP [ZIP...]")
51 for _, arg := range args {
52 if strings.HasPrefix(arg, "-") {
53 return errors.New("usage: onechat import [-n] ZIP [ZIP...]")
59 return fmt.Errorf("find import data directory: %w", err)
62 tokens, err := lockTokens(dir)
64 return fmt.Errorf("lock import tokens: %w", err)
68 closeErr := tokens.Close()
70 err = errors.Join(err, fmt.Errorf("close import token lock: %w", closeErr))
74 root := filepath.Join(dir, "agent")
75 err = rebuildIdle(root)
77 return fmt.Errorf("check import work: %w", err)
80 old, err := rebuildReadLog(ctx, root)
82 return fmt.Errorf("read import log: %w", err)
85 names, owner, err := whatsappImportNames(ctx, dir)
87 return fmt.Errorf("read import names: %w", err)
90 plan, err := whatsappImportPlan(ctx, args, old, nil, names, owner, time.Local)
92 return fmt.Errorf("plan import: %w", err)
97 return fmt.Errorf("write import plan: %w", err)
100 if planOnly || len(plan.added) == 0 {
104 // An existing agent is required so that a failed import never creates
105 // or changes the original agent's configuration.
106 _, err = os.Stat(filepath.Join(root, "config.json"))
108 return fmt.Errorf("read agent config: %w", err)
111 agent, err := openAgent(root)
113 return fmt.Errorf("open import agent: %w", err)
116 config, err := loadConfig(dir)
118 return fmt.Errorf("load import configuration: %w", err)
121 address, auth, err := modelEndpoint(config, dir)
123 return fmt.Errorf("select import endpoint: %w", err)
126 usage, err := os.CreateTemp(dir, ".import-usage-*.jsonl")
128 return fmt.Errorf("create import usage: %w", err)
131 usagePath := usage.Name()
133 closeErr := os.Remove(usagePath)
135 err = errors.Join(err, fmt.Errorf("remove import usage: %w", closeErr))
141 return fmt.Errorf("close import usage: %w", err)
144 client := newModelClient(address, auth, agent.CacheKey(), usagePath)
145 backup, err := rebuild(ctx, agent, plan.added, client, newCompactor(client, agent.Prompt, config.Compactor, agent.Tools()), out)
147 return fmt.Errorf("rebuild imported memory: %w", err)
150 _, err = fmt.Fprintf(out, "import complete: backup %s\n", backup)
152 return fmt.Errorf("write import result: %w", err)
158 // WhatsAppImportPlan holds the new chat-day batches and the size of the merged
159 // log. Its estimate uses the measured cost of 1.3 calls per message, not a
160 // promise about a particular export or compaction retry.
161 type WhatsAppImportPlan struct {
162 cutoffs map[string]time.Time
174 // Write prints the plan without chat text or the owner's profile name.
175 func (p WhatsAppImportPlan) Write(out io.Writer) error {
176 cutoff := "none (no live WhatsApp batch)"
177 if !p.cutoff.IsZero() {
178 cutoff = p.cutoff.Format(time.RFC3339)
182 if !p.first.IsZero() {
183 dateRange = p.first.Format(time.RFC3339) + " to " + p.last.Format(time.RFC3339)
186 owner := "known (mapped to You)"
188 owner = "unknown (sender names kept as written)"
191 _, err := fmt.Fprintf(out, "chats: %d\n", len(p.chats))
193 return fmt.Errorf("write import plan: %w", err)
196 for _, chat := range p.chats {
198 cutoff, found := p.cutoffs[chat]
200 line += " (cutoff: " + cutoff.Format(time.RFC3339) + ")"
203 _, err = fmt.Fprintln(out, line)
205 return fmt.Errorf("write import plan: %w", err)
209 estimate := 1.3 * float64(p.messages)
210 if len(p.added) == 0 {
215 fmt.Sprintf("batches: %d (%d chat-days skipped)", len(p.added), p.skipped),
216 "dates: " + dateRange,
219 fmt.Sprintf("bytes: %d (new batch text)", p.bytes),
220 fmt.Sprintf("messages: %d (merged log)", p.messages),
221 fmt.Sprintf("estimated calls: %.0f (about 1.3 per message, limit %d)", estimate, rebuildCallLimit),
224 _, err = io.WriteString(out, strings.Join(lines, "\n")+"\n")
226 return fmt.Errorf("write import plan: %w", err)
232 // whatsappImportNames reads only local session data. Group names that the
233 // library keeps only in memory can also come from logged or pending batches.
234 // Ambiguous names have no ID. A missing session keeps sender names unchanged.
235 func whatsappImportNames(ctx context.Context, dir string) (names map[string]WhatsAppChat, owner string, err error) {
236 names = make(map[string]WhatsAppChat)
237 path := filepath.Join(dir, whatsappDirName, whatsappSessionFileName)
238 _, err = os.Stat(path)
239 if errors.Is(err, os.ErrNotExist) {
240 return names, "", nil
244 return nil, "", fmt.Errorf("read import session: %w", err)
247 address, err := whatsappSessionAddress(path)
249 return nil, "", fmt.Errorf("find import session address: %w", err)
252 container, err := sqlstore.New(ctx, "sqlite", address, waLog.Noop)
254 return nil, "", fmt.Errorf("open import session: %w", err)
258 closeErr := container.Close()
260 err = errors.Join(err, fmt.Errorf("close import session: %w", closeErr))
264 device, err := container.GetFirstDevice(ctx)
266 return nil, "", fmt.Errorf("read import device: %w", err)
269 if device.ID == nil {
270 return names, "", nil
273 owner = device.PushName
274 own := whatsappOwnIDs(device.ID.User, device.LID.User)
275 contacts, err := device.Contacts.GetAllContacts(ctx)
277 return nil, "", fmt.Errorf("read import contacts: %w", err)
280 for jid, contact := range contacts {
281 if slices.Contains(own, jid.User) {
285 for _, name := range []string{contact.FirstName, contact.FullName, contact.PushName, contact.BusinessName} {
287 whatsappImportName(names, WhatsAppChat{
288 ID: jid.ToNonAD().String(),
290 Group: jid.Server == "g.us",
296 data, err := os.ReadFile(filepath.Join(dir, whatsappDirName, whatsappStateFileName))
297 if errors.Is(err, os.ErrNotExist) {
298 return names, owner, nil
302 return nil, "", fmt.Errorf("read pending import names: %w", err)
305 var state WhatsAppStateFile
307 err = json.Unmarshal(data, &state)
309 return nil, "", fmt.Errorf("decode pending import names: %w", err)
312 for _, batch := range state.Batches {
313 whatsappImportName(names, batch.Chat)
316 return names, owner, nil
319 // whatsappImportName adds an exact match unless distinct IDs share its name.
320 func whatsappImportName(names map[string]WhatsAppChat, chat WhatsAppChat) {
321 old, found := names[chat.Name]
322 if found && old.ID != chat.ID {
326 names[chat.Name] = chat
329 // whatsappImportPlan filters each chat at its earliest live message across the
330 // target and root logs. Only imported days in the target are skipped. Each
331 // remaining chat-day becomes a work message dated at its last exported message.
332 // Overlapping input chat-days are refused.
333 func whatsappImportPlan(ctx context.Context, paths []string, old, rootLog []DatedMessage, names map[string]WhatsAppChat, owner string, location *time.Location) (WhatsAppImportPlan, error) {
334 plan := WhatsAppImportPlan{
335 ownerKnown: owner != "",
336 cutoffs: make(map[string]time.Time),
339 names = maps.Clone(names)
341 names = make(map[string]WhatsAppChat)
344 imported, err := whatsappImportHistory(old, rootLog, names, plan.cutoffs, location)
346 return plan, fmt.Errorf("read import history: %w", err)
349 seen := make(map[string]bool)
350 for _, path := range paths {
351 if ctx.Err() != nil {
352 return plan, ctx.Err()
355 chat, messages, err := whatsappImportRead(path, owner, location)
357 return plan, fmt.Errorf("read import export: %w", err)
360 known, found := names[chat.Name]
365 if !slices.Contains(plan.chats, chat.Name) {
366 plan.chats = append(plan.chats, chat.Name)
369 cutoff := plan.cutoffs[chat.Name]
370 if !cutoff.IsZero() && (plan.cutoff.IsZero() || cutoff.Before(plan.cutoff)) {
374 days := make(map[string][]WhatsAppMessage)
375 for _, message := range messages {
376 if !cutoff.IsZero() && !message.Time.Before(cutoff) {
380 day := message.Time.In(location).Format(time.DateOnly)
381 days[day] = append(days[day], message)
384 for _, day := range slices.Sorted(maps.Keys(days)) {
385 key := chat.Name + "\n" + day
392 return plan, errors.New("import inputs overlap: give only one export for each chat and day")
396 batch := WhatsAppBatch{
401 text := batch.Text(location)
402 first, body, _ := strings.Cut(text, "\n")
404 first = "[WhatsApp] " + chat.Name + ", " + batch.Messages[0].Time.In(location).Format(whatsappBatchDayLayout)
407 text = first + whatsappImportMark + "\n" + body
408 plan.added = append(plan.added, DatedMessage{
411 Date: batch.Messages[len(batch.Messages)-1].Time.Format(time.RFC3339Nano),
414 plan.bytes += len(text)
415 firstDate, lastDate := batch.Messages[0].Time, batch.Messages[len(batch.Messages)-1].Time
416 if plan.first.IsZero() || firstDate.Before(plan.first) {
417 plan.first = firstDate
420 if plan.last.IsZero() || lastDate.After(plan.last) {
426 plan.added, err = rebuildMerge(nil, plan.added)
428 return plan, fmt.Errorf("order import batches: %w", err)
431 plan.messages = len(old) + len(plan.added)
432 slices.Sort(plan.chats)
437 // whatsappImportHistory reads batch names and live cutoffs from both logs.
438 // Imported chat-days belong only to the target log, which is first.
439 func whatsappImportHistory(target, root []DatedMessage, names map[string]WhatsAppChat, cutoffs map[string]time.Time, location *time.Location) (map[string]bool, error) {
440 imported := make(map[string]bool)
441 for source, entries := range [][]DatedMessage{target, root} {
442 for _, entry := range entries {
443 if entry.Kind != "work" || !strings.HasPrefix(entry.Text, "[WhatsApp] ") {
447 first, _, _ := strings.Cut(entry.Text, "\n")
448 marked := strings.HasSuffix(first, whatsappImportMark)
449 first = strings.TrimSuffix(first, whatsappImportMark)
450 match := whatsappImportHeader.FindStringSubmatch(first)
453 name, day = match[1], match[3]
454 whatsappImportName(names, WhatsAppChat{
457 Group: strings.Contains(first, ", group "+match[2]+","),
460 match = whatsappImportUnknown.FindStringSubmatch(first)
462 name, day = match[1], match[2]
467 return nil, errors.New("invalid WhatsApp batch header")
472 imported[name+"\n"+day] = true
478 _, body, found := strings.Cut(entry.Text, "\n")
479 if !found || len(body) < len("15:04 ") {
480 return nil, errors.New("missing first WhatsApp message time")
483 date, err := time.ParseInLocation("2006-01-02 15:04", day+" "+body[:5], location)
485 return nil, fmt.Errorf("parse first live message time: %w", err)
488 if cutoffs[name].IsZero() || date.Before(cutoffs[name]) {
497 // whatsappImportRead reads the single _chat.txt in an export without extracting
498 // archive paths. Non-start lines continue the preceding message, and all
499 // invisible left-to-right marks are removed. Dates use location. Media and
500 // system messages remain text, including the written sender.
501 func whatsappImportRead(path, owner string, location *time.Location) (chat WhatsAppChat, messages []WhatsAppMessage, err error) {
502 chat.Name = strings.TrimPrefix(strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)), "WhatsApp Chat - ")
503 if chat.Name == "" || strings.ContainsAny(chat.Name, "\r\n") {
504 return chat, nil, errors.New("read export: chat name must be one nonempty line")
507 archive, err := zip.OpenReader(path)
509 return chat, nil, fmt.Errorf("open export: %w", err)
513 closeErr := archive.Close()
515 err = errors.Join(err, fmt.Errorf("close export: %w", closeErr))
521 for _, entry := range archive.File {
522 if filepath.Base(entry.Name) == "_chat.txt" && !entry.FileInfo().IsDir() {
524 return chat, nil, errors.New("read export: more than one _chat.txt")
532 return chat, nil, errors.New("read export: _chat.txt is missing")
535 if file.UncompressedSize64 > whatsappImportBytes {
536 return chat, nil, errors.New("read export: chat text exceeds 64 MiB")
539 reader, err := file.Open()
541 return chat, nil, fmt.Errorf("open chat text: %w", err)
545 closeErr := reader.Close()
547 err = errors.Join(err, fmt.Errorf("close export chat text: %w", closeErr))
551 scanner := bufio.NewScanner(io.LimitReader(reader, whatsappImportBytes+1))
552 scanner.Buffer(make([]byte, 64*1024), buildLineBytes)
555 line := strings.ReplaceAll(scanner.Text(), "\u200e", "")
556 match := whatsappImportStart.FindStringSubmatch(line)
558 if len(messages) == 0 {
563 return chat, nil, errors.New("read export: text before the first dated message")
566 messages[len(messages)-1].Text += "\n" + line
573 date, err = time.ParseInLocation("02/01/2006, 15:04:05", match[1], location)
575 return chat, nil, fmt.Errorf("parse export local date: %w", err)
578 messages = append(messages, WhatsAppMessage{
582 Mine: owner != "" && match[2] == owner && match[2] != chat.Name,
585 if match[2] == chat.Name {
590 if scanner.Err() != nil {
591 return chat, nil, fmt.Errorf("read chat text: %w", scanner.Err())
594 if len(messages) == 0 {
595 return chat, nil, errors.New("read export: no dated messages")
598 slices.SortStableFunc(messages, func(a, b WhatsAppMessage) int {
599 return a.Time.Compare(b.Time)
602 return chat, messages, nil