-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPaste.go
91 lines (75 loc) · 1.95 KB
/
getPaste.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
package main
import (
"encoding/json"
"fmt"
"html"
"os"
"strings"
)
// informations needed to retrieve a paste
type userDataRetrieve struct {
UrlId string `json:"urlId"`
}
// to retrieve a paste
type pasteRetrieve struct {
Event event `json:"event"`
Sender string `json:"sender"`
Data userDataRetrieve `json:"data"`
}
type textRetrieved struct {
Text string `json:"text"`
NumView int `json:"num_view"`
CreatedOn string `json:"created_on"`
Burn bool `json:"is_burn"`
BurnView int `json:"burn_view"`
Ipfs bool `json:"is_ipfs"`
EncParams encParams `json:"encParams"`
}
func getPaste(urlId string, key string, selfAddress string) (textRetrieved, clearObjectUser) {
var urlIdData userDataRetrieve
urlIdData.UrlId = urlId
var userKey string
userKey = key
// if url is paste extract urlId and key
if strings.Contains(urlId, "http") {
urlId, key := extractLink(urlId)
urlIdData.UrlId = urlId
userKey = key
}
textToGet := pasteRetrieve{
Event: getText,
Sender: selfAddress,
Data: urlIdData,
}
receivedMessage := sendTextWithReply(&textToGet, 0, false)
messageByte := []byte(receivedMessage.Message)
var textData textRetrieved
err := json.Unmarshal(messageByte, &textData)
if err != nil {
panic(err)
}
decodedText := html.UnescapeString(textData.Text)
var content []byte
if decodedText == "" {
fmt.Printf("%s\nText not found%s\n", Red, Reset)
connectionData.ws.Close()
os.Exit(1)
}
if userKey != "" {
encParams := textData.EncParams
content = []byte(decrypt(userKey, &decodedText, encParams))
} else {
content = []byte(decodedText)
}
var clearObjectUser clearObjectUser
err = json.Unmarshal(content, &clearObjectUser)
if err != nil {
fmt.Printf("\n%sError with text retrieved. --key is needed for decryption%s\n", Red, Reset)
if *debug {
panic(err.Error())
}
connectionData.ws.Close()
os.Exit(1)
}
return textData, clearObjectUser
}