Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,35 @@ func (a *app) searchCmd() *command {
c.long = `Search messages in every chat, or in one with --chat. Telegram does the
matching: by whole words and their beginnings and inflections, not by
substring, so "introduce" also finds "introducing". Newest matches are kept
when there are more than --limit.`
when there are more than --limit. --since and --until narrow it to a time
range, --kind a search everywhere to one kind of chat.`
chatRef := c.flags.StringP("chat", "c", "", "search only in this chat")
limit := c.flags.IntP("limit", "n", 30, "maximum number of messages")
since := c.flags.String("since", "", "only messages from then on: 2026-11-01, 2026-11-01T15:04:05Z, or an age like 7d or 12h")
until := c.flags.String("until", "", "only messages until then, same forms. A day counts in full")
kind := c.flags.String("kind", "", "search everywhere only in chats of this kind: user (bots too), group or channel")
c.run = func(ctx context.Context, args []string) error {
q := core.SearchQuery{Text: strings.Join(args, " "), Limit: *limit, Kind: core.ChatKind(*kind)}
switch q.Kind {
case "", core.KindUser, core.KindGroup, core.KindChannel:
default:
return errUsage{fmt.Errorf("--kind %q: use user, group or channel", *kind)}
}
if q.Kind != "" && *chatRef != "" {
return errUsage{errors.New("--kind only applies to a search everywhere, drop --chat or --kind")}
}
now := time.Now()
var err error
if q.Since, err = parseWhen(*since, now, false); err != nil {
return errUsage{fmt.Errorf("--since: %w", err)}
}
if q.Until, err = parseWhen(*until, now, true); err != nil {
return errUsage{fmt.Errorf("--until: %w", err)}
}
if !q.Since.IsZero() && !q.Until.IsZero() && q.Since.After(q.Until) {
return errUsage{errors.New("--since is after --until, nothing can match: swap them")}
}
return a.backend(ctx, false, func(ctx context.Context, be core.Backend) error {
q := core.SearchQuery{Text: strings.Join(args, " "), Limit: *limit}
var chat *core.Chat
if *chatRef != "" {
resolved, err := be.Resolve(ctx, *chatRef)
Expand All @@ -495,6 +518,35 @@ when there are more than --limit.`
return c
}

// parseWhen reads a --since or --until value: a day, an RFC 3339 time, or an
// age before now in days (7d), hours or minutes (12h, 30m). With endOfDay, a
// day means its end, so --until 2026-11-01 includes that day. Empty is zero.
// Times are cut to the second, as message dates are.
func parseWhen(s string, now time.Time, endOfDay bool) (time.Time, error) {
if s == "" {
return time.Time{}, nil
}
now = now.Truncate(time.Second)
if days, ok := strings.CutSuffix(s, "d"); ok {
if n, err := strconv.Atoi(days); err == nil && n >= 0 {
return now.AddDate(0, 0, -n), nil
}
}
if age, err := time.ParseDuration(s); err == nil && age >= 0 {
return now.Add(-age).Truncate(time.Second), nil
}
if day, err := time.ParseInLocation(time.DateOnly, s, time.Local); err == nil {
if endOfDay {
day = day.AddDate(0, 0, 1).Add(-time.Second)
}
return day, nil
}
if t, err := time.Parse(time.RFC3339, s); err == nil {
return t.Truncate(time.Second), nil
}
return time.Time{}, fmt.Errorf("%q is not a date (2026-11-01), a time (2026-11-01T15:04:05Z) or an age (7d, 12h)", s)
}

