-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
130 lines (106 loc) · 2.85 KB
/
utils.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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/gookit/color"
)
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func generateRandomString(length int) string {
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
var ids = make(map[int]int)
func toggleMap(response []CheckMail) {
for _, v := range response {
if _, found := ids[v.Id]; !found {
ids[v.Id] = 0
}
}
}
func handleInterrupt(name string, domainOnly string) {
channel := make(chan os.Signal, 1)
signal.Notify(channel, os.Interrupt, syscall.SIGTERM)
go func() {
<-channel
err := deleteMail(name, domainOnly)
if err != nil {
log.Fatal(err)
}
clearConsole()
fmt.Println("Emails Deleted. Exiting.")
os.Exit(0)
}()
}
func clearConsole() {
switch runtime.GOOS {
case "linux", "darwin":
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
case "windows":
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
default:
fmt.Println("Unsupported platform. Cannot clear console.")
}
}
func saveMailsToFile(name string, domain string) {
for k := range ids {
if ids[k] == 0 {
s := fmt.Sprintf("https://www.1secmail.com/api/v1/?action=readMessage&login=%v&domain=%v&id=%v", name, domain, k)
resp, _ := http.Get(s)
body, _ := io.ReadAll(resp.Body)
time.Sleep(1 * time.Second)
resp.Body.Close()
var receivedEmail CheckMail
err := json.Unmarshal([]byte(body), &receivedEmail)
if err != nil {
fmt.Println(err)
os.Exit(1)
} else {
receivedMail := fmt.Sprintf("ID: %v\nFrom: %v\nSubject: %v\nDate: %v\nText: %v\n", receivedEmail.Id, receivedEmail.From, receivedEmail.Subject, receivedEmail.Date, receivedEmail.Textbody)
path := domain + "/" + name + ".txt"
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if _, err := f.WriteString(string(receivedMail)); err != nil {
log.Println(err)
}
saveDocuments(receivedEmail, name, domain, k)
ids[k] = 1
}
}
}
}
func saveDocuments(receivedEmail CheckMail, name string, domain string, index int) {
for _, value := range receivedEmail.Attachments {
s := fmt.Sprintf("https://www.1secmail.com/api/v1/?action=download&login=%v&domain=%v&id=%v&file=%v", name, domain, index, value.Filename)
fileResponse, _ := http.Get(s)
defer fileResponse.Body.Close()
out, err := os.Create(domain + "/" + value.Filename)
if err != nil {
return
}
defer out.Close()
io.Copy(out, fileResponse.Body)
color.Success.Println(value.Filename, "downloaded successfully!")
time.Sleep(1 * time.Second)
}
}