Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Irc chat #2

Merged
merged 13 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ func GetVideotextCharByte(c byte) byte {
return byte(strings.LastIndexByte(CharTable, c))
}

func IsValidChar(c byte) bool {
func IsByteAValidChar(c byte) bool {
return c >= Sp && c <= Del
}

func IsUintAValidChar(u uint) bool {
return u >= Sp && u <= Del
}
46 changes: 46 additions & 0 deletions examples/chat/irc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"crypto/tls"
"fmt"

irc "github.com/thoj/go-ircevent"
)

const channel = "#go-eventirc-test"
const serverssl = "irc.freenode.net:7000"

func startIRC(envoi chan []byte, messageList *Messages) {
ircnick1 := "minitel"
irccon := irc.IRC(ircnick1, "IRCTestSSL")
irccon.UseTLS = true
irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true}
irccon.AddCallback("001", func(e *irc.Event) { irccon.Join(channel) })
irccon.AddCallback("366", func(e *irc.Event) {})

irccon.AddCallback("PRIVMSG", func(event *irc.Event) {
msg := event.Message()
nick := event.Nick

messageList.AppendMessage(nick, msg)
fmt.Printf("%s: %s\n", nick, msg)
})

go func() {
for {
select {
case msg := <-envoi:
irccon.Privmsg(channel, string(msg))
default:
continue
}
}
}()

err := irccon.Connect(serverssl)
if err != nil {
fmt.Printf("Err %s", err)
return
}
irccon.Loop()
}
117 changes: 117 additions & 0 deletions examples/chat/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"context"
"fmt"
"log"
"net/http"
"time"

"github.com/NoelM/minigo"
"nhooyr.io/websocket"
)

func main() {
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{OriginPatterns: []string{"*"}})
if err != nil {
log.Println(err)
return
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")

ctx, cancel := context.WithTimeout(r.Context(), time.Minute*10)
defer cancel()

recvKey := make(chan uint)
go listenKeys(c, ctx, recvKey)

envoi := make(chan []byte)
messageList := Messages{}
go startIRC(envoi, &messageList)

chat(c, ctx, recvKey, envoi, &messageList)
})

err := http.ListenAndServe("192.168.1.34:3615", fn)
log.Fatal(err)
}

func listenKeys(c *websocket.Conn, ctx context.Context, recvChan chan uint) {
fullRead := true
var keyBuffer []byte
var keyValue uint
var done bool

for {
var err error
var wsMsg []byte

if fullRead {
_, wsMsg, err = c.Read(ctx)
if err != nil {
continue
}
fullRead = false
}

for id, b := range wsMsg {
keyBuffer = append(keyBuffer, b)

done, keyValue, err = minigo.ReadKey(keyBuffer)
if done || err != nil {
keyBuffer = []byte{}
}
if done {
recvChan <- keyValue
}

if id == len(wsMsg)-1 {
fullRead = true
}
}

if ctx.Err() != nil {
return
}
}
}

func chat(c *websocket.Conn, ctx context.Context, recvKey chan uint, envoi chan []byte, messagesList *Messages) {
userInput := []byte{}

for {
select {
case key := <-recvKey:
if key == minigo.Envoi {
messagesList.AppendTeletelMessage("minitel", userInput)
envoi <- userInput

clearInput(c, ctx)
updateScreen(c, ctx, messagesList)
userInput = []byte{}

} else if key == minigo.Repetition {
updateScreen(c, ctx, messagesList)
updateInput(c, ctx, userInput)

} else if key == minigo.Correction {
corrInput(c, ctx, len(userInput))
userInput = userInput[:len(userInput)-2]

} else if minigo.IsUintAValidChar(key) {
appendInput(c, ctx, len(userInput), byte(key))
userInput = append(userInput, byte(key))

} else {
fmt.Printf("key: %d not supported", key)
}
default:
continue
}

if ctx.Err() != nil {
return
}
}
}
43 changes: 43 additions & 0 deletions examples/chat/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import "sync"

type MessageType int64

const (
Message_UTF8 MessageType = iota
Message_Teletel
)

type Message struct {
Nick string
Text string
Type MessageType
}

type Messages struct {
List []Message
Mtx sync.RWMutex
}

