Added Roster IQs

Added an overly primitive "disconnect" for the client to use in the chat client example
This commit is contained in:
CORNIERE Rémi
2019-12-23 09:04:18 +01:00
parent f0179ad90e
commit 390336b894
9 changed files with 278 additions and 11 deletions

View File

@@ -15,6 +15,7 @@ const (
rawInputWindow = "rw" // Where raw stanzas are written
contactsListWindow = "cl" // Where the contacts list is shown, and contacts are selectable
menuWindow = "mw" // Where the menu is shown
disconnectMsg = "msg"
// Menu options
disconnect = "Disconnect"
@@ -188,6 +189,12 @@ func setKeyBindings(g *gocui.Gui) {
log.Panicln(err)
}
// ==========================
// Disconnect message
if err := g.SetKeybinding(disconnectMsg, gocui.KeyEnter, gocui.ModNone, delMsg); err != nil {
log.Panicln(err)
}
}
// General
@@ -209,7 +216,20 @@ func getLine(g *gocui.Gui, v *gocui.View) error {
if len(cv.ViewBufferLines()) == 0 {
printContactsToWindow(g, viewState.contacts)
}
} else if l == disconnect || l == askServerForRoster {
} else if l == disconnect {
maxX, maxY := g.Size()
msg := "You disconnected from the server. Press enter to quit."
if v, err := g.SetView(disconnectMsg, maxX/2-30, maxY/2, maxX/2-29+len(msg), maxY/2+2, 0); err != nil {
if !gocui.IsUnknownView(err) {
return err
}
fmt.Fprintln(v, msg)
if _, err := g.SetCurrentView(disconnectMsg); err != nil {
return err
}
}
killChan <- disconnectErr
} else if l == askServerForRoster {
chlw, _ := g.View(chatLogWindow)
fmt.Fprintln(chlw, infoFormat+" Not yet implemented !")
} else if l == rawMode {
@@ -326,3 +346,11 @@ func cursorUp(g *gocui.Gui, v *gocui.View) error {
}
return nil
}
func delMsg(g *gocui.Gui, v *gocui.View) error {
if err := g.DeleteView(disconnectMsg); err != nil {
return err
}
errChan <- gocui.ErrQuit // Quit the program
return nil
}

View File

@@ -6,6 +6,7 @@ xmpp_chat_client is a demo client that connect on an XMPP server to chat with ot
import (
"encoding/xml"
"errors"
"flag"
"fmt"
"github.com/awesome-gocui/gocui"
@@ -40,10 +41,11 @@ var (
CorrespChan = make(chan string, 1)
textChan = make(chan string, 5)
rawTextChan = make(chan string, 5)
killChan = make(chan struct{}, 1)
killChan = make(chan error, 1)
errChan = make(chan error)
logger *log.Logger
logger *log.Logger
disconnectErr = errors.New("disconnecting client")
)
type config struct {
@@ -160,7 +162,7 @@ func startClient(g *gocui.Gui, config *config) {
router.HandleFunc("message", handlerWithGui)
if client, err = xmpp.NewClient(clientCfg, router, errorHandler); err != nil {
panic(fmt.Sprintf("Could not create a new client ! %s", err))
log.Panicln(fmt.Sprintf("Could not create a new client ! %s", err))
}
@@ -196,7 +198,13 @@ func startMessaging(client xmpp.Sender, config *config) {
var correspondent string
for {
select {
case <-killChan:
case err := <-killChan:
if err == disconnectErr {
sc := client.(xmpp.StreamClient)
sc.Disconnect()
} else {
logger.Println(err)
}
return
case text = <-textChan:
reply := stanza.Message{Attrs: stanza.Attrs{To: correspondent, From: config.Client[clientJid], Type: stanza.MessageTypeChat}, Body: text}
@@ -265,8 +273,7 @@ func readConfig() *config {
// If an error occurs, this is used to kill the client
func errorHandler(err error) {
fmt.Printf("%v", err)
killChan <- struct{}{}
killChan <- err
}
// Read the client roster from the config. This does not check with the server that the roster is correct.