format code, add confirmation to destroy muc, implement some disconnect logic

This commit is contained in:
2026-02-19 11:28:18 +00:00
parent 3f40d3da29
commit 39156af48a
4 changed files with 136 additions and 59 deletions

View File

@@ -80,6 +80,7 @@ func dropToSignInPage(err error) {
conf.Password = password_entry.Text() conf.Password = password_entry.Text()
conf.Nick = nickname_entry.Text() conf.Nick = nickname_entry.Text()
conf.Insecure = insecure_check.Active() conf.Insecure = insecure_check.Active()
conf.JoinBookmarks = true
var b bytes.Buffer var b bytes.Buffer
e := toml.NewEncoder(&b) e := toml.NewEncoder(&b)

178
main.go
View File

@@ -298,10 +298,11 @@ func main() {
TransportConfiguration: xmpp.TransportConfiguration{ TransportConfiguration: xmpp.TransportConfiguration{
Address: loadedConfig.Server, Address: loadedConfig.Server,
}, },
Jid: loadedConfig.Username + "/lambda." + str, Jid: loadedConfig.Username + "/lambda." + str,
Credential: xmpp.Password(loadedConfig.Password), Credential: xmpp.Password(loadedConfig.Password),
Insecure: loadedConfig.Insecure, Insecure: loadedConfig.Insecure,
StreamLogger: os.Stdout, StreamLogger: os.Stdout,
StreamManagementEnable: true,
} }
router := xmpp.NewRouter() router := xmpp.NewRouter()
@@ -525,9 +526,9 @@ func main() {
}) })
c, err := xmpp.NewClient(&config, router, func(err error) { c, err := xmpp.NewClient(&config, router, func(err error) {
showErrorDialog(err) connectionStatus.SetText(fmt.Sprintf("Disconnected: %s", err.Error()))
panic(err)
}) })
if err != nil { if err != nil {
showErrorDialog(err) showErrorDialog(err)
panic(err) panic(err)
@@ -540,66 +541,70 @@ func main() {
go func() { go func() {
for { for {
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
pingStatus.AddCSSClass("pending")
before := time.Now() before := time.Now()
iq := new(stanza.IQ) iq := new(stanza.IQ)
iq.From = clientroot.Session.BindJid iq.From = clientroot.Session.BindJid
iq.To = iq.From iq.To = iq.From
iq.Type = "get" iq.Type = "get"
ctx, _ := context.WithTimeout(context.Background(), 30 * time.Second) ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
mychan, err := client.SendIQ(ctx, iq) mychan, err := client.SendIQ(ctx, iq)
if err != nil { if err != nil {
panic(err) continue
} }
_ = <- mychan _ = <-mychan
pingStatus.SetText(fmt.Sprintf("%d ms", time.Since(before) / time.Millisecond)) pingStatus.RemoveCSSClass("pending")
pingStatus.SetText(fmt.Sprintf("%d ms", time.Since(before)/time.Millisecond))
} }
}() }()
connectionStatus.SetText(fmt.Sprintf("Connected as %s", JidMustParse(clientroot.Session.BindJid).Bare())) connectionStatus.SetText(fmt.Sprintf("Connected as %s", JidMustParse(clientroot.Session.BindJid).Bare()))
// Join rooms in bookmarks // Join rooms in bookmarks
books, err := stanza.NewItemsRequest("", "urn:xmpp:bookmarks:1", 0) if loadedConfig.JoinBookmarks {
if err == nil { books, err := stanza.NewItemsRequest("", "urn:xmpp:bookmarks:1", 0)
mychan, err := c.SendIQ(context.TODO(), books)
result := <-mychan
if err == nil { if err == nil {
res, ok := result.Payload.(*stanza.PubSubGeneric) mychan, err := c.SendIQ(context.TODO(), books)
if ok { result := <-mychan
for _, item := range res.Items.List { if err == nil {
go func() { res, ok := result.Payload.(*stanza.PubSubGeneric)
jid := item.Id if ok {
node := item.Any for _, item := range res.Items.List {
autojoin := false go func() {
nick := loadedConfig.Nick jid := item.Id
for _, attr := range node.Attrs { node := item.Any
if attr.Name.Local == "autojoin" { autojoin := false
autojoin = attr.Value == "true" nick := loadedConfig.Nick
} for _, attr := range node.Attrs {
} if attr.Name.Local == "autojoin" {
autojoin = attr.Value == "true"
for _, node := range node.Nodes { }
if node.XMLName.Local == "nick" {
nick = node.Content
}
}
_, ok := tabs.Load(jid)
if !ok && autojoin {
err := joinMuc(client, clientroot.Session.BindJid, jid, nick)
if err != nil {
panic(err)
} }
createTab(jid, true) for _, node := range node.Nodes {
b := gtk.NewButtonWithLabel(jid) if node.XMLName.Local == "nick" {
b.ConnectClicked(func() { nick = node.Content
b.AddCSSClass("accent") }
switchToTab(jid, &window.Window) }
})
menu.Append(b) _, ok := tabs.Load(jid)
} if !ok && autojoin {
}() err := joinMuc(client, clientroot.Session.BindJid, jid, nick)
if err != nil {
panic(err)
}
createTab(jid, true)
b := gtk.NewButtonWithLabel(jid)
b.ConnectClicked(func() {
b.AddCSSClass("accent")
switchToTab(jid, &window.Window)
})
menu.Append(b)
}
}()
}
} }
} }
} }
@@ -611,8 +616,8 @@ func main() {
connectionStatus.SetText("Connecting...") connectionStatus.SetText("Connecting...")
err = cm.Run() err = cm.Run()
if err != nil { if err != nil {
showErrorDialog(err) fmt.Println(err.Error())
panic(err) connectionStatus.SetText(fmt.Sprintf("Disconnected: %s", err.Error()))
} }
}() }()
@@ -654,7 +659,30 @@ func activate(app *gtk.Application) {
if ok { if ok {
cur := cur.(*chatTab) cur := cur.(*chatTab)
if cur.isMuc { if cur.isMuc {
client.SendRaw(fmt.Sprintf(` win := gtk.NewWindow()
win.SetTitle("Destroy MUC")
win.SetDefaultSize(400, 1)
win.SetResizable(false)
box := gtk.NewBox(gtk.OrientationVertical, 0)
box.Append(gtk.NewLabel("Are you sure? This MUC will be gone forever! (a very long time)"))
box.Append(gtk.NewLabel("If you wish to continue, type 'I understand'"))
cancel := gtk.NewButtonWithLabel("Cancel")
cancel.ConnectClicked(func() {
win.SetVisible(false)
})
en := gtk.NewEntry()
en.SetPlaceholderText("...")
submit := gtk.NewButtonWithLabel("Destroy")
submit.ConnectClicked(func() {
fmt.Println(en.Text())
if en.Text() == "I understand" {
cur, ok := tabs.Load(current)
if ok {
cur := cur.(*chatTab)
if cur.isMuc {
client.SendRaw(fmt.Sprintf(`
<iq from='%s' <iq from='%s'
id='begone' id='begone'
to='%s' to='%s'
@@ -666,8 +694,50 @@ func activate(app *gtk.Application) {
</query> </query>
</iq> </iq>
`, clientroot.Session.BindJid, current, JidMustParse(clientroot.Session.BindJid).Bare())) `, clientroot.Session.BindJid, current, JidMustParse(clientroot.Session.BindJid).Bare()))
}
}
win.SetVisible(false)
}
})
box.Append(en)
box.Append(submit)
box.Append(cancel)
mu, ok := mucmembers.Load(current)
if ok {
typed_mu := mu.(mucUnit)
typed_mu.Members.Range(func(k, v any) bool {
user, ok := v.(stanza.Presence)
if ok {
mu := MucUser{}
ok := user.Get(&mu)
if ok {
if mu.MucUserItem.JID != "" {
if JidMustParse(mu.MucUserItem.JID).Bare() == JidMustParse(clientroot.Session.BindJid).Bare() {
if mu.MucUserItem.Affiliation != "owner" {
box.Append(gtk.NewLabel("You are not an owner of this MUC and thus will most likely not be able to delete it"))
}
// return false
}
}
} else {
panic("not ok")
}
} else {
panic("not ok")
}
return true
})
} else {
panic("not ok")
}
win.SetChild(box)
win.SetVisible(true)
} }
} }
}) })
joinAction := gio.NewSimpleAction("join", nil) joinAction := gio.NewSimpleAction("join", nil)
@@ -704,6 +774,7 @@ func activate(app *gtk.Application) {
win := gtk.NewWindow() win := gtk.NewWindow()
win.SetTitle("Join MUC") win.SetTitle("Join MUC")
win.SetDefaultSize(400, 1) win.SetDefaultSize(400, 1)
win.SetResizable(false)
win.SetChild(box) win.SetChild(box)
btn.ConnectClicked(func() { btn.ConnectClicked(func() {
@@ -762,17 +833,16 @@ func activate(app *gtk.Application) {
cBox := gtk.NewBox(gtk.OrientationHorizontal, 0) cBox := gtk.NewBox(gtk.OrientationHorizontal, 0)
cBox.Append(gtk.NewImageFromPaintable(clientAssets["disconnect"])) cBox.Append(gtk.NewImageFromPaintable(clientAssets["disconnect"]))
connectionStatus = gtk.NewLabel("Disconnected") connectionStatus = gtk.NewLabel("Disconnected")
cBox.Append(connectionStatus) cBox.Append(connectionStatus)
statBar.Append(cBox) statBar.Append(cBox)
pBox := gtk.NewBox(gtk.OrientationHorizontal, 0) pBox := gtk.NewBox(gtk.OrientationHorizontal, 0)
pBox.SetTooltipText("Ping between you and your XMPP server") pBox.SetTooltipText("Ping between you and your XMPP server")
i := (gtk.NewImageFromPaintable(clientAssets["chart_bar"])) i := (gtk.NewImageFromPaintable(clientAssets["chart_bar"]))
i.AddCSSClass("icon") i.AddCSSClass("icon")
pBox.Append(i) pBox.Append(i)
pingStatus = gtk.NewLabel("...") pingStatus = gtk.NewLabel("...")
pBox.Append(pingStatus) pBox.Append(pingStatus)
statBar.Append(pBox) statBar.Append(pBox)

View File

@@ -34,6 +34,11 @@
color: grey; color: grey;
} }
.pending {
color: grey;
transition-duration: 0.5s;
}
.hat { .hat {
background-color: orange; background-color: orange;
color: black; color: black;

View File

@@ -11,11 +11,12 @@ type chatTab struct {
} }
type lambdaConfig struct { type lambdaConfig struct {
Server string Server string
Username string Username string
Password string Password string
Insecure bool Insecure bool
Nick string Nick string
JoinBookmarks bool
} }
type mucUnit struct { type mucUnit struct {