func (a *app) markReadCmd() *command {
c := a.newCommand("mark-read <chat>", "Mark every message of a chat as read", 1, 1)
c.run = func(ctx context.Context, args []string) error {
Expand Down
33 changes: 33 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func TestUsageErrors(t *testing.T) {
{"read", "alice", "--bogus"},
{"nope"},
{"send", "alice"}, // no text, stdin is not a pipe with content
{"search", "x", "--kind", "bot"},
{"search", "x", "--kind", "channel", "-c", "alice"},
{"search", "x", "--since", "yesterday"},
{"search", "x", "--since", "2026-11-02", "--until", "2026-11-01"},
} {
if err := run(t, args...); !errors.As(err, new(errUsage)) {
t.Errorf("tg %s: got %v, want a usage error", strings.Join(args, " "), err)
Expand All @@ -110,6 +114,35 @@ func TestUsageErrors(t *testing.T) {
}
}

// A day passed to --until counts in full, so --since and --until with the
// same day cover that day.
func TestParseWhen(t *testing.T) {
now := time.Date(2026, 11, 21, 12, 0, 0, 0, time.Local)
for _, c := range []struct {
in string
endOfDay bool
want time.Time
}{
{"", false, time.Time{}},
{"7d", false, now.AddDate(0, 0, -7)},
{"12h", false, now.Add(-12 * time.Hour)},
{"1.5s", false, now.Add(-2 * time.Second)},
{"2026-11-01", false, time.Date(2026, 11, 1, 0, 0, 0, 0, time.Local)},
{"2026-11-01", true, time.Date(2026, 11, 1, 23, 59, 59, 0, time.Local)},
{"2026-11-01T15:04:05.5Z", true, time.Date(2026, 11, 1, 15, 4, 5, 0, time.UTC)},
} {
got, err := parseWhen(c.in, now, c.endOfDay)
if err != nil || !got.Equal(c.want) {
t.Errorf("parseWhen(%q, %v) = %v, %v, want %v", c.in, c.endOfDay, got, err, c.want)
}
}
for _, in := range []string{"yesterday", "-3d", "-2h", "7"} {
if _, err := parseWhen(in, now, false); err == nil {
t.Errorf("parseWhen(%q) accepted", in)
}
}
}

func TestMessageLine(t *testing.T) {
date := time.Date(2026, 3, 4, 9, 5, 0, 0, time.UTC)
cases := map[string]core.Message{
Expand Down
5 changes: 5 additions & 0 deletions internal/cli/guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ ALSO
tg read <chat> --after <id> only what is newer than a message
tg read <chat> --before <id> page back through older history
tg search <text> [-c <chat>] search everywhere, or in one chat
tg search <text> --since 2026-10-01 --until 2026-10-31 --kind channel -n 500
only in a time range (a date, an RFC
3339 time or an age like 7d, 12h), or
everywhere in chats of one kind: user,
group or channel
tg send <chat> -f <path> "caption" attach a file or image
tg download <chat> <id> -o <dir> save a message's photo or file. Never
over an existing file, never into tg's
Expand Down
5 changes: 5 additions & 0 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ type SearchQuery struct {
Text string
ChatID int64
Limit int
// Since and Until bound the message dates when set.
Since, Until time.Time
// Kind, when set, keeps a search everywhere to chats of that kind.
// KindUser covers bots too, Telegram does not tell them apart here.
Kind ChatKind
}

// EventKind tells what happened in an Event.
Expand Down
14 changes: 11 additions & 3 deletions internal/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,23 @@ func (b *Backend) Search(_ context.Context, q core.SearchQuery) ([]core.Message,
defer b.mu.Unlock()
var out []core.Message
for _, c := range b.chats {
if q.ChatID != 0 && c.ID != q.ChatID {
kind := c.Kind
if kind == core.KindBot {
kind = core.KindUser // as on Telegram
}
if q.ChatID != 0 && c.ID != q.ChatID || q.Kind != "" && kind != q.Kind {
continue
}
for _, m := range b.messages[c.ID] {
if strings.Contains(strings.ToLower(m.Text), strings.ToLower(q.Text)) {
out = append(out, m)
if !strings.Contains(strings.ToLower(m.Text), strings.ToLower(q.Text)) ||
!q.Since.IsZero() && m.Date.Before(q.Since) || !q.Until.IsZero() && m.Date.After(q.Until) {
continue
}
m.Chat = c.Title
out = append(out, m)
}
}
slices.SortStableFunc(out, func(x, y core.Message) int { return x.Date.Compare(y.Date) })
if q.Limit > 0 && len(out) > q.Limit {
out = out[len(out)-q.Limit:]
}
Expand Down
119 changes: 104 additions & 15 deletions internal/mtp/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"math"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -353,30 +354,89 @@ func (b *backend) CreateGroup(ctx context.Context, title string) (core.Chat, err
return core.Chat{}, errors.New("Telegram did not return the new group")
}

// Search pages through Telegram's results until q.Limit matches. Global
// search moves on with a (rate, chat, ID) cursor, search in one chat with the
// last message ID. Paging stops when a page is empty, repeats the last one,
// or ends the count Telegram gave.
func (b *backend) Search(ctx context.Context, q core.SearchQuery) ([]core.Message, error) {
limit := q.Limit
if limit <= 0 || limit > pageSize {
if limit <= 0 {
limit = pageSize
}
var res tg.MessagesMessagesClass
var err error
if q.ChatID == 0 {
res, err = b.api.MessagesSearchGlobal(ctx, &tg.MessagesSearchGlobalRequest{
Q: q.Text, Filter: &tg.InputMessagesFilterEmpty{}, OffsetPeer: &tg.InputPeerEmpty{}, Limit: limit,
})
} else {
var peer tg.InputPeerClass
var peer tg.InputPeerClass
if q.ChatID != 0 {
var err error
if peer, err = b.peers.input(q.ChatID); err != nil {
return nil, err
}
res, err = b.api.MessagesSearch(ctx, &tg.MessagesSearchRequest{
Peer: peer, Q: q.Text, Filter: &tg.InputMessagesFilterEmpty{}, Limit: limit,
})
}
if err != nil {
return nil, err
// Telegram's bounds leave out the second they name, the query's include it.
var minDate, maxDate int
if !q.Since.IsZero() {
minDate = clampDate(q.Since.Unix() - 1)
}
if !q.Until.IsZero() {
maxDate = clampDate(q.Until.Unix() + 1)
}
global := tg.MessagesSearchGlobalRequest{
Q: q.Text, Filter: &tg.InputMessagesFilterEmpty{}, MinDate: minDate, MaxDate: maxDate,
OffsetPeer: &tg.InputPeerEmpty{},
BroadcastsOnly: q.Kind == core.KindChannel, GroupsOnly: q.Kind == core.KindGroup, UsersOnly: q.Kind == core.KindUser,
}
inChat := tg.MessagesSearchRequest{Peer: peer, Q: q.Text, Filter: &tg.InputMessagesFilterEmpty{}, MinDate: minDate, MaxDate: maxDate}
var out []core.Message
var lastChat int64 // the chat of global.OffsetID
seen := 0
for len(out) < limit {
want := min(pageSize, limit-len(out))
var res tg.MessagesMessagesClass
var err error
if peer == nil {
global.Limit = want
res, err = b.api.MessagesSearchGlobal(ctx, &global)
} else {
inChat.Limit = want
res, err = b.api.MessagesSearch(ctx, &inChat)
}
if err != nil {
return nil, err
}
page, ok := res.AsModified()
if !ok {
break
}
last, ok := lastMessage(page.GetMessages())
if !ok {
break
}
chat := chatIDOf(last.GetPeerID())
// Results come newest first: a page that does not go past the
// cursor repeats what was already taken.
if peer != nil && inChat.OffsetID != 0 && last.GetID() >= inChat.OffsetID ||
peer == nil && last.GetID() == global.OffsetID && chat == lastChat {
break
}
out = append(out, b.messages(res)...)
seen += len(page.GetMessages())
if complete(res, seen) {
break
}
if peer != nil {
inChat.OffsetID = last.GetID()
continue
}
next, err := b.peers.input(chat)
if err != nil {
break
}
rate := last.GetDate() // what offset_rate falls back to without a next rate
if slice, ok := res.(*tg.MessagesMessagesSlice); ok {
if r, ok := slice.GetNextRate(); ok {
rate = r
}
}
global.OffsetRate, global.OffsetPeer, global.OffsetID, lastChat = rate, next, last.GetID(), chat
}
out := b.messages(res)
for i := range out {
if chat, ok := b.peers.chat(out[i].ChatID); ok {
out[i].Chat = chat.Title
Expand All @@ -386,6 +446,35 @@ func (b *backend) Search(ctx context.Context, q core.SearchQuery) ([]core.Messag
return out, nil
}

// lastMessage is the oldest message of a page of results, skipping deleted ones.
func lastMessage(raw []tg.MessageClass) (tg.NotEmptyMessage, bool) {
for i := len(raw) - 1; i >= 0; i-- {
if m, ok := raw[i].AsNotEmpty(); ok {
return m, true
}
}
return nil, false
}

// complete tells whether seen messages are all the results there are. A
// count of 0 alongside messages means Telegram did not give one.
func complete(res tg.MessagesMessagesClass, seen int) bool {
switch r := res.(type) {
case *tg.MessagesMessages:
return true
case *tg.MessagesMessagesSlice:
return !r.Inexact && r.Count > 0 && seen >= r.Count
case *tg.MessagesChannelMessages:
return !r.Inexact && r.Count > 0 && seen >= r.Count
}
return false
}

// clampDate fits a Unix time in the 32 bits Telegram sends dates in.
func clampDate(unix int64) int {
return int(min(max(unix, 1), math.MaxInt32))
}

func (b *backend) Avatar(ctx context.Context, chat core.Chat) ([]byte, error) {
if chat.PhotoID == 0 {
return nil, fmt.Errorf("chat %d has no photo", chat.ID)
Expand Down
Loading
Loading