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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
package main
import (
"bufio"
"fmt"
"net"
"os"
"io"
"strings"
"time"
)
var server string = "localhost"
var port string = "6667"
var channel string = "#lms"
var nick string = "bab"
var commander string = ":Lord!Lord@geeknode.fuckyeah"
var onchan bool
var rouge string = "\033[1;31m"
var vert string = "\033[1;32m"
var jaune string = "\033[1;33m"
var cyan string = "\033[1;36m"
var normal string = "\033[0m"
var me string
var debug bool
type Ircconnection struct {
Server string
Port string
Nick string
Channel string
Bidule bool
Conn net.Conn
Receiver chan string
Emitter chan string
}
func (connection *Ircconnection) Connect(){
var err error
connection.Conn,err = net.Dial("tcp",server+":"+port)
fmt.Println(rouge+"Connection to "+server+":"+port+normal)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
connection.Receiver = make(chan string)
connection.Emitter = make(chan string)
fmt.Println(rouge+">> NICK "+nick+normal)
io.WriteString(connection.Conn,"NICK "+nick+"\n")
fmt.Println(rouge+">> USER "+nick+" 0.0.0.0 "+nick+" :"+nick+" bot"+normal)
io.WriteString(connection.Conn,"USER "+nick+" 0.0.0.0 "+nick+" :"+nick+" bot\n")
go io.Copy(connection.Conn, os.Stdin)
go connection.handleIncoming()
}
func (connection *Ircconnection) Disconnect(){
connection.Conn.Close()
}
func (connection Ircconnection) Join(channel string){
time.Sleep(1000 * time.Millisecond)
fmt.Println(rouge+">> JOIN "+channel+normal)
io.WriteString(connection.Conn,"JOIN "+channel+"\n")
}
func (connection Ircconnection) SendMsg(msg string){
fmt.Println(rouge+">> PRIVMSG "+connection.Channel+" :"+msg+normal)
io.WriteString(connection.Conn,"PRIVMSG "+connection.Channel+" :"+msg+"\n")
}
func NewIrcconnection() Ircconnection{
return Ircconnection{Server:server,Port:port,Nick:nick,Channel:channel}
}
func (connection *Ircconnection) SetNick(newnick string){
connection.Nick = newnick
fmt.Println(rouge+"Changement de pseudo pour : "+vert+newnick+normal)
io.WriteString(connection.Conn,"NICK :"+newnick+"\n")
}
func (connection *Ircconnection) handleIncoming(){
fmt.Println("Entrée dans handleIncoming()")
scanner := bufio.NewScanner(connection.Conn)
go func(){
for scanner.Scan(){
ln := scanner.Text()
fmt.Println(jaune+"<< "+ln+normal)
connection.Receiver <- ln
}
}()
fmt.Println("Sortie de handleIncoming()")
}
func (connection *Ircconnection) Interact(){
fmt.Println("Entrée dans Parse()")
for {
select {
case reader := <- connection.Receiver :
// fmt.Println(cyan+reader+normal)
parseIrc(*connection,reader)
case writer := <- connection.Emitter :
fmt.Println(cyan+writer+normal)
}
}
fmt.Println("Sortie de Parse()")
}
/////////////////// MAIN //////////////////////////////////////
func main() {
if len(os.Args) > 1 {
server = os.Args[1]
}
connection := NewIrcconnection()
go incoming(connection)
connection.Connect()
go connection.Join(channel)
connection.Interact()
//time.Sleep(5000 * time.Millisecond)
}
// ------------------
// Côté IRC
// ------------------
func parseIrc(connection Ircconnection, msg string){
var elements []string = strings.Fields(msg)
if debug {
for i, element := range elements {
fmt.Print(cyan+"[",i,"|"+normal+element+cyan+"] "+normal )
}
}
if len(elements)<2{
return
}
fmt.Println("")
if elements[0] == "PING" {
// connection.Emitter <- "Envoyer un PONG serait judicieux !"
fmt.Println(rouge+">> PONG "+strings.TrimPrefix(msg, "PING :")+normal)
io.WriteString(connection.Conn,"PONG "+strings.TrimPrefix(msg, "PING :")+"\n")
}
switch elements[1] {
case "421" :
fmt.Println("!! Commande non reconnue par le serveur !!")
case "433" :
nick = nick+"_"
io.WriteString(connection.Conn,"NICK "+nick+"\n")
case "JOIN" :
if ":"+nick == stringCut(elements[0],"!"){
me = strings.TrimPrefix(elements[0],":")
fmt.Println(me+" a rejoin le salon "+strings.Trim(elements[2],":"))
onchan = true
connection.SendMsg("c'est moi que vla!")
}
case "PART" :
fmt.Println("On est parti de "+strings.Trim(elements[2],":"))
onchan = false
case "KICK" :
if elements[3] == nick {
fmt.Println("Ptain on s'est fait kicker de "+elements[2]+" par "+elements[4]+" !")
io.WriteString(connection.Conn,"JOIN "+channel+"\n")
}
case ":Closing" :
// Déconnecté
time.Sleep (5000 * time.Millisecond)
connection.Connect()
}
if len(elements) > 3 {
if elements[3] == ":cycle" {
connection.Disconnect()
connection.Connect()
}
}
}
// ------------------
// Serveur en écoute
// ------------------
func incoming(connection Ircconnection){
in, err :=net.Listen("tcp",":4321")
defer in.Close()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for {
inconn, err :=in.Accept()
if err != nil {
fmt.Println(err)
continue
}
go handleIncoming(inconn,connection)
}
}
func handleIncoming(in net.Conn,connection Ircconnection){
fmt.Println(vert+"Incoming from ",in.RemoteAddr(),normal)
inbuf := bufio.NewReader(in)
for {
inmsg, err := inbuf.ReadString('\n')
if err != nil || !onchan || inmsg == "\n" {
break
}
fmt.Print(vert+"<<]] "+inmsg+normal)
connection.SendMsg(inmsg)
time.Sleep(500 * time.Millisecond)
}
}
// ------------
// Génériques
// ------------
func stringCut(incoming string, pattern string) string{
var results = strings.Split(incoming, pattern)
if len(results) < 1 {
return incoming
} else {
return results[0]
}
}
|