forked from clementlecorre/mail-to-telegram
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
88 lines (71 loc) · 2.09 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
package main
import (
"flag"
"github.com/emersion/go-imap/client"
"log"
"os"
"reflect"
"strconv"
)
var config Config
func init() {
// Email config
flag.StringVar(&config.EmailServer, "email-server", os.Getenv("EMAIL_SERVER"), "Email server (example: mail.domain.com:993)")
flag.StringVar(&config.EmailLogin, "email-login", os.Getenv("EMAIL_LOGIN"), "The login of your email account")
flag.StringVar(&config.EmailPassword, "email-passowrd", os.Getenv("EMAIL_PASSWORD"), "The password of your email account")
// Telegram config
flag.Int64Var(&config.TelegramUserID, "telegram-userid", 0, "Please ask to telegram bot @myidbot")
flag.StringVar(&config.TelegramToken, "telegran-token", os.Getenv("TELEGRAM_TOKEN"), "Telegram bot token (https://core.telegram.org/bots/api)")
// Other
flag.BoolVar(&config.Verbose, "v", false, "Enable verbose/debug")
flag.Parse()
if config.TelegramUserID == 0 {
var err error
config.TelegramUserID, err = strconv.ParseInt(os.Getenv("TELEGRAM_USER_ID"), 10, 64)
if err != nil {
log.Fatal("TELEGRAM_USER_ID invalid int")
}
}
if config.Verbose {
checkConfig := reflect.ValueOf(config)
typeOfS := checkConfig.Type()
for i := 0; i < checkConfig.NumField(); i++ {
log.Printf("Field: %s\tValue: %v\n", typeOfS.Field(i).Name, checkConfig.Field(i).Interface())
}
}
userID.ID = config.TelegramUserID
}
func dialClient() (*client.Client, func()) {
// Let's assume config is an IMAP client
log.Println("Connecting to server...")
// Connect to server
c, err := client.DialTLS(config.EmailServer, nil)
if err != nil {
log.Fatal(err)
}
if config.Verbose {
c.SetDebug(os.Stdout)
}
log.Println("Connected")
// Login
if err := c.Login(config.EmailLogin, config.EmailPassword); err != nil {
log.Fatal(err)
}
log.Println("Logged in")
// Select a mailbox
if _, err := c.Select("INBOX", false); err != nil {
log.Fatal(err)
}
return c, func() {
err := c.Logout() // Don't forget to logout
if err != nil {
log.Fatal(err)
}
}
}
func main() {
c, cleanup1 := dialClient()
defer cleanup1()
mc := IdleMailClient{Client: c}
mc.ListenForEmails()
}