/
main.go
109 lines (88 loc) · 2.18 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
package main
import (
"github.com/laouji/gaufre/commands"
"github.com/laouji/gaufre/config"
"github.com/thoj/go-ircevent"
"log"
"net/http"
"regexp"
"strings"
)
type Stash struct {
Mentions []string
}
var stash = &Stash{
Mentions: nil,
}
func main() {
conf := config.LoadConfig()
con := irc.IRC(conf.Nickname, conf.Username)
con.UseTLS = true
con.Password = conf.Password
prefix := conf.MessagePrefix
if err := con.Connect(conf.Server); err != nil {
log.Fatal(err)
}
// RPL_WELCOME
con.AddCallback("001", func(e *irc.Event) {
con.Join(conf.Channel)
log.Println("-- connected --")
})
con.AddCallback("PRIVMSG", func(e *irc.Event) {
message := e.Message()
if targetName(message) != conf.AdminUsername {
return
}
if (isIgnoreNick(e.Nick)) {
return
}
message = e.Nick + ": " + message
stash.Mentions = stash.Mentions[:0]
stash.Mentions = append(stash.Mentions, message)
con.Whois(conf.AdminUsername)
})
// 301 - RPL_AWAY
con.AddCallback("301", func(e *irc.Event) {
log.Println("-- status is away --")
botReply := conf.AdminUsername + "が今いないけど、メッセージ転送しといたよ"
for mentionCount() > 0 {
args := strings.Split(nextMention(), " ")
statusCode, err := commands.SendPush(args)
if err != nil || statusCode != http.StatusOK {
botReply = "ごめん!" + conf.AdminUsername + "をIRCに呼ぼうとしたら、こんなエラーが出た: " + err.Error()
}
con.Privmsg(conf.Channel, prefix+botReply)
}
})
// 318 - RPL_ENDOFWHOIS
con.AddCallback("318", func(e *irc.Event) {
//stash.Mentions = stash.Mentions[:0]
})
con.Loop()
}
func targetName(msg string) string {
pattern := `^([^:]+): `
matches := regexp.MustCompile(pattern).FindStringSubmatch(msg)
if len(matches) > 1 {
return matches[1]
}
return ""
}
func isIgnoreNick(nick string) bool {
conf := config.LoadConfig()
ignoreNicks := conf.IgnoreNicks
for _, v := range ignoreNicks {
if v == nick {
return true
}
}
return false
}
func mentionCount() int {
return len(stash.Mentions)
}
func nextMention() string {
var mention = ""
mention, stash.Mentions = stash.Mentions[0], stash.Mentions[mentionCount():]
return mention
}