-
Notifications
You must be signed in to change notification settings - Fork 4
/
reject.go
183 lines (153 loc) · 4.46 KB
/
reject.go
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
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"os"
"path/filepath"
"time"
"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
"github.com/nbd-wtf/go-nostr"
)
type scriptPath string
const (
REJECT_EVENT scriptPath = "reject-event.tengo"
REJECT_FILTER scriptPath = "reject-filter.tengo"
)
var (
rejectEventCompiled *tengo.Compiled
lastRejectEventModtime time.Time
rejectFilterCompiled *tengo.Compiled
lastRejectFilterModtime time.Time
)
var defaultScripts = map[scriptPath]string{
REJECT_EVENT: `export func(event, relay, conn) {
if (event.kind == 0) {
if (conn.get_authed_pubkey() == "") {
return "auth-required: please auth before publishing metadata"
} else {
return undefined
}
}
if (event.kind != 1) {
return "we only accept kind:1 notes"
}
if len(event.content) > 140 {
return "notes must have up to 140 characters only"
}
if len(event.tags) > 0 {
return "notes cannot have tags"
}
for metadata in relay.query({ kinds: [0], authors: [event.pubkey] }) {
// if there is a metadata event we will accept this kind:1 note
return undefined
}
return "publish your metadata here first"
}`,
REJECT_FILTER: `http := import("http")
export func(filter, relay, conn) {
authed := conn.get_authed_pubkey()
if (!authed) {
return "auth-required: take a selfie and send it to the CIA"
}
random := http.get("https://www.random.org/integers/?num=1&min=1&max=9&col=1&base=10&format=plain&rnd=new")
if int(random) > 4 {
return format("you were not lucky enough: got %d but needed 4 or less", int(random))
}
return undefined
}`,
}
func rejectEvent(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
fpath := filepath.Join(s.CustomDirectory, string(REJECT_EVENT))
fstat, err := os.Stat(fpath)
if err != nil {
return true, "couldn't find script file"
}
var this *tengo.Compiled
if fstat.ModTime().After(lastRejectEventModtime) {
lastRejectEventModtime = fstat.ModTime()
script := tengo.NewScript([]byte(`
reject := import("userscript")
res := reject(event, relay, conn)
`))
modules := tengo.NewModuleMap()
for name, mod := range stdlib.SourceModules {
modules.AddSourceModule(name, []byte(mod))
}
for name, mod := range stdlib.BuiltinModules {
modules.AddBuiltinModule(name, mod)
}
source, _ := os.ReadFile(fpath)
modules.AddSourceModule("userscript", source)
script.SetImports(modules)
script.Add("event", nil)
script.Add("relay", nil)
script.Add("conn", nil)
rejectEventCompiled, err = script.Compile()
if err != nil {
return true, "script is invalid: " + err.Error()
}
this = rejectEventCompiled.Clone()
} else {
this = rejectEventCompiled.Clone()
}
this.Set("event", eventToTengo(event))
this.Set("relay", makeRelayObject(ctx))
this.Set("conn", makeConnectionObject(ctx))
if err := this.RunContext(ctx); err != nil {
return true, "script failed to run: " + err.Error()
}
res := this.Get("res")
if res.String() == "" {
return false, ""
} else {
return true, res.String()
}
}
func rejectFilter(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
fpath := filepath.Join(s.CustomDirectory, string(REJECT_FILTER))
fstat, err := os.Stat(fpath)
if err != nil {
return true, "couldn't find script file"
}
var this *tengo.Compiled
if fstat.ModTime().After(lastRejectFilterModtime) {
lastRejectFilterModtime = fstat.ModTime()
script := tengo.NewScript([]byte(`
reject := import("userscript")
res := reject(filter, relay, conn)
`))
modules := tengo.NewModuleMap()
for name, mod := range stdlib.SourceModules {
modules.AddSourceModule(name, []byte(mod))
}
for name, mod := range stdlib.BuiltinModules {
modules.AddBuiltinModule(name, mod)
}
source, _ := os.ReadFile(fpath)
modules.AddSourceModule("userscript", source)
modules.AddBuiltinModule("http", tengoHttp)
script.SetImports(modules)
script.Add("filter", nil)
script.Add("relay", nil)
script.Add("conn", nil)
rejectFilterCompiled, err = script.Compile()
if err != nil {
return true, "script is invalid: " + err.Error()
}
this = rejectFilterCompiled.Clone()
} else {
this = rejectFilterCompiled.Clone()
}
this.Set("filter", filterToTengo(filter))
this.Set("relay", makeRelayObject(ctx))
this.Set("conn", makeConnectionObject(ctx))
if err := this.RunContext(ctx); err != nil {
return true, "script failed to run: " + err.Error()
}
res := this.Get("res")
if res.String() == "" {
return false, ""
} else {
return true, res.String()
}
}