From 63ad9247bc449c8e3ba25b6fcc559c27352fa0a6 Mon Sep 17 00:00:00 2001 From: sunglocto Date: Fri, 30 Jan 2026 15:56:58 +0000 Subject: [PATCH] a lodda changes my guy --- cache.go | 3 +-- gtk-helpers.go | 18 ++++++++++++++-- gtk-message.go | 44 ++++++++++++++++++++------------------- gtk-xmpp-markup.go | 37 ++++++++++++++++----------------- main.go | 14 ++++++------- style.css | 4 ++++ types.go | 51 +++++++++++++++++++++++----------------------- version.go | 3 +++ xmpp-helpers.go | 5 ++--- xmpp-vcard.go | 4 ++-- 10 files changed, 101 insertions(+), 82 deletions(-) create mode 100644 version.go diff --git a/cache.go b/cache.go index ad10da2..337171a 100644 --- a/cache.go +++ b/cache.go @@ -8,11 +8,11 @@ import ( "fmt" "github.com/diamondburned/gotk4/pkg/gdk/v4" "github.com/diamondburned/gotk4/pkg/gtk/v4" + "github.com/kirsle/configdir" "io" "net/http" "os" "path/filepath" - "github.com/kirsle/configdir" ) // global or app-level map/cache @@ -105,7 +105,6 @@ func newImageFromWeb(url string) *gtk.Image { return nil } - fullpath := filepath.Join(pa, sum) // step 3: save it diff --git a/gtk-helpers.go b/gtk-helpers.go index 8355d4f..318a5d5 100644 --- a/gtk-helpers.go +++ b/gtk-helpers.go @@ -1,11 +1,11 @@ package main import ( + "fmt" "github.com/diamondburned/gotk4/pkg/glib/v2" "github.com/diamondburned/gotk4/pkg/gtk/v4" "gosrc.io/xmpp/stanza" Jid "mellium.im/xmpp/jid" - "fmt" ) func scrollToBottomAfterUpdate(scrolledWindow *gtk.ScrolledWindow) { @@ -39,8 +39,22 @@ func switchToTab(jid string) { gen := gtk.NewBox(gtk.OrientationVertical, 0) mm.Range(func(k, v any) bool { + userbox := gtk.NewBox(gtk.OrientationHorizontal, 0) + u := v.(stanza.Presence) - gen.Append(gtk.NewLabel(Jid.MustParse(u.From).Resourcepart())) + var mu MucUser + u.Get(&mu) + + nick_label := gtk.NewLabel(Jid.MustParse(u.From).Resourcepart()) + affil_label := gtk.NewLabel(mu.MucUserItem.Affiliation) + + // nick_label.SetHAlign(gtk.AlignStart) + // affil_label.SetHAlign(gtk.AlignEnd) + + userbox.Append(nick_label) + userbox.Append(affil_label) + + gen.Append(userbox) return true }) diff --git a/gtk-message.go b/gtk-message.go index d9a75a5..4d192da 100644 --- a/gtk-message.go +++ b/gtk-message.go @@ -3,16 +3,17 @@ package main // This file contains the function that generates message widgets depending on message stanza import ( - "os" "context" + "encoding/base64" "fmt" "github.com/diamondburned/gotk4/pkg/gtk/v4" + "github.com/google/uuid" + "github.com/jacoblockett/sanitizefilename" + "github.com/jasonlovesdoggo/gopen" "gosrc.io/xmpp/stanza" "mellium.im/xmpp/jid" - "github.com/google/uuid" - "github.com/jasonlovesdoggo/gopen" - "encoding/base64" - "github.com/jacoblockett/sanitizefilename" + "os" + "path/filepath" ) func generateMessageWidget(p stanza.Packet) gtk.Widgetter { @@ -31,7 +32,7 @@ func generateMessageWidget(p stanza.Packet) gtk.Widgetter { reactions := gtk.NewBox(gtk.OrientationHorizontal, 0) reactions.SetVisible(false) - reaction := []string{"👍", "👎", "♥️", "🤣", "😭",} + reaction := []string{"👍", "👎", "♥️", "🤣", "😭"} for _, v := range reaction { like := gtk.NewButton() like.SetLabel(v) @@ -72,7 +73,6 @@ func generateMessageWidget(p stanza.Packet) gtk.Widgetter { ocu := OccupantID{} m.Get(&ocu) - authorBox := gtk.NewBox(gtk.OrientationHorizontal, 10) contentBox := gtk.NewBox(gtk.OrientationHorizontal, 0) // im := newImageFromPath("debug.png") @@ -80,6 +80,7 @@ func generateMessageWidget(p stanza.Packet) gtk.Widgetter { // authorBox.Append(im) im := getAvatar(m.From, ocu.ID) im.SetPixelSize(40) + im.AddCSSClass("author_img") authorBox.Append(im) al := gtk.NewLabel(jid.MustParse(m.From).Resourcepart()) al.AddCSSClass("author") @@ -101,20 +102,18 @@ func generateMessageWidget(p stanza.Packet) gtk.Widgetter { oob := stanza.OOB{} ok = m.Get(&oob) if ok { - // media := newPictureFromWeb(oob.URL) + // media := newPictureFromWeb(oob.URL) // media.SetCanShrink(false) // mainBox.Append(media) // media.AddCSSClass("chat_image") mbtn := gtk.NewButtonWithLabel("🖼️") // mbtn.SetChild(newImageFromWeb(oob.URL)) - mbtn.ConnectClicked(func(){ + mbtn.ConnectClicked(func() { gopen.Open(oob.URL) }) authorBox.Append(mbtn) } - - return mainBox } @@ -123,10 +122,15 @@ func getVAdjustment(scrolledWindow *gtk.ScrolledWindow) *gtk.Adjustment { return val } -func getAvatar(j, hash string) *gtk.Image { - hash = sanitizefilename.Sanitize(hash) +func getAvatar(j, hash string) *gtk.Image { // TODO: This function probably shouldn't be here, and should probably be in xmpp-helpers or somewhere similar. + p, err := ensureCache() + if err != nil { + return newImageFromPath("debug.png") + } - _, err := os.ReadFile(hash) + hash = filepath.Join(p, sanitizefilename.Sanitize(hash)) + + _, err = os.ReadFile(hash) if err == nil { return newImageFromPath(hash) } @@ -134,9 +138,9 @@ func getAvatar(j, hash string) *gtk.Image { iqResp, err := stanza.NewIQ(stanza.Attrs{ Type: "get", From: clientroot.Session.BindJid, - To: j, - Id: "vc2", - Lang: "e", + To: j, + Id: "vc2", + Lang: "en", }) if err != nil { @@ -150,7 +154,7 @@ func getAvatar(j, hash string) *gtk.Image { if err != nil { panic(err) } - result := <- mychan + result := <-mychan card, ok := result.Payload.(*VCard) if !ok { return newImageFromPath("debug.png") @@ -166,12 +170,10 @@ func getAvatar(j, hash string) *gtk.Image { panic(err) } - err = os.WriteFile(hash, data, 0644) if err != nil { panic(err) } - // TODO: Implement caching! return newImageFromPath(hash) -} +} diff --git a/gtk-xmpp-markup.go b/gtk-xmpp-markup.go index f4ddceb..814745f 100644 --- a/gtk-xmpp-markup.go +++ b/gtk-xmpp-markup.go @@ -1,19 +1,18 @@ -package main - -import ( - _ "regexp" -) - -// This file implements XEP-0393 partially by providing a function to get a Pango string. -// https://xmpp.org/extensions/xep-0393.html - - -func convertXEPToPango(text string) string { - /* FIXME this RegEx causes certain strings to appear invisible - text = regexp.MustCompile(`\*([^*]+)\*`).ReplaceAllString(text, "$1") - text = regexp.MustCompile(`_([^_]+)_`).ReplaceAllString(text, "$1") - text = regexp.MustCompile(`~([^~]+)~`).ReplaceAllString(text, "$1") - text = regexp.MustCompile("`([^`]+)`").ReplaceAllString(text, "$1") - */ - return text -} +package main + +import ( + _ "regexp" +) + +// This file implements XEP-0393 partially by providing a function to get a Pango string. +// https://xmpp.org/extensions/xep-0393.html + +func convertXEPToPango(text string) string { + /* FIXME this RegEx causes certain strings to appear invisible + text = regexp.MustCompile(`\*([^*]+)\*`).ReplaceAllString(text, "$1") + text = regexp.MustCompile(`_([^_]+)_`).ReplaceAllString(text, "$1") + text = regexp.MustCompile(`~([^~]+)~`).ReplaceAllString(text, "$1") + text = regexp.MustCompile("`([^`]+)`").ReplaceAllString(text, "$1") + */ + return text +} diff --git a/main.go b/main.go index 8f3b6aa..4c09a50 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( _ "embed" "encoding/xml" + "runtime" ) var loadedConfig lambdaConfig @@ -70,10 +71,10 @@ func main() { TransportConfiguration: xmpp.TransportConfiguration{ Address: loadedConfig.Server, }, - Jid: loadedConfig.Username, - Credential: xmpp.Password(loadedConfig.Password), - Insecure: loadedConfig.Insecure, - // StreamLogger: os.Stdout, + Jid: loadedConfig.Username, + Credential: xmpp.Password(loadedConfig.Password), + Insecure: loadedConfig.Insecure, + StreamLogger: os.Stdout, } router := xmpp.NewRouter() @@ -125,13 +126,12 @@ func main() { } v := &stanza.Version{} - v = v.SetInfo("Lambda", "1.0", "Windows") // TODO: Allow spoofing and report correct information + v = v.SetInfo("Lambda", lambda_version, runtime.GOOS) // TODO: Allow spoofing on user request iqResp.Payload = v s.Send(iqResp) }) - router.HandleFunc("message", func(s xmpp.Sender, p stanza.Packet) { m, ok := p.(stanza.Message) if !ok { @@ -155,7 +155,6 @@ func main() { _, ok = tabs[originator] if ok { - fmt.Println("valid") tabs[originator].msgs.Append(b) scrollToBottomAfterUpdate(scroller) } else { @@ -253,6 +252,7 @@ func activate(app *gtk.Application) { app.SetMenubar(gio.NewMenu()) window.SetTitle("Lambda") + window.AddCSSClass("ssd") menu := gtk.NewBox(gtk.OrientationHorizontal, 0) /* f_menu := gtk.NewMenuButton() diff --git a/style.css b/style.css index f9a924a..d282138 100644 --- a/style.css +++ b/style.css @@ -6,3 +6,7 @@ max-width: 50px; max-height: 50px; } + +.author_img { + border-radius 100%; +} diff --git a/types.go b/types.go index 22f9e79..c8ea861 100644 --- a/types.go +++ b/types.go @@ -1,26 +1,25 @@ -package main - -import ( - "sync" - "github.com/diamondburned/gotk4/pkg/gtk/v4" -) - -type chatTab struct { - isMuc bool - msgs *gtk.ListBox -} - -type lambdaConfig struct { - Server string - Username string - Password string - Insecure bool - Nick string -} - - -type mucUnit struct { - // key: OccupantID - // value: last user presence - Members sync.Map -} +package main + +import ( + "github.com/diamondburned/gotk4/pkg/gtk/v4" + "sync" +) + +type chatTab struct { + isMuc bool + msgs *gtk.ListBox +} + +type lambdaConfig struct { + Server string + Username string + Password string + Insecure bool + Nick string +} + +type mucUnit struct { + // key: OccupantID + // value: last user presence + Members sync.Map +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..069a1a6 --- /dev/null +++ b/version.go @@ -0,0 +1,3 @@ +package main + +var lambda_version string = "0.1.0" diff --git a/xmpp-helpers.go b/xmpp-helpers.go index b28b0ce..851e614 100644 --- a/xmpp-helpers.go +++ b/xmpp-helpers.go @@ -27,7 +27,7 @@ func sendMessage(c xmpp.Sender, sendTo string, msgType stanza.StanzaType, body s } // Joins a MUC -func joinMuc(c xmpp.Sender, jid string, muc string, nick string) error{ +func joinMuc(c xmpp.Sender, jid string, muc string, nick string) error { addr := muc + "/" + nick fmt.Println(addr) joinPresence := stanza.Presence{ @@ -36,8 +36,7 @@ func joinMuc(c xmpp.Sender, jid string, muc string, nick string) error{ To: addr, }, Extensions: []stanza.PresExtension{ - &stanza.MucPresence{ - }, + &stanza.MucPresence{}, }, } diff --git a/xmpp-vcard.go b/xmpp-vcard.go index 46b99e6..879ce35 100644 --- a/xmpp-vcard.go +++ b/xmpp-vcard.go @@ -9,8 +9,8 @@ import ( ) type VCard struct { - XMLName xml.Name `xml:"vcard-temp vCard"` - Photo Photo `xml:"PHOTO"` + XMLName xml.Name `xml:"vcard-temp vCard"` + Photo Photo `xml:"PHOTO"` ResultSet *stanza.ResultSet `xml:"set,omitempty"` }