go-xmpp/_example/example-gui.go

114 lines
2.3 KiB
Go
Raw Permalink Normal View History

2011-07-12 06:03:21 -07:00
package main
import (
2014-09-16 20:35:47 -07:00
"crypto/tls"
2011-07-12 06:03:21 -07:00
"log"
"os"
"strings"
2024-01-31 21:54:58 -08:00
2024-05-23 16:01:55 -07:00
"github.com/matterbridge/go-xmpp"
2024-01-31 21:54:58 -08:00
"github.com/mattn/go-gtk/gtk"
2011-07-12 06:03:21 -07:00
)
func main() {
gtk.Init(&os.Args)
2012-12-07 03:16:09 -08:00
window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
2011-07-12 06:03:21 -07:00
window.SetTitle("GoTalk")
window.Connect("destroy", func() {
gtk.MainQuit()
})
2012-12-07 03:16:09 -08:00
vbox := gtk.NewVBox(false, 1)
scrolledwin := gtk.NewScrolledWindow(nil, nil)
textview := gtk.NewTextView()
2011-07-12 06:03:21 -07:00
textview.SetEditable(false)
textview.SetCursorVisible(false)
scrolledwin.Add(textview)
vbox.Add(scrolledwin)
buffer := textview.GetBuffer()
2012-12-07 03:16:09 -08:00
entry := gtk.NewEntry()
2011-07-12 06:03:21 -07:00
vbox.PackEnd(entry, false, false, 0)
window.Add(vbox)
window.SetSizeRequest(300, 400)
window.ShowAll()
2012-12-07 03:16:09 -08:00
dialog := gtk.NewDialog()
2011-07-12 06:03:21 -07:00
dialog.SetTitle(window.GetTitle())
2012-12-07 03:16:09 -08:00
sgroup := gtk.NewSizeGroup(gtk.SIZE_GROUP_HORIZONTAL)
2011-07-12 06:03:21 -07:00
2012-12-07 03:16:09 -08:00
hbox := gtk.NewHBox(false, 1)
2011-07-12 06:03:21 -07:00
dialog.GetVBox().Add(hbox)
2012-12-07 03:16:09 -08:00
label := gtk.NewLabel("username:")
2012-04-03 09:46:21 -07:00
sgroup.AddWidget(label)
2011-07-12 06:03:21 -07:00
hbox.Add(label)
2012-12-07 03:16:09 -08:00
username := gtk.NewEntry()
2011-07-12 06:03:21 -07:00
hbox.Add(username)
2012-12-07 03:16:09 -08:00
hbox = gtk.NewHBox(false, 1)
2011-07-12 06:03:21 -07:00
dialog.GetVBox().Add(hbox)
2012-12-07 03:16:09 -08:00
label = gtk.NewLabel("password:")
2012-04-03 09:46:21 -07:00
sgroup.AddWidget(label)
2011-07-12 06:03:21 -07:00
hbox.Add(label)
2012-12-07 03:16:09 -08:00
password := gtk.NewEntry()
2011-07-12 06:03:21 -07:00
password.SetVisibility(false)
hbox.Add(password)
2013-02-11 19:47:23 -08:00
dialog.AddButton(gtk.STOCK_OK, gtk.RESPONSE_OK)
dialog.AddButton(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
2014-09-16 20:35:47 -07:00
dialog.SetDefaultResponse(gtk.RESPONSE_OK)
2011-07-12 06:03:21 -07:00
dialog.SetTransientFor(window)
dialog.ShowAll()
res := dialog.Run()
username_ := username.GetText()
password_ := password.GetText()
dialog.Destroy()
2012-12-07 03:16:09 -08:00
if res != gtk.RESPONSE_OK {
2011-07-12 06:03:21 -07:00
os.Exit(0)
}
2014-10-04 09:29:19 -07:00
xmpp.DefaultConfig = tls.Config{
ServerName: "talk.google.com",
2014-09-16 20:35:47 -07:00
InsecureSkipVerify: false,
}
2013-10-20 20:07:59 -07:00
talk, err := xmpp.NewClient("talk.google.com:443", username_, password_, false)
2011-07-12 06:03:21 -07:00
if err != nil {
log.Fatal(err)
}
entry.Connect("activate", func() {
text := entry.GetText()
tokens := strings.SplitN(text, " ", 2)
if len(tokens) == 2 {
func() {
defer recover()
talk.Send(xmpp.Chat{Remote: tokens[0], Type: "chat", Text: tokens[1]})
entry.SetText("")
}()
}
})
go func() {
for {
func() {
defer recover()
chat, err := talk.Recv()
if err != nil {
log.Fatal(err)
}
2012-12-07 03:16:09 -08:00
var iter gtk.TextIter
2011-07-12 06:03:21 -07:00
buffer.GetStartIter(&iter)
2013-02-11 19:47:23 -08:00
if msg, ok := chat.(xmpp.Chat); ok {
buffer.Insert(&iter, msg.Remote+": "+msg.Text+"\n")
}
2011-07-12 06:03:21 -07:00
}()
}
}()
gtk.Main()
}