This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
103 lines (96 loc) · 2.51 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
package main
import (
"context"
"io/ioutil"
"os"
"path"
"strconv"
"sync"
"github.com/spf13/viper"
"github.com/elap5e/go-mobileqq-api/log"
"github.com/elap5e/go-mobileqq-api/mobileqq"
"github.com/elap5e/go-mobileqq-api/mobileqq/client"
"github.com/elap5e/go-mobileqq-api/mobileqq/client/auth"
)
var (
homeDir, _ = os.UserHomeDir()
baseDir = path.Join(homeDir, "."+mobileqq.PackageName)
config mobileqq.Config
)
func init() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(baseDir)
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
configPath := path.Join(baseDir, "config.yaml")
_ = ioutil.WriteFile(
configPath,
[]byte(mobileqq.ConfigYAML),
0600,
)
log.Fatal().Msgf("x_x [init] create config.yaml in %s", configPath)
} else {
// Config file was found but another error was produced
log.Fatal().Msgf("x_x [init] failed to load config.yaml")
}
} else {
if err := viper.Unmarshal(&config); err != nil {
log.Fatal().Err(err).Msg("x_x [init] failed to unmarshal config")
}
}
}
func main() {
ctx := context.Background()
cfg := mobileqq.NewClientConfigFromViper()
mqq := mobileqq.NewClient(&mobileqq.Options{
BaseDir: baseDir,
LogLevel: viper.GetString("configs.logLevel"),
Client: cfg,
})
if err := mqq.Run(ctx, func(ctx context.Context, once bool, restart chan struct{}) error {
errCh := make(chan error, 1)
wg := sync.WaitGroup{}
for _, account := range config.Accounts {
wg.Add(1)
go func(username, password string) {
defer wg.Done()
rpc := mqq.GetClient()
if once {
if err := auth.NewFlow(&auth.FlowOptions{
Username: username,
Password: password,
AuthAddr: config.Configs.Auth.Address,
CacheDir: cfg.CacheDir,
}, auth.NewHandler(&auth.HandlerOptions{
BaseDir: cfg.BaseDir,
Client: cfg.Engine.Client,
Device: cfg.Engine.Device,
}, rpc)).Run(ctx); err != nil {
errCh <- err
return
}
}
uin, _ := strconv.ParseInt(username, 10, 64)
if _, err := rpc.AccountSetStatus(ctx, client.NewAccountSetStatusRequest(
uint64(uin),
client.AccountStatusOnline,
false,
)); err != nil {
errCh <- err
return
}
}(account.Username, account.Password)
}
wg.Wait()
select {
case err := <-errCh:
return err
case <-restart:
return nil
}
}); err != nil {
log.Panic().Err(err).Msg("client unexpected exit")
}
}