-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.odin
157 lines (124 loc) · 2.73 KB
/
main.odin
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
package main
import "core:bufio"
import "core:encoding/ini"
import "core:encoding/json"
import "core:fmt"
import "core:os"
import "core:os/os2"
import "core:strings"
import "onostr"
InputMessage :: struct {
type: string `json:"type"`,
event: onostr.Event `json:"event"`,
received_at: i64 `json:"receivedAt"`,
source_type: string `json:"sourceType"`,
source_info: string `json:"sourceType"`,
}
OutputMessage :: struct {
id: string `json:"id"`,
action: string `json:"action"`,
msg: string `json:"msg"`,
}
NIP29_KINDS :: [?]int {
9,
10,
11,
12,
9000,
9001,
9002,
9003,
9004,
9005,
9007,
9008,
9021,
9022,
39000,
39001,
39002,
39003,
}
main :: proc() {
// Read ini file for configuration
config, err, ok := ini.load_map_from_path("config.ini", context.allocator)
defer delete(config)
if !ok {
panic("Failed to load config.ini")
}
pk_hex := config["config"]["pk_hex"]
// Create KeyPair for signing
kp, kpok := onostr.make_keypair_from_hex(pk_hex).?
defer if kpok {
onostr.destroy_keypair(&kp)
}
if !kpok {
panic("Failed to create keypair")
}
strfry_cmd_path := config["config"]["strfry_cmd_path"]
if strfry_cmd_path == "" {
strfry_cmd_path = "strfry"
}
reader: bufio.Reader
bufio.reader_init(&reader, os.stream_from_handle(os.stdin))
defer bufio.reader_destroy(&reader)
for {
// Set allocator to temp_allocator for the current context
context.allocator = context.temp_allocator
// Clear all allocations in temp_allocator at the end of the loop iteration
defer free_all(context.temp_allocator)
line, err := bufio.reader_read_string(&reader, '\n')
if err != nil {
break
}
line = strings.trim_right_space(line)
accept(line)
}
}
accept :: proc(id: string) {
msg := OutputMessage {
id = id,
action = "accept",
msg = "",
}
json_bytes, err := json.marshal(msg)
if err != nil {
panic("Failed to marshal JSON")
}
fmt.println(string(json_bytes))
os.flush(os.stdout)
}
reject :: proc(id: string, msg: string) {
msg := OutputMessage {
id = id,
action = "reject",
msg = msg,
}
json_bytes, err := json.marshal(msg)
if err != nil {
panic("Failed to marshal JSON")
}
fmt.println(string(json_bytes))
os.flush(os.stdout)
}
query_with_command :: proc(cmd: []string) -> [dynamic]onostr.Event {
process_desc := os2.Process_Desc {
command = cmd,
stdin = os2.stdin,
}
_, so, _, _ := os2.process_exec(process_desc, context.allocator)
lines := strings.split_lines(string(so))
events := make([dynamic]onostr.Event, context.allocator)
for line in lines {
if len(line) == 0 {
continue
}
event: onostr.Event
err := json.unmarshal_string(line, &event)
if err != nil {
continue
}
append(&events, event)
}
return events
}