1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
package entry
import (
"errors"
"fmt"
"os"
"regexp"
"slices"
"strings"
"time"
"codeberg.org/snonux/gos/internal/prompt"
"codeberg.org/snonux/gos/internal/timestamp"
)
type State int
const (
Unknown State = iota
Inboxed // TODO: Implement
Queued
Posted
)
var (
validTags = []string{"ask", "prio", "now"} // TODO: Document all tags
ErrSizeLimitExceeded = errors.New("message size limit exceeded")
)
func (s State) String() string {
switch s {
case Unknown:
return "unknown"
case Inboxed:
return "inboxed"
case Queued:
return "queued"
case Posted:
return "posted"
default:
panic(fmt.Sprintf("unknown state: %d", int(s)))
}
}
var Zero = Entry{}
type Entry struct {
Path string
Time time.Time
State State
tags []string
}
func (e Entry) String() string {
if e.State == Inboxed {
return fmt.Sprintf("Path:%s;State:%s", e.Path, e.State)
}
return fmt.Sprintf("Path:%s;Stamp:%s,State:%s", e.Path, e.Time.Format(timestamp.Format), e.State)
}
// filePath format: /foo/foobarbaz.something.here.txt.STAMP.{posted,queued}
// or for inboxed: /foo.txt
// or inboxed with tags: /foo.prio.ask.txt
func New(filePath string) (Entry, error) {
e := Entry{Path: filePath}
// We want to get the STAMP!
parts := strings.Split(filePath, ".")
if len(parts) < 2 {
// Could be 2 if inboxed
return e, fmt.Errorf("not a valid entry path: %s", filePath)
}
for _, part := range parts {
if slices.Contains(validTags, part) {
e.tags = append(e.tags, part)
}
}
switch parts[len(parts)-1] {
case "queued":
e.State = Queued
case "posted":
e.State = Posted
default:
e.State = Inboxed
return e, nil
}
if len(parts) < 4 {
// If not inboxed, must be longer.
return e, fmt.Errorf("not a valid entry path: %s", filePath)
}
var err error
if e.Time, err = timestamp.Parse(parts[len(parts)-2]); err != nil {
return e, err
}
if e.Time.Before(timestamp.OldestValidTime()) {
return e, fmt.Errorf("entry time does not seem legit, it is too old: %v", e.Time)
}
return e, nil
}
func (e *Entry) Content() (string, []string, error) {
bytes, err := os.ReadFile(e.Path)
if err != nil {
return "", []string{}, err
}
content := strings.TrimSpace(string(bytes))
return content, extractURLs(content), nil
}
func (e Entry) ContentWithLimit(sizeLimit int) (string, []string, error) {
content, urls, err := e.Content()
if err != nil {
return "", urls, err
}
if len(content) > sizeLimit {
err := fmt.Errorf("%w (%d > %d): %v", ErrSizeLimitExceeded, len(content), sizeLimit, e)
if err2 := prompt.Acknowledge("You need to shorten the content as "+err.Error(), content); err2 != nil {
return "", urls, errors.Join(err, err2)
}
if err2 := e.Edit(); err2 != nil {
return "", urls, errors.Join(err, err2)
}
return e.ContentWithLimit(sizeLimit)
}
return content, urls, nil
}
func (e *Entry) MarkPosted() error {
if e.State == Inboxed {
return errors.New("entry still inboxed, can not mark as posted")
}
if e.State != Queued {
return errors.New("entry is not queued")
}
if e.State == Posted {
return errors.New("entry is already posted")
}
newPath, err := timestamp.UpdateInFilename(strings.TrimSuffix(e.Path, ".queued")+".posted", -2)
if err != nil {
return err
}
if err := os.Rename(e.Path, newPath); err != nil {
return err
}
e.State = Posted
return nil
}
func (e Entry) HasTag(tag string) bool {
return slices.Contains(e.tags, tag)
}
func (e Entry) Edit() error {
if err := prompt.EditFile(e.Path); err != nil {
return err
}
return nil
}
func (e Entry) Remove() error {
return os.Remove(e.Path)
}
func extractURLs(input string) []string {
urlPattern := `(http://|https://|ftp://)[^\s]+`
re := regexp.MustCompile(urlPattern)
return re.FindAllString(input, -1)
}
|