-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoodlebot.go
132 lines (104 loc) · 2.8 KB
/
moodlebot.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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"strings"
"time"
"github.com/emersion/go-smtp"
"github.com/jhillyerd/enmime"
)
const meuemail = "Seu nome <seu email>"
// The Backend implements SMTP server methods.
type Backend struct{}
// Login returns a session after login. Here we do not demand any login
func (bkd *Backend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
return &Session{}, nil
}
// AnonymousLogin requires clients to authenticate using SMTP AUTH before sending emails
func (bkd *Backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
return &Session{}, nil
}
// A Session is returned after successful login.
type Session struct{}
// Mail starts a session after smtp connection
func (s *Session) Mail(from string, opts smtp.MailOptions) error {
log.Println("Mail from:", from)
return nil
}
// Rcpt prints the receipt
func (s *Session) Rcpt(to string) error {
log.Println("Rcpt to:", to)
return nil
}
// Data records the mail Data
func (s *Session) Data(r io.Reader) error {
if b, err := ioutil.ReadAll(r); err != nil {
return err
} else {
r := strings.NewReader(string(b))
env, err := enmime.ReadEnvelope(r)
if err != nil {
fmt.Println("deu erro no readnenvelope!")
fmt.Print(err)
}
fmt.Println("===================")
fmt.Println(env.Root.Header.Get("From"))
fmt.Println("===================")
if meuemail != env.Root.Header.Get("From") {
return err
}
url := "<discord hook>"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
_ = writer.WriteField("username", "<bot name>")
_ = writer.WriteField("content", env.Text)
err = writer.Close()
if err != nil {
fmt.Println(err)
}
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
}
req.Header.Add("Cookie", "__cfduid=d6498a7f20355e0632cb7500cbfe46be51591589127; __cfruid=8ffd542f0f49d35a80f0e69b2488f40b44813423-1591589127")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
log.Println("Data:", string(body))
}
return nil
}
func (s *Session) Reset() {}
func (s *Session) Logout() error {
return nil
}
// A message is sent to Discord
type Message struct {
url string
method string
username string
message string
}
func main() {
be := &Backend{}
s := smtp.NewServer(be)
s.Addr = ":25"
s.Domain = "stat-math.com.br"
s.ReadTimeout = 10 * time.Second
s.WriteTimeout = 10 * time.Second
s.MaxMessageBytes = 20 * 1024
s.MaxRecipients = 50
s.AllowInsecureAuth = true
log.Println("Starting server at", s.Addr)
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)
}
}