Une semaine pour coder par soi même : Jour 6 et 7

Bon aujourd'hui je groupe le week-end dans un seul post. Hier ? J'ai … rien fait.

Voilà. Aujourd'hui ? J'ai … débloqué le tout petit truc qui me coinçait.

Bref je pense avoir surpassé tous les ptits trucs qui me faisaient chier et maintenant j'ai un code que je comprends de partout et qui marche comme je l'intuitais. Je suis super content.

En fait, le truc qui m'a complètement paralysé dans ma fonction connection.Interact() était tout bête : j'arrivais à récupérer le texte et donc à l'afficher, mais étrangement je ne parvenais pas à réagir à ce texte reçu. Du coup lorsque je recevais un PING, je le voyais, je créais le PONG qui va bien, mais il partait pas. Et j'ai buté sur ça depuis vendredi soir.

Et encore une fois cette nuit, l'illumination ! Dans ma fonction connection.Interact(), lorsque je recevais le PING, j'étais dans le case reader en train d'éxecuter la commande parseIrc, mais du coup, je n'étais plus en mesure d'écouter le chan entrant. Bon c'est dur à expliquer mais, le truc pour débloquer ça, a été tout simplement de foutre le parseIrc dans une routine en parallèle, comme ça, pendant que ça fait son boulot, la boucle se termine et elle est de nouveau disponible en écoute.

Voilà Samedi je n'y ai pas touché à cause de ça (bon j'ai été assez pris à côté faut dire, mais du coup je n'ai pas été très motivé). Une fois ce verrou enlevé j'ai eu envie de me refaire chier à faire des trucs tordus.

J'ai donc commencé un système antiflood, pour ralentir le texte sortant afin de ne pas se faire jarter comme un pleutre lorsqu'on balance trop de texte d'un coup. J'utilise donc une variable que j'incrémente à chaque fois que j'envoie du texte et qui se décrémente petit à petit chaque seconde. Bon j'ai pas encore fait tourner le truc comme il faut mais c'est une première étape.

Avant de rajouter des commandes à proprement parlé au bot, j'ai envie de gérer le texte sortant comme une FIFO et donc gérer ça avec un fichier comme j'ai fait dans le bot en bash. Je vais donc m'attarder à regarder un peu la doc de Go pour voir comment gérer les fichiers mais ça ne devrait pas être bien compliqué.

Bon et même si à la base je ne voulais que dédier une semaine à ce projet, il est suffisamment avancé pour que je le finisse maintenant. Vais pouvoir foutre au rencard mon Frankenscript et remplacer par ce ptit programme.

Le code 🔗

  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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main

import (
	"bufio"
	"fmt"
	"io"
	"net"
	"os"
	"strings"
	"time"
)

var server string = "localhost"
var port string = "6667"
var channel string = "#lms"
var nick string = "bab"
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 = false

type Ircconnection struct {
	Server   string
	Port     string
	Nick     string
	Channel  string
	Bidule   bool
	Conn     net.Conn
	Receiver chan string
	Emitter  chan string
	RawEmitter	chan string
	counter	 int
}

func (connection *Ircconnection) Connect() {
	var err error
	connection.Conn, err = net.Dial("tcp", server+":"+port)
//	defer connection.Conn.Close()
	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)
	connection.RawEmitter = 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()
//	go connection.handleCounter()
	go connection.Join(channel)
	go connection.Interact()
}
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")
	connection.Emitter <- msg
}
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() {
	scanner := bufio.NewScanner(connection.Conn)
	go func() {
		for scanner.Scan() {
			ln := scanner.Text()
			fmt.Println(jaune + "<< " + ln + normal)
			connection.Receiver <- ln
		}
	}()
}

func (connection *Ircconnection) Interact() {
	for {
		select {
			case writer := <-connection.Emitter:
    				connection.counter+= 200
    				if debug {
					fmt.Println(cyan + writer +" "+jaune+"[",connection.counter,"]"+ normal)
    				}
				go io.WriteString(connection.Conn,"PRIVMSG "+channel+" :"+writer+"\n")
			case writer := <-connection.RawEmitter:
				fmt.Println(cyan + writer + normal)
				go io.WriteString(connection.Conn,writer+"\n")
			case reader := <-connection.Receiver:
				//fmt.Println(cyan+reader+normal)
				go parseIrc(connection, reader)

		}
	}
}

// Futur système anti-flood. tout pourri pour le moment.
func (connection *Ircconnection) handleCounter(){
	for {
    		fmt.Println("On boucle : ",connection.counter)
		time.Sleep(time.Duration(connection.counter+500) * time.Millisecond)
		if (connection.counter > 0) {
			connection.counter -= 10
		} else {
    			
			connection.counter = 0
		}
	}
}

/////////////////// MAIN //////////////////////////////////////
func main() {
	if len(os.Args) > 1 {
		server = os.Args[1]
	}

	connection := NewIrcconnection()
	connection.Connect()

	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)
	}
	
}

// ------------------
// 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 {
    		fmt.Println("Syntax IRC ERROR !!!!")
		return
	}

	if elements[0] == "PING" {
		connection.RawEmitter <- "PONG :"+strings.TrimPrefix(msg, "PING :")
	}

	switch elements[1] {
	case "421":
		fmt.Println("!! Commande non reconnue par le serveur !!")
	case "433":
		fmt.Println("le pseudo déconne")
		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.Emitter<- "Salut "+channel+" !"
		}
	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":
    		fmt.Println("\n\n\nHa et merde on est déconnecté !\n\n\n")
		// Déconnecté
		time.Sleep(10000 * time.Millisecond)
		connection.Connect()
	}
	if len(elements) > 3 {
		switch elements[3] {
    			case ":cycle" :
				connection.Disconnect()
				connection.Connect()
			case ":heure" :
    				connection.Emitter<-"Paies-toi une montre vaut rien!"+msg
		}
	}
}

// ------------------
// 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]
	}
}

Bon bha voilà que s'achève cette semaine de blogposts chiants. Je vous referai ptet un article une fois terminé mais c'est tout.