-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
106 lines (92 loc) · 2.84 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
// Author: Niels A.D.
// Project: gowarcraft3 (https://github.com/nielsAD/gowarcraft3)
// License: Mozilla Public License, v2.0
// capiclient is a command-line interface for the official classic Battle.net chat API.
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/fatih/color"
"github.com/nielsAD/gowarcraft3/network"
"github.com/nielsAD/gowarcraft3/network/chat"
"github.com/nielsAD/gowarcraft3/protocol/capi"
"golang.org/x/term"
)
var (
endpoint = flag.String("e", capi.Endpoint, "Endpoint")
apikey = flag.String("k", "", "API Key")
)
var logOut = log.New(color.Output, "", log.Ltime)
var logErr = log.New(color.Error, "", log.Ltime)
var stdin = bufio.NewReader(os.Stdin)
func main() {
flag.Parse()
if *apikey == "" {
fmt.Print("Enter API key: ")
if b, err := term.ReadPassword(int(os.Stdin.Fd())); err == nil {
*apikey = string(b)
} else {
logErr.Fatal("ReadPassword error: ", err)
}
fmt.Println()
}
b, err := chat.NewBot(&chat.Config{
Endpoint: *endpoint,
APIKey: *apikey,
})
if err != nil {
logErr.Fatal("NewBot error: ", err)
}
b.On(&network.AsyncError{}, func(ev *network.Event) {
var err = ev.Arg.(*network.AsyncError)
logErr.Println(color.RedString("[ERROR] %s", err.Error()))
})
b.On(&capi.ConnectEvent{}, func(ev *network.Event) {
var event = ev.Arg.(*capi.ConnectEvent)
logOut.Println(color.MagentaString("Joined channel '%s'", event.Channel))
})
b.On(&capi.UserUpdateEvent{}, func(ev *network.Event) {
var event = ev.Arg.(*capi.UserUpdateEvent)
logOut.Println(color.YellowString("%s has been updated (%+v)", event.Username, event))
})
b.On(&capi.UserLeaveEvent{}, func(ev *network.Event) {
var event = ev.Arg.(*capi.UserLeaveEvent)
if u, ok := b.User(event.UserID); ok {
logOut.Println(color.YellowString("%s has left the channel (after %dm)", u.Username, int(time.Since(u.Joined).Minutes())))
} else {
logOut.Println(color.YellowString("%s has left the channel", event.UserID))
}
})
b.On(&capi.MessageEvent{}, func(ev *network.Event) {
var event = ev.Arg.(*capi.MessageEvent)
if u, ok := b.User(event.UserID); ok {
logOut.Printf("[%s] %s: %s\n", strings.ToUpper(event.Type.String()), u.Username, event.Message)
} else {
logOut.Printf("[%s] %s\n", strings.ToUpper(event.Type.String()), event.Message)
}
})
if err := b.Connect(); err != nil {
logErr.Fatal("Connect error: ", err)
}
logOut.Println(color.MagentaString("Succesfully connected to %s", *endpoint))
go func() {
for {
line, err := stdin.ReadString('\n')
if err != nil {
b.Close()
break
}
if err := b.SendMessage(strings.TrimRight(line, "\r\n")); err != nil {
logErr.Println(color.RedString("[ERROR] %s", err.Error()))
}
}
}()
if err := b.Run(); err != nil && !network.IsCloseError(err) {
logErr.Println(color.RedString("[ERROR] %s", err.Error()))
}
}