func (m *Messages) AppendTeletelMessage(nick string, text []byte) {
m.Mtx.Lock()
defer m.Mtx.Unlock()

m.List = append(m.List, Message{
Nick: nick,
Text: string(text),
Type: Message_Teletel,
})
}

func (m *Messages) AppendMessage(nick string, text string) {
m.Mtx.Lock()
defer m.Mtx.Unlock()

m.List = append(m.List, Message{
Nick: nick,
Text: text,
Type: Message_UTF8,
})
}
77 changes: 77 additions & 0 deletions examples/chat/screen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"context"
"fmt"

"github.com/NoelM/minigo"
"nhooyr.io/websocket"
)

func clearInput(c *websocket.Conn, ctx context.Context) {
buf := minigo.GetMoveCursorXY(1, 20)
buf = append(buf, minigo.GetCleanScreenFromCursor()...)
c.Write(ctx, websocket.MessageBinary, buf)
}

func updateScreen(c *websocket.Conn, ctx context.Context, list *Messages) {
currentLine := 1

list.Mtx.RLock()
defer list.Mtx.RUnlock()

for i := len(list.List) - 1; i >= 0; i -= 1 {
// 3 because the format is: "nick > text"
msgLen := len(list.List[i].Nick) + len(list.List[i].Text) + 3

// 2 because if msgLen < 40, the divide gives 0 and one break another line for readability
// nick > text
// <blank>
// nick > text2
msgLines := msgLen/40 + 2

if currentLine+msgLines > 20 {
break
}

buf := minigo.GetMoveCursorXY(0, currentLine)
buf = append(buf, minigo.EncodeMessage(fmt.Sprintf("%s > ", list.List[i].Nick))...)

if list.List[i].Type == Message_Teletel {
buf = append(buf, list.List[i].Text...)
} else {
buf = append(buf, minigo.EncodeMessage(list.List[i].Text)...)
}

buf = append(buf, minigo.GetCleanLineFromCursor()...)
buf = append(buf, minigo.GetMoveCursorReturn(1)...)
buf = append(buf, minigo.GetCleanLine()...)
c.Write(ctx, websocket.MessageBinary, buf)

currentLine += msgLines
}
}

func appendInput(c *websocket.Conn, ctx context.Context, inputLen int, key byte) {
y := inputLen / 40
x := inputLen % 40

buf := minigo.GetMoveCursorXY(x+1, y+20)
buf = append(buf, key)
c.Write(ctx, websocket.MessageBinary, buf)
}

func corrInput(c *websocket.Conn, ctx context.Context, inputLen int) {
y := (inputLen - 1) / 40
x := (inputLen - 1) % 40

buf := minigo.GetMoveCursorXY(x+1, y+20)
buf = append(buf, minigo.GetCleanLineFromCursor()...)
c.Write(ctx, websocket.MessageBinary, buf)
}

func updateInput(c *websocket.Conn, ctx context.Context, userInput []byte) {
buf := minigo.GetMoveCursorXY(1, 20)
buf = append(buf, userInput...)
c.Write(ctx, websocket.MessageBinary, buf)
}
33 changes: 33 additions & 0 deletions examples/irc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"crypto/tls"
"fmt"

irc "github.com/thoj/go-ircevent"
)

const channel = "#go-eventirc-test"
const serverssl = "irc.freenode.net:7000"

func main() {
ircnick1 := "blatiblat"
irccon := irc.IRC(ircnick1, "IRCTestSSL")
//irccon.VerboseCallbackHandler = true
//irccon.Debug = true
irccon.UseTLS = true
irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true}
irccon.AddCallback("001", func(e *irc.Event) { irccon.Join(channel) })
irccon.AddCallback("366", func(e *irc.Event) {})
irccon.AddCallback("PRIVMSG", func(event *irc.Event) {
msg := event.Message()
nick := event.Nick
fmt.Printf("%s: %s\n", nick, msg)
})
err := irccon.Connect(serverssl)
if err != nil {
fmt.Printf("Err %s", err)
return
}
irccon.Loop()
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ go 1.17

require (
github.com/gobwas/ws v1.0.2
github.com/thoj/go-ircevent v0.0.0-20210723090443-73e444401d64
nhooyr.io/websocket v1.8.7
)

require (
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee // indirect
github.com/gobwas/pool v0.2.0 // indirect
github.com/klauspost/compress v1.10.3 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading