-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.go
67 lines (60 loc) · 1.49 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
package irc
import (
"fmt"
"golang.org/x/net/proxy"
"log"
"net"
)
type BotInterface interface {
Connect(conn net.Conn, err error)
Send(string)
}
type Bot struct {
proxy string
server string
Nick string
User string
Channel string
pass string
pread, pwrite chan string
conn net.Conn
}
func (bot *Bot) Send(command string) {
fmt.Fprintf(bot.conn, "%s \r\n", command)
}
func NewBot(proxy string, server string, Nick string, User string, Channel string, pass string) *Bot {
return &Bot{
proxy: proxy,
server: server,
Nick: Nick,
User: User,
Channel: Channel,
pass: pass,
conn: nil,
}
}
func (bot *Bot) Connect() (conn net.Conn, err error) {
if len(bot.proxy) > 0 {
dialer, err := proxy.SOCKS5("tcp", bot.proxy, nil, proxy.Direct)
if err != nil {
log.Fatal("unable to connect to proxy server ", err)
}
bot.conn, err = dialer.Dial("tcp", bot.server)
if err != nil {
log.Fatal("unable to connect to IRC server ", err)
}
} else {
bot.conn, err = net.Dial("tcp", bot.server)
if err != nil {
log.Fatal("unable to connect to IRC server ", err)
}
}
log.Printf("Connected to IRC server %s (%s) \n", bot.server, bot.conn.RemoteAddr())
if len(bot.pass) > 0 {
bot.Send(fmt.Sprintf("PASS %s \r\n", bot.pass))
}
bot.Send(fmt.Sprintf("USER %s 8 * :%s", bot.User, bot.User))
bot.Send(fmt.Sprintf("NICK %s \r\n", bot.Nick))
bot.Send(fmt.Sprintf("JOIN %s \r\n", bot.Channel))
return bot.conn, err
}