143 lines
3.2 KiB
Go
143 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"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
|
|
}
|
|
|
|
func sendVoiceRequest(c xmpp.Sender, sendTo string) error {
|
|
err := c.SendRaw(fmt.Sprintf(`
|
|
<message from='%s'
|
|
id='yd53c486'
|
|
to='%s'>
|
|
<x xmlns='jabber:x:data' type='submit'>
|
|
<field var='FORM_TYPE'>
|
|
<value>http://jabber.org/protocol/muc#request</value>
|
|
</field>
|
|
<field var='muc#role'
|
|
type='list-single'
|
|
label='Requested role'>
|
|
<value>participant</value>
|
|
</field>
|
|
</x>
|
|
</message>
|
|
`, clientroot.Session.BindJid, sendTo))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|