119 lines
2.5 KiB
Go
119 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"gosrc.io/xmpp"
|
|
"gosrc.io/xmpp/stanza"
|
|
)
|
|
|
|
// This file has small functions that can be used to do XMPP stuff without writing tons of boilerplate
|
|
|
|
// Basic message sender. Anything more complex should be written by hand
|
|
func sendMessage(c xmpp.Sender, sendTo string, msgType stanza.StanzaType, body string, subject string, thread string, exts []stanza.MsgExtension) error {
|
|
m := stanza.Message{
|
|
Attrs: stanza.Attrs{
|
|
To: sendTo,
|
|
Type: msgType,
|
|
},
|
|
Body: body,
|
|
Subject: subject,
|
|
Thread: thread,
|
|
Extensions: exts,
|
|
}
|
|
err := c.Send(m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Joins a MUC
|
|
func joinMuc(c xmpp.Sender, jid string, muc string, nick string, password string) error {
|
|
var joinPresence stanza.Presence
|
|
addr := muc + "/" + nick
|
|
if password == "" {
|
|
joinPresence = stanza.Presence{
|
|
Attrs: stanza.Attrs{
|
|
From: jid,
|
|
To: addr,
|
|
},
|
|
Extensions: []stanza.PresExtension{
|
|
&stanza.MucPresence{},
|
|
},
|
|
}
|
|
} else {
|
|
joinPresence = stanza.Presence{
|
|
Attrs: stanza.Attrs{
|
|
From: jid,
|
|
To: addr,
|
|
},
|
|
Extensions: []stanza.PresExtension{
|
|
&stanza.MucPresence{
|
|
Password: password,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
err := client.Send(joinPresence)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func joinMucWithoutHistory(c xmpp.Sender, jid string, muc string, nick string, password string) error {
|
|
var joinPresence stanza.Presence
|
|
addr := muc + "/" + nick
|
|
if password == "" {
|
|
joinPresence = stanza.Presence{
|
|
Attrs: stanza.Attrs{
|
|
From: jid,
|
|
To: addr,
|
|
},
|
|
Extensions: []stanza.PresExtension{
|
|
&stanza.MucPresence{
|
|
History: stanza.History{
|
|
MaxChars: stanza.NewNullableInt(0),
|
|
MaxStanzas: stanza.NewNullableInt(0),
|
|
Seconds: stanza.NewNullableInt(0),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
} else {
|
|
joinPresence = stanza.Presence{
|
|
Attrs: stanza.Attrs{
|
|
From: jid,
|
|
To: addr,
|
|
},
|
|
Extensions: []stanza.PresExtension{
|
|
&stanza.MucPresence{
|
|
Password: password,
|
|
History: stanza.History{
|
|
MaxChars: stanza.NewNullableInt(0),
|
|
MaxStanzas: stanza.NewNullableInt(0),
|
|
Seconds: stanza.NewNullableInt(0),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
err := client.Send(joinPresence)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// jid MustParse but using gosrc's instead of mellium
|
|
// This function will panic if its an invalid JID
|
|
|
|
func JidMustParse(s string) *stanza.Jid {
|
|
j, err := stanza.NewJid(s)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return j
|
|
}
|