This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
214 lines (184 loc) · 4.98 KB
/
main.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"io/ioutil"
"io"
"path/filepath"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"
"flag"
)
var cyan = color.New(color.FgCyan)
var cyanBold = cyan.Add(color.Bold)
var m = make(map[int]int) // IDs as key
var path string
func setPath(){
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0777)
}
}
func incrementMap(response []CheckMailTemplate) {
for _, v := range response {
if _, found := m[v.Id]; !found {
m[v.Id] = 0
}
}
}
type CheckMailTemplate struct {
Id int `json:"id"`
From string `json:"from"`
Subject string `json:"subject"`
Date string `json:"date"`
Attachments []struct {
Filename string `json:"filename"`
ContentType string `json:"contentType"`
Size int `json:"size"`
}
Body string `json:"body"`
Textbody string `json:"textBody"`
// Htmlbody string `json:"htmlbody,omitempty"`
}
var response []CheckMailTemplate // getMessage
func checkMail(name string, domainOnly string) int {
s := fmt.Sprintf("https://www.1secmail.com/api/v1/?action=getMessages&login=%v&domain=%v", name, domainOnly)
resp, _ := http.Get(s)
body, _ := ioutil.ReadAll(resp.Body)
err := json.Unmarshal([]byte(body), &response)
if err != nil {
fmt.Println(err)
}
resp.Body.Close()
numOfEmails := len(response)
switch numOfEmails {
case 0:
fmt.Printf("\r%s", "Mailbox is empty")
case 1:
fmt.Printf("\r%s", "You received 1 mail in your mailbox")
default:
e := fmt.Sprintf("You received %v mails in your mailbox", numOfEmails)
fmt.Printf("\r%s", e)
}
return numOfEmails
}
func saveMail(name, domainOnly string) { //use this function to save emails
for k, _ := range m {
if m[k] == 0 {
s := fmt.Sprintf("https://www.1secmail.com/api/v1/?action=readMessage&login=%v&domain=%v&id=%v", name, domainOnly, k)
resp, _ := http.Get(s)
body, _ := ioutil.ReadAll(resp.Body)
time.Sleep(1 * time.Second)
resp.Body.Close()
var recvmail CheckMailTemplate
err := json.Unmarshal([]byte(body), &recvmail)
if err != nil {
fmt.Println(err)
os.Exit(1)
} else {
receivedMail := fmt.Sprintf("ID: %v\nFrom: %v\nSubject: %v\nDate: %v\nText: %v\n", recvmail.Id, recvmail.From, recvmail.Subject, recvmail.Date, recvmail.Textbody)
filename := fmt.Sprintf("%v%v", path, recvmail.Id)
err := ioutil.WriteFile(filename, []byte(receivedMail), 0666)
if err != nil {
log.Fatal(err)
}
m[k] = 1
}
}
}
}
func deleteMail(name, domainOnly string) error {
delUrl := "https://www.1secmail.com/mailbox"
data := url.Values{}
data.Set("action", "deleteMailbox")
data.Set("login", name)
data.Set("domain", domainOnly)
_, err := http.PostForm(delUrl, data)
return err
}
func handleInterrupt(name, domainOnly string) {
signalChannel := make(chan os.Signal)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChannel
deleteMail(name, domainOnly)
err := os.RemoveAll(path)
if err != nil {
log.Fatal(err)
}
clear()
fmt.Println("Emails Deleted. Exiting.")
os.Exit(0)
}()
}
func verifyName() string {
bannedEmail := []string{"abuse", "webmaster", "contact", "postmaster", "hostmaster", "admin"}
var name string
fmt.Print("Input name: ")
fmt.Scan(&name)
name = strings.ToLower(name)
for _, v := range bannedEmail {
if name == v {
fmt.Printf("You cannot read messages from these addresses: \n%v", bannedEmail)
fmt.Println()
fmt.Println("All other addresses are free to use.")
os.Exit(1)
}
}
return name
}
func generateEmail(name string) (string, string) {
domain := "1secmail"
rootDomain := []string{".com", ".net", ".org"}
randomIndex := rand.Intn(len(rootDomain))
value := rootDomain[randomIndex]
domainOnly := domain + value
email := name + "@" + domain + value
return email, domainOnly
}
func createEmail(name string, domainOnly string) {
info := fmt.Sprintf("https://www.1secmail.com/?login=%v&domain=%v", name, domainOnly)
resp, err := http.Get(info)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
fmt.Printf("Status Code: %v. Email not created.", resp.StatusCode)
os.Exit(1)
}
}
func clear() {
c := exec.Command("clear")
c.Stdout = os.Stdout
c.Run()
}
func main() {
flag.StringVar(&path, "path", "/tmp/1secmails/", "specify directory to store emails")
flag.Parse()
setPath()
name := verifyName()
email, domainOnly := generateEmail(name)
createEmail(name, domainOnly)
clear()
printName := cyanBold.Sprintf(email)
fmt.Printf("Your Temporary Email: %v\n", printName)
fmt.Println("Mailbox content is refreshed automatically every 5 seconds.")
fmt.Printf("All Emails are saved in %v\n", path)
handleInterrupt(name, domainOnly) // goroutine
for {
checkMail(name, domainOnly)
// handleInterrupt(name, domainOnly) // while checking for mails
incrementMap(response)
saveMail(name, domainOnly)
// handleInterrupt(name, domainOnly) // after receiving mails
time.Sleep(5 * time.Second)
}
}