This repository has been archived by the owner on May 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gobot-base.go
79 lines (72 loc) · 1.96 KB
/
gobot-base.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
// Steve Phillips / elimisteve
// 2011.03.25
package main
import (
"fmt"
"net"
"os"
"strings"
)
const (
BOT_NICK = "sbhx-gitgo"
IRC_SERVER = "irc.freenode.net:6667"
IRC_CHANNEL = "#sbhackerspace"
)
func ircSetup() (c net.Conn, e os.Error) {
//IRC_CHANNELS := []string{"#sbhackerspace"}
conn, err := net.Dial("tcp", "", IRC_SERVER)
if err == nil {
ircMsg(conn, "NICK " + BOT_NICK)
ircMsg(conn, "USER " + strings.Repeat(BOT_NICK+" ", 4))
ircMsg(conn, "JOIN " + IRC_CHANNEL)
// for _, channel := range IRC_CHANNELS {
// ircMsg(conn, "JOIN " + channel)
// }
}
return conn, err
}
func main() {
conn, err := ircSetup()
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf("Remote Address: %v\n", conn.RemoteAddr())
//
// Main loop
for { //
read_buf := make([]byte, 512)
length, err := conn.Read(read_buf)
if err != nil {
fmt.Printf("Error: %v\n", err)
conn.Close()
os.Exit(1)
} else {
msg := string(read_buf[:length])
fmt.Printf("%v\n", msg)
//
// Respond to PING
//
if strings.HasPrefix(msg, "PING") {
ircMsg(conn, "PONG " + msg)
fmt.Printf("PONG\n")
}
//
// Parse nick
//
if strings.Contains(msg, "PRIVMSG") {
nick := strings.Split(msg, "!", 2)[0][1:] // I love Go
fmt.Printf("Sent by " + nick)
}
//
// Parse msg
//
/*
* Future code goes here
*/
}
}
}
}
func ircMsg(c net.Conn, str string) {
c.Write([]uint8(str+"\n"))
}