-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.go
208 lines (180 loc) · 4.63 KB
/
bot.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/dalinaum/gdgevent/event"
"log"
"net/textproto"
"strings"
"time"
)
type Bot struct {
server string
port string
nick string
user string
pass string
conn *textproto.Conn
retry int
}
func NewBot() *Bot {
return &Bot{
server: "irc.ozinger.org",
port: "6668",
nick: "안드냥",
pass: "",
conn: nil,
user: "gdgand",
}
}
func (bot *Bot) Connect() {
conn, err := textproto.Dial("tcp", bot.server+":"+bot.port)
if err != nil {
log.Fatal("unable to connect to IRC server", err)
}
bot.conn = conn
log.Printf("Connected to IRC server %s:%s\n", bot.server, bot.port)
userCommand := fmt.Sprintf("USER %s 8 * :%s\n", bot.user, bot.user)
bot.conn.PrintfLine(userCommand)
bot.conn.PrintfLine("NICK " + bot.nick)
}
func (bot *Bot) Pong(token string) {
request := fmt.Sprintf("PONG %s", token)
bot.conn.PrintfLine(request)
}
func (bot *Bot) Close() {
bot.conn.Close()
}
type Channel struct {
bot *Bot
channel string
}
func (b *Bot) NewChannel(channel string) *Channel {
return &Channel{
b,
channel,
}
}
func (c *Channel) Talk(msg string) {
text := fmt.Sprintf("PRIVMSG %s :%s", c.channel, msg)
c.bot.conn.PrintfLine(text)
}
func (c *Channel) Op(user string) {
request := fmt.Sprintf("MODE %s +o %s", c.channel, user)
c.bot.conn.PrintfLine(request)
}
func (c *Channel) Log(db *sql.DB, nick string, message string) {
request := fmt.Sprintf("INSERT INTO andnyang_log(date, channel, nick, message) VALUES ('%s', '%s', '%s', '%s')", time.Now().UTC(), c.channel, nick, message)
_, error := db.Exec(request)
if error != nil {
log.Print(error)
}
}
type GDGChannel struct {
*Channel
chapterId string
}
func (b *Bot) NewGDGChannel(channel, chapterId string) *GDGChannel {
return &GDGChannel{
b.NewChannel(channel),
chapterId,
}
}
func (c *GDGChannel) Activities() {
start := time.Unix(0, 0)
for _, e := range event.GetGDGEvents(c.chapterId, start, start) {
event := fmt.Sprintf("[%s] %s (https://developers.google.com%s)", e.GetStart(), e.Title, e.Link)
c.Talk(event)
}
}
func main() {
ircbot := NewBot()
ircbot.Connect()
defer ircbot.Close()
channels := [...]*GDGChannel{
ircbot.NewGDGChannel("#gdgand", "115078626730671785458"),
ircbot.NewGDGChannel("#gdgwomen", "108196114606467432743"),
}
db, error := sql.Open("mysql", "gdg:gdg@tcp(173.194.105.240:3306)/andnyang")
if error != nil {
log.Print(error)
}
defer db.Close()
for {
line, err := ircbot.conn.ReadLine()
if err != nil {
break
}
arr := TokenizeLine(line)
// Each line is started with ':' except 'PING' messages.
if arr[0] == "PING" {
token := arr[1]
ircbot.Pong(token)
continue
} else if arr[0][0] != ':' {
fmt.Printf("Something is wrong!\n")
fmt.Printf(">>> %s\n", line)
continue
}
systemMessageNo := arr[1]
if systemMessageNo == "433" {
ircbot.retry++
modifiedNick := fmt.Sprintf("NICK %s%d", ircbot.nick, ircbot.retry)
ircbot.conn.PrintfLine(modifiedNick)
continue
}
// We will send join message after we will get the first notification. Otherwise, the message we sent are not processed by the server.
if systemMessageNo == "001" {
for _, channel := range channels {
request := fmt.Sprintf("JOIN %s", channel.channel)
ircbot.conn.PrintfLine(request)
}
continue
}
command := arr[1]
name := strings.Split(arr[0][1:], "!")[0]
for _, channel := range channels {
if command == "PRIVMSG" && arr[2] == channel.channel && arr[3][1:] == "!action" {
channel.Activities()
} else if command == "PRIVMSG" && arr[2] == channel.channel && arr[3][1] == '!' {
fmt.Printf(">>> %s\n", line)
channel.Talk(arr[3][2:])
} else if command == "PRIVMSG" && arr[2] == channel.channel {
channel.Log(db, name, arr[3][1:])
} else if command == "JOIN" && arr[2][1:] == channel.channel {
fmt.Printf(">>> %s\n", line)
if name == ircbot.nick {
channel.Talk("오랜만이에요. :) 모두 안녕하세요.")
} else {
channel.Op(name)
}
}
}
fmt.Printf(">>> %s\n", line)
}
}
func TokenizeLine(line string) []string {
// Each line will break into four strings. First three strings will be splitted by whitespace, all rest will be the fourth string.
output := make([]string, 4)
oi := 0
n := 0
space := false
for i, c := range line {
if oi == 3 {
continue
} else if space == false && c == ' ' && n < i {
output[oi] = line[n:i]
space = true
n = i + 1
oi++
} else if space == true && c == ' ' {
n = i + 1
continue
} else if space == true && c != ' ' {
space = false
}
}
output[oi] = line[n:]
return output
}