This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
printer.go
155 lines (138 loc) · 3.87 KB
/
printer.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
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
"github.com/dustin/go-humanize"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip04"
"gopkg.in/yaml.v2"
)
var kindNames = map[int]string{
nostr.KindSetMetadata: "Profile Metadata",
nostr.KindTextNote: "Text Note",
nostr.KindRecommendServer: "Relay Recommendation",
nostr.KindContactList: "Contact List",
nostr.KindEncryptedDirectMessage: "Encrypted Message",
nostr.KindDeletion: "Deletion Notice",
nostr.KindBoost: "Boost",
nostr.KindReaction: "Reaction",
nostr.KindChannelCreation: "Channel Creation",
nostr.KindChannelMetadata: "Channel Metadata",
nostr.KindChannelMessage: "Channel Message",
nostr.KindChannelHideMessage: "Channel Hide Message",
nostr.KindChannelMuteUser: "Channel Mute User",
}
func printEvent(evt nostr.Event, nick *string, verbose bool, jsonformat bool) {
kind, ok := kindNames[evt.Kind]
if !ok {
kind = fmt.Sprintf("Unknown Kind (%d)", evt.Kind)
}
// Don't print encrypted messages that aren't for me or from me
pubkey := getPubKey(config.PrivateKey)
if evt.Kind == nostr.KindEncryptedDirectMessage {
if (!evt.Tags.ContainsAny("p", nostr.Tag{getPubKey(config.PrivateKey)})) && (evt.PubKey != pubkey) {
return
}
}
// json
if jsonformat {
jevt, _ := json.MarshalIndent(evt, "", "\t")
fmt.Print(string(jevt))
return
}
var ID string = shorten(evt.ID)
var fromField string = shorten(evt.PubKey)
if nick != nil {
fromField = fmt.Sprintf("%s (%s)", *nick, shorten(evt.PubKey))
}
if verbose {
ID = evt.ID
if nick == nil {
fromField = evt.PubKey
} else {
fromField = fmt.Sprintf("%s (%s)", *nick, evt.PubKey)
}
}
fmt.Printf("%s [%s] from %s %s\n",
kind,
ID,
fromField,
humanize.Time(evt.CreatedAt),
)
switch evt.Kind {
case nostr.KindSetMetadata:
var metadata Metadata
err := json.Unmarshal([]byte(evt.Content), &metadata)
if err != nil {
fmt.Printf("Invalid JSON: '%s',\n %s",
err.Error(), evt.Content)
return
}
y, _ := yaml.Marshal(metadata)
spl := strings.Split(string(y), "\n")
for i, v := range spl {
spl[i] = " " + v
}
str := strings.Join(spl, "\n")
fmt.Print(str)
case nostr.KindTextNote:
fmt.Print(" " + strings.ReplaceAll(evt.Content, "\n", "\n "))
case nostr.KindBoost:
var event nostr.Event
err := json.Unmarshal([]byte(evt.Content), &event)
if err != nil {
fmt.Println("ERR", err)
return
}
kind, ok := kindNames[event.Kind]
if !ok {
kind = "Unknown Kind"
}
var ID string = shorten(event.ID)
var fromField string = shorten(event.PubKey)
fmt.Printf(" %s [%s] from %s %s\n",
kind,
ID,
fromField,
humanize.Time(event.CreatedAt),
)
fmt.Print(" ", event.Content)
case nostr.KindRecommendServer:
case nostr.KindContactList:
case nostr.KindEncryptedDirectMessage:
sharedSecret, err := nip04.ComputeSharedSecret(config.PrivateKey, evt.PubKey)
if err != nil {
log.Printf("Error computing shared key: %s. \n", err.Error())
return
}
txt, err := nip04.Decrypt(evt.Content, sharedSecret)
if err != nil {
log.Printf("Error decrypting message: %s. \n", err.Error())
return
}
fmt.Print(txt)
default:
fmt.Print(evt.Content)
}
fmt.Printf("\n")
}
func shorten(id string) string {
if len(id) < 12 {
return id
}
return id[0:4] + "..." + id[len(id)-4:]
}
func printPublishStatus(event *nostr.Event, statuses chan nostr.PublishStatus) {
for status := range statuses {
switch status.Status {
case nostr.PublishStatusSent:
fmt.Printf("Sent event %s to '%s'.\n", event.ID, status.Relay)
case nostr.PublishStatusFailed:
fmt.Printf("Failed to send event %s to '%s'.\n", event.ID, status.Relay)
case nostr.PublishStatusSucceeded:
fmt.Printf("Seen %s on '%s'.\n", event.ID, status.Relay)
}
}
}