-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
158 lines (125 loc) · 4.21 KB
/
client.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
package main
import "net"
import "fmt"
import "bufio"
import "strings"
var conn, _ = net.Dial("tcp", "91.212.186.20:6667")
// this is the preffered nickname, check what we get back
var my_nickname = "MrRobot"
func main() {
raw := bufio.NewReader(conn)
sendtoserver("NICK " + my_nickname)
sendtoserver("USER Robot robot my.hostname :I'm a Robot")
sendtoserver("JOIN #sam")
addcallbacks()
for {
rawmessage, _ := raw.ReadString('\n')
parsefromserver(rawmessage)
}
}
func sendtoserver(message string) {
fmt.Fprintf(conn, "%s\n", message)
fmt.Printf("<< %s\n", message)
}
func sendtotarget(target, message string) {
fmt.Fprintf(conn, "PRIVMSG %s :%s\n",target, message)
fmt.Printf("<< PRIVMSG %s :%s\n",target, message)
}
func parsefromserver(raw string) {
fmt.Printf(">> %s", raw)
splitted_semi := strings.Split(raw, ":")
if(len(splitted_semi) >= 1) {
if(len(splitted_semi[0]) > 0) {
// REPLY TO SERVER PINGS
if(strings.Contains(splitted_semi[0], "PING")) {
fmt.Fprintf(conn, "PONG :%s\n", splitted_semi[1])
fmt.Printf("<< PONG :%s\n", splitted_semi[1])
}
} else {
splitted := strings.Split(splitted_semi[1], " ")
if(len(splitted_semi) >= 1) {
parsecommand(splitted[0] ,splitted[1], raw)
}
}
}
}
func parsecommand(source, command, raw string) {
valid := false
for _,item := range bot.irccommands {
if(item.command == command) {
item.callback(source,raw)
valid = true
}
}
if(!valid) {
//fmt.Printf("[!] Unknown command %s -- SOURCE %s\n", command, source)
}
}
/**********************************************************************************************************/
var bot = BOT{}
type BOT struct {
irccommands []IRCCommand
botchancommands []BotChanCommand
botprivatecommands []BotPrivateCommand
}
func (b *BOT) addIRCcommand(command string, callback func(string,string)) {
b.irccommands = append(b.irccommands, IRCCommand{ command: command, callback: callback })
}
func (b *BOT) addChancommand(command string, callback func(string,string,string)) {
b.botchancommands = append(b.botchancommands, BotChanCommand{ command: command, callback: callback})
}
func (b *BOT) addPrivatecommand(command string, callback func(string,string)) {
b.botprivatecommands = append(b.botprivatecommands, BotPrivateCommand{command: command, callback: callback})
}
type IRCCommand struct {
command string
callback func(string, string)
}
type BotChanCommand struct {
command string
callback func(string, string, string)
}
type BotPrivateCommand struct {
command string
callback func(string, string)
}
/**********************************************************************************************************/
func addcallbacks() {
// IRC server stuff
bot.addIRCcommand("PRIVMSG", callback_privmsg)
// Public channel commands
bot.addChancommand("!hello", callback_pub_hello)
// Private bot commands
bot.addPrivatecommand("help", callback_prv_help)
}
/**********************************************************************************************************/
var callback_privmsg = func(source, raw string) {
// who is the message for ?
// :[email protected] PRIVMSG #sam :jow
// :[email protected] PRIVMSG MrRobot :jowkes
// One way or another, when splitting on " " it should be splitted[2]
splitted := strings.Split(raw, " ")
target := splitted[2]
// what is the mssage ?
// when splitting on : it should be splitted [2]
splitted = strings.Split(raw, ":")
message := strings.TrimSpace(splitted[2])
// who is it coming from ?
source_nick := source[0:strings.Index(source, "!")]
// if i'm the target, get the sender's nickname
if(strings.EqualFold(target,my_nickname)) {
for _,item := range bot.botprivatecommands {
if(strings.EqualFold(item.command,message)) { item.callback(source_nick, raw) }
}
} else {
for _,item := range bot.botchancommands {
if(strings.EqualFold(item.command,message)) { item.callback(target, source_nick, raw) }
}
}
}
var callback_prv_help = func(source_nick, raw string) {
sendtotarget(source_nick, "Help? I can't help " + source_nick)
}
var callback_pub_hello = func(channel, source_nick, raw string) {
sendtotarget(channel, "Hello " + source_nick)
}