commit
f8c4ecb59d
@ -0,0 +1,38 @@
|
||||
name: Run tests
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths:
|
||||
- '**.go'
|
||||
- 'go.*'
|
||||
- .github/workflows/test.yaml
|
||||
|
||||
push:
|
||||
paths:
|
||||
- '**.go'
|
||||
- 'go.*'
|
||||
- .github/workflows/test.yaml
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Set up Go 1.13
|
||||
uses: actions/setup-go@v1
|
||||
with:
|
||||
go-version: 1.13
|
||||
id: go
|
||||
- uses: actions/checkout@v1
|
||||
- name: Run tests
|
||||
run: |
|
||||
go test ./... -v -race -coverprofile cover.out -covermode=atomic
|
||||
- name: Convert coverage to lcov
|
||||
uses: jandelgado/gcov2lcov-action@v1.0.0
|
||||
with:
|
||||
infile: cover.out
|
||||
outfile: coverage.lcov
|
||||
- name: Coveralls
|
||||
uses: coverallsapp/github-action@v1.0.1
|
||||
with:
|
||||
github-token: ${{ secrets.github_token }}
|
||||
path-to-lcov: coverage.lcov
|
@ -1,4 +0,0 @@
|
||||
FROM golang:1.13
|
||||
WORKDIR /xmpp
|
||||
RUN curl -o codecov.sh -s https://codecov.io/bash && chmod +x codecov.sh
|
||||
COPY . ./
|
@ -0,0 +1,3 @@
|
||||
# XMPP Multi-User (MUC) chat bot example
|
||||
|
||||
This code shows how to build a simple basic XMPP Multi-User chat bot using Fluux Go XMPP library.
|
@ -0,0 +1,51 @@
|
||||
# Chat TUI example
|
||||
This is a simple chat example, with a TUI.
|
||||
It shows the library usage and a few of its capabilities.
|
||||
## How to run
|
||||
### Build
|
||||
You can build the client using :
|
||||
```
|
||||
go build -o example_client
|
||||
```
|
||||
and then run with (on unix for example):
|
||||
```
|
||||
./example_client
|
||||
```
|
||||
or you can simply build + run in one command while at the example directory root, like this:
|
||||
```
|
||||
go run xmpp_chat_client.go interface.go
|
||||
```
|
||||
|
||||
### Configuration
|
||||
The example needs a configuration file to run. A sample file is provided.
|
||||
By default, the example will look for a file named "config" in the current directory.
|
||||
To provide a different configuration file, pass the following argument to the example :
|
||||
```
|
||||
go run xmpp_chat_client.go interface.go -c /path/to/config
|
||||
```
|
||||
where /path/to/config is the path to the directory containing the configuration file. The configuration file must be named
|
||||
"config" and be using the yaml format.
|
||||
|
||||
Required fields are :
|
||||
```yaml
|
||||
Server :
|
||||
- full_address: "localhost:5222"
|
||||
Client : # This is you
|
||||
- jid: "testuser2@localhost"
|
||||
- pass: "pass123" #Password in a config file yay
|
||||
|
||||
# Contacts list, ";" separated
|
||||
Contacts : "testuser1@localhost;testuser3@localhost"
|
||||
# Should we log stanzas ?
|
||||
LogStanzas:
|
||||
- logger_on: "true"
|
||||
- logfile_path: "./logs" # Path to directory, not file.
|
||||
```
|
||||
|
||||
## How to use
|
||||
Shortcuts :
|
||||
- ctrl+space : switch between input window and menu window.
|
||||
- While in input window :
|
||||
- enter : sends a message if in message mode (see menu options)
|
||||
- ctrl+e : sends a raw stanza when in raw mode (see menu options)
|
||||
- ctrl+c : quit
|
@ -0,0 +1,13 @@
|
||||
# Sample config for the client
|
||||
Server :
|
||||
- full_address: "localhost:5222"
|
||||
Client :
|
||||
- jid: "testuser2@localhost"
|
||||
- pass: "pass123" #Password in a config file yay
|
||||
|
||||
Contacts : "testuser1@localhost;testuser3@localhost"
|
||||
|
||||
LogStanzas:
|
||||
- logger_on: "true"
|
||||
- logfile_path: "./logs"
|
||||
|
@ -0,0 +1,10 @@
|
||||
module go-xmpp/_examples/xmpp_chat_client
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/awesome-gocui/gocui v0.6.1-0.20191115151952-a34ffb055986
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/spf13/viper v1.6.1
|
||||
gosrc.io/xmpp v0.4.0
|
||||
)
|
@ -0,0 +1,371 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/awesome-gocui/gocui"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// Windows
|
||||
chatLogWindow = "clw" // Where (received and sent) messages are logged
|
||||
chatInputWindow = "iw" // Where messages are written
|
||||
rawInputWindow = "rw" // Where raw stanzas are written
|
||||
contactsListWindow = "cl" // Where the contacts list is shown, and contacts are selectable
|
||||
menuWindow = "mw" // Where the menu is shown
|
||||
disconnectMsg = "msg"
|
||||
|
||||
// Windows titles
|
||||
chatLogWindowTitle = "Chat log"
|
||||
menuWindowTitle = "Menu"
|
||||
chatInputWindowTitle = "Write a message :"
|
||||
rawInputWindowTitle = "Write or paste a raw stanza. Press \"Ctrl+E\" to send :"
|
||||
contactsListWindowTitle = "Contacts"
|
||||
|
||||
// Menu options
|
||||
disconnect = "Disconnect"
|
||||
askServerForRoster = "Ask server for roster"
|
||||
rawMode = "Switch to Send Raw Mode"
|
||||
messageMode = "Switch to Send Message Mode"
|
||||
contactList = "Contacts list"
|
||||
backFromContacts = "<- Go back"
|
||||
)
|
||||
|
||||
// To store names of views on top
|
||||
type viewsState struct {
|
||||
input string // Which input view is on top
|
||||
side string // Which side view is on top
|
||||
contacts []string // Contacts list
|
||||
currentContact string // Contact we are currently messaging
|
||||
}
|
||||
|
||||
var (
|
||||
// Which window is on top currently on top of the other.
|
||||
// This is the init setup
|
||||
viewState = viewsState{
|
||||
input: chatInputWindow,
|
||||
side: menuWindow,
|
||||
}
|
||||
menuOptions = []string{contactList, rawMode, askServerForRoster, disconnect}
|
||||
// Errors
|
||||
servConnFail = errors.New("failed to connect to server. Check your configuration ? Exiting")
|
||||
)
|
||||
|
||||
func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) {
|
||||
if _, err := g.SetCurrentView(name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return g.SetViewOnTop(name)
|
||||
}
|
||||
|
||||
func layout(g *gocui.Gui) error {
|
||||
maxX, maxY := g.Size()
|
||||
|
||||
if v, err := g.SetView(chatLogWindow, maxX/5, 0, maxX-1, 5*maxY/6-1, 0); err != nil {
|
||||
if !gocui.IsUnknownView(err) {
|
||||
return err
|
||||
}
|
||||
v.Title = chatLogWindowTitle
|
||||
v.Wrap = true
|
||||
v.Autoscroll = true
|
||||
}
|
||||
|
||||
if v, err := g.SetView(contactsListWindow, 0, 0, maxX/5-1, 5*maxY/6-2, 0); err != nil {
|
||||
if !gocui.IsUnknownView(err) {
|
||||
return err
|
||||
}
|
||||
v.Title = contactsListWindowTitle
|
||||
v.Wrap = true
|
||||
// If we set this to true, the contacts list will "fit" in the window but if the number
|
||||
// of contacts exceeds the maximum height, some contacts will be hidden...
|
||||
// If set to false, we can scroll up and down the contact list... infinitely. Meaning lower lines
|
||||
// will be unlimited and empty... Didn't find a way to quickfix yet.
|
||||
v.Autoscroll = false
|
||||
}
|
||||
|
||||
if v, err := g.SetView(menuWindow, 0, 0, maxX/5-1, 5*maxY/6-2, 0); err != nil {
|
||||
if !gocui.IsUnknownView(err) {
|
||||
return err
|
||||
}
|
||||
v.Title = menuWindowTitle
|
||||
v.Wrap = true
|
||||
v.Autoscroll = true
|
||||
fmt.Fprint(v, strings.Join(menuOptions, "\n"))
|
||||
if _, err = setCurrentViewOnTop(g, menuWindow); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if v, err := g.SetView(rawInputWindow, 0, 5*maxY/6-1, maxX/1-1, maxY-1, 0); err != nil {
|
||||
if !gocui.IsUnknownView(err) {
|
||||
return err
|
||||
}
|
||||
v.Title = rawInputWindowTitle
|
||||
v.Editable = true
|
||||
v.Wrap = true
|
||||
}
|
||||
|
||||
if v, err := g.SetView(chatInputWindow, 0, 5*maxY/6-1, maxX/1-1, maxY-1, 0); err != nil {
|
||||
if !gocui.IsUnknownView(err) {
|
||||
return err
|
||||
}
|
||||
v.Title = chatInputWindowTitle
|
||||
v.Editable = true
|
||||
v.Wrap = true
|
||||
|
||||
if _, err = setCurrentViewOnTop(g, chatInputWindow); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func quit(g *gocui.Gui, v *gocui.View) error {
|
||||
return gocui.ErrQuit
|
||||
}
|
||||
|
||||
// Sends an input text from the user to the backend while also printing it in the chatlog window.
|
||||
// KeyEnter is viewed as "\n" by gocui, so messages should only be one line, whereas raw sending has a different key
|
||||
// binding and therefor should work with this too (for multiple lines stanzas)
|
||||
func writeInput(g *gocui.Gui, v *gocui.View) error {
|
||||
chatLogWindow, _ := g.View(chatLogWindow)
|
||||
|
||||
input := strings.Join(v.ViewBufferLines(), "\n")
|
||||
|
||||
fmt.Fprintln(chatLogWindow, "Me : ", input)
|
||||
if viewState.input == rawInputWindow {
|
||||
rawTextChan <- input
|
||||
} else {
|
||||
textChan <- input
|
||||
}
|
||||
|
||||
v.Clear()
|
||||
v.EditDeleteToStartOfLine()
|
||||
return nil
|
||||
}
|
||||
|
||||
func setKeyBindings(g *gocui.Gui) {
|
||||
// ==========================
|
||||
// All views
|
||||
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
// ==========================
|
||||
// Chat input
|
||||
if err := g.SetKeybinding(chatInputWindow, gocui.KeyEnter, gocui.ModNone, writeInput); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding(chatInputWindow, gocui.KeyCtrlSpace, gocui.ModNone, nextView); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
// ==========================
|
||||
// Raw input
|
||||
if err := g.SetKeybinding(rawInputWindow, gocui.KeyCtrlE, gocui.ModNone, writeInput); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding(rawInputWindow, gocui.KeyCtrlSpace, gocui.ModNone, nextView); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
// ==========================
|
||||
// Menu
|
||||
if err := g.SetKeybinding(menuWindow, gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
if err := g.SetKeybinding(menuWindow, gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
if err := g.SetKeybinding(menuWindow, gocui.KeyCtrlSpace, gocui.ModNone, nextView); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
if err := g.SetKeybinding(menuWindow, gocui.KeyEnter, gocui.ModNone, getLine); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
// ==========================
|
||||
// Contacts list
|
||||
if err := g.SetKeybinding(contactsListWindow, gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
if err := g.SetKeybinding(contactsListWindow, gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
if err := g.SetKeybinding(contactsListWindow, gocui.KeyEnter, gocui.ModNone, getLine); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
if err := g.SetKeybinding(contactsListWindow, gocui.KeyCtrlSpace, gocui.ModNone, nextView); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
// ==========================
|
||||
// Disconnect message
|
||||
if err := g.SetKeybinding(disconnectMsg, gocui.KeyEnter, gocui.ModNone, delMsg); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
}
|
||||
|
||||
// General
|
||||
// Used to handle menu selections and navigations
|
||||
func getLine(g *gocui.Gui, v *gocui.View) error {
|
||||
var l string
|
||||
var err error
|
||||
|
||||
_, cy := v.Cursor()
|
||||
if l, err = v.Line(cy); err != nil {
|
||||
l = ""
|
||||
}
|
||||
if viewState.side == menuWindow {
|
||||
if l == contactList {
|
||||
cv, _ := g.View(contactsListWindow)
|
||||
viewState.side = contactsListWindow
|
||||
g.SetViewOnTop(contactsListWindow)
|
||||
g.SetCurrentView(contactsListWindow)
|
||||
if len(cv.ViewBufferLines()) == 0 {
|
||||
printContactsToWindow(g, viewState.contacts)
|
||||
}
|
||||
} else if l == disconnect {
|
||||
maxX, maxY := g.Size()
|
||||
msg := "You disconnected from the server. Press enter to quit."
|
||||
if v, err := g.SetView(disconnectMsg, maxX/2-30, maxY/2, maxX/2-29+len(msg), maxY/2+2, 0); err != nil {
|
||||
if !gocui.IsUnknownView(err) {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(v, msg)
|
||||
if _, err := g.SetCurrentView(disconnectMsg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
killChan <- disconnectErr
|
||||
} else if l == askServerForRoster {
|
||||
chlw, _ := g.View(chatLogWindow)
|
||||
fmt.Fprintln(chlw, infoFormat+"Asking server for contacts list...")
|
||||
rosterChan <- struct{}{}
|
||||
} else if l == rawMode {
|
||||
mw, _ := g.View(menuWindow)
|
||||
viewState.input = rawInputWindow
|
||||
g.SetViewOnTop(rawInputWindow)
|
||||
g.SetCurrentView(rawInputWindow)
|
||||
menuOptions[1] = messageMode
|
||||
v.Clear()
|
||||
v.EditDeleteToStartOfLine()
|
||||
fmt.Fprintln(mw, strings.Join(menuOptions, "\n"))
|
||||
message := "Now sending in raw stanza mode"
|
||||
clv, _ := g.View(chatLogWindow)
|
||||
fmt.Fprintln(clv, infoFormat+message)
|
||||
} else if l == messageMode {
|
||||
mw, _ := g.View(menuWindow)
|
||||
viewState.input = chatInputWindow
|
||||
g.SetViewOnTop(chatInputWindow)
|
||||
g.SetCurrentView(chatInputWindow)
|
||||
menuOptions[1] = rawMode
|
||||
v.Clear()
|
||||
v.EditDeleteToStartOfLine()
|
||||
fmt.Fprintln(mw, strings.Join(menuOptions, "\n"))
|
||||
message := "Now sending in messages mode"
|
||||
clv, _ := g.View(chatLogWindow)
|
||||
fmt.Fprintln(clv, infoFormat+message)
|
||||
}
|
||||
} else if viewState.side == contactsListWindow {
|
||||
if l == backFromContacts {
|
||||
viewState.side = menuWindow
|
||||
g.SetViewOnTop(menuWindow)
|
||||
g.SetCurrentView(menuWindow)
|
||||
} else if l == "" {
|
||||
return nil
|
||||
} else {
|
||||
// Updating the current correspondent, back-end side.
|
||||
CorrespChan <- l
|
||||
viewState.currentContact = l
|
||||
// Showing the selected contact in contacts list
|
||||
cl, _ := g.View(contactsListWindow)
|
||||
cts := cl.ViewBufferLines()
|
||||
cl.Clear()
|
||||
printContactsToWindow(g, cts)
|
||||
// Showing a message to the user, and switching back to input after the new contact is selected.
|
||||
message := "Now sending messages to : " + l + " in a private conversation"
|
||||
clv, _ := g.View(chatLogWindow)
|
||||
fmt.Fprintln(clv, infoFormat+message)
|
||||
g.SetCurrentView(chatInputWindow)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func printContactsToWindow(g *gocui.Gui, contactsList []string) {
|
||||
cl, _ := g.View(contactsListWindow)
|
||||
for _, c := range contactsList {
|
||||
c = strings.ReplaceAll(c, " *", "")
|
||||
if c == viewState.currentContact {
|
||||
fmt.Fprintf(cl, c+" *\n")
|
||||
} else {
|
||||
fmt.Fprintf(cl, c+"\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Changing view between input and "menu/contacts" when pressing the specific key.
|
||||
func nextView(g *gocui.Gui, v *gocui.View) error {
|
||||
if v == nil || v.Name() == chatInputWindow || v.Name() == rawInputWindow {
|
||||
_, err := g.SetCurrentView(viewState.side)
|
||||
return err
|
||||
} else if v.Name() == menuWindow || v.Name() == contactsListWindow {
|
||||
_, err := g.SetCurrentView(viewState.input)
|
||||
return err
|
||||
}
|
||||
|
||||
// Should not be reached right now
|
||||
_, err := g.SetCurrentView(chatInputWindow)
|
||||
return err
|
||||
}
|
||||
|
||||
func cursorDown(g *gocui.Gui, v *gocui.View) error {
|
||||
if v != nil {
|
||||
cx, cy := v.Cursor()
|
||||
// Avoid going below the list of contacts. Although lines are stored in the view as a slice
|
||||
// in the used lib. Therefor, if the number of lines is too big, the cursor will go past the last line since
|
||||
// increasing slice capacity is done by doubling it. Last lines will be "nil" and reachable by the cursor
|
||||
// in a dynamic context (such as contacts list)
|
||||
cv := g.CurrentView()
|
||||
h := cv.LinesHeight()
|
||||
if cy+1 >= h {
|
||||
return nil
|
||||
}
|
||||
// Lower cursor
|
||||
if err := v.SetCursor(cx, cy+1); err != nil {
|
||||
ox, oy := v.Origin()
|
||||
if err := v.SetOrigin(ox, oy+1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cursorUp(g *gocui.Gui, v *gocui.View) error {
|
||||
if v != nil {
|
||||
ox, oy := v.Origin()
|
||||
cx, cy := v.Cursor()
|
||||
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
|
||||
if err := v.SetOrigin(ox, oy-1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func delMsg(g *gocui.Gui, v *gocui.View) error {
|
||||
if err := g.DeleteView(disconnectMsg); err != nil {
|
||||
return err
|
||||
}
|
||||
errChan <- gocui.ErrQuit // Quit the program
|
||||
return nil
|
||||
}
|
@ -0,0 +1,339 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
xmpp_chat_client is a demo client that connect on an XMPP server to chat with other members
|
||||
*/
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/awesome-gocui/gocui"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
infoFormat = "====== "
|
||||
// Default configuration
|
||||
defaultConfigFilePath = "./"
|
||||
|
||||
configFileName = "config"
|
||||
configType = "yaml"
|
||||
logStanzasOn = "logger_on"
|
||||
logFilePath = "logfile_path"
|
||||
// Keys in config
|
||||
serverAddressKey = "full_address"
|
||||
clientJid = "jid"
|
||||
clientPass = "pass"
|
||||
configContactSep = ";"
|
||||
)
|
||||
|
||||
var (
|
||||
CorrespChan = make(chan string, 1)
|
||||
textChan = make(chan string, 5)
|
||||
rawTextChan = make(chan string, 5)
|
||||
killChan = make(chan error, 1)
|
||||
errChan = make(chan error)
|
||||
rosterChan = make(chan struct{})
|
||||
|
||||
logger *log.Logger
|
||||
disconnectErr = errors.New("disconnecting client")
|
||||
)
|
||||
|
||||
type config struct {
|
||||
Server map[string]string `mapstructure:"server"`
|
||||
Client map[string]string `mapstructure:"client"`
|
||||
Contacts string `string:"contact"`
|
||||
LogStanzas map[string]string `mapstructure:"logstanzas"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
// ============================================================
|
||||
// Parse the flag with the config directory path as argument
|
||||
flag.String("c", defaultConfigFilePath, "Provide a path to the directory that contains the configuration"+
|
||||
" file you want to use. Config file should be named \"config\" and be in YAML format..")
|
||||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
|
||||
pflag.Parse()
|
||||
|
||||
// ==========================
|
||||
// Read configuration
|
||||
c := readConfig()
|
||||
|
||||
//================================
|
||||
// Setup logger
|
||||
on, err := strconv.ParseBool(c.LogStanzas[logStanzasOn])
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
if on {
|
||||
f, err := os.OpenFile(path.Join(c.LogStanzas[logFilePath], "logs.txt"), os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
logger = log.New(f, "", log.Lshortfile|log.Ldate|log.Ltime)
|
||||
logger.SetOutput(f)
|
||||
defer f.Close()
|
||||
}
|
||||
|
||||
// ==========================
|
||||
// Create TUI
|
||||
g, err := gocui.NewGui(gocui.OutputNormal, true)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
defer g.Close()
|
||||
g.Highlight = true
|
||||
g.Cursor = true
|
||||
g.SelFgColor = gocui.ColorGreen
|
||||
g.SetManagerFunc(layout)
|
||||
setKeyBindings(g)
|
||||
|
||||
// ==========================
|
||||
// Run TUI
|
||||
go func() {
|
||||
errChan <- g.MainLoop()
|
||||
}()
|
||||
|
||||
// ==========================
|
||||
// Start XMPP client
|
||||
go startClient(g, c)
|
||||
|
||||
select {
|
||||
case err := <-errChan:
|
||||
if err == gocui.ErrQuit {
|
||||
log.Println("Closing client.")
|
||||
} else {
|
||||
log.Panicln(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func startClient(g *gocui.Gui, config *config) {
|
||||
|
||||
// ==========================
|
||||
// Client setup
|
||||
clientCfg := xmpp.Config{
|
||||
TransportConfiguration: xmpp.TransportConfiguration{
|
||||
Address: config.Server[serverAddressKey],
|
||||
},
|
||||
Jid: config.Client[clientJid],
|
||||
Credential: xmpp.Password(config.Client[clientPass]),
|
||||
Insecure: true}
|
||||
|
||||
var client *xmpp.Client
|
||||
var err error
|
||||
router := xmpp.NewRouter()
|
||||
|
||||
handlerWithGui := func(_ xmpp.Sender, p stanza.Packet) {
|
||||
msg, ok := p.(stanza.Message)
|
||||
if logger != nil {
|
||||
m, _ := xml.Marshal(msg)
|
||||
logger.Println(string(m))
|
||||
}
|
||||
|
||||
v, err := g.View(chatLogWindow)
|
||||
if !ok {
|
||||
fmt.Fprintf(v, "%sIgnoring packet: %T\n", infoFormat, p)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
g.Update(func(g *gocui.Gui) error {
|
||||
if msg.Error.Code != 0 {
|
||||
_, err := fmt.Fprintf(v, "Error from server : %s : %s \n", msg.Error.Reason, msg.XMLName.Space)
|
||||
return err
|
||||
}
|
||||
if len(strings.TrimSpace(msg.Body)) != 0 {
|
||||
_, err := fmt.Fprintf(v, "%s : %s \n", msg.From, msg.Body)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
router.HandleFunc("message", handlerWithGui)
|
||||
if client, err = xmpp.NewClient(clientCfg, router, errorHandler); err != nil {
|
||||
log.Panicln(fmt.Sprintf("Could not create a new client ! %s", err))
|
||||
|
||||
}
|
||||
|
||||
// ==========================
|
||||
// Client connection
|
||||
if err = client.Connect(); err != nil {
|
||||
msg := fmt.Sprintf("%sXMPP connection failed: %s", infoFormat, err)
|
||||
g.Update(func(g *gocui.Gui) error {
|
||||
v, err := g.View(chatLogWindow)
|
||||
fmt.Fprintf(v, msg)
|
||||
return err
|
||||
})
|
||||
fmt.Println("Failed to connect to server. Exiting...")
|
||||
errChan <- servConnFail
|
||||
return
|
||||
}
|
||||
|
||||
// ==========================
|
||||
// Start working
|
||||
updateRosterFromConfig(config)
|
||||
// Sending the default contact in a channel. Default value is the first contact in the list from the config.
|
||||
viewState.currentContact = strings.Split(config.Contacts, configContactSep)[0]
|
||||
// Informing user of the default contact
|
||||
clw, _ := g.View(chatLogWindow)
|
||||
fmt.Fprintf(clw, infoFormat+"Now sending messages to "+viewState.currentContact+" in a private conversation\n")
|
||||
CorrespChan <- viewState.currentContact
|
||||
startMessaging(client, config, g)
|
||||
}
|
||||
|
||||
func startMessaging(client xmpp.Sender, config *config, g *gocui.Gui) {
|
||||
var text string
|
||||
var correspondent string
|
||||
for {
|
||||
select {
|
||||
case err := <-killChan:
|
||||
if err == disconnectErr {
|
||||
sc := client.(xmpp.StreamClient)
|
||||
sc.Disconnect()
|
||||
} else {
|
||||
logger.Println(err)
|
||||
}
|
||||
return
|
||||
case text = <-textChan:
|
||||
reply := stanza.Message{Attrs: stanza.Attrs{To: correspondent, Type: stanza.MessageTypeChat}, Body: text}
|
||||
if logger != nil {
|
||||
raw, _ := xml.Marshal(reply)
|
||||
logger.Println(string(raw))
|
||||
}
|
||||
err := client.Send(reply)
|
||||
if err != nil {
|
||||
fmt.Printf("There was a problem sending the message : %v", reply)
|
||||
return
|
||||
}
|
||||
case text = <-rawTextChan:
|
||||
if logger != nil {
|
||||
logger.Println(text)
|
||||
}
|
||||
err := client.SendRaw(text)
|
||||
if err != nil {
|
||||
fmt.Printf("There was a problem sending the message : %v", text)
|
||||
return
|
||||
}
|
||||
case crrsp := <-CorrespChan:
|
||||
correspondent = crrsp
|
||||
case <-rosterChan:
|
||||
askForRoster(client, g, config)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Only reads and parses the configuration
|
||||
func readConfig() *config {
|
||||
viper.SetConfigName(configFileName) // name of config file (without extension)
|
||||
viper.BindPFlags(pflag.CommandLine)
|
||||
viper.AddConfigPath(viper.GetString("c")) // path to look for the config file in
|
||||
err := viper.ReadInConfig() // Find and read the config file
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
log.Fatalf("%s %s", err, "Please make sure you give a path to the directory of the config and not to the config itself.")
|
||||
} else {
|
||||
log.Panicln(err)
|
||||
}
|
||||
}
|
||||
|
||||
viper.SetConfigType(configType)
|
||||
var config config
|
||||
err = viper.Unmarshal(&config)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Unable to decode Config: %s \n", err))
|
||||
}
|
||||
|
||||
// Check if we have contacts to message
|
||||
if len(strings.TrimSpace(config.Contacts)) == 0 {
|
||||
log.Panicln("You appear to have no contacts to message !")
|
||||
}
|
||||
// Check logging
|
||||
config.LogStanzas[logFilePath] = path.Clean(config.LogStanzas[logFilePath])
|
||||
on, err := strconv.ParseBool(config.LogStanzas[logStanzasOn])
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
if d, e := isDirectory(config.LogStanzas[logFilePath]); (e != nil || !d) && on {
|
||||
log.Panicln("The log file path could not be found or is not a directory.")
|
||||
}
|
||||
|
||||
return &config
|
||||
}
|
||||
|
||||
// If an error occurs, this is used to kill the client
|
||||
func errorHandler(err error) {
|
||||
killChan <- err
|
||||
}
|
||||
|
||||
// Read the client roster from the config. This does not check with the server that the roster is correct.
|
||||
// If user tries to send a message to someone not registered with the server, the server will return an error.
|
||||
func updateRosterFromConfig(config *config) {
|
||||
viewState.contacts = append(strings.Split(config.Contacts, configContactSep), backFromContacts)
|
||||
// Put a "go back" button at the end of the list
|
||||
viewState.contacts = append(viewState.contacts, backFromContacts)
|
||||
}
|
||||
|
||||
// Updates the menu panel of the view with the current user's roster, by asking the server.
|
||||
func askForRoster(client xmpp.Sender, g *gocui.Gui, config *config) {
|
||||
// Craft a roster request
|
||||
req := stanza.NewIQ(stanza.Attrs{From: config.Client[clientJid], Type: stanza.IQTypeGet})
|
||||
req.RosterItems()
|
||||
if logger != nil {
|
||||
m, _ := xml.Marshal(req)
|
||||
logger.Println(string(m))
|
||||
}
|
||||
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
|
||||
// Send the roster request to the server
|
||||
c, err := client.SendIQ(ctx, req)
|
||||
if err != nil {
|
||||
logger.Panicln(err)
|
||||
}
|
||||
|
||||
// Sending a IQ has a channel spawned to process the response once we receive it.
|
||||
// In order not to block the client, we spawn a goroutine to update the TUI once the server has responded.
|
||||
go func() {
|
||||
serverResp := <-c
|
||||
if logger != nil {
|
||||
m, _ := xml.Marshal(serverResp)
|
||||
logger.Println(string(m))
|
||||
}
|
||||
// Update contacts with the response from the server
|
||||
chlw, _ := g.View(chatLogWindow)
|
||||
if rosterItems, ok := serverResp.Payload.(*stanza.RosterItems); ok {
|
||||
viewState.contacts = []string{}
|
||||
for _, item := range rosterItems.Items {
|
||||
viewState.contacts = append(viewState.contacts, item.Jid)
|
||||
}
|
||||
// Put a "go back" button at the end of the list
|
||||
viewState.contacts = append(viewState.contacts, backFromContacts)
|
||||
fmt.Fprintln(chlw, infoFormat+"Contacts list updated !")
|
||||
return
|
||||
}
|
||||
fmt.Fprintln(chlw, infoFormat+"Failed to update contact list !")
|
||||
}()
|
||||
}
|
||||
|
||||
func isDirectory(path string) (bool, error) {
|
||||
fileInfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return fileInfo.IsDir(), err
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
# xmpp_component2
|
||||
|
||||
|
||||
This program is an example of the simplest XMPP component: it connects to an XMPP server using XEP 114 protocol, perform a discovery query on the server and print the response.
|
@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
|
||||
Connect to an XMPP server using XEP 114 protocol, perform a discovery query on the server and print the response
|
||||
|
||||
*/
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
const (
|
||||
domain = "mycomponent.localhost"
|
||||
address = "build.vpn.p1:8888"
|
||||
)
|
||||
|
||||
// Init and return a component
|
||||
func makeComponent() *xmpp.Component {
|
||||
opts := xmpp.ComponentOptions{
|
||||
TransportConfiguration: xmpp.TransportConfiguration{
|
||||
Address: address,
|
||||
Domain: domain,
|
||||
},
|
||||
Domain: domain,
|
||||
Secret: "secret",
|
||||
}
|
||||
router := xmpp.NewRouter()
|
||||
c, err := xmpp.NewComponent(opts, router, handleError)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func handleError(err error) {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
func main() {
|
||||
c := makeComponent()
|
||||
|
||||
// Connect Component to the server
|
||||
fmt.Printf("Connecting to %v\n", address)
|
||||
err := c.Connect()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// make a disco iq
|
||||
iqReq, err := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeGet,
|
||||
From: domain,
|
||||
To: "localhost",
|
||||
Id: "my-iq1"})
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create IQ: %v", err)
|
||||
}
|
||||
disco := iqReq.DiscoInfo()
|
||||
iqReq.Payload = disco
|
||||
|
||||
// res is the channel used to receive the result iq
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
res, _ := c.SendIQ(ctx, iqReq)
|
||||
|
||||
select {
|
||||
case iqResponse := <-res:
|
||||
// Got response from server
|
||||
fmt.Print(iqResponse.Payload)
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
cancel()
|
||||
panic("No iq response was received in time")
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
# Jukebox example
|
||||
|
||||
## Requirements
|
||||
- You need mpg123 installed on your computer because the example runs it as a command :
|
||||
[Official MPG123 website](https://mpg123.de/)
|
||||
Most linux distributions have a package for it.
|
||||
- You need a soundcloud ID to play a music from the website through mpg123. You currently cannot play music files with this example.
|
||||
Your user ID is available in your account settings on the [soundcloud website](https://soundcloud.com/)
|
||||
**One is provided for convenience.**
|
||||
- You need a running jabber server. You can run your local instance of [ejabberd](https://www.ejabberd.im/) for example.
|
||||
- You need a registered user on the running jabber server.
|
||||
|
||||
## Run
|
||||
You can edit the soundcloud ID in the example file with your own, or use the provided one :
|
||||
```go
|
||||
const scClientID = "dde6a0075614ac4f3bea423863076b22"
|
||||
```
|
||||
|
||||
To run the example, build it with (while in the example directory) :
|
||||
```
|
||||
go build xmpp_jukebox.go
|
||||
```
|
||||
|
||||
then run it with (update the command arguments accordingly):
|
||||
```
|
||||
./xmpp_jukebox -jid=MY_USERE@MY_DOMAIN/jukebox -password=MY_PASSWORD -address=MY_SERVER:MY_SERVER_PORT
|
||||
```
|
||||
Make sure to have a resource, for instance "/jukebox", on your jid.
|
||||
|
||||
Then you can send the following stanza to "MY_USERE@MY_DOMAIN/jukebox" (with the resource) to play a song (update the soundcloud URL accordingly) :
|
||||
```xml
|
||||
<iq id="1" to="MY_USERE@MY_DOMAIN/jukebox" type="set">
|
||||
<set xml:lang="en" xmlns="urn:xmpp:iot:control">
|
||||
<string name="url" value="https://soundcloud.com/UPDATE/ME"/>
|
||||
</set>
|
||||
</iq>
|
||||
```
|
@ -0,0 +1,17 @@
|
||||
# PubSub client example
|
||||
|
||||
## Description
|
||||
This is a simple example of a client that :
|
||||
* Creates a node on a service
|
||||
* Subscribes to that node
|
||||
* Publishes to that node
|
||||
* Gets the notification from the publication and prints it on screen
|
||||
|
||||
## Requirements
|
||||
You need to have a running jabber server, like [ejabberd](https://www.ejabberd.im/) that supports [XEP-0060](https://xmpp.org/extensions/xep-0060.html).
|
||||
|
||||
## How to use
|
||||
Just run :
|
||||
```
|
||||
go run xmpp_ps_client.go
|
||||
```
|
@ -0,0 +1,278 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
userJID = "testuser2@localhost"
|
||||
serverAddress = "localhost:5222"
|
||||
nodeName = "lel_node"
|
||||
serviceName = "pubsub.localhost"
|
||||
)
|
||||
|
||||
var invalidResp = errors.New("invalid response")
|
||||
|
||||
func main() {
|
||||
|
||||
config := xmpp.Config{
|
||||
TransportConfiguration: xmpp.TransportConfiguration{
|
||||
Address: serverAddress,
|
||||
},
|
||||
Jid: userJID,
|
||||
Credential: xmpp.Password("pass123"),
|
||||
// StreamLogger: os.Stdout,
|
||||
Insecure: true,
|
||||
}
|
||||
router := xmpp.NewRouter()
|
||||
router.NewRoute().Packet("message").
|
||||
HandlerFunc(func(s xmpp.Sender, p stanza.Packet) {
|
||||
data, _ := xml.Marshal(p)
|
||||
log.Println("Received a message ! => \n" + string(data))
|
||||
})
|
||||
|
||||
client, err := xmpp.NewClient(&config, router, func(err error) { log.Println(err) })
|
||||
if err != nil {
|
||||
log.Fatalf("%+v", err)
|
||||
}
|
||||
|
||||
// ==========================
|
||||
// Client connection
|
||||
err = client.Connect()
|
||||
if err != nil {
|
||||
log.Fatalf("%+v", err)
|
||||
}
|
||||
|
||||
// ==========================
|
||||
// Create a node
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
createNode(ctx, cancel, client)
|
||||
|
||||
// ================================================================================
|
||||
// Configure the node. This can also be done in a single message with the creation
|
||||
configureNode(ctx, cancel, client)
|
||||
|
||||
// ====================================
|
||||
// Subscribe to this node :
|
||||
subToNode(ctx, cancel, client)
|
||||
|
||||
// ==========================
|
||||
// Publish to that node
|
||||
pubToNode(ctx, cancel, client)
|
||||
|
||||
// =============================
|
||||
// Let's purge the node :
|
||||
purgeRq, _ := stanza.NewPurgeAllItems(serviceName, nodeName)
|
||||
purgeCh, err := client.SendIQ(ctx, purgeRq)
|
||||
if err != nil {
|
||||
log.Fatalf("could not send purge request: %v", err)
|
||||
}
|
||||
select {
|
||||
case purgeResp := <-purgeCh:
|
||||
|
||||
if purgeResp.Type == stanza.IQTypeError {
|
||||
cancel()
|
||||
if vld, err := purgeResp.IsValid(); !vld {
|
||||
log.Fatalf(invalidResp.Error()+" %v"+" reason: %v", purgeResp, err)
|
||||
}
|
||||
log.Fatalf("error while purging node : %s", purgeResp.Error.Text)
|
||||
}
|
||||
log.Println("node successfully purged")
|
||||
case <-time.After(1000 * time.Millisecond):
|
||||
cancel()
|
||||
log.Fatal("No iq response was received in time while purging node")
|
||||
}
|
||||
|
||||
cancel()
|
||||
}
|
||||
|
||||
func createNode(ctx context.Context, cancel context.CancelFunc, client *xmpp.Client) {
|
||||
rqCreate, err := stanza.NewCreateNode(serviceName, nodeName)
|
||||
if err != nil {
|
||||
log.Fatalf("%+v", err)
|
||||
}
|
||||
createCh, err := client.SendIQ(ctx, rqCreate)
|
||||
if err != nil {
|
||||
log.Fatalf("%+v", err)
|
||||
} else {
|
||||
|
||||
if createCh != nil {
|
||||
select {
|
||||
case respCr := <-createCh:
|
||||
// Got response from server
|
||||
if respCr.Type == stanza.IQTypeError {
|
||||
if vld, err := respCr.IsValid(); !vld {
|
||||
log.Fatalf(invalidResp.Error()+" %+v"+" reason: %s", respCr, err)
|
||||
}
|
||||
if respCr.Error.Reason != "conflict" {
|
||||
log.Fatalf("%+v", respCr.Error.Text)
|
||||
}
|
||||
log.Println(respCr.Error.Text)
|
||||
} else {
|
||||
fmt.Print("successfully created channel")
|
||||
}
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
cancel()
|
||||
log.Fatal("No iq response was received in time while creating node")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func configureNode(ctx context.Context, cancel context.CancelFunc, client *xmpp.Client) {
|
||||
// First, ask for a form with the config options
|
||||
confRq, _ := stanza.NewConfigureNode(serviceName, nodeName)
|
||||
confReqCh, err := client.SendIQ(ctx, confRq)
|
||||
if err != nil {
|
||||
log.Fatalf("could not send iq : %v", err)
|
||||
}
|
||||
select {
|
||||
case confForm := <-confReqCh:
|
||||
// If the request was successful, we now have a form with configuration options to update
|
||||
fields, err := confForm.GetFormFields()
|
||||
if err != nil {
|
||||
log.Fatal("No config fields found !")
|
||||
}
|
||||
|
||||
// These are some common fields expected to be present. Change processing to your liking
|
||||
if fields["pubsub#max_payload_size"] != nil {
|
||||
fields["pubsub#max_payload_size"].ValuesList[0] = "100000"
|
||||
}
|
||||
|
||||
if fields["pubsub#notification_type"] != nil {
|
||||
fields["pubsub#notification_type"].ValuesList[0] = "headline"
|
||||
}
|
||||
|
||||
// Send the modified fields as a form
|
||||
submitConf, err := stanza.NewFormSubmissionOwner(serviceName,
|
||||
nodeName,
|
||||
[]*stanza.Field{
|
||||
fields["pubsub#max_payload_size"],
|
||||
fields["pubsub#notification_type"],
|
||||
})
|
||||
|
||||
c, _ := client.SendIQ(ctx, submitConf)
|
||||
select {
|
||||
case confResp := <-c:
|
||||
if confResp.Type == stanza.IQTypeError {
|
||||
cancel()
|
||||
if vld, err := confResp.IsValid(); !vld {
|
||||
log.Fatalf(invalidResp.Error()+" %v"+" reason: %v", confResp, err)
|
||||
}
|
||||
log.Fatalf("node configuration failed : %s", confResp.Error.Text)
|
||||
}
|
||||
log.Println("node configuration was successful")
|
||||
return
|
||||
|
||||
case <-time.After(300 * time.Millisecond):
|
||||
cancel()
|
||||
log.Fatal("No iq response was received in time while configuring the node")
|
||||
}
|
||||
|
||||
case <-time.After(300 * time.Millisecond):
|
||||
cancel()
|
||||
log.Fatal("No iq response was received in time while asking for the config form")
|
||||
}
|
||||
}
|
||||
|
||||
func subToNode(ctx context.Context, cancel context.CancelFunc, client *xmpp.Client) {
|
||||
rqSubscribe, err := stanza.NewSubRq(serviceName, stanza.SubInfo{
|
||||
Node: nodeName,
|
||||
Jid: userJID,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("%+v", err)
|
||||
}
|
||||
subRespCh, _ := client.SendIQ(ctx, rqSubscribe)
|
||||
if subRespCh != nil {
|
||||
select {
|
||||
case <-subRespCh:
|
||||
log.Println("Subscribed to the service")
|
||||
case <-time.After(300 * time.Millisecond):
|
||||
cancel()
|
||||
log.Fatal("No iq response was received in time while subscribing")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func pubToNode(ctx context.Context, cancel context.CancelFunc, client *xmpp.Client) {
|
||||
pub, err := stanza.NewPublishItemRq(serviceName, nodeName, "", stanza.Item{
|
||||
Publisher: "testuser2",
|
||||
Any: &stanza.Node{
|
||||
XMLName: xml.Name{
|
||||
Space: "http://www.w3.org/2005/Atom",
|
||||
Local: "entry",
|
||||
},
|
||||
Nodes: []stanza.Node{
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "title"},
|
||||
Attrs: nil,
|
||||
Content: "My pub item title",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "summary"},
|
||||
Attrs: nil,
|
||||
Content: "My pub item content summary",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "link"},
|
||||
Attrs: []xml.Attr{
|
||||
{
|
||||
Name: xml.Name{Space: "", Local: "rel"},
|
||||
Value: "alternate",
|
||||
},
|
||||
{
|
||||
Name: xml.Name{Space: "", Local: "type"},
|
||||
Value: "text/html",
|
||||
},
|
||||
{
|
||||
Name: xml.Name{Space: "", Local: "href"},
|
||||
Value: "http://denmark.lit/2003/12/13/atom03",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "id"},
|
||||
Attrs: nil,
|
||||
Content: "My pub item content ID",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "published"},
|
||||
Attrs: nil,
|
||||
Content: "2003-12-13T18:30:02Z",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "updated"},
|
||||
Attrs: nil,
|
||||
Content: "2003-12-13T18:30:02Z",
|
||||
Nodes: nil,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("%+v", err)
|
||||
}
|
||||
pubRespCh, _ := client.SendIQ(ctx, pub)
|
||||
if pubRespCh != nil {
|
||||
select {
|
||||
case <-pubRespCh:
|
||||
log.Println("Published item to the service")
|
||||
case <-time.After(300 * time.Millisecond):
|
||||
cancel()
|
||||
log.Fatal("No iq response was received in time while publishing")
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package xmpp
|
||||
|
||||
type BiDirIterator interface {
|
||||
// Next returns the next element of this iterator, if a response is available within t milliseconds
|
||||
Next(t int) (BiDirIteratorElt, error)
|
||||
// Previous returns the previous element of this iterator, if a response is available within t milliseconds
|
||||
Previous(t int) (BiDirIteratorElt, error)
|
||||
}
|
||||
|
||||
type BiDirIteratorElt interface {
|
||||
NoOp()
|
||||
}
|
@ -1 +0,0 @@
|
||||
comment: off
|
@ -1,5 +0,0 @@
|
||||
build:
|
||||
build:
|
||||
image: fluux/build
|
||||
dockerfile: Dockerfile
|
||||
encrypted_env_file: codeship.env.encrypted
|
@ -1,5 +0,0 @@
|
||||
- type: serial
|
||||
steps:
|
||||
- name: test
|
||||
service: build
|
||||
command: ./test.sh
|
@ -1 +0,0 @@
|
||||
yVKgVFeKW6SSnC/KgLYpfYtTcqqTke1gOIW5GUiVvRijnhweOJiYKFPmwPjpt1FVrg4WVELQUNbxn3lmfyHVVF7r
|
@ -0,0 +1,153 @@
|
||||
package stanza
|
||||
|
||||
import "encoding/xml"
|
||||
|
||||
// Implements the XEP-0050 extension
|
||||
|
||||
const (
|
||||
CommandActionCancel = "cancel"
|
||||
CommandActionComplete = "complete"
|
||||
CommandActionExecute = "execute"
|
||||
CommandActionNext = "next"
|
||||
CommandActionPrevious = "prev"
|
||||
|
||||
CommandStatusCancelled = "canceled"
|
||||
CommandStatusCompleted = "completed"
|
||||
CommandStatusExecuting = "executing"
|
||||
|
||||
CommandNoteTypeErr = "error"
|
||||
CommandNoteTypeInfo = "info"
|
||||
CommandNoteTypeWarn = "warn"
|
||||
)
|
||||
|
||||
type Command struct {
|
||||
XMLName xml.Name `xml:"http://jabber.org/protocol/commands command"`
|
||||
|
||||
CommandElement CommandElement
|
||||
|
||||
BadAction *struct{} `xml:"bad-action,omitempty"`
|
||||
BadLocale *struct{} `xml:"bad-locale,omitempty"`
|
||||
BadPayload *struct{} `xml:"bad-payload,omitempty"`
|
||||
BadSessionId *struct{} `xml:"bad-sessionid,omitempty"`
|
||||
MalformedAction *struct{} `xml:"malformed-action,omitempty"`
|
||||
SessionExpired *struct{} `xml:"session-expired,omitempty"`
|
||||
|
||||
// Attributes
|
||||
Action string `xml:"action,attr,omitempty"`
|
||||
Node string `xml:"node,attr"`
|
||||
SessionId string `xml:"sessionid,attr,omitempty"`
|
||||
Status string `xml:"status,attr,omitempty"`
|
||||
Lang string `xml:"lang,attr,omitempty"`
|
||||
|
||||
// Result sets
|
||||
ResultSet *ResultSet `xml:"set,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Command) Namespace() string {
|
||||
return c.XMLName.Space
|
||||
}
|
||||
|
||||
func (c *Command) GetSet() *ResultSet {
|
||||
return c.ResultSet
|
||||
}
|
||||
|
||||
type CommandElement interface {
|
||||
Ref() string
|
||||
}
|
||||
|
||||
type Actions struct {
|
||||
Prev *struct{} `xml:"prev,omitempty"`
|
||||
Next *struct{} `xml:"next,omitempty"`
|
||||
Complete *struct{} `xml:"complete,omitempty"`
|
||||
|
||||
Execute string `xml:"execute,attr,omitempty"`
|
||||
}
|
||||
|
||||
func (a *Actions) Ref() string {
|
||||
return "actions"
|
||||
}
|
||||
|
||||
type Note struct {
|
||||
Text string `xml:",cdata"`
|
||||
Type string `xml:"type,attr,omitempty"`
|
||||
}
|
||||
|
||||
func (n *Note) Ref() string {
|
||||
return "note"
|
||||
}
|
||||
func (f *Form) Ref() string { return "form" }
|
||||
|
||||
func (n *Node) Ref() string {
|
||||
return "node"
|
||||
}
|
||||
|
||||
func (c *Command) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
c.XMLName = start.Name
|
||||
|
||||
// Extract packet attributes
|
||||
for _, attr := range start.Attr {
|
||||
if attr.Name.Local == "action" {
|
||||
c.Action = attr.Value
|
||||
}
|
||||
if attr.Name.Local == "node" {
|
||||
c.Node = attr.Value
|
||||
}
|
||||
if attr.Name.Local == "sessionid" {
|
||||
c.SessionId = attr.Value
|
||||
}
|
||||
if attr.Name.Local == "status" {
|
||||
c.Status = attr.Value
|
||||
}
|
||||
if attr.Name.Local == "lang" {
|
||||
c.Lang = attr.Value
|
||||
}
|
||||
}
|
||||
|
||||
// decode inner elements
|
||||
for {
|
||||
t, err := d.Token()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch tt := t.(type) {
|
||||
|
||||
case xml.StartElement:
|
||||
// Decode sub-elements
|
||||
var err error
|
||||
switch tt.Name.Local {
|
||||
|
||||
case "affiliations":
|
||||
a := Actions{}
|
||||
err = d.DecodeElement(&a, &tt)
|
||||
c.CommandElement = &a
|
||||
case "configure":
|
||||
nt := Note{}
|
||||
err = d.DecodeElement(&nt, &tt)
|
||||
c.CommandElement = &nt
|
||||
case "x":
|
||||
f := Form{}
|
||||
err = d.DecodeElement(&f, &tt)
|
||||
c.CommandElement = &f
|
||||
default:
|
||||
n := Node{}
|
||||
err = d.DecodeElement(&n, &tt)
|
||||
c.CommandElement = &n
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case xml.EndElement:
|
||||
if tt == start.End() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
TypeRegistry.MapExtension(PKTIQ, xml.Name{Space: "http://jabber.org/protocol/commands", Local: "command"}, Command{})
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package stanza_test
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMarshalCommands(t *testing.T) {
|
||||
input := "<command xmlns=\"http://jabber.org/protocol/commands\" node=\"list\" " +
|
||||
"sessionid=\"list:20020923T213616Z-700\" status=\"completed\"><x xmlns=\"jabber:x:data\" " +
|
||||
"type=\"result\"><title>Available Services</title><reported xmlns=\"jabber:x:data\"><field var=\"service\" " +
|
||||
"label=\"Service\"></field><field var=\"runlevel-1\" label=\"Single-User mode\">" +
|
||||
"</field><field var=\"runlevel-2\" label=\"Non-Networked Multi-User mode\"></field><field var=\"runlevel-3\" " +
|
||||
"label=\"Full Multi-User mode\"></field><field var=\"runlevel-5\" label=\"X-Window mode\"></field></reported>" +
|
||||
"<item xmlns=\"jabber:x:data\"><field var=\"service\"><value>httpd</value></field><field var=\"runlevel-1\">" +
|
||||
"<value>off</value></field><field var=\"runlevel-2\"><value>off</value></field><field var=\"runlevel-3\">" +
|
||||
"<value>on</value></field><field var=\"runlevel-5\"><value>on</value></field></item>" +
|
||||
"<item xmlns=\"jabber:x:data\"><field var=\"service\"><value>postgresql</value></field>" +
|
||||
"<field var=\"runlevel-1\"><value>off</value></field><field var=\"runlevel-2\"><value>off</value></field>" +
|
||||
"<field var=\"runlevel-3\"><value>on</value></field><field var=\"runlevel-5\"><value>on</value></field></item>" +
|
||||
"<item xmlns=\"jabber:x:data\"><field var=\"service\"><value>jabberd</value></field><field var=\"runlevel-1\">" +
|
||||
"<value>off</value></field><field var=\"runlevel-2\"><value>off</value></field><field var=\"runlevel-3\">" +
|
||||
"<value>on</value></field><field var=\"runlevel-5\"><value>on</value></field></item></x></command>"
|
||||
var c stanza.Command
|
||||
err := xml.Unmarshal([]byte(input), &c)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal initial input")
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(c)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal unmarshalled input")
|
||||
}
|
||||
|
||||
if err := compareMarshal(input, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package stanza
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Helper structures and functions to manage dates and timestamps as defined in
|
||||
// XEP-0082: XMPP Date and Time Profiles (https://xmpp.org/extensions/xep-0082.html)
|
||||
|
||||
const dateLayoutXEP0082 = "2006-01-02"
|
||||
const timeLayoutXEP0082 = "15:04:05+00:00"
|
||||
|
||||
var InvalidDateInput = errors.New("could not parse date. Input might not be in a supported format")
|
||||
var InvalidDateOutput = errors.New("could not format date as desired")
|
||||
|
||||
type JabberDate struct {
|
||||
value time.Time
|
||||
}
|
||||
|
||||
func (d JabberDate) DateToString() string {
|
||||
return d.value.Format(dateLayoutXEP0082)
|
||||
}
|
||||
|
||||
func (d JabberDate) DateTimeToString(nanos bool) string {
|
||||
if nanos {
|
||||
return d.value.Format(time.RFC3339Nano)
|
||||
}
|
||||
return d.value.Format(time.RFC3339)
|
||||
}
|
||||
|
||||
func (d JabberDate) TimeToString(nanos bool) (string, error) {
|
||||
if nanos {
|
||||
spl := strings.Split(d.value.Format(time.RFC3339Nano), "T")
|
||||
if len(spl) != 2 {
|
||||
return "", InvalidDateOutput
|
||||
}
|
||||
return spl[1], nil
|
||||
}
|
||||
spl := strings.Split(d.value.Format(time.RFC3339), "T")
|
||||
if len(spl) != 2 {
|
||||
return "", InvalidDateOutput
|
||||
}
|
||||
return spl[1], nil
|
||||
}
|
||||
|
||||
func NewJabberDateFromString(strDate string) (JabberDate, error) {
|
||||
t, err := time.Parse(time.RFC3339, strDate)
|
||||
if err == nil {
|
||||
return JabberDate{value: t}, nil
|
||||
}
|
||||
|
||||
t, err = time.Parse(time.RFC3339Nano, strDate)
|
||||
if err == nil {
|
||||
return JabberDate{value: t}, nil
|
||||
}
|
||||
|
||||
t, err = time.Parse(dateLayoutXEP0082, strDate)
|
||||
if err == nil {
|
||||
return JabberDate{value: t}, nil
|
||||
}
|
||||
|
||||
t, err = time.Parse(timeLayoutXEP0082, strDate)
|
||||
if err == nil {
|
||||
return JabberDate{value: t}, nil
|
||||
}
|
||||
|
||||
return JabberDate{}, InvalidDateInput
|
||||
}
|
@ -0,0 +1,191 @@
|
||||
package stanza
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDateToString(t *testing.T) {
|
||||
t1 := JabberDate{value: time.Now()}
|
||||
t2 := JabberDate{value: time.Now().Add(24 * time.Hour)}
|
||||
|
||||
t1Str := t1.DateToString()
|
||||
t2Str := t2.DateToString()
|
||||
|
||||
if t1Str == t2Str {
|
||||
t.Fatalf("time representations should not be identical")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDateToStringOracle(t *testing.T) {
|
||||
expected := "2009-11-10"
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
t1 := JabberDate{value: time.Date(2009, time.November, 10, 23, 3, 22, 89, loc)}
|
||||
|
||||
t1Str := t1.DateToString()
|
||||
if t1Str != expected {
|
||||
t.Fatalf("time is different than expected. Expected: %s, Actual: %s", expected, t1Str)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDateTimeToString(t *testing.T) {
|
||||
t1 := JabberDate{value: time.Now()}
|
||||
t2 := JabberDate{value: time.Now().Add(10 * time.Second)}
|
||||
|
||||
t1Str := t1.DateTimeToString(false)
|
||||
t2Str := t2.DateTimeToString(false)
|
||||
|
||||
if t1Str == t2Str {
|
||||
t.Fatalf("time representations should not be identical")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDateTimeToStringOracle(t *testing.T) {
|
||||
expected := "2009-11-10T23:03:22+08:00"
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
t1 := JabberDate{value: time.Date(2009, time.November, 10, 23, 3, 22, 89, loc)}
|
||||
|
||||
t1Str := t1.DateTimeToString(false)
|
||||
if t1Str != expected {
|
||||
t.Fatalf("time is different than expected. Expected: %s, Actual: %s", expected, t1Str)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDateTimeToStringNanos(t *testing.T) {
|
||||
t1 := JabberDate{value: time.Now()}
|
||||
time.After(10 * time.Millisecond)
|
||||
t2 := JabberDate{value: time.Now()}
|
||||
|
||||
t1Str := t1.DateTimeToString(true)
|
||||
t2Str := t2.DateTimeToString(true)
|
||||
|
||||
if t1Str == t2Str {
|
||||
t.Fatalf("time representations should not be identical")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDateTimeToStringNanosOracle(t *testing.T) {
|
||||
expected := "2009-11-10T23:03:22.000000089+08:00"
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
t1 := JabberDate{value: time.Date(2009, time.November, 10, 23, 3, 22, 89, loc)}
|
||||
|
||||
t1Str := t1.DateTimeToString(true)
|
||||
if t1Str != expected {
|
||||
t.Fatalf("time is different than expected. Expected: %s, Actual: %s", expected, t1Str)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeToString(t *testing.T) {
|
||||
t1 := JabberDate{value: time.Now()}
|
||||
t2 := JabberDate{value: time.Now().Add(10 * time.Second)}
|
||||
|
||||
t1Str, err := t1.TimeToString(false)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
t2Str, err := t2.TimeToString(false)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if t1Str == t2Str {
|
||||
t.Fatalf("time representations should not be identical")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeToStringOracle(t *testing.T) {
|
||||
expected := "23:03:22+08:00"
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
t1 := JabberDate{value: time.Date(2009, time.November, 10, 23, 3, 22, 89, loc)}
|
||||
|
||||
t1Str, err := t1.TimeToString(false)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if t1Str != expected {
|
||||
t.Fatalf("time is different than expected. Expected: %s, Actual: %s", expected, t1Str)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeToStringNanos(t *testing.T) {
|
||||
t1 := JabberDate{value: time.Now()}
|
||||
time.After(10 * time.Millisecond)
|
||||
t2 := JabberDate{value: time.Now()}
|
||||
|
||||
t1Str, err := t1.TimeToString(true)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
t2Str, err := t2.TimeToString(true)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if t1Str == t2Str {
|
||||
t.Fatalf("time representations should not be identical")
|
||||
}
|
||||
}
|
||||
func TestTimeToStringNanosOracle(t *testing.T) {
|
||||
expected := "23:03:22.000000089+08:00"
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
t1 := JabberDate{value: time.Date(2009, time.November, 10, 23, 3, 22, 89, loc)}
|
||||
|
||||
t1Str, err := t1.TimeToString(true)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if t1Str != expected {
|
||||
t.Fatalf("time is different than expected. Expected: %s, Actual: %s", expected, t1Str)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJabberDateParsing(t *testing.T) {
|
||||
date := "2009-11-10"
|
||||
_, err := NewJabberDateFromString(date)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
dateTime := "2009-11-10T23:03:22+08:00"
|
||||
_, err = NewJabberDateFromString(dateTime)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
dateTimeNanos := "2009-11-10T23:03:22.000000089+08:00"
|
||||
_, err = NewJabberDateFromString(dateTimeNanos)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
// TODO : fix these. Parsing a time with an offset doesn't work
|
||||
//time := "23:03:22+08:00"
|
||||
//_, err = NewJabberDateFromString(time)
|
||||
//if err != nil {
|
||||
// t.Fatalf(err.Error())
|
||||
//}
|
||||
|
||||
//timeNanos := "23:03:22.000000089+08:00"
|
||||
//_, err = NewJabberDateFromString(timeNanos)
|
||||
//if err != nil {
|
||||
// t.Fatalf(err.Error())
|
||||
//}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package stanza
|
||||
|
||||
// FIFO queue for string contents
|
||||
// Implementations have no guarantee regarding thread safety !
|
||||
type FifoQueue interface {
|
||||
// Pop returns the first inserted element still in queue and deletes it from queue. If queue is empty, returns nil
|
||||
// No guarantee regarding thread safety !
|
||||
Pop() Queueable
|
||||
|
||||
// PopN returns the N first inserted elements still in queue and deletes them from queue. If queue is empty or i<=0, returns nil
|
||||
// If number to pop is greater than queue length, returns all queue elements
|
||||
// No guarantee regarding thread safety !
|
||||
PopN(i int) []Queueable
|
||||
|
||||
// Peek returns a copy of the first inserted element in queue without deleting it. If queue is empty, returns nil
|
||||
// No guarantee regarding thread safety !
|
||||
Peek() Queueable
|
||||
|
||||
// Peek returns a copy of the first inserted element in queue without deleting it. If queue is empty or i<=0, returns nil.
|
||||
// If number to peek is greater than queue length, returns all queue elements
|
||||
// No guarantee regarding thread safety !
|
||||
PeekN() []Queueable
|
||||
// Push adds an element to the queue
|
||||
// No guarantee regarding thread safety !
|
||||
Push(s Queueable) error
|
||||
|
||||
// Empty returns true if queue is empty
|
||||
// No guarantee regarding thread safety !
|
||||
Empty() bool
|
||||
}
|
||||
|
||||
type Queueable interface {
|
||||
QueueableName() string
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package stanza
|
||||
|
||||
import "encoding/xml"
|
||||
|
||||
type FormType string
|
||||
|
||||
const (
|
||||
FormTypeCancel = "cancel"
|
||||
FormTypeForm = "form"
|
||||
FormTypeResult = "result"
|
||||
FormTypeSubmit = "submit"
|
||||
)
|
||||
|
||||
// See XEP-0004 and XEP-0068
|
||||
// Pointer semantics
|
||||
type Form struct {
|
||||
XMLName xml.Name `xml:"jabber:x:data x"`
|
||||
Instructions []string `xml:"instructions"`
|
||||
Title string `xml:"title,omitempty"`
|
||||
Fields []*Field `xml:"field,omitempty"`
|
||||
Reported *FormItem `xml:"reported"`
|
||||
Items []FormItem `xml:"item,omitempty"`
|
||||
Type string `xml:"type,attr"`
|
||||
}
|
||||
|
||||
type FormItem struct {
|
||||
XMLName xml.Name
|
||||
Fields []Field `xml:"field,omitempty"`
|
||||
}
|
||||
|
||||
type Field struct {
|
||||
XMLName xml.Name `xml:"field"`
|
||||
Description string `xml:"desc,omitempty"`
|
||||
Required *string `xml:"required"`
|
||||
ValuesList []string `xml:"value"`
|
||||
Options []Option `xml:"option,omitempty"`
|
||||
Var string `xml:"var,attr,omitempty"`
|
||||
Type string `xml:"type,attr,omitempty"`
|
||||
Label string `xml:"label,attr,omitempty"`
|
||||
}
|
||||
|
||||
func NewForm(fields []*Field, formType string) *Form {
|
||||
return &Form{
|
||||
Type: formType,
|
||||
Fields: fields,
|
||||
}
|
||||
}
|
||||
|
||||
type FieldType string
|
||||
|
||||
const (
|
||||
FieldTypeBool = "boolean"
|
||||
FieldTypeFixed = "fixed"
|
||||
FieldTypeHidden = "hidden"
|
||||
FieldTypeJidMulti = "jid-multi"
|
||||
FieldTypeJidSingle = "jid-single"
|
||||
FieldTypeListMulti = "list-multi"
|
||||
FieldTypeListSingle = "list-single"
|
||||
FieldTypeTextMulti = "text-multi"
|
||||
FieldTypeTextPrivate = "text-private"
|
||||
FieldTypeTextSingle = "text-Single"
|
||||
)
|
||||
|
||||
type Option struct {
|
||||
XMLName xml.Name `xml:"option"`
|
||||
Label string `xml:"label,attr,omitempty"`
|
||||
ValuesList []string `xml:"value"`
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
package stanza
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
formSubmit = "<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\">" +
|
||||
"<configure node=\"princely_musings\">" +
|
||||
"<x xmlns=\"jabber:x:data\" type=\"submit\">" +
|
||||
"<field var=\"FORM_TYPE\" type=\"hidden\">" +
|
||||
"<value>http://jabber.org/protocol/pubsub#node_config</value>" +
|
||||
"</field>" +
|
||||
"<field var=\"pubsub#title\">" +
|
||||
"<value>Princely Musings (Atom)</value>" +
|
||||
"</field>" +
|
||||
"<field var=\"pubsub#deliver_notifications\">" +
|
||||
"<value>1</value>" +
|
||||
"</field>" +
|
||||
"<field var=\"pubsub#access_model\">" +
|
||||
"<value>roster</value>" +
|
||||
"</field>" +
|
||||
"<field var=\"pubsub#roster_groups_allowed\">" +
|
||||
"<value>friends</value>" +
|
||||
"<value>servants</value>" +
|
||||
"<value>courtiers</value>" +
|
||||
"</field>" +
|
||||
"<field var=\"pubsub#type\">" +
|
||||
"<value>http://www.w3.org/2005/Atom</value>" +
|
||||
"</field>" +
|
||||
"<field var=\"pubsub#notification_type\" type=\"list-single\"" +
|
||||
"label=\"Specify the delivery style for event notifications\">" +
|
||||
"<value>headline</value>" +
|
||||
"<option>" +
|
||||
"<value>normal</value>" +
|
||||
"</option>" +
|
||||
"<option>" +
|
||||
"<value>headline</value>" +
|
||||
"</option>" +
|
||||
"</field>" +
|
||||
"</x>" +
|
||||
"</configure>" +
|
||||
"</pubsub>"
|
||||
|
||||
clientJid = "hamlet@denmark.lit/elsinore"
|
||||
serviceJid = "pubsub.shakespeare.lit"
|
||||
iqId = "config1"
|
||||
serviceNode = "princely_musings"
|
||||
)
|
||||
|
||||
func TestMarshalFormSubmit(t *testing.T) {
|
||||
formIQ, err := NewIQ(Attrs{From: clientJid, To: serviceJid, Id: iqId, Type: IQTypeSet})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create formIQ: %v", err)
|
||||
}
|
||||
formIQ.Payload = &PubSubOwner{
|
||||
OwnerUseCase: &ConfigureOwner{
|
||||
Node: serviceNode,
|
||||
Form: &Form{
|
||||
Type: FormTypeSubmit,
|
||||
Fields: []*Field{
|
||||
{Var: "FORM_TYPE", Type: FieldTypeHidden, ValuesList: []string{"http://jabber.org/protocol/pubsub#node_config"}},
|
||||
{Var: "pubsub#title", ValuesList: []string{"Princely Musings (Atom)"}},
|
||||
{Var: "pubsub#deliver_notifications", ValuesList: []string{"1"}},
|
||||
{Var: "pubsub#access_model", ValuesList: []string{"roster"}},
|
||||
{Var: "pubsub#roster_groups_allowed", ValuesList: []string{"friends", "servants", "courtiers"}},
|
||||
{Var: "pubsub#type", ValuesList: []string{"http://www.w3.org/2005/Atom"}},
|
||||
{
|
||||
Var: "pubsub#notification_type",
|
||||
Type: "list-single",
|
||||
Label: "Specify the delivery style for event notifications",
|
||||
ValuesList: []string{"headline"},
|
||||
Options: []Option{
|
||||
{ValuesList: []string{"normal"}},
|
||||
{ValuesList: []string{"headline"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
b, err := xml.Marshal(formIQ.Payload)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not marshal formIQ : %v", err)
|
||||
}
|
||||
|
||||
if strings.ReplaceAll(string(b), " ", "") != strings.ReplaceAll(formSubmit, " ", "") {
|
||||
t.Fatalf("Expected formIQ and marshalled one are different.\nExepected : %s\nMarshalled : %s", formSubmit, string(b))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUnmarshalFormSubmit(t *testing.T) {
|
||||
var f PubSubOwner
|
||||
mErr := xml.Unmarshal([]byte(formSubmit), &f)
|
||||
if mErr != nil {
|
||||
t.Fatalf("failed to unmarshal formSubmit ! %s", mErr)
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(&f)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal formSubmit")
|
||||
}
|
||||
|
||||
if strings.ReplaceAll(string(data), " ", "") != strings.ReplaceAll(formSubmit, " ", "") {
|
||||
t.Fatalf("failed unmarshal/marshal for formSubmit : %s\n%s", string(data), formSubmit)
|
||||
}
|
||||
}
|
@ -0,0 +1,126 @@
|
||||
package stanza
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
// Roster
|
||||
|
||||
const (
|
||||
// NSRoster is the Roster IQ namespace
|
||||
NSRoster = "jabber:iq:roster"
|
||||
// SubscriptionNone indicates the user does not have a subscription to
|
||||
// the contact's presence, and the contact does not have a subscription
|
||||
// to the user's presence; this is the default value, so if the subscription
|
||||
// attribute is not included then the state is to be understood as "none"
|
||||
SubscriptionNone = "none"
|
||||
|
||||
// SubscriptionTo indicates the user has a subscription to the contact's
|
||||
// presence, but the contact does not have a subscription to the user's presence.
|
||||
SubscriptionTo = "to"
|
||||
|
||||
// SubscriptionFrom indicates the contact has a subscription to the user's
|
||||
// presence, but the user does not have a subscription to the contact's presence
|
||||
SubscriptionFrom = "from"
|
||||
|
||||
// SubscriptionBoth indicates the user and the contact have subscriptions to each
|
||||
// other's presence (also called a "mutual subscription")
|
||||
SubscriptionBoth = "both"
|
||||
)
|
||||
|
||||
// ----------
|
||||
// Namespaces
|
||||
|
||||
// Roster struct represents Roster IQs
|
||||
type Roster struct {
|
||||
XMLName xml.Name `xml:"jabber:iq:roster query"`
|
||||
// Result sets
|
||||
ResultSet *ResultSet `xml:"set,omitempty"`
|
||||
}
|
||||
|
||||
// Namespace defines the namespace for the RosterIQ
|
||||
func (r *Roster) Namespace() string {
|
||||
return r.XMLName.Space
|
||||
}
|
||||
func (r *Roster) GetSet() *ResultSet {
|
||||
return r.ResultSet
|
||||
}
|
||||
|
||||
// ---------------
|
||||
// Builder helpers
|
||||
|
||||
// RosterIQ builds a default Roster payload
|
||||
func (iq *IQ) RosterIQ() *Roster {
|
||||
r := Roster{
|
||||
XMLName: xml.Name{
|
||||
Space: NSRoster,
|
||||
Local: "query",
|
||||
},
|
||||
}
|
||||
iq.Payload = &r
|
||||
return &r
|
||||
}
|
||||
|
||||
// -----------
|
||||
// SubElements
|
||||
|
||||
// RosterItems represents the list of items in a roster IQ
|
||||
type RosterItems struct {
|
||||
XMLName xml.Name `xml:"jabber:iq:roster query"`
|
||||
Items []RosterItem `xml:"item"`
|
||||
// Result sets
|
||||
ResultSet *ResultSet `xml:"set,omitempty"`
|
||||
}
|
||||
|
||||
// Namespace lets RosterItems implement the IQPayload interface
|
||||
func (r *RosterItems) Namespace() string {
|
||||
return r.XMLName.Space
|
||||
}
|
||||
|
||||
func (r *RosterItems) GetSet() *ResultSet {
|
||||
return r.ResultSet
|
||||
}
|
||||
|
||||
// RosterItem represents an item in the roster iq
|
||||
type RosterItem struct {
|
||||
XMLName xml.Name `xml:"jabber:iq:roster item"`
|
||||
Jid string `xml:"jid,attr"`
|
||||
Ask string `xml:"ask,attr,omitempty"`
|
||||
Name string `xml:"name,attr,omitempty"`
|
||||
Subscription string `xml:"subscription,attr,omitempty"`
|
||||
Groups []string `xml:"group"`
|
||||
}
|
||||
|
||||
// ---------------
|
||||
// Builder helpers
|
||||
|
||||
// RosterItems builds a default RosterItems payload
|
||||
func (iq *IQ) RosterItems() *RosterItems {
|
||||
ri := RosterItems{
|
||||
XMLName: xml.Name{Space: "jabber:iq:roster", Local: "query"},
|
||||
}
|
||||
iq.Payload = &ri
|
||||
return &ri
|
||||
}
|
||||
|
||||
// AddItem builds an item and ads it to the roster IQ
|
||||
func (r *RosterItems) AddItem(jid, subscription, ask, name string, groups []string) *RosterItems {
|
||||
item := RosterItem{
|
||||
Jid: jid,
|
||||
Name: name,
|
||||
Groups: groups,
|
||||
Subscription: subscription,
|
||||
Ask: ask,
|
||||
}
|
||||
r.Items = append(r.Items, item)
|
||||
return r
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Registry init
|
||||
|
||||
func init() {
|
||||
TypeRegistry.MapExtension(PKTIQ, xml.Name{Space: NSRoster, Local: "query"}, Roster{})
|
||||
TypeRegistry.MapExtension(PKTIQ, xml.Name{Space: NSRoster, Local: "query"}, RosterItems{})
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
package stanza
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRosterBuilder(t *testing.T) {
|
||||
iq, err := NewIQ(Attrs{Type: IQTypeResult, From: "romeo@montague.net/orchard"})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create IQ: %v", err)
|
||||
}
|
||||
var noGroup []string
|
||||
|
||||
iq.RosterItems().AddItem("xl8ceawrfu8zdneomw1h6h28d@crypho.com",
|
||||
SubscriptionBoth,
|
||||
"",
|
||||
"xl8ceaw",
|
||||
[]string{"0flucpm8i2jtrjhxw01uf1nd2",
|
||||
"bm2bajg9ex4e1swiuju9i9nu5",
|
||||
"rvjpanomi4ejpx42fpmffoac0"}).
|
||||
AddItem("9aynsym60zbu78jbdvpho7s68@crypho.com",
|
||||
SubscriptionBoth,
|
||||
"",
|
||||
"9aynsym60",
|
||||
[]string{"mzaoy73i6ra5k502182zi1t97"}).
|
||||
AddItem("admin@crypho.com",
|
||||
SubscriptionBoth,
|
||||
"",
|
||||
"admin",
|
||||
noGroup)
|
||||
|
||||
parsedIQ, err := checkMarshalling(t, iq)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Check result
|
||||
pp, ok := parsedIQ.Payload.(*RosterItems)
|
||||
if !ok {
|
||||
t.Errorf("Parsed stanza does not contain correct IQ payload")
|
||||
}
|
||||
|
||||
// Check items
|
||||
items := []RosterItem{
|
||||
{
|
||||
XMLName: xml.Name{},
|
||||
Name: "xl8ceaw",
|
||||
Ask: "",
|
||||
Jid: "xl8ceawrfu8zdneomw1h6h28d@crypho.com",
|
||||
Subscription: SubscriptionBoth,
|
||||
Groups: []string{"0flucpm8i2jtrjhxw01uf1nd2",
|
||||
"bm2bajg9ex4e1swiuju9i9nu5",
|
||||
"rvjpanomi4ejpx42fpmffoac0"},
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{},
|
||||
Name: "9aynsym60",
|
||||
Ask: "",
|
||||
Jid: "9aynsym60zbu78jbdvpho7s68@crypho.com",
|
||||
Subscription: SubscriptionBoth,
|
||||
Groups: []string{"mzaoy73i6ra5k502182zi1t97"},
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{},
|
||||
Name: "admin",
|
||||
Ask: "",
|
||||
Jid: "admin@crypho.com",
|
||||
Subscription: SubscriptionBoth,
|
||||
Groups: noGroup,
|
||||
},
|
||||
}
|
||||
if len(pp.Items) != len(items) {
|
||||
t.Errorf("List length mismatch: %#v", pp.Items)
|
||||
} else {
|
||||
for i, item := range pp.Items {
|
||||
if item.Jid != items[i].Jid {
|
||||
t.Errorf("Jid Mismatch (expected: %s): %s", items[i].Jid, item.Jid)
|
||||
}
|
||||
if !reflect.DeepEqual(item.Groups, items[i].Groups) {
|
||||
t.Errorf("Node Mismatch (expected: %s): %s", items[i].Jid, item.Jid)
|
||||
}
|
||||
if item.Name != items[i].Name {
|
||||
t.Errorf("Name Mismatch (expected: %s): %s", items[i].Jid, item.Jid)
|
||||
}
|
||||
if item.Ask != items[i].Ask {
|
||||
t.Errorf("Name Mismatch (expected: %s): %s", items[i].Jid, item.Jid)
|
||||
}
|
||||
if item.Subscription != items[i].Subscription {
|
||||
t.Errorf("Name Mismatch (expected: %s): %s", items[i].Jid, item.Jid)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func checkMarshalling(t *testing.T, iq *IQ) (*IQ, error) {
|
||||
// Marshall
|
||||
data, err := xml.Marshal(iq)
|
||||
if err != nil {
|
||||
t.Errorf("cannot marshal iq: %s\n%#v", err, iq)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Unmarshall
|
||||
var parsedIQ IQ
|
||||
err = xml.Unmarshal(data, &parsedIQ)
|
||||
if err != nil {
|
||||
t.Errorf("Unmarshal returned error: %s\n%s", err, data)
|
||||
}
|
||||
return &parsedIQ, err
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package xmpp
|
||||
package stanza
|
||||
|
||||
import (
|
||||
"testing"
|
@ -0,0 +1,36 @@
|
||||
package stanza
|
||||
|
||||
import "encoding/xml"
|
||||
|
||||
/*
|
||||
Support for:
|
||||
- XEP-0334: Message Processing Hints: https://xmpp.org/extensions/xep-0334.html
|
||||
Pointers should be used to keep consistent with unmarshal. Eg :
|
||||
msg.Extensions = append(msg.Extensions, &stanza.HintNoCopy{}, &stanza.HintStore{})
|
||||
*/
|
||||
|
||||
type HintNoPermanentStore struct {
|
||||
MsgExtension
|
||||
XMLName xml.Name `xml:"urn:xmpp:hints no-permanent-store"`
|
||||
}
|
||||
|
||||
type HintNoStore struct {
|
||||
MsgExtension
|
||||
XMLName xml.Name `xml:"urn:xmpp:hints no-store"`
|
||||
}
|
||||
|
||||
type HintNoCopy struct {
|
||||
MsgExtension
|
||||
XMLName xml.Name `xml:"urn:xmpp:hints no-copy"`
|
||||
}
|
||||
type HintStore struct {
|
||||
MsgExtension
|
||||
XMLName xml.Name `xml:"urn:xmpp:hints store"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-permanent-store"}, HintNoPermanentStore{})
|
||||
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-store"}, HintNoStore{})
|
||||
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-copy"}, HintNoCopy{})
|
||||
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "store"}, HintStore{})
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package stanza_test
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const msg_const = `
|
||||
<message
|
||||
from="romeo@montague.lit/laptop"
|
||||
to="juliet@capulet.lit/laptop">
|
||||
<body>V unir avtugf pybnx gb uvqr zr sebz gurve fvtug</body>
|
||||
<no-copy xmlns="urn:xmpp:hints"></no-copy>
|
||||
<no-permanent-store xmlns="urn:xmpp:hints"></no-permanent-store>
|
||||
<no-store xmlns="urn:xmpp:hints"></no-store>
|
||||
<store xmlns="urn:xmpp:hints"></store>
|
||||
</message>`
|
||||
|
||||
func TestSerializationHint(t *testing.T) {
|
||||
msg := stanza.NewMessage(stanza.Attrs{To: "juliet@capulet.lit/laptop", From: "romeo@montague.lit/laptop"})
|
||||
msg.Body = "V unir avtugf pybnx gb uvqr zr sebz gurve fvtug"
|
||||
msg.Extensions = append(msg.Extensions, stanza.HintNoCopy{}, stanza.HintNoPermanentStore{}, stanza.HintNoStore{}, stanza.HintStore{})
|
||||
data, _ := xml.Marshal(msg)
|
||||
if strings.ReplaceAll(strings.Join(strings.Fields(msg_const), ""), "\n", "") != strings.Join(strings.Fields(string(data)), "") {
|
||||
t.Fatalf("marshalled message does not match expected message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalHints(t *testing.T) {
|
||||
// Init message as in the const value
|
||||
msgConst := stanza.NewMessage(stanza.Attrs{To: "juliet@capulet.lit/laptop", From: "romeo@montague.lit/laptop"})
|
||||
msgConst.Body = "V unir avtugf pybnx gb uvqr zr sebz gurve fvtug"
|
||||
msgConst.Extensions = append(msgConst.Extensions, &stanza.HintNoCopy{}, &stanza.HintNoPermanentStore{}, &stanza.HintNoStore{}, &stanza.HintStore{})
|
||||
|
||||
// Compare message with the const value
|
||||
msg := stanza.Message{}
|
||||
err := xml.Unmarshal([]byte(msg_const), &msg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if msgConst.XMLName.Local != msg.XMLName.Local {
|
||||
t.Fatalf("message tags do not match. Expected: %s, Actual: %s", msgConst.XMLName.Local, msg.XMLName.Local)
|
||||
}
|
||||
if msgConst.Body != msg.Body {
|
||||
t.Fatalf("message bodies do not match. Expected: %s, Actual: %s", msgConst.Body, msg.Body)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(msgConst.Attrs, msg.Attrs) {
|
||||
t.Fatalf("attributes do not match")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(msgConst.Error, msg.Error) {
|
||||
t.Fatalf("attributes do not match")
|
||||
}
|
||||
var found bool
|
||||
for _, ext := range msgConst.Extensions {
|
||||
for _, strExt := range msg.Extensions {
|
||||
if reflect.TypeOf(ext) == reflect.TypeOf(strExt) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("extensions do not match")
|
||||
}
|
||||
found = false
|
||||
}
|
||||
}
|
@ -0,0 +1,213 @@
|
||||
package stanza
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
)
|
||||
|
||||
// Implementation of the http://jabber.org/protocol/pubsub#event namespace
|
||||
type PubSubEvent struct {
|
||||
XMLName xml.Name `xml:"http://jabber.org/protocol/pubsub#event event"`
|
||||
MsgExtension
|
||||
EventElement EventElement
|
||||
//List ItemsEvent
|
||||
}
|
||||
|
||||
func init() {
|
||||
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "http://jabber.org/protocol/pubsub#event", Local: "event"}, PubSubEvent{})
|
||||
}
|
||||
|
||||
type EventElement interface {
|
||||
Name() string
|
||||
}
|
||||
|
||||
// *********************
|
||||
// Collection
|
||||
// *********************
|
||||
|
||||
const PubSubCollectionEventName = "Collection"
|
||||
|
||||
type CollectionEvent struct {
|
||||
AssocDisassoc AssocDisassoc
|
||||
Node string `xml:"node,attr,omitempty"`
|
||||
}
|
||||
|
||||
func (c CollectionEvent) Name() string {
|
||||
return PubSubCollectionEventName
|
||||
}
|
||||
|
||||
// *********************
|
||||
// Associate/Disassociate
|
||||
// *********************
|
||||
type AssocDisassoc interface {
|
||||
GetAssocDisassoc() string
|
||||
}
|
||||
|
||||
// *********************
|
||||
// Associate
|
||||
// *********************
|
||||
const Assoc = "Associate"
|
||||
|
||||
type AssociateEvent struct {
|
||||
XMLName xml.Name `xml:"associate"`
|
||||
Node string `xml:"node,attr"`
|
||||
}
|
||||
|
||||
func (a *AssociateEvent) GetAssocDisassoc() string {
|
||||
return Assoc
|
||||
}
|
||||
|
||||
// *********************
|
||||
// Disassociate
|
||||
// *********************
|
||||
const Disassoc = "Disassociate"
|
||||
|
||||
type DisassociateEvent struct {
|
||||
XMLName xml.Name `xml:"disassociate"`
|
||||
Node string `xml:"node,attr"`
|
||||
}
|
||||
|
||||
func (e *DisassociateEvent) GetAssocDisassoc() string {
|
||||
return Disassoc
|
||||
}
|
||||
|
||||
// *********************
|
||||
// Configuration
|
||||
// *********************
|
||||
|
||||
const PubSubConfigEventName = "Configuration"
|
||||
|
||||
type ConfigurationEvent struct {
|
||||
Node string `xml:"node,attr,omitempty"`
|
||||
Form *Form
|
||||
}
|
||||
|
||||
func (c ConfigurationEvent) Name() string {
|
||||
return PubSubConfigEventName
|
||||
}
|
||||
|
||||
// *********************
|
||||
// Delete
|
||||
// *********************
|
||||
const PubSubDeleteEventName = "Delete"
|
||||
|
||||
type DeleteEvent struct {
|
||||
Node string `xml:"node,attr"`
|
||||
Redirect *RedirectEvent `xml:"redirect"`
|
||||
}
|
||||
|
||||
func (c DeleteEvent) Name() string {
|
||||
return PubSubConfigEventName
|
||||
}
|
||||
|
||||
// *********************
|
||||
// Redirect
|
||||
// *********************
|
||||
type RedirectEvent struct {
|
||||
URI string `xml:"uri,attr"`
|
||||
}
|
||||
|
||||
// *********************
|
||||
// List
|
||||
// *********************
|
||||
|
||||
const PubSubItemsEventName = "List"
|
||||
|
||||
type ItemsEvent struct {
|
||||
XMLName xml.Name `xml:"items"`
|
||||
Items []ItemEvent `xml:"item,omitempty"`
|
||||
Node string `xml:"node,attr"`
|
||||
Retract *RetractEvent `xml:"retract"`
|
||||
}
|
||||
|
||||
type ItemEvent struct {
|
||||
XMLName xml.Name `xml:"item"`
|
||||
Id string `xml:"id,attr,omitempty"`
|
||||
Publisher string `xml:"publisher,attr,omitempty"`
|
||||
Any *Node `xml:",any"`
|
||||
}
|
||||
|
||||
func (i ItemsEvent) Name() string {
|
||||
return PubSubItemsEventName
|
||||
}
|
||||
|
||||
// *********************
|
||||
// List
|
||||
// *********************
|
||||
|
||||
type RetractEvent struct {
|
||||
XMLName xml.Name `xml:"retract"`
|
||||
ID string `xml:"node,attr"`
|
||||
}
|
||||
|
||||
// *********************
|
||||
// Purge
|
||||
// *********************
|
||||
const PubSubPurgeEventName = "Purge"
|
||||
|
||||
type PurgeEvent struct {
|
||||
XMLName xml.Name `xml:"purge"`
|
||||
Node string `xml:"node,attr"`
|
||||
}
|
||||
|
||||
func (p PurgeEvent) Name() string {
|
||||
return PubSubPurgeEventName
|
||||
}
|
||||
|
||||
// *********************
|
||||
// Subscription
|
||||
// *********************
|
||||
const PubSubSubscriptionEventName = "Subscription"
|
||||
|
||||
type SubscriptionEvent struct {
|
||||
SubStatus string `xml:"subscription,attr,omitempty"`
|
||||
Expiry string `xml:"expiry,attr,omitempty"`
|
||||
SubInfo `xml:",omitempty"`
|
||||
}
|
||||
|
||||
func (s SubscriptionEvent) Name() string {
|
||||
return PubSubSubscriptionEventName
|
||||
}
|
||||
|
||||
func (pse *PubSubEvent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
pse.XMLName = start.Name
|
||||
// decode inner elements
|
||||
for {
|
||||
t, err := d.Token()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var ee EventElement
|
||||
switch tt := t.(type) {
|
||||
case xml.StartElement:
|
||||
switch tt.Name.Local {
|
||||
case "collection":
|
||||
ee = &CollectionEvent{}
|
||||
case "configuration":
|
||||
ee = &ConfigurationEvent{}
|
||||
case "delete":
|
||||
ee = &DeleteEvent{}
|
||||
case "items":
|
||||
ee = &ItemsEvent{}
|
||||
case "purge":
|
||||
ee = &PurgeEvent{}
|
||||
case "subscription":
|
||||
ee = &SubscriptionEvent{}
|
||||
default:
|
||||
ee = nil
|
||||
}
|
||||
// known child element found, decode it
|
||||
if ee != nil {
|
||||
err = d.DecodeElement(ee, &tt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pse.EventElement = ee
|
||||
}
|
||||
case xml.EndElement:
|
||||
if tt == start.End() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,162 @@
|
||||
package stanza_test
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDecodeMsgEvent(t *testing.T) {
|
||||
str := `<message from='pubsub.shakespeare.lit' to='francisco@denmark.lit' id='foo'>
|
||||
<event xmlns='http://jabber.org/protocol/pubsub#event'>
|
||||
<items node='princely_musings'>
|
||||
<item id='ae890ac52d0df67ed7cfdf51b644e901'>
|
||||
<entry xmlns='http://www.w3.org/2005/Atom'>
|
||||
<title>Soliloquy</title>
|
||||
<summary>
|
||||
To be, or not to be: that is the question:
|
||||
Whether 'tis nobler in the mind to suffer
|
||||
The slings and arrows of outrageous fortune,
|
||||
Or to take arms against a sea of troubles,
|
||||
And by opposing end them?
|
||||
</summary>
|
||||
<link rel='alternate' type='text/html'
|
||||
href='http://denmark.lit/2003/12/13/atom03'/>
|
||||
<id>tag:denmark.lit,2003:entry-32397</id>
|
||||
<published>2003-12-13T18:30:02Z</published>
|
||||
<updated>2003-12-13T18:30:02Z</updated>
|
||||
</entry>
|
||||
</item>
|
||||
</items>
|
||||
</event>
|
||||
</message>
|
||||
`
|
||||
parsedMessage := stanza.Message{}
|
||||
if err := xml.Unmarshal([]byte(str), &parsedMessage); err != nil {
|
||||
t.Errorf("message receipt unmarshall error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if parsedMessage.Body != "" {
|
||||
t.Errorf("Unexpected body: '%s'", parsedMessage.Body)
|
||||
}
|
||||
|
||||
if len(parsedMessage.Extensions) < 1 {
|
||||
t.Errorf("no extension found on parsed message")
|
||||
return
|
||||
}
|
||||
|
||||
switch ext := parsedMessage.Extensions[0].(type) {
|
||||
case *stanza.PubSubEvent:
|
||||
if ext.XMLName.Local != "event" {
|
||||
t.Fatalf("unexpected extension: %s:%s", ext.XMLName.Space, ext.XMLName.Local)
|
||||
}
|
||||
tmp, ok := parsedMessage.Extensions[0].(*stanza.PubSubEvent)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected extension element: %s:%s", ext.XMLName.Space, ext.XMLName.Local)
|
||||
}
|
||||
ie, ok := tmp.EventElement.(*stanza.ItemsEvent)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected extension element: %s:%s", ext.XMLName.Space, ext.XMLName.Local)
|
||||
}
|
||||
if ie.Items[0].Any.Nodes[0].Content != "Soliloquy" {
|
||||
t.Fatalf("could not read title ! Read this : %s", ie.Items[0].Any.Nodes[0].Content)
|
||||
}
|
||||
|
||||
if len(ie.Items[0].Any.Nodes) != 6 {
|
||||
t.Fatalf("some nodes were not correctly parsed")
|
||||
}
|
||||
default:
|
||||
t.Fatalf("could not find pubsub event extension")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestEncodeEvent(t *testing.T) {
|
||||
expected := "<message><event xmlns=\"http://jabber.org/protocol/pubsub#event\">" +
|
||||
"<items node=\"princely_musings\"><item id=\"ae890ac52d0df67ed7cfdf51b644e901\">" +
|
||||
"<entry xmlns=\"http://www.w3.org/2005/Atom\"><title>My pub item title</title>" +
|
||||
"<summary>My pub item content summary</summary><link rel=\"alternate\" " +
|
||||
"type=\"text/html\" href=\"http://denmark.lit/2003/12/13/atom03\">" +
|
||||
"</link><id>My pub item content ID</id><published>2003-12-13T18:30:02Z</published>" +
|
||||
"<updated>2003-12-13T18:30:02Z</updated></entry></item></items></event></message>"
|
||||
message := stanza.Message{
|
||||
Extensions: []stanza.MsgExtension{
|
||||
stanza.PubSubEvent{
|
||||
EventElement: stanza.ItemsEvent{
|
||||
Items: []stanza.ItemEvent{
|
||||
{
|
||||
Id: "ae890ac52d0df67ed7cfdf51b644e901",
|
||||
Any: &stanza.Node{
|
||||
XMLName: xml.Name{
|
||||
Space: "http://www.w3.org/2005/Atom",
|
||||
Local: "entry",
|
||||
},
|
||||
Attrs: nil,
|
||||
Content: "",
|
||||
Nodes: []stanza.Node{
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "title"},
|
||||
Attrs: nil,
|
||||
Content: "My pub item title",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "summary"},
|
||||
Attrs: nil,
|
||||
Content: "My pub item content summary",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "link"},
|
||||
Attrs: []xml.Attr{
|
||||
{
|
||||
Name: xml.Name{Space: "", Local: "rel"},
|
||||
Value: "alternate",
|
||||
},
|
||||
{
|
||||
Name: xml.Name{Space: "", Local: "type"},
|
||||
Value: "text/html",
|
||||
},
|
||||
{
|
||||
Name: xml.Name{Space: "", Local: "href"},
|
||||
Value: "http://denmark.lit/2003/12/13/atom03",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "id"},
|
||||
Attrs: nil,
|
||||
Content: "My pub item content ID",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "published"},
|
||||
Attrs: nil,
|
||||
Content: "2003-12-13T18:30:02Z",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "updated"},
|
||||
Attrs: nil,
|
||||
Content: "2003-12-13T18:30:02Z",
|
||||
Nodes: nil,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Node: "princely_musings",
|
||||
Retract: nil,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
data, _ := xml.Marshal(message)
|
||||
if strings.TrimSpace(string(data)) != strings.TrimSpace(expected) {
|
||||
t.Errorf("event was not encoded properly : \nexpected:%s \ngot: %s", expected, string(data))
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,451 @@
|
||||
package stanza
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PubSubOwner struct {
|
||||
XMLName xml.Name `xml:"http://jabber.org/protocol/pubsub#owner pubsub"`
|
||||
OwnerUseCase OwnerUseCase
|
||||
// Result sets
|
||||
ResultSet *ResultSet `xml:"set,omitempty"`
|
||||
}
|
||||
|
||||
func (pso *PubSubOwner) Namespace() string {
|
||||
return pso.XMLName.Space
|
||||
}
|
||||
|
||||
func (pso *PubSubOwner) GetSet() *ResultSet {
|
||||
return pso.ResultSet
|
||||
}
|
||||
|
||||
type OwnerUseCase interface {
|
||||
UseCase() string
|
||||
}
|
||||
|
||||
type AffiliationsOwner struct {
|
||||
XMLName xml.Name `xml:"affiliations"`
|
||||
Affiliations []AffiliationOwner `xml:"affiliation,omitempty"`
|
||||
Node string `xml:"node,attr"`
|
||||
}
|
||||
|
||||
func (AffiliationsOwner) UseCase() string {
|
||||
return "affiliations"
|
||||
}
|
||||
|
||||
type AffiliationOwner struct {
|
||||
XMLName xml.Name `xml:"affiliation"`
|
||||
AffiliationStatus string `xml:"affiliation,attr"`
|
||||
Jid string `xml:"jid,attr"`
|
||||
}
|
||||
|
||||
const (
|
||||
AffiliationStatusMember = "member"
|
||||
AffiliationStatusNone = "none"
|
||||
AffiliationStatusOutcast = "outcast"
|
||||
AffiliationStatusOwner = "owner"
|
||||
AffiliationStatusPublisher = "publisher"
|
||||
AffiliationStatusPublishOnly = "publish-only"
|
||||
)
|
||||
|
||||
type ConfigureOwner struct {
|
||||
XMLName xml.Name `xml:"configure"`
|
||||
Node string `xml:"node,attr,omitempty"`
|
||||
Form *Form `xml:"x,omitempty"`
|
||||
}
|
||||
|
||||
func (*ConfigureOwner) UseCase() string {
|
||||
return "configure"
|
||||
}
|
||||
|
||||
type DefaultOwner struct {
|
||||
XMLName xml.Name `xml:"default"`
|
||||
Form *Form `xml:"x,omitempty"`
|
||||
}
|
||||
|
||||
func (*DefaultOwner) UseCase() string {
|
||||
return "default"
|
||||
}
|
||||
|
||||
type DeleteOwner struct {
|
||||
XMLName xml.Name `xml:"delete"`
|
||||
RedirectOwner *RedirectOwner `xml:"redirect,omitempty"`
|
||||
Node string `xml:"node,attr,omitempty"`
|
||||
}
|
||||
|
||||
func (*DeleteOwner) UseCase() string {
|
||||
return "delete"
|
||||
}
|
||||
|
||||
type RedirectOwner struct {
|
||||
XMLName xml.Name `xml:"redirect"`
|
||||
URI string `xml:"uri,attr"`
|
||||
}
|
||||
|
||||
type PurgeOwner struct {
|
||||
XMLName xml.Name `xml:"purge"`
|
||||
Node string `xml:"node,attr"`
|
||||
}
|
||||
|
||||
func (*PurgeOwner) UseCase() string {
|
||||
return "purge"
|
||||
}
|
||||
|
||||
type SubscriptionsOwner struct {
|
||||
XMLName xml.Name `xml:"subscriptions"`
|
||||
Subscriptions []SubscriptionOwner `xml:"subscription"`
|
||||
Node string `xml:"node,attr"`
|
||||
}
|
||||
|
||||
func (*SubscriptionsOwner) UseCase() string {
|
||||
return "subscriptions"
|
||||
}
|
||||
|
||||
type SubscriptionOwner struct {
|
||||
SubscriptionStatus string `xml:"subscription"`
|
||||
Jid string `xml:"jid,attr"`
|
||||
}
|
||||
|
||||
const (
|
||||
SubscriptionStatusNone = "none"
|
||||
SubscriptionStatusPending = "pending"
|
||||
SubscriptionStatusSubscribed = "subscribed"
|
||||
SubscriptionStatusUnconfigured = "unconfigured"
|
||||
)
|
||||
|
||||
// NewConfigureNode creates a request to configure a node on the given service.
|
||||
// A form will be returned by the service, to which the user must respond using for instance the NewFormSubmission function.
|
||||
// See 8.2 Configure a Node
|
||||
func NewConfigureNode(serviceId, nodeName string) (*IQ, error) {
|
||||
iq, err := NewIQ(Attrs{Type: IQTypeGet, To: serviceId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iq.Payload = &PubSubOwner{
|
||||
OwnerUseCase: &ConfigureOwner{Node: nodeName},
|
||||
}
|
||||
return iq, nil
|
||||
}
|
||||
|
||||
// NewDelNode creates a request to delete node "nodeID" from the "serviceId" service
|
||||
// See 8.4 Delete a Node
|
||||
func NewDelNode(serviceId, nodeID string) (*IQ, error) {
|
||||
if strings.TrimSpace(nodeID) == "" {
|
||||
return nil, errors.New("cannot delete a node without a target node ID")
|
||||
}
|
||||
iq, err := NewIQ(Attrs{Type: IQTypeSet, To: serviceId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iq.Payload = &PubSubOwner{
|
||||
OwnerUseCase: &DeleteOwner{Node: nodeID},
|
||||
}
|
||||
return iq, nil
|
||||
}
|
||||
|
||||
// NewPurgeAllItems creates a new purge request for the "nodeId" node, at "serviceId" service
|
||||
// See 8.5 Purge All Node Items
|
||||
func NewPurgeAllItems(serviceId, nodeId string) (*IQ, error) {
|
||||
iq, err := NewIQ(Attrs{Type: IQTypeSet, To: serviceId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iq.Payload = &PubSubOwner{
|
||||
OwnerUseCase: &PurgeOwner{Node: nodeId},
|
||||
}
|
||||
return iq, nil
|
||||
}
|
||||
|
||||
// NewRequestDefaultConfig build a request to ask the service for the default config of its nodes
|
||||
// See 8.3 Request Default Node Configuration Options
|
||||
func NewRequestDefaultConfig(serviceId string) (*IQ, error) {
|
||||
iq, err := NewIQ(Attrs{Type: IQTypeGet, To: serviceId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iq.Payload = &PubSubOwner{
|
||||
OwnerUseCase: &DefaultOwner{},
|
||||
}
|
||||
return iq, nil
|
||||
}
|
||||
|
||||
// NewApproveSubRequest creates a new sub approval response to a request from the service to the owner of the node
|
||||
// In order to approve the request, the owner shall submit the form and set the "pubsub#allow" field to a value of "1" or "true"
|
||||
// For tracking purposes the message MUST reflect the 'id' attribute originally provided in the request.
|
||||
// See 8.6 Manage Subscription Requests
|
||||
func NewApproveSubRequest(serviceId, reqID string, apprForm *Form) (Message, error) {
|
||||
if serviceId == "" {
|
||||
return Message{}, errors.New("need a target service serviceId send approval serviceId")
|
||||
}
|
||||
if reqID == "" {
|
||||
return Message{}, errors.New("the request ID is empty but must be used for the approval")
|
||||
}
|
||||
if apprForm == nil {
|
||||
return Message{}, errors.New("approval form is nil")
|
||||
}
|
||||
apprMess := NewMessage(Attrs{To: serviceId})
|
||||
apprMess.Extensions = []MsgExtension{apprForm}
|
||||
apprMess.Id = reqID
|
||||
|
||||
return apprMess, nil
|
||||
}
|
||||
|
||||
// NewGetPendingSubRequests creates a new request for all pending subscriptions to all their nodes at a service
|
||||
// This feature MUST be implemented using the Ad-Hoc Commands (XEP-0050) protocol
|
||||
// 8.7 Process Pending Subscription Requests
|
||||
func NewGetPendingSubRequests(serviceId string) (*IQ, error) {
|
||||
iq, err := NewIQ(Attrs{Type: IQTypeSet, To: serviceId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iq.Payload = &Command{
|
||||
// the command name ('node' attribute of the command element) MUST have a value of "http://jabber.org/protocol/pubsub#get-pending"
|
||||
Node: "http://jabber.org/protocol/pubsub#get-pending",
|
||||
Action: CommandActionExecute,
|
||||
}
|
||||
return iq, nil
|
||||
}
|
||||
|
||||
// NewGetPendingSubRequests creates a new request for all pending subscriptions to be approved on a given node
|
||||
// Upon receiving the data form for managing subscription requests, the owner then MAY request pending subscription
|
||||
// approval requests for a given node.
|
||||
// See 8.7.4 Per-Node Request
|
||||
func NewApprovePendingSubRequest(serviceId, sessionId, nodeId string) (*IQ, error) {
|
||||
if sessionId == "" {
|
||||
return nil, errors.New("the sessionId must be maintained for the command")
|
||||
}
|
||||
|
||||
form := &Form{
|
||||
Type: FormTypeSubmit,
|
||||
Fields: []*Field{{Var: "pubsub#node", ValuesList: []string{nodeId}}},
|
||||
}
|
||||
data, err := xml.Marshal(form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var n Node
|
||||
err = xml.Unmarshal(data, &n)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
iq, err := NewIQ(Attrs{Type: IQTypeSet, To: serviceId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iq.Payload = &Command{
|
||||
// the command name ('node' attribute of the command element) MUST have a value of "http://jabber.org/protocol/pubsub#get-pending"
|
||||
Node: "http://jabber.org/protocol/pubsub#get-pending",
|
||||
Action: CommandActionExecute,
|
||||
SessionId: sessionId,
|
||||
CommandElement: &n,
|
||||
}
|
||||
return iq, nil
|
||||
}
|
||||
|
||||
// NewSubListRequest creates a request to list subscriptions of the client, for all nodes at the service.
|
||||
// It's a Get type IQ
|
||||
// 8.8.1 Retrieve Subscriptions
|
||||
func NewSubListRqPl(serviceId, nodeID string) (*IQ, error) {
|
||||
iq, err := NewIQ(Attrs{Type: IQTypeGet, To: serviceId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iq.Payload = &PubSubOwner{
|
||||
OwnerUseCase: &SubscriptionsOwner{Node: nodeID},
|
||||
}
|
||||
return iq, nil
|
||||
}
|
||||
|
||||
func NewSubsForEntitiesRequest(serviceId, nodeID string, subs []SubscriptionOwner) (*IQ, error) {
|
||||
iq, err := NewIQ(Attrs{Type: IQTypeSet, To: serviceId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iq.Payload = &PubSubOwner{
|
||||
OwnerUseCase: &SubscriptionsOwner{Node: nodeID, Subscriptions: subs},
|
||||
}
|
||||
return iq, nil
|
||||
}
|
||||
|
||||
// NewModifAffiliationRequest creates a request to either modify one or more affiliations, or delete one or more affiliations
|
||||
// 8.9.2 Modify Affiliation & 8.9.2.4 Multiple Simultaneous Modifications & 8.9.3 Delete an Entity (just set the status to "none")
|
||||
func NewModifAffiliationRequest(serviceId, nodeID string, newAffils []AffiliationOwner) (*IQ, error) {
|
||||
iq, err := NewIQ(Attrs{Type: IQTypeSet, To: serviceId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iq.Payload = &PubSubOwner{
|
||||
OwnerUseCase: &AffiliationsOwner{
|
||||
Node: nodeID,
|
||||
Affiliations: newAffils,
|
||||
},
|
||||
}
|
||||
return iq, nil
|
||||
}
|
||||
|
||||
// NewAffiliationListRequest creates a request to list all affiliated entities
|
||||
// See 8.9.1 Retrieve List List
|
||||
func NewAffiliationListRequest(serviceId, nodeID string) (*IQ, error) {
|
||||
iq, err := NewIQ(Attrs{Type: IQTypeGet, To: serviceId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iq.Payload = &PubSubOwner{
|
||||
OwnerUseCase: &AffiliationsOwner{
|
||||
Node: nodeID,
|
||||
},
|
||||
}
|
||||
return iq, nil
|
||||
}
|
||||
|
||||
// NewFormSubmission builds a form submission pubsub IQ, in the Owner namespace
|
||||
// This is typically used to respond to a form issued by the server when configuring a node.
|
||||
// See 8.2.4 Form Submission
|
||||
func NewFormSubmissionOwner(serviceId, nodeName string, fields []*Field) (*IQ, error) {
|
||||
if serviceId == "" || nodeName == "" {
|
||||
return nil, errors.New("serviceId and nodeName must be filled for this request to be valid")
|
||||
}
|
||||
|
||||
submitConf, err := NewIQ(Attrs{Type: IQTypeSet, To: serviceId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
submitConf.Payload = &PubSubOwner{
|
||||
OwnerUseCase: &ConfigureOwner{
|
||||
Node: nodeName,
|
||||
Form: NewForm(fields,
|
||||
FormTypeSubmit)},
|
||||
}
|
||||
|
||||
return submitConf, nil
|
||||
}
|
||||
|
||||
// GetFormFields gets the fields from a form in a IQ stanza of type result, as a map.
|
||||
// Key is the "var" attribute of the field, and field is the value.
|
||||
// The user can then select and modify the fields they want to alter, and submit a new form to the service using the
|
||||
// NewFormSubmission function to build the IQ.
|
||||
// TODO : remove restriction on IQ type ?
|
||||
func (iq *IQ) GetFormFields() (map[string]*Field, error) {
|
||||
if iq.Type != IQTypeResult {
|
||||
return nil, errors.New("this IQ is not a result type IQ. Cannot extract the form from it")
|
||||
}
|
||||
switch payload := iq.Payload.(type) {
|
||||
// We support IOT Control IQ
|
||||
case *PubSubGeneric:
|
||||
fieldMap := make(map[string]*Field)
|
||||
for _, elt := range payload.Configure.Form.Fields {
|
||||
fieldMap[elt.Var] = elt
|
||||
}
|
||||
return fieldMap, nil
|
||||
case *PubSubOwner:
|
||||
fieldMap := make(map[string]*Field)
|
||||
co, ok := payload.OwnerUseCase.(*ConfigureOwner)
|
||||
if !ok {
|
||||
return nil, errors.New("this IQ does not contain a PubSub payload with a configure tag for the owner namespace")
|
||||
}
|
||||
for _, elt := range co.Form.Fields {
|
||||
fieldMap[elt.Var] = elt
|
||||
}
|
||||
return fieldMap, nil
|
||||
|
||||
case *Command:
|
||||
fieldMap := make(map[string]*Field)
|
||||
co, ok := payload.CommandElement.(*Form)
|
||||
if !ok {
|
||||
return nil, errors.New("this IQ does not contain a command payload with a form")
|
||||
}
|
||||
for _, elt := range co.Fields {
|
||||
fieldMap[elt.Var] = elt
|
||||
}
|
||||
return fieldMap, nil
|
||||
default:
|
||||
if iq.Any != nil {
|
||||
fieldMap := make(map[string]*Field)
|
||||
if iq.Any.XMLName.Local != "command" {
|
||||
return nil, errors.New("this IQ does not contain a form")
|
||||
}
|
||||
|
||||
for _, nde := range iq.Any.Nodes {
|
||||
if nde.XMLName.Local == "x" {
|
||||
for _, n := range nde.Nodes {
|
||||
if n.XMLName.Local == "field" {
|
||||
f := Field{}
|
||||
data, err := xml.Marshal(n)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
err = xml.Unmarshal(data, &f)
|
||||
if err == nil {
|
||||
fieldMap[f.Var] = &f
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return fieldMap, nil
|
||||
}
|
||||
return nil, errors.New("this IQ does not contain a form")
|
||||
}
|
||||
}
|
||||
|
||||
func (pso *PubSubOwner) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
pso.XMLName = start.Name
|
||||
// decode inner elements
|
||||
for {
|
||||
t, err := d.Token()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch tt := t.(type) {
|
||||
|
||||
case xml.StartElement:
|
||||
// Decode sub-elements
|
||||
var err error
|
||||
switch tt.Name.Local {
|
||||
|
||||
case "affiliations":
|
||||
aff := AffiliationsOwner{}
|
||||
err = d.DecodeElement(&aff, &tt)
|
||||
pso.OwnerUseCase = &aff
|
||||
case "configure":
|
||||
co := ConfigureOwner{}
|
||||
err = d.DecodeElement(&co, &tt)
|
||||
pso.OwnerUseCase = &co
|
||||
case "default":
|
||||
def := DefaultOwner{}
|
||||
err = d.DecodeElement(&def, &tt)
|
||||
pso.OwnerUseCase = &def
|
||||
case "delete":
|
||||
del := DeleteOwner{}
|
||||
err = d.DecodeElement(&del, &tt)
|
||||
pso.OwnerUseCase = &del
|
||||
case "purge":
|
||||
pu := PurgeOwner{}
|
||||
err = d.DecodeElement(&pu, &tt)
|
||||
pso.OwnerUseCase = &pu
|
||||
case "subscriptions":
|
||||
subs := SubscriptionsOwner{}
|
||||
err = d.DecodeElement(&subs, &tt)
|
||||
pso.OwnerUseCase = &subs
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case xml.EndElement:
|
||||
if tt == start.End() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
TypeRegistry.MapExtension(PKTIQ, xml.Name{Space: "http://jabber.org/protocol/pubsub#owner", Local: "pubsub"}, PubSubOwner{})
|
||||
}
|
@ -0,0 +1,885 @@
|
||||
package stanza_test
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// ******************************
|
||||
// * 8.2 Configure a Node
|
||||
// ******************************
|
||||
func TestNewConfigureNode(t *testing.T) {
|
||||
expectedReq := "<iq type=\"get\" id=\"config1\" to=\"pubsub.shakespeare.lit\" > " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> <configure node=\"princely_musings\"></configure> " +
|
||||
"</pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewConfigureNode("pubsub.shakespeare.lit", "princely_musings")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a configure node request: %v", err)
|
||||
}
|
||||
subR.Id = "config1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
|
||||
if pubsub.OwnerUseCase == nil {
|
||||
t.Fatalf("owner use case is nil")
|
||||
}
|
||||
|
||||
ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner)
|
||||
if !ok {
|
||||
t.Fatalf("owner use case is not a configure tag")
|
||||
}
|
||||
|
||||
if ownrUsecase.Node == "" {
|
||||
t.Fatalf("could not parse node from config tag")
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewConfigureNodeResp(t *testing.T) {
|
||||
response := `
|
||||
<iq from="pubsub.shakespeare.lit" id="config1" to="hamlet@denmark.lit/elsinore" type="result">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
|
||||
<configure node="princely_musings">
|
||||
<x type="form" xmlns="jabber:x:data">
|
||||
<field type="hidden" var="FORM_TYPE">
|
||||
<value>http://jabber.org/protocol/pubsub#node_config</value>
|
||||
</field>
|
||||
<field label="Purge all items when the relevant publisher goes offline?" type="boolean" var="pubsub#purge_offline">
|
||||
<value>0</value>
|
||||
</field>
|
||||
<field label="Max Payload size in bytes" type="text-single" var="pubsub#max_payload_size">
|
||||
<value>1028</value>
|
||||
</field>
|
||||
<field label="When to send the last published item" type="list-single" var="pubsub#send_last_published_item">
|
||||
<option label="Never">
|
||||
<value>never</value>
|
||||
</option>
|
||||
<option label="When a new subscription is processed">
|
||||
<value>on_sub</value>
|
||||
</option>
|
||||
<option label="When a new subscription is processed and whenever a subscriber comes online">
|
||||
<value>on_sub_and_presence</value>
|
||||
</option>
|
||||
<value>never</value>
|
||||
</field>
|
||||
<field label="Deliver event notifications only to available users" type="boolean" var="pubsub#presence_based_delivery">
|
||||
<value>0</value>
|
||||
</field>
|
||||
<field label="Specify the delivery style for event notifications" type="list-single" var="pubsub#notification_type">
|
||||
<option>
|
||||
<value>normal</value>
|
||||
</option>
|
||||
<option>
|
||||
<value>headline</value>
|
||||
</option>
|
||||
<value>headline</value>
|
||||
</field>
|
||||
<field label="Specify the type of payload data to be provided at this node" type="text-single" var="pubsub#type">
|
||||
<value>http://www.w3.org/2005/Atom</value>
|
||||
</field>
|
||||
<field label="Payload XSLT" type="text-single" var="pubsub#dataform_xslt"/>
|
||||
</x>
|
||||
</configure>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
|
||||
pubsub, err := getPubSubOwnerPayload(response)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
if pubsub.OwnerUseCase == nil {
|
||||
t.Fatalf("owner use case is nil")
|
||||
}
|
||||
|
||||
ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner)
|
||||
if !ok {
|
||||
t.Fatalf("owner use case is not a configure tag")
|
||||
}
|
||||
|
||||
if ownrUsecase.Form == nil {
|
||||
t.Fatalf("form is nil in the parsed config tag")
|
||||
}
|
||||
|
||||
if len(ownrUsecase.Form.Fields) != 8 {
|
||||
t.Fatalf("one or more fields in the response form could not be parsed correctly")
|
||||
}
|
||||
}
|
||||
|
||||
// *************************************************
|
||||
// * 8.3 Request Default Node Configuration Options
|
||||
// *************************************************
|
||||
|
||||
func TestNewRequestDefaultConfig(t *testing.T) {
|
||||
expectedReq := "<iq type=\"get\" id=\"def1\" to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> <default></default> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewRequestDefaultConfig("pubsub.shakespeare.lit")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a default config request: %v", err)
|
||||
}
|
||||
subR.Id = "def1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
|
||||
if pubsub.OwnerUseCase == nil {
|
||||
t.Fatalf("owner use case is nil")
|
||||
}
|
||||
|
||||
_, ok = pubsub.OwnerUseCase.(*stanza.DefaultOwner)
|
||||
if !ok {
|
||||
t.Fatalf("owner use case is not a default tag")
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRequestDefaultConfigResp(t *testing.T) {
|
||||
response := `
|
||||
<iq from="pubsub.shakespeare.lit" id="config1" to="hamlet@denmark.lit/elsinore" type="result">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
|
||||
<configure node="princely_musings">
|
||||
<x type="form" xmlns="jabber:x:data">
|
||||
<field type="hidden" var="FORM_TYPE">
|
||||
<value>http://jabber.org/protocol/pubsub#node_config</value>
|
||||
</field>
|
||||
<field label="Purge all items when the relevant publisher goes offline?" type="boolean" var="pubsub#purge_offline">
|
||||
<value>0</value>
|
||||
</field>
|
||||
<field label="Max Payload size in bytes" type="text-single" var="pubsub#max_payload_size">
|
||||
<value>1028</value>
|
||||
</field>
|
||||
<field label="When to send the last published item" type="list-single" var="pubsub#send_last_published_item">
|
||||
<option label="Never">
|
||||
<value>never</value>
|
||||
</option>
|
||||
<option label="When a new subscription is processed">
|
||||
<value>on_sub</value>
|
||||
</option>
|
||||
<option label="When a new subscription is processed and whenever a subscriber comes online">
|
||||
<value>on_sub_and_presence</value>
|
||||
</option>
|
||||
<value>never</value>
|
||||
</field>
|
||||
<field label="Deliver event notifications only to available users" type="boolean" var="pubsub#presence_based_delivery">
|
||||
<value>0</value>
|
||||
</field>
|
||||
<field label="Specify the delivery style for event notifications" type="list-single" var="pubsub#notification_type">
|
||||
<option>
|
||||
<value>normal</value>
|
||||
</option>
|
||||
<option>
|
||||
<value>headline</value>
|
||||
</option>
|
||||
<value>headline</value>
|
||||
</field>
|
||||
<field label="Specify the type of payload data to be provided at this node" type="text-single" var="pubsub#type">
|
||||
<value>http://www.w3.org/2005/Atom</value>
|
||||
</field>
|
||||
<field label="Payload XSLT" type="text-single" var="pubsub#dataform_xslt"/>
|
||||
</x>
|
||||
</configure>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
|
||||
pubsub, err := getPubSubOwnerPayload(response)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
if pubsub.OwnerUseCase == nil {
|
||||
t.Fatalf("owner use case is nil")
|
||||
}
|
||||
|
||||
ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner)
|
||||
if !ok {
|
||||
t.Fatalf("owner use case is not a configure tag")
|
||||
}
|
||||
|
||||
if ownrUsecase.Form == nil {
|
||||
t.Fatalf("form is nil in the parsed config tag")
|
||||
}
|
||||
|
||||
if len(ownrUsecase.Form.Fields) != 8 {
|
||||
t.Fatalf("one or more fields in the response form could not be parsed correctly")
|
||||
}
|
||||
}
|
||||
|
||||
// ***********************
|
||||
// * 8.4 Delete a Node
|
||||
// ***********************
|
||||
|
||||
func TestNewDelNode(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\" id=\"delete1\" to=\"pubsub.shakespeare.lit\" >" +
|
||||
" <pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> " +
|
||||
"<delete node=\"princely_musings\"></delete> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewDelNode("pubsub.shakespeare.lit", "princely_musings")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a node delete request: %v", err)
|
||||
}
|
||||
subR.Id = "delete1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
|
||||
if pubsub.OwnerUseCase == nil {
|
||||
t.Fatalf("owner use case is nil")
|
||||
}
|
||||
|
||||
_, ok = pubsub.OwnerUseCase.(*stanza.DeleteOwner)
|
||||
if !ok {
|
||||
t.Fatalf("owner use case is not a delete tag")
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDelNodeResp(t *testing.T) {
|
||||
response := `
|
||||
<iq id="delete1" to="pubsub.shakespeare.lit" type="set">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
|
||||
<delete node="princely_musings">
|
||||
<redirect uri="xmpp:hamlet@denmark.lit"/>
|
||||
</delete>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
|
||||
pubsub, err := getPubSubOwnerPayload(response)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
if pubsub.OwnerUseCase == nil {
|
||||
t.Fatalf("owner use case is nil")
|
||||
}
|
||||
|
||||
ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.DeleteOwner)
|
||||
if !ok {
|
||||
t.Fatalf("owner use case is not a configure tag")
|
||||
}
|
||||
|
||||
if ownrUsecase.RedirectOwner == nil {
|
||||
t.Fatalf("redirect is nil in the delete tag")
|
||||
}
|
||||
|
||||
if ownrUsecase.RedirectOwner.URI == "" {
|
||||
t.Fatalf("could not parse redirect uri")
|
||||
}
|
||||
}
|
||||
|
||||
// ****************************
|
||||
// * 8.5 Purge All Node Items
|
||||
// ****************************
|
||||
|
||||
func TestNewPurgeAllItems(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\" id=\"purge1\" to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> " +
|
||||
"<purge node=\"princely_musings\"></purge> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewPurgeAllItems("pubsub.shakespeare.lit", "princely_musings")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a purge all items request: %v", err)
|
||||
}
|
||||
subR.Id = "purge1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
|
||||
if pubsub.OwnerUseCase == nil {
|
||||
t.Fatalf("owner use case is nil")
|
||||
}
|
||||
|
||||
purge, ok := pubsub.OwnerUseCase.(*stanza.PurgeOwner)
|
||||
if !ok {
|
||||
t.Fatalf("owner use case is not a delete tag")
|
||||
}
|
||||
|
||||
if purge.Node == "" {
|
||||
t.Fatalf("could not parse purge targer node")
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// ************************************
|
||||
// * 8.6 Manage Subscription Requests
|
||||
// ************************************
|
||||
func TestNewApproveSubRequest(t *testing.T) {
|
||||
expectedReq := "<message id=\"approve1\" to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<x xmlns=\"jabber:x:data\" type=\"submit\"> <field var=\"FORM_TYPE\" type=\"hidden\"> " +
|
||||
"<value>http://jabber.org/protocol/pubsub#subscribe_authorization</value> </field> <field var=\"pubsub#subid\">" +
|
||||
" <value>123-abc</value> </field> <field var=\"pubsub#node\"> <value>princely_musings</value> </field> " +
|
||||
"<field var=\"pubsub#subscriber_jid\"> <value>horatio@denmark.lit</value> </field> <field var=\"pubsub#allow\"> " +
|
||||
"<value>true</value> </field> </x> </message>"
|
||||
|
||||
apprForm := &stanza.Form{
|
||||
Type: stanza.FormTypeSubmit,
|
||||
Fields: []*stanza.Field{
|
||||
{Var: "FORM_TYPE", Type: stanza.FieldTypeHidden, ValuesList: []string{"http://jabber.org/protocol/pubsub#subscribe_authorization"}},
|
||||
{Var: "pubsub#subid", ValuesList: []string{"123-abc"}},
|
||||
{Var: "pubsub#node", ValuesList: []string{"princely_musings"}},
|
||||
{Var: "pubsub#subscriber_jid", ValuesList: []string{"horatio@denmark.lit"}},
|
||||
{Var: "pubsub#allow", ValuesList: []string{"true"}},
|
||||
},
|
||||
}
|
||||
|
||||
subR, err := stanza.NewApproveSubRequest("pubsub.shakespeare.lit", "approve1", apprForm)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a sub approval request: %v", err)
|
||||
}
|
||||
subR.Id = "approve1"
|
||||
|
||||
frm, ok := subR.Extensions[0].(*stanza.Form)
|
||||
if !ok {
|
||||
t.Fatalf("extension is not a from !")
|
||||
}
|
||||
|
||||
var allowField *stanza.Field
|
||||
|
||||
for _, f := range frm.Fields {
|
||||
if f.Var == "pubsub#allow" {
|
||||
allowField = f
|
||||
}
|
||||
}
|
||||
if allowField == nil || allowField.ValuesList[0] != "true" {
|
||||
t.Fatalf("could not correctly parse the allow field in the response from")
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// ********************************************
|
||||
// * 8.7 Process Pending Subscription Requests
|
||||
// ********************************************
|
||||
|
||||
func TestNewGetPendingSubRequests(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\" id=\"pending1\" to=\"pubsub.shakespeare.lit\" > " +
|
||||
"<command xmlns=\"http://jabber.org/protocol/commands\" action=\"execute\" node=\"http://jabber.org/protocol/pubsub#get-pending\" >" +
|
||||
"</command> </iq>"
|
||||
|
||||
subR, err := stanza.NewGetPendingSubRequests("pubsub.shakespeare.lit")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a get pending subs request: %v", err)
|
||||
}
|
||||
subR.Id = "pending1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
command, ok := subR.Payload.(*stanza.Command)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a command !")
|
||||
}
|
||||
|
||||
if command.Action != stanza.CommandActionExecute {
|
||||
t.Fatalf("command should be execute !")
|
||||
}
|
||||
|
||||
if command.Node != "http://jabber.org/protocol/pubsub#get-pending" {
|
||||
t.Fatalf("command node should be http://jabber.org/protocol/pubsub#get-pending !")
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewGetPendingSubRequestsResp(t *testing.T) {
|
||||
response := `
|
||||
<iq from="pubsub.shakespeare.lit" id="pending1" to="hamlet@denmark.lit/elsinore" type="result">
|
||||
<command action="execute" node="http://jabber.org/protocol/pubsub#get-pending" sessionid="pubsub-get-pending:20031021T150901Z-600" status="executing" xmlns="http://jabber.org/protocol/commands">
|
||||
<x type="form" xmlns="jabber:x:data">
|
||||
<field type="hidden" var="FORM_TYPE">
|
||||
<value>http://jabber.org/protocol/pubsub#subscribe_authorization</value>
|
||||
</field>
|
||||
<field type="list-single" var="pubsub#node">
|
||||
<option>
|
||||
<value>princely_musings</value>
|
||||
</option>
|
||||
<option>
|
||||
<value>news_from_elsinore</value>
|
||||
</option>
|
||||
</field>
|
||||
</x>
|
||||
</command>
|
||||
</iq>
|
||||
`
|
||||
|
||||
var respIQ stanza.IQ
|
||||
err := xml.Unmarshal([]byte(response), &respIQ)
|
||||
if err != nil {
|
||||
t.Fatalf("could not parse iq")
|
||||
}
|
||||
|
||||
_, ok := respIQ.Payload.(*stanza.Command)
|
||||
if !ok {
|
||||
t.Fatal("this iq payload is not a command")
|
||||
}
|
||||
|
||||
fMap, err := respIQ.GetFormFields()
|
||||
if err != nil || len(fMap) != 2 {
|
||||
t.Fatal("could not parse command form fields")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ********************************************
|
||||
// * 8.7 Process Pending Subscription Requests
|
||||
// ********************************************
|
||||
|
||||
func TestNewApprovePendingSubRequest(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\" id=\"pending2\" to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<command xmlns=\"http://jabber.org/protocol/commands\" action=\"execute\"" +
|
||||
"node=\"http://jabber.org/protocol/pubsub#get-pending\"sessionid=\"pubsub-get-pending:20031021T150901Z-600\"> " +
|
||||
"<x xmlns=\"jabber:x:data\" type=\"submit\"> <field xmlns=\"jabber:x:data\" var=\"pubsub#node\"> " +
|
||||
"<value xmlns=\"jabber:x:data\">princely_musings</value> </field> </x> </command> </iq>"
|
||||
|
||||
subR, err := stanza.NewApprovePendingSubRequest("pubsub.shakespeare.lit",
|
||||
"pubsub-get-pending:20031021T150901Z-600",
|
||||
"princely_musings")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a approve pending sub request: %v", err)
|
||||
}
|
||||
subR.Id = "pending2"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
command, ok := subR.Payload.(*stanza.Command)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a command !")
|
||||
}
|
||||
|
||||
if command.Action != stanza.CommandActionExecute {
|
||||
t.Fatalf("command should be execute !")
|
||||
}
|
||||
|
||||
//if command.Node != "http://jabber.org/protocol/pubsub#get-pending"{
|
||||
// t.Fatalf("command node should be http://jabber.org/protocol/pubsub#get-pending !")
|
||||
//}
|
||||
//
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// ********************************************
|
||||
// * 8.8.1 Retrieve Subscriptions List
|
||||
// ********************************************
|
||||
|
||||
func TestNewSubListRqPl(t *testing.T) {
|
||||
expectedReq := "<iq type=\"get\" id=\"subman1\" to=\"pubsub.shakespeare.lit\" > " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> " +
|
||||
"<subscriptions node=\"princely_musings\"></subscriptions> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewSubListRqPl("pubsub.shakespeare.lit", "princely_musings")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a sub list request: %v", err)
|
||||
}
|
||||
subR.Id = "subman1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub in namespace owner !")
|
||||
}
|
||||
|
||||
subs, ok := pubsub.OwnerUseCase.(*stanza.SubscriptionsOwner)
|
||||
if !ok {
|
||||
t.Fatalf("pubsub doesn not contain a subscriptions node !")
|
||||
}
|
||||
|
||||
if subs.Node != "princely_musings" {
|
||||
t.Fatalf("subs node attribute should be princely_musings. Found %s", subs.Node)
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSubListRqPlResp(t *testing.T) {
|
||||
response := `
|
||||
<iq from="pubsub.shakespeare.lit" id="subman1" to="hamlet@denmark.lit/elsinore" type="result">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
|
||||
<subscriptions node="princely_musings">
|
||||
<subscription jid="hamlet@denmark.lit" subscription="subscribed"></subscription>
|
||||
<subscription jid="polonius@denmark.lit" subscription="unconfigured"></subscription>
|
||||
<subscription jid="bernardo@denmark.lit" subid="123-abc" subscription="subscribed"></subscription>
|
||||
<subscription jid="bernardo@denmark.lit" subid="004-yyy" subscription="subscribed"></subscription>
|
||||
</subscriptions>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
|
||||
var respIQ stanza.IQ
|
||||
err := xml.Unmarshal([]byte(response), &respIQ)
|
||||
if err != nil {
|
||||
t.Fatalf("could not parse iq")
|
||||
}
|
||||
|
||||
pubsub, ok := respIQ.Payload.(*stanza.PubSubOwner)
|
||||
if !ok {
|
||||
t.Fatal("this iq payload is not a command")
|
||||
}
|
||||
|
||||
subs, ok := pubsub.OwnerUseCase.(*stanza.SubscriptionsOwner)
|
||||
if !ok {
|
||||
t.Fatalf("pubsub doesn not contain a subscriptions node !")
|
||||
}
|
||||
|
||||
if len(subs.Subscriptions) != 4 {
|
||||
t.Fatalf("expected to find 4 subscriptions but got %d", len(subs.Subscriptions))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ********************************************
|
||||
// * 8.9.1 Retrieve Affiliations List
|
||||
// ********************************************
|
||||
|
||||
func TestNewAffiliationListRequest(t *testing.T) {
|
||||
expectedReq := "<iq type=\"get\" id=\"ent1\" to=\"pubsub.shakespeare.lit\" > " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> " +
|
||||
"<affiliations node=\"princely_musings\"></affiliations> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewAffiliationListRequest("pubsub.shakespeare.lit", "princely_musings")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create an affiliations list request: %v", err)
|
||||
}
|
||||
subR.Id = "ent1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub in namespace owner !")
|
||||
}
|
||||
|
||||
affils, ok := pubsub.OwnerUseCase.(*stanza.AffiliationsOwner)
|
||||
if !ok {
|
||||
t.Fatalf("pubsub doesn not contain an affiliations node !")
|
||||
}
|
||||
|
||||
if affils.Node != "princely_musings" {
|
||||
t.Fatalf("affils node attribute should be princely_musings. Found %s", affils.Node)
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAffiliationListRequestResp(t *testing.T) {
|
||||
response := `
|
||||
<iq from="pubsub.shakespeare.lit" id="ent1" to="hamlet@denmark.lit/elsinore" type="result">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
|
||||
<affiliations node="princely_musings">
|
||||
<affiliation affiliation="owner" jid="hamlet@denmark.lit"/>
|
||||
<affiliation affiliation="outcast" jid="polonius@denmark.lit"/>
|
||||
</affiliations>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
|
||||
var respIQ stanza.IQ
|
||||
err := xml.Unmarshal([]byte(response), &respIQ)
|
||||
if err != nil {
|
||||
t.Fatalf("could not parse iq")
|
||||
}
|
||||
|
||||
pubsub, ok := respIQ.Payload.(*stanza.PubSubOwner)
|
||||
if !ok {
|
||||
t.Fatal("this iq payload is not a command")
|
||||
}
|
||||
|
||||
affils, ok := pubsub.OwnerUseCase.(*stanza.AffiliationsOwner)
|
||||
if !ok {
|
||||
t.Fatalf("pubsub doesn not contain an affiliations node !")
|
||||
}
|
||||
|
||||
if len(affils.Affiliations) != 2 {
|
||||
t.Fatalf("expected to find 2 subscriptions but got %d", len(affils.Affiliations))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ********************************************
|
||||
// * 8.9.2 Modify Affiliation
|
||||
// ********************************************
|
||||
|
||||
func TestNewModifAffiliationRequest(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\" id=\"ent3\" to=\"pubsub.shakespeare.lit\" > " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> <affiliations node=\"princely_musings\"> " +
|
||||
"<affiliation affiliation=\"none\" jid=\"hamlet@denmark.lit\"></affiliation> " +
|
||||
"<affiliation affiliation=\"none\" jid=\"polonius@denmark.lit\"></affiliation> " +
|
||||
"<affiliation affiliation=\"publisher\" jid=\"bard@shakespeare.lit\"></affiliation> </affiliations> </pubsub> " +
|
||||
"</iq>"
|
||||
|
||||
affils := []stanza.AffiliationOwner{
|
||||
{
|
||||
AffiliationStatus: stanza.AffiliationStatusNone,
|
||||
Jid: "hamlet@denmark.lit",
|
||||
},
|
||||
{
|
||||
AffiliationStatus: stanza.AffiliationStatusNone,
|
||||
Jid: "polonius@denmark.lit",
|
||||
},
|
||||
{
|
||||
AffiliationStatus: stanza.AffiliationStatusPublisher,
|
||||
Jid: "bard@shakespeare.lit",
|
||||
},
|
||||
}
|
||||
|
||||
subR, err := stanza.NewModifAffiliationRequest("pubsub.shakespeare.lit", "princely_musings", affils)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a modif affiliation request: %v", err)
|
||||
}
|
||||
subR.Id = "ent3"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub in namespace owner !")
|
||||
}
|
||||
|
||||
as, ok := pubsub.OwnerUseCase.(*stanza.AffiliationsOwner)
|
||||
if !ok {
|
||||
t.Fatalf("pubsub doesn not contain an affiliations node !")
|
||||
}
|
||||
|
||||
if as.Node != "princely_musings" {
|
||||
t.Fatalf("affils node attribute should be princely_musings. Found %s", as.Node)
|
||||
}
|
||||
if len(as.Affiliations) != 3 {
|
||||
t.Fatalf("expected 3 affiliations, found %d", len(as.Affiliations))
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFormFields(t *testing.T) {
|
||||
response := `
|
||||
<iq from="pubsub.shakespeare.lit" id="config1" to="hamlet@denmark.lit/elsinore" type="result">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
|
||||
<configure node="princely_musings">
|
||||
<x type="form" xmlns="jabber:x:data">
|
||||
<field type="hidden" var="FORM_TYPE">
|
||||
<value>http://jabber.org/protocol/pubsub#node_config</value>
|
||||
</field>
|
||||
<field label="Purge all items when the relevant publisher goes offline?" type="boolean" var="pubsub#purge_offline">
|
||||
<value>0</value>
|
||||
</field>
|
||||
<field label="Max Payload size in bytes" type="text-single" var="pubsub#max_payload_size">
|
||||
<value>1028</value>
|
||||
</field>
|
||||
<field label="When to send the last published item" type="list-single" var="pubsub#send_last_published_item">
|
||||
<option label="Never">
|
||||
<value>never</value>
|
||||
</option>
|
||||
<option label="When a new subscription is processed">
|
||||
<value>on_sub</value>
|
||||
</option>
|
||||
<option label="When a new subscription is processed and whenever a subscriber comes online">
|
||||
<value>on_sub_and_presence</value>
|
||||
</option>
|
||||
<value>never</value>
|
||||
</field>
|
||||
<field label="Deliver event notifications only to available users" type="boolean" var="pubsub#presence_based_delivery">
|
||||
<value>0</value>
|
||||
</field>
|
||||
<field label="Specify the delivery style for event notifications" type="list-single" var="pubsub#notification_type">
|
||||
<option>
|
||||
<value>normal</value>
|
||||
</option>
|
||||
<option>
|
||||
<value>headline</value>
|
||||
</option>
|
||||
<value>headline</value>
|
||||
</field>
|
||||
<field label="Specify the type of payload data to be provided at this node" type="text-single" var="pubsub#type">
|
||||
<value>http://www.w3.org/2005/Atom</value>
|
||||
</field>
|
||||
<field label="Payload XSLT" type="text-single" var="pubsub#dataform_xslt"/>
|
||||
</x>
|
||||
</configure>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
var iq stanza.IQ
|
||||
err := xml.Unmarshal([]byte(response), &iq)
|
||||
if err != nil {
|
||||
t.Fatalf("could not parse IQ")
|
||||
}
|
||||
|
||||
fields, err := iq.GetFormFields()
|
||||
if len(fields) != 8 {
|
||||
t.Fatalf("could not correctly parse fields. Expected 8, found : %v", len(fields))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestGetFormFieldsCmd(t *testing.T) {
|
||||
response := `
|
||||
<iq from="pubsub.shakespeare.lit" id="pending1" to="hamlet@denmark.lit/elsinore" type="result">
|
||||
<command action="execute" node="http://jabber.org/protocol/pubsub#get-pending" sessionid="pubsub-get-pending:20031021T150901Z-600" status="executing" xmlns="http://jabber.org/protocol/commands">
|
||||
<x type="form" xmlns="jabber:x:data">
|
||||
<field type="hidden" var="FORM_TYPE">
|
||||
<value>http://jabber.org/protocol/pubsub#subscribe_authorization</value>
|
||||
</field>
|
||||
<field type="list-single" var="pubsub#node">
|
||||
<option>
|
||||
<value>princely_musings</value>
|
||||
</option>
|
||||
<option>
|
||||
<value>news_from_elsinore</value>
|
||||
</option>
|
||||
</field>
|
||||
</x>
|
||||
</command>
|
||||
</iq>
|
||||
`
|
||||
var iq stanza.IQ
|
||||
err := xml.Unmarshal([]byte(response), &iq)
|
||||
if err != nil {
|
||||
t.Fatalf("could not parse IQ")
|
||||
}
|
||||
|
||||
fields, err := iq.GetFormFields()
|
||||
if len(fields) != 2 {
|
||||
t.Fatalf("could not correctly parse fields. Expected 2, found : %v", len(fields))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNewFormSubmissionOwner(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\" id=\"config2\" to=\"pubsub.shakespeare.lit\">" +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> <configure node=\"princely_musings\"> " +
|
||||
"<x xmlns=\"jabber:x:data\" type=\"submit\" > <field var=\"FORM_TYPE\" type=\"hidden\"> " +
|
||||
"<value>http://jabber.org/protocol/pubsub#node_config</value> </field> <field var=\"pubsub#item_expire\"> " +
|
||||
"<value>604800</value> </field> <field var=\"pubsub#access_model\"> <value>roster</value> </field> " +
|
||||
"<field var=\"pubsub#roster_groups_allowed\"> <value>friends</value> <value>servants</value> " +
|
||||
"<value>courtiers</value> </field> </x> </configure> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewFormSubmissionOwner("pubsub.shakespeare.lit",
|
||||
"princely_musings",
|
||||
[]*stanza.Field{
|
||||
{Var: "FORM_TYPE", Type: stanza.FieldTypeHidden, ValuesList: []string{"http://jabber.org/protocol/pubsub#node_config"}},
|
||||
{Var: "pubsub#item_expire", ValuesList: []string{"604800"}},
|
||||
{Var: "pubsub#access_model", ValuesList: []string{"roster"}},
|
||||
{Var: "pubsub#roster_groups_allowed", ValuesList: []string{"friends", "servants", "courtiers"}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a form submission request: %v", err)
|
||||
}
|
||||
subR.Id = "config2"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub in namespace owner !")
|
||||
}
|
||||
|
||||
conf, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner)
|
||||
if !ok {
|
||||
t.Fatalf("pubsub does not contain a configure node !")
|
||||
}
|
||||
|
||||
if conf.Form == nil {
|
||||
t.Fatalf("the form is absent from the configuration submission !")
|
||||
}
|
||||
if len(conf.Form.Fields) != 4 {
|
||||
t.Fatalf("expected 4 fields, found %d", len(conf.Form.Fields))
|
||||
}
|
||||
if len(conf.Form.Fields[3].ValuesList) != 3 {
|
||||
t.Fatalf("expected 3 values in fourth field, found %d", len(conf.Form.Fields[3].ValuesList))
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func getPubSubOwnerPayload(response string) (*stanza.PubSubOwner, error) {
|
||||
var respIQ stanza.IQ
|
||||
err := xml.Unmarshal([]byte(response), &respIQ)
|
||||
|
||||
if err != nil {
|
||||
return &stanza.PubSubOwner{}, err
|
||||
}
|
||||
|
||||
pubsub, ok := respIQ.Payload.(*stanza.PubSubOwner)
|
||||
if !ok {
|
||||
return nil, errors.New("this iq payload is not a pubsub of the owner namespace")
|
||||
}
|
||||
|
||||
return pubsub, nil
|
||||
}
|
@ -0,0 +1,922 @@
|
||||
package stanza_test
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var submitFormExample = stanza.NewForm([]*stanza.Field{
|
||||
{Var: "FORM_TYPE", Type: stanza.FieldTypeHidden, ValuesList: []string{"http://jabber.org/protocol/pubsub#node_config"}},
|
||||
{Var: "pubsub#title", ValuesList: []string{"Princely Musings (Atom)"}},
|
||||
{Var: "pubsub#deliver_notifications", ValuesList: []string{"1"}},
|
||||
{Var: "pubsub#access_model", ValuesList: []string{"roster"}},
|
||||
{Var: "pubsub#roster_groups_allowed", ValuesList: []string{"friends", "servants", "courtiers"}},
|
||||
{Var: "pubsub#type", ValuesList: []string{"http://www.w3.org/2005/Atom"}},
|
||||
{
|
||||
Var: "pubsub#notification_type",
|
||||
Type: "list-single",
|
||||
Label: "Specify the delivery style for event notifications",
|
||||
ValuesList: []string{"headline"},
|
||||
Options: []stanza.Option{
|
||||
{ValuesList: []string{"normal"}},
|
||||
{ValuesList: []string{"headline"}},
|
||||
},
|
||||
},
|
||||
}, stanza.FormTypeSubmit)
|
||||
|
||||
// ***********************************
|
||||
// * 6.1 Subscribe to a Node
|
||||
// ***********************************
|
||||
|
||||
func TestNewSubRequest(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\"id=\"sub1\"to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <subscribe node=\"princely_musings\"jid=\"francisco@denmark.lit\"></subscribe>" +
|
||||
" </pubsub> </iq>"
|
||||
|
||||
subInfo := stanza.SubInfo{
|
||||
Node: "princely_musings", Jid: "francisco@denmark.lit",
|
||||
}
|
||||
subR, err := stanza.NewSubRq("pubsub.shakespeare.lit", subInfo)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a sub request: %v", err)
|
||||
}
|
||||
subR.Id = "sub1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNewSubResp(t *testing.T) {
|
||||
response := `
|
||||
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit/barracks" id="sub1">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub">
|
||||
<subscription node="princely_musings" jid="francisco@denmark.lit"
|
||||
subid="ba49252aaa4f5d320c24d3766f0bdcade78c78d3" subscription="subscribed"/>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
|
||||
pubsub, err := getPubSubGenericPayload(response)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if pubsub.Subscription == nil {
|
||||
t.Fatalf("subscription node is nil")
|
||||
}
|
||||
if pubsub.Subscription.Node == "" ||
|
||||
pubsub.Subscription.Jid == "" ||
|
||||
pubsub.Subscription.SubId == nil ||
|
||||
pubsub.Subscription.SubStatus == "" {
|
||||
t.Fatalf("one or more of the subscription attributes was not successfully decoded")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ***********************************
|
||||
// * 6.2 Unsubscribe from a Node
|
||||
// ***********************************
|
||||
|
||||
func TestNewUnSubRequest(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\"id=\"unsub1\"to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> " +
|
||||
"<unsubscribe node=\"princely_musings\"jid=\"francisco@denmark.lit\"></unsubscribe> </pubsub> </iq>"
|
||||
|
||||
subInfo := stanza.SubInfo{
|
||||
Node: "princely_musings", Jid: "francisco@denmark.lit",
|
||||
}
|
||||
subR, err := stanza.NewUnsubRq("pubsub.shakespeare.lit", subInfo)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create an unsub request: %v", err)
|
||||
}
|
||||
subR.Id = "unsub1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
if pubsub.Unsubscribe == nil {
|
||||
t.Fatalf("Unsubscribe tag should be present in sub config options request")
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewUnsubResp(t *testing.T) {
|
||||
response := `
|
||||
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit/barracks" id="unsub1">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub">
|
||||
<subscription node="princely_musings" jid="francisco@denmark.lit" subscription="none"
|
||||
subid="ba49252aaa4f5d320c24d3766f0bdcade78c78d3"/>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
|
||||
pubsub, err := getPubSubGenericPayload(response)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if pubsub.Subscription == nil {
|
||||
t.Fatalf("subscription node is nil")
|
||||
}
|
||||
if pubsub.Subscription.Node == "" ||
|
||||
pubsub.Subscription.Jid == "" ||
|
||||
pubsub.Subscription.SubId == nil ||
|
||||
pubsub.Subscription.SubStatus == "" {
|
||||
t.Fatalf("one or more of the subscription attributes was not successfully decoded")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ***************************************
|
||||
// * 6.3 Configure Subscription Options
|
||||
// ***************************************
|
||||
func TestNewSubOptsRq(t *testing.T) {
|
||||
expectedReq := "<iq type=\"get\"id=\"options1\"to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> " +
|
||||
"<options node=\"princely_musings\" jid=\"francisco@denmark.lit\"></options> </pubsub> </iq>"
|
||||
|
||||
subInfo := stanza.SubInfo{
|
||||
Node: "princely_musings", Jid: "francisco@denmark.lit",
|
||||
}
|
||||
subR, err := stanza.NewSubOptsRq("pubsub.shakespeare.lit", subInfo)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a sub options request: %v", err)
|
||||
}
|
||||
subR.Id = "options1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
if pubsub.SubOptions == nil {
|
||||
t.Fatalf("Options tag should be present in sub config options request")
|
||||
}
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewNewConfOptsRsp(t *testing.T) {
|
||||
response := `
|
||||
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit/barracks" id="options1">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub">
|
||||
<options node="princely_musings" jid="francisco@denmark.lit">
|
||||
<x xmlns="jabber:x:data" type="form">
|
||||
<field var="FORM_TYPE" type="hidden">
|
||||
<value>http://jabber.org/protocol/pubsub#subscribe_options</value>
|
||||
</field>
|
||||
<field var="pubsub#deliver" type="boolean" label="Enable delivery?">
|
||||
<value>1</value>
|
||||
</field>
|
||||
<field var="pubsub#digest" type="boolean"
|
||||
label="Receive digest notifications (approx. one per day)?">
|
||||
<value>0</value>
|
||||
</field>
|
||||
<field var="pubsub#include_body" type="boolean"
|
||||
label="Receive message body in addition to payload?">
|
||||
<value>false</value>
|
||||
</field>
|
||||
<field var="pubsub#show-values" type="list-multi"
|
||||
label="Select the presence types which are
|
||||
allowed to receive event notifications">
|
||||
<option label="Want to Chat">
|
||||
<value>chat</value>
|
||||
</option>
|
||||
<option label="Available">
|
||||
<value>online</value>
|
||||
</option>
|
||||
<option label="Away">
|
||||
<value>away</value>
|
||||
</option>
|
||||
<option label="Extended Away">
|
||||
<value>xa</value>
|
||||
</option>
|
||||
<option label="Do Not Disturb">
|
||||
<value>dnd</value>
|
||||
</option>
|
||||
<value>chat</value>
|
||||
<value>online</value>
|
||||
</field>
|
||||
</x>
|
||||
</options>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
|
||||
pubsub, err := getPubSubGenericPayload(response)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
if pubsub.SubOptions == nil {
|
||||
t.Fatalf("sub options node is nil")
|
||||
}
|
||||
if pubsub.SubOptions.Form == nil {
|
||||
t.Fatalf("the response form is nil")
|
||||
}
|
||||
|
||||
if len(pubsub.SubOptions.Form.Fields) != 5 {
|
||||
t.Fatalf("one or more fields in the response form could not be parsed correctly")
|
||||
}
|
||||
}
|
||||
|
||||
// ***************************************
|
||||
// * 6.3.5 Form Submission
|
||||
// ***************************************
|
||||
func TestNewFormSubmission(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\" id=\"options2\" to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <options node=\"princely_musings\" jid=\"francisco@denmark.lit\"> " +
|
||||
"<x xmlns=\"jabber:x:data\" type=\"submit\"> <field var=\"FORM_TYPE\" type=\"hidden\">" +
|
||||
" <value>http://jabber.org/protocol/pubsub#node_config</value> </field> <field var=\"pubsub#title\"> " +
|
||||
"<value>Princely Musings (Atom)</value> </field> <field var=\"pubsub#deliver_notifications\"> " +
|
||||
"<value>1</value> </field> <field var=\"pubsub#access_model\"> <value>roster</value> </field> " +
|
||||
"<field var=\"pubsub#roster_groups_allowed\"> <value>friends</value> <value>servants</value>" +
|
||||
" <value>courtiers</value> </field> <field var=\"pubsub#type\"> <value>http://www.w3.org/2005/Atom</value> " +
|
||||
"</field> <field var=\"pubsub#notification_type\" type=\"list-single\"label=\"Specify the delivery style for event notifications\"> " +
|
||||
"<value>headline</value> <option> <value>normal</value> </option> <option> <value>headline</value> </option> " +
|
||||
"</field> </x> </options> </pubsub> </iq>"
|
||||
|
||||
subInfo := stanza.SubInfo{
|
||||
Node: "princely_musings", Jid: "francisco@denmark.lit",
|
||||
}
|
||||
|
||||
subR, err := stanza.NewFormSubmission("pubsub.shakespeare.lit", subInfo, submitFormExample)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a form submission request: %v", err)
|
||||
}
|
||||
subR.Id = "options2"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
if pubsub.SubOptions == nil {
|
||||
t.Fatalf("Options tag should be present in sub config options request")
|
||||
}
|
||||
if pubsub.SubOptions.Form == nil {
|
||||
t.Fatalf("No form in form submit request !")
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// ***************************************
|
||||
// * 6.3.7 Subscribe and Configure
|
||||
// ***************************************
|
||||
|
||||
func TestNewSubAndConfig(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\"id=\"sub1\"to=\"pubsub.shakespeare.lit\">" +
|
||||
" <pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <subscribe node=\"princely_musings\" jid=\"francisco@denmark.lit\"> " +
|
||||
"</subscribe>" +
|
||||
"<options> <x xmlns=\"jabber:x:data\" type=\"submit\"> <field var=\"FORM_TYPE\" type=\"hidden\">" +
|
||||
" <value>http://jabber.org/protocol/pubsub#node_config</value> </field> <field var=\"pubsub#title\"> " +
|
||||
"<value>Princely Musings (Atom)</value> </field> <field var=\"pubsub#deliver_notifications\"> " +
|
||||
"<value>1</value> </field> <field var=\"pubsub#access_model\"> <value>roster</value> </field> " +
|
||||
"<field var=\"pubsub#roster_groups_allowed\"> <value>friends</value> <value>servants</value>" +
|
||||
" <value>courtiers</value> </field> <field var=\"pubsub#type\"> <value>http://www.w3.org/2005/Atom</value> " +
|
||||
"</field> <field var=\"pubsub#notification_type\" type=\"list-single\"label=\"Specify the delivery style for event notifications\"> " +
|
||||
"<value>headline</value> <option> <value>normal</value> </option> <option> <value>headline</value> </option> " +
|
||||
"</field> </x> </options> </pubsub> </iq>"
|
||||
|
||||
subInfo := stanza.SubInfo{
|
||||
Node: "princely_musings", Jid: "francisco@denmark.lit",
|
||||
}
|
||||
|
||||
subR, err := stanza.NewSubAndConfig("pubsub.shakespeare.lit", subInfo, submitFormExample)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a sub and config request: %v", err)
|
||||
}
|
||||
subR.Id = "sub1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
if pubsub.SubOptions == nil {
|
||||
t.Fatalf("Options tag should be present in sub config options request")
|
||||
}
|
||||
if pubsub.SubOptions.Form == nil {
|
||||
t.Fatalf("No form in form submit request !")
|
||||
}
|
||||
|
||||
// The <options/> element MUST NOT possess a 'node' attribute or 'jid' attribute
|
||||
// See XEP-0060
|
||||
if pubsub.SubOptions.SubInfo.Node != "" || pubsub.SubOptions.SubInfo.Jid != "" {
|
||||
t.Fatalf("SubInfo node and jid should be empty for the options tag !")
|
||||
}
|
||||
if pubsub.Subscribe.Node == "" || pubsub.Subscribe.Jid == "" {
|
||||
t.Fatalf("SubInfo node and jid should NOT be empty for the subscribe tag !")
|
||||
}
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSubAndConfigResp(t *testing.T) {
|
||||
response := `
|
||||
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit/barracks" id="sub1">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub">
|
||||
<subscription node="princely_musings" jid="francisco@denmark.lit"
|
||||
subid="ba49252aaa4f5d320c24d3766f0bdcade78c78d3" subscription="subscribed"/>
|
||||
<options>
|
||||
<x xmlns="jabber:x:data" type="result">
|
||||
<field var="FORM_TYPE" type="hidden">
|
||||
<value>http://jabber.org/protocol/pubsub#subscribe_options</value>
|
||||
</field>
|
||||
<field var="pubsub#deliver">
|
||||
<value>1</value>
|
||||
</field>
|
||||
<field var="pubsub#digest">
|
||||
<value>0</value>
|
||||
</field>
|
||||
<field var="pubsub#include_body">
|
||||
<value>false</value>
|
||||
</field>
|
||||
<field var="pubsub#show-values">
|
||||
<value>chat</value>
|
||||
<value>online</value>
|
||||
<value>away</value>
|
||||
</field>
|
||||
</x>
|
||||
</options>
|
||||
</pubsub>
|
||||
</iq>
|
||||
|
||||
`
|
||||
|
||||
pubsub, err := getPubSubGenericPayload(response)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
if pubsub.Subscription == nil {
|
||||
t.Fatalf("sub node is nil")
|
||||
}
|
||||
|
||||
if pubsub.SubOptions == nil {
|
||||
t.Fatalf("sub options node is nil")
|
||||
}
|
||||
if pubsub.SubOptions.Form == nil {
|
||||
t.Fatalf("the response form is nil")
|
||||
}
|
||||
|
||||
if len(pubsub.SubOptions.Form.Fields) != 5 {
|
||||
t.Fatalf("one or more fields in the response form could not be parsed correctly")
|
||||
}
|
||||
}
|
||||
|
||||
// ***************************************
|
||||
// * 6.5.2 Requesting All List
|
||||
// ***************************************
|
||||
func TestNewItemsRequest(t *testing.T) {
|
||||
subR, err := stanza.NewItemsRequest("pubsub.shakespeare.lit", "princely_musings", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create an items request : %s", err)
|
||||
}
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
if pubsub.Items == nil {
|
||||
t.Fatalf("List tag should be present to request items from a service")
|
||||
}
|
||||
if len(pubsub.Items.List) != 0 {
|
||||
t.Fatalf("There should be no items in the <items> tag to request all items from a service")
|
||||
}
|
||||
}
|
||||
func TestNewItemsResp(t *testing.T) {
|
||||
response := `
|
||||
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit/barracks" id="items2">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub">
|
||||
<items node="princely_musings">
|
||||
<item id="4e30f35051b7b8b42abe083742187228">
|
||||
<entry xmlns="http://www.w3.org/2005/Atom">
|
||||
<title>Alone</title>
|
||||
<summary> Now I am alone. O, what a rogue and peasant slave am I! </summary>
|
||||
<link rel="alternate" type="text/html"
|
||||
href="http://denmark.lit/2003/12/13/atom03"/>
|
||||
<id>tag:denmark.lit,2003:entry-32396</id>
|
||||
<published>2003-12-13T11:09:53Z</published>
|
||||
<updated>2003-12-13T11:09:53Z</updated>
|
||||
</entry>
|
||||
</item>
|
||||
<item id="ae890ac52d0df67ed7cfdf51b644e901">
|
||||
<entry xmlns="http://www.w3.org/2005/Atom">
|
||||
<title>Soliloquy</title>
|
||||
<summary> To be, or not to be: that is the question: Whether 'tis nobler in the
|
||||
mind to suffer The slings and arrows of outrageous fortune, Or to take arms
|
||||
against a sea of troubles, And by opposing end them? </summary>
|
||||
<link rel="alternate" type="text/html"
|
||||
href="http://denmark.lit/2003/12/13/atom03"/>
|
||||
<id>tag:denmark.lit,2003:entry-32397</id>
|
||||
<published>2003-12-13T18:30:02Z</published>
|
||||
<updated>2003-12-13T18:30:02Z</updated>
|
||||
</entry>
|
||||
</item>
|
||||
</items>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
|
||||
pubsub, err := getPubSubGenericPayload(response)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
if pubsub.Items == nil {
|
||||
t.Fatalf("sub options node is nil")
|
||||
}
|
||||
if pubsub.Items.List == nil {
|
||||
t.Fatalf("the response form is nil")
|
||||
}
|
||||
|
||||
if len(pubsub.Items.List) != 2 {
|
||||
t.Fatalf("one or more items in the response could not be parsed correctly")
|
||||
}
|
||||
}
|
||||
|
||||
// ***************************************
|
||||
// * 6.5.8 Requesting a Particular Item
|
||||
// ***************************************
|
||||
func TestNewSpecificItemRequest(t *testing.T) {
|
||||
expectedReq := "<iq type=\"get\" id=\"items3\"to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <items node=\"princely_musings\"> " +
|
||||
"<item id=\"ae890ac52d0df67ed7cfdf51b644e901\"></item> </items> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewSpecificItemRequest("pubsub.shakespeare.lit", "princely_musings", "ae890ac52d0df67ed7cfdf51b644e901")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a specific item request: %v", err)
|
||||
}
|
||||
subR.Id = "items3"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
if pubsub.Items == nil {
|
||||
t.Fatalf("List tag should be present to request items from a service")
|
||||
}
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// ***************************************
|
||||
// * 7.1 Publish an Item to a Node
|
||||
// ***************************************
|
||||
func TestNewPublishItemRq(t *testing.T) {
|
||||
item := stanza.Item{
|
||||
XMLName: xml.Name{},
|
||||
Id: "",
|
||||
Publisher: "",
|
||||
Any: &stanza.Node{
|
||||
XMLName: xml.Name{
|
||||
Space: "http://www.w3.org/2005/Atom",
|
||||
Local: "entry",
|
||||
},
|
||||
Attrs: nil,
|
||||
Content: "",
|
||||
Nodes: []stanza.Node{
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "title"},
|
||||
Attrs: nil,
|
||||
Content: "My pub item title",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "summary"},
|
||||
Attrs: nil,
|
||||
Content: "My pub item content summary",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "link"},
|
||||
Attrs: []xml.Attr{
|
||||
{
|
||||
Name: xml.Name{Space: "", Local: "rel"},
|
||||
Value: "alternate",
|
||||
},
|
||||
{
|
||||
Name: xml.Name{Space: "", Local: "type"},
|
||||
Value: "text/html",
|
||||
},
|
||||
{
|
||||
Name: xml.Name{Space: "", Local: "href"},
|
||||
Value: "http://denmark.lit/2003/12/13/atom03",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "id"},
|
||||
Attrs: nil,
|
||||
Content: "My pub item content ID",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "published"},
|
||||
Attrs: nil,
|
||||
Content: "2003-12-13T18:30:02Z",
|
||||
Nodes: nil,
|
||||
},
|
||||
{
|
||||
XMLName: xml.Name{Space: "", Local: "updated"},
|
||||
Attrs: nil,
|
||||
Content: "2003-12-13T18:30:02Z",
|
||||
Nodes: nil,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
subR, err := stanza.NewPublishItemRq("pubsub.shakespeare.lit", "princely_musings", "bnd81g37d61f49fgn581", item)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create an item pub request : %s", err)
|
||||
}
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
|
||||
if strings.TrimSpace(pubsub.Publish.Node) == "" {
|
||||
t.Fatalf("the <publish/> element MUST possess a 'node' attribute, specifying the NodeID of the node.")
|
||||
}
|
||||
if pubsub.Publish.Items[0].Id == "" {
|
||||
t.Fatalf("an id was provided for the item and it should be used")
|
||||
}
|
||||
}
|
||||
|
||||
// ***************************************
|
||||
// * 7.1.5 Publishing Options
|
||||
// ***************************************
|
||||
|
||||
func TestNewPublishItemOptsRq(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\"id=\"pub1\"to=\"pubsub.shakespeare.lit\"> <pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> " +
|
||||
"<publish node=\"princely_musings\"> <item id=\"ae890ac52d0df67ed7cfdf51b644e901\"> " +
|
||||
"<entry xmlns=\"http://www.w3.org/2005/Atom\"> <title>Soliloquy</title> " +
|
||||
"<summary> To be, or not to be: that is the question: Whether \"tis nobler in the mind to suffer The " +
|
||||
"slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? " +
|
||||
"</summary> <link rel=\"alternate\" type=\"text/html\"href=\"http://denmark.lit/2003/12/13/atom03\"></link> " +
|
||||
"<id>tag:denmark.lit,2003:entry-32397</id> <published>2003-12-13T18:30:02Z</published> " +
|
||||
"<updated>2003-12-13T18:30:02Z</updated> </entry> </item> </publish> <publish-options> " +
|
||||
"<x xmlns=\"jabber:x:data\" type=\"submit\"> <field var=\"FORM_TYPE\" type=\"hidden\"> " +
|
||||
"<value>http://jabber.org/protocol/pubsub#publish-options</value> </field> <field var=\"pubsub#access_model\"> " +
|
||||
"<value>presence</value> </field> </x> </publish-options> </pubsub> </iq>"
|
||||
|
||||
var iq stanza.IQ
|
||||
err := xml.Unmarshal([]byte(expectedReq), &iq)
|
||||
if err != nil {
|
||||
t.Fatalf("could not unmarshal example request : %s", err)
|
||||
}
|
||||
|
||||
pubsub, ok := iq.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
if pubsub.Publish == nil {
|
||||
t.Fatalf("Publish tag is empty")
|
||||
}
|
||||
if len(pubsub.Publish.Items) != 1 {
|
||||
t.Fatalf("could not parse item properly")
|
||||
}
|
||||
}
|
||||
|
||||
// ***************************************
|
||||
// * 7.2 Delete an Item from a Node
|
||||
// ***************************************
|
||||
|
||||
func TestNewDelItemFromNode(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\"id=\"retract1\"to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <retract node=\"princely_musings\"> " +
|
||||
"<item id=\"ae890ac52d0df67ed7cfdf51b644e901\"></item> </retract> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewDelItemFromNode("pubsub.shakespeare.lit", "princely_musings", "ae890ac52d0df67ed7cfdf51b644e901", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a delete item from node request: %v", err)
|
||||
}
|
||||
subR.Id = "retract1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
if pubsub.Retract == nil {
|
||||
t.Fatalf("Retract tag should be present to del an item from a service")
|
||||
}
|
||||
|
||||
if strings.TrimSpace(pubsub.Retract.Items[0].Id) == "" {
|
||||
t.Fatalf("Item id, for the item to delete, should be non empty")
|
||||
}
|
||||
if pubsub.Retract.Items[0].Any != nil {
|
||||
t.Fatalf("Item node must be empty")
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// ***************************************
|
||||
// * 8.1 Create a Node
|
||||
// ***************************************
|
||||
|
||||
func TestNewCreateNode(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\"id=\"create1\"to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <create node=\"princely_musings\"></create> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewCreateNode("pubsub.shakespeare.lit", "princely_musings")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a create node request: %v", err)
|
||||
}
|
||||
subR.Id = "create1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
if pubsub.Create == nil {
|
||||
t.Fatalf("Create tag should be present to create a node on a service")
|
||||
}
|
||||
|
||||
if strings.TrimSpace(pubsub.Create.Node) == "" {
|
||||
t.Fatalf("Expected node name to be present")
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewCreateNodeResp(t *testing.T) {
|
||||
response := `
|
||||
<iq type="result" from="pubsub.shakespeare.lit" to="hamlet@denmark.lit/elsinore" id="create2">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub">
|
||||
<create node="25e3d37dabbab9541f7523321421edc5bfeb2dae"/>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
pubsub, err := getPubSubGenericPayload(response)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
if pubsub.Create == nil {
|
||||
t.Fatalf("create segment is nil")
|
||||
}
|
||||
if pubsub.Create.Node == "" {
|
||||
t.Fatalf("could not parse generated nodeId")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ***************************************
|
||||
// * 8.1.3 Create and Configure a Node
|
||||
// ***************************************
|
||||
|
||||
func TestNewCreateAndConfigNode(t *testing.T) {
|
||||
expectedReq := "<iq type=\"set\" id=\"create1\" to=\"pubsub.shakespeare.lit\" > " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <create node=\"princely_musings\"></create> " +
|
||||
"<configure> <x xmlns=\"jabber:x:data\" type=\"submit\"> <field var=\"FORM_TYPE\" type=\"hidden\" > " +
|
||||
"<value>http://jabber.org/protocol/pubsub#node_config</value> </field> <field var=\"pubsub#notify_retract\"> " +
|
||||
"<value>0</value> </field> <field var=\"pubsub#notify_sub\"> <value>0</value> </field> " +
|
||||
"<field var=\"pubsub#max_payload_size\"> <value>1028</value> </field> </x> </configure> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewCreateAndConfigNode("pubsub.shakespeare.lit",
|
||||
"princely_musings",
|
||||
&stanza.Form{
|
||||
Type: stanza.FormTypeSubmit,
|
||||
Fields: []*stanza.Field{
|
||||
{Var: "FORM_TYPE", Type: stanza.FieldTypeHidden, ValuesList: []string{"http://jabber.org/protocol/pubsub#node_config"}},
|
||||
{Var: "pubsub#notify_retract", ValuesList: []string{"0"}},
|
||||
{Var: "pubsub#notify_sub", ValuesList: []string{"0"}},
|
||||
{Var: "pubsub#max_payload_size", ValuesList: []string{"1028"}},
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a create and config node request: %v", err)
|
||||
}
|
||||
subR.Id = "create1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
|
||||
}
|
||||
|
||||
pubsub, ok := subR.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("payload is not a pubsub !")
|
||||
}
|
||||
if pubsub.Create == nil {
|
||||
t.Fatalf("Create tag should be present to create a node on a service")
|
||||
}
|
||||
|
||||
if strings.TrimSpace(pubsub.Create.Node) == "" {
|
||||
t.Fatalf("Expected node name to be present")
|
||||
}
|
||||
|
||||
if pubsub.Configure == nil {
|
||||
t.Fatalf("Configure tag should be present to configure a node during its creation on a service")
|
||||
}
|
||||
|
||||
if pubsub.Configure.Form == nil {
|
||||
t.Fatalf("Expected a form to be present, to configure the node")
|
||||
}
|
||||
if len(pubsub.Configure.Form.Fields) != 4 {
|
||||
t.Fatalf("Expected 4 elements to be present in the config form but got : %v", len(pubsub.Configure.Form.Fields))
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expectedReq, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ********************************
|
||||
// * 5.7 Retrieve Subscriptions
|
||||
// ********************************
|
||||
|
||||
func TestNewRetrieveAllSubsRequest(t *testing.T) {
|
||||
expected := "<iq type=\"get\" id=\"subscriptions1\" to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <subscriptions></subscriptions> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewRetrieveAllSubsRequest("pubsub.shakespeare.lit")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a get all subs request: %v", err)
|
||||
}
|
||||
subR.Id = "subscriptions1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expected, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetrieveAllSubsResp(t *testing.T) {
|
||||
response := `
|
||||
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit" id="subscriptions1">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub">
|
||||
<subscriptions>
|
||||
<subscription node="node1" jid="francisco@denmark.lit" subscription="subscribed"/>
|
||||
<subscription node="node2" jid="francisco@denmark.lit" subscription="subscribed"/>
|
||||
<subscription node="node5" jid="francisco@denmark.lit" subscription="unconfigured"/>
|
||||
<subscription node="node6" jid="francisco@denmark.lit" subscription="subscribed"
|
||||
subid="123-abc"/>
|
||||
<subscription node="node6" jid="francisco@denmark.lit" subscription="subscribed"
|
||||
subid="004-yyy"/>
|
||||
</subscriptions>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
var respIQ stanza.IQ
|
||||
err := xml.Unmarshal([]byte(response), &respIQ)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("could not unmarshal response: %s", err)
|
||||
}
|
||||
|
||||
pubsub, ok := respIQ.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("umarshalled payload is not a pubsub")
|
||||
}
|
||||
|
||||
if pubsub.Subscriptions == nil {
|
||||
t.Fatalf("subscriptions node is nil")
|
||||
}
|
||||
if len(pubsub.Subscriptions.List) != 5 {
|
||||
t.Fatalf("incorrect number of decoded subscriptions")
|
||||
}
|
||||
}
|
||||
|
||||
// ********************************
|
||||
// * 5.7 Retrieve Affiliations
|
||||
// ********************************
|
||||
|
||||
func TestNewRetrieveAllAffilsRequest(t *testing.T) {
|
||||
expected := "<iq type=\"get\"id=\"affil1\"to=\"pubsub.shakespeare.lit\"> " +
|
||||
"<pubsub xmlns=\"http://jabber.org/protocol/pubsub\"> <affiliations></affiliations> </pubsub> </iq>"
|
||||
|
||||
subR, err := stanza.NewRetrieveAllAffilsRequest("pubsub.shakespeare.lit")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create a get all affiliations request: %v", err)
|
||||
}
|
||||
subR.Id = "affil1"
|
||||
|
||||
if _, e := checkMarshalling(t, subR); e != nil {
|
||||
t.Fatalf("Failed to check marshalling for generated retreive all affiliations request : %s", e)
|
||||
}
|
||||
|
||||
data, err := xml.Marshal(subR)
|
||||
if err := compareMarshal(expected, string(data)); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetrieveAllAffilsResp(t *testing.T) {
|
||||
response := `
|
||||
<iq type="result" from="pubsub.shakespeare.lit" to="francisco@denmark.lit" id="affil1">
|
||||
<pubsub xmlns="http://jabber.org/protocol/pubsub">
|
||||
<affiliations>
|
||||
<affiliation node="node1" affiliation="owner"/>
|
||||
<affiliation node="node2" affiliation="publisher"/>
|
||||
<affiliation node="node5" affiliation="outcast"/>
|
||||
<affiliation node="node6" affiliation="owner"/>
|
||||
</affiliations>
|
||||
</pubsub>
|
||||
</iq>
|
||||
`
|
||||
var respIQ stanza.IQ
|
||||
err := xml.Unmarshal([]byte(response), &respIQ)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("could not unmarshal response: %s", err)
|
||||
}
|
||||
|
||||
pubsub, ok := respIQ.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
t.Fatalf("umarshalled payload is not a pubsub")
|
||||
}
|
||||
|
||||
if pubsub.Affiliations == nil {
|
||||
t.Fatalf("subscriptions node is nil")
|
||||
}
|
||||
if len(pubsub.Affiliations.List) != 4 {
|
||||
t.Fatalf("incorrect number of decoded subscriptions")
|
||||
}
|
||||
}
|
||||
|
||||
func getPubSubGenericPayload(response string) (*stanza.PubSubGeneric, error) {
|
||||
var respIQ stanza.IQ
|
||||
err := xml.Unmarshal([]byte(response), &respIQ)
|
||||
|
||||
if err != nil {
|
||||
return &stanza.PubSubGeneric{}, err
|
||||
}
|
||||
|
||||
pubsub, ok := respIQ.Payload.(*stanza.PubSubGeneric)
|
||||
if !ok {
|
||||
return nil, errors.New("this iq payload is not a pubsub")
|
||||
}
|
||||
|
||||
return pubsub, nil
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package stanza
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
)
|
||||
|
||||
// Support for XEP-0059
|
||||
// See https://xmpp.org/extensions/xep-0059
|
||||
const (
|
||||
// Common but not only possible namespace for query blocks in a result set context
|
||||
NSQuerySet = "jabber:iq:search"
|
||||
)
|
||||
|
||||
type ResultSet struct {
|
||||
XMLName xml.Name `xml:"http://jabber.org/protocol/rsm set"`
|
||||
After *string `xml:"after,omitempty"`
|
||||
Before *string `xml:"before,omitempty"`
|
||||
Count *int `xml:"count,omitempty"`
|
||||
First *First `xml:"first,omitempty"`
|
||||
Index *int `xml:"index,omitempty"`
|
||||
Last *string `xml:"last,omitempty"`
|
||||
Max *int `xml:"max,omitempty"`
|
||||
}
|
||||
|
||||
type First struct {
|
||||
XMLName xml.Name `xml:"first"`
|
||||
Content string
|
||||
Index *int `xml:"index,attr,omitempty"`
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package stanza_test
|
||||
|
||||
import (
|
||||
"gosrc.io/xmpp/stanza"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Limiting the number of items
|
||||
func TestNewResultSetReq(t *testing.T) {
|
||||
expectedRq := "<iq id=\"q29302\" type=\"set\"> <query xmlns=\"urn:xmpp:mam:2\"> " +
|
||||
"<x type=\"submit\" xmlns=\"jabber:x:data\"> <field type=\"hidden\" var=\"FORM_TYPE\"> " +
|
||||
"<value>urn:xmpp:mam:2</value> </field> <field var=\"start\"> <value>2010-08-07T00:00:00Z</value> </field> </x> " +
|
||||
"<set xmlns=\"http://jabber.org/protocol/rsm\"> <max>10</max> </set> </query> </iq>"
|
||||
|
||||
maxVal := 10
|
||||
rs := &stanza.ResultSet{
|
||||
Max: &maxVal,
|
||||
}
|
||||
|
||||
// TODO when Mam is implemented
|
||||
_ = expectedRq
|
||||
_ = rs
|
||||
}
|
||||
|
||||
func TestUnmarshalResultSeqReq(t *testing.T) {
|
||||
// TODO when Mam is implemented
|
||||
|
||||
}
|
@ -0,0 +1,171 @@
|
||||
package stanza
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
)
|
||||
|
||||
type StanzaErrorGroup interface {
|
||||
GroupErrorName() string
|
||||
}
|
||||
|
||||
type BadFormat struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas bad-format"`
|
||||
}
|
||||
|
||||
func (e *BadFormat) GroupErrorName() string { return "bad-format" }
|
||||
|
||||
type BadNamespacePrefix struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas bad-namespace-prefix"`
|
||||
}
|
||||
|
||||
func (e *BadNamespacePrefix) GroupErrorName() string { return "bad-namespace-prefix" }
|
||||
|
||||
type Conflict struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas conflict"`
|
||||
}
|
||||
|
||||
func (e *Conflict) GroupErrorName() string { return "conflict" }
|
||||
|
||||
type ConnectionTimeout struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas connection-timeout"`
|
||||
}
|
||||
|
||||
func (e *ConnectionTimeout) GroupErrorName() string { return "connection-timeout" }
|
||||
|
||||
type HostGone struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas host-gone"`
|
||||
}
|
||||
|
||||
func (e *HostGone) GroupErrorName() string { return "host-gone" }
|
||||
|
||||
type HostUnknown struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas host-unknown"`
|
||||
}
|
||||
|
||||
func (e *HostUnknown) GroupErrorName() string { return "host-unknown" }
|
||||
|
||||
type ImproperAddressing struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas improper-addressing"`
|
||||
}
|
||||
|
||||
func (e *ImproperAddressing) GroupErrorName() string { return "improper-addressing" }
|
||||
|
||||
type InternalServerError struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas internal-server-error"`
|
||||
}
|
||||
|
||||
func (e *InternalServerError) GroupErrorName() string { return "internal-server-error" }
|
||||
|
||||
type InvalidForm struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas invalid-from"`
|
||||
}
|
||||
|
||||
func (e *InvalidForm) GroupErrorName() string { return "invalid-from" }
|
||||
|
||||
type InvalidId struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas invalid-id"`
|
||||
}
|
||||
|
||||
func (e *InvalidId) GroupErrorName() string { return "invalid-id" }
|
||||
|
||||
type InvalidNamespace struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas invalid-namespace"`
|
||||
}
|
||||
|
||||
func (e *InvalidNamespace) GroupErrorName() string { return "invalid-namespace" }
|
||||
|
||||
type InvalidXML struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas invalid-xml"`
|
||||
}
|
||||
|
||||
func (e *InvalidXML) GroupErrorName() string { return "invalid-xml" }
|
||||
|
||||
type NotAuthorized struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas not-authorized"`
|
||||
}
|
||||
|
||||
func (e *NotAuthorized) GroupErrorName() string { return "not-authorized" }
|
||||
|
||||
type NotWellFormed struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas not-well-formed"`
|
||||
}
|
||||
|
||||
func (e *NotWellFormed) GroupErrorName() string { return "not-well-formed" }
|
||||
|
||||
type PolicyViolation struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas policy-violation"`
|
||||
}
|
||||
|
||||
func (e *PolicyViolation) GroupErrorName() string { return "policy-violation" }
|
||||
|
||||
type RemoteConnectionFailed struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas remote-connection-failed"`
|
||||
}
|
||||
|
||||
func (e *RemoteConnectionFailed) GroupErrorName() string { return "remote-connection-failed" }
|
||||
|
||||
type Reset struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas reset"`
|
||||
}
|
||||
|
||||
func (e *Reset) GroupErrorName() string { return "reset" }
|
||||
|
||||
type ResourceConstraint struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas resource-constraint"`
|
||||
}
|
||||
|
||||
func (e *ResourceConstraint) GroupErrorName() string { return "resource-constraint" }
|
||||
|
||||
type RestrictedXML struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas restricted-xml"`
|
||||
}
|
||||
|
||||
func (e *RestrictedXML) GroupErrorName() string { return "restricted-xml" }
|
||||
|
||||
type SeeOtherHost struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas see-other-host"`
|
||||
}
|
||||
|
||||
func (e *SeeOtherHost) GroupErrorName() string { return "see-other-host" }
|
||||
|
||||
type SystemShutdown struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas system-shutdown"`
|
||||
}
|
||||
|
||||
func (e *SystemShutdown) GroupErrorName() string { return "system-shutdown" }
|
||||
|
||||
type UndefinedCondition struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas undefined-condition"`
|
||||
}
|
||||
|
||||
func (e *UndefinedCondition) GroupErrorName() string { return "undefined-condition" }
|
||||
|
||||
type UnsupportedEncoding struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas unsupported-encoding"`
|
||||
}
|
||||
|
||||
type UnexpectedRequest struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas unexpected-request"`
|
||||
}
|
||||
|
||||
func (e *UnexpectedRequest) GroupErrorName() string { return "unexpected-request" }
|
||||
|
||||
func (e *UnsupportedEncoding) GroupErrorName() string { return "unsupported-encoding" }
|
||||
|
||||
type UnsupportedStanzaType struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas unsupported-stanza-type"`
|
||||
}
|
||||
|
||||
func (e *UnsupportedStanzaType) GroupErrorName() string { return "unsupported-stanza-type" }
|
||||
|
||||
type UnsupportedVersion struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas unsupported-version"`
|
||||
}
|
||||
|
||||
func (e *UnsupportedVersion) GroupErrorName() string { return "unsupported-version" }
|
||||
|
||||
type XMLNotWellFormed struct {
|
||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas xml-not-well-formed"`
|
||||
}
|
||||
|
||||
func (e *XMLNotWellFormed) GroupErrorName() string { return "xml-not-well-formed" }
|
@ -0,0 +1,226 @@
|
||||
package stanza_test
|
||||
|
||||
import (
|
||||
"gosrc.io/xmpp/stanza"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPopEmptyQueue(t *testing.T) {
|
||||
var uaq stanza.UnAckQueue
|
||||
popped := uaq.Pop()
|
||||
if popped != nil {
|
||||
t.Fatalf("queue is empty but something was popped !")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushUnack(t *testing.T) {
|
||||
uaq := initUnAckQueue()
|
||||
toPush := stanza.UnAckedStz{
|
||||
Id: 3,
|
||||
Stz: `<iq type='submit'
|
||||
from='confucius@scholars.lit/home'
|
||||
to='registrar.scholars.lit'
|
||||
id='kj3b157n'
|
||||
xml:lang='en'>
|
||||
<query xmlns='jabber:iq:register'>
|
||||
<username>confucius</username>
|
||||
<first>Qui</first>
|
||||
<last>Kong</last>
|
||||
</query>
|
||||
</iq>`,
|
||||
}
|
||||
|
||||
err := uaq.Push(&toPush)
|
||||
if err != nil {
|
||||
t.Fatalf("could not push element to the queue : %v", err)
|
||||
}
|
||||
|
||||
if len(uaq.Uslice) != 4 {
|
||||
t.Fatalf("push to the non-acked queue failed")
|
||||
}
|
||||
for i := 0; i < 4; i++ {
|
||||
if uaq.Uslice[i].Id != i+1 {
|
||||
t.Fatalf("indexes were not updated correctly. Expected %d got %d", i, uaq.Uslice[i].Id)
|
||||
}
|
||||
}
|
||||
|
||||
// Check that the queue is a fifo : popped element should not be the one we just pushed.
|
||||
popped := uaq.Pop()
|
||||
poppedElt, ok := popped.(*stanza.UnAckedStz)
|
||||
if !ok {
|
||||
t.Fatalf("popped element is not a *stanza.UnAckedStz")
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(*poppedElt, toPush) {
|
||||
t.Fatalf("pushed element is at the top of the fifo queue when it should be at the bottom")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPeekUnack(t *testing.T) {
|
||||
uaq := initUnAckQueue()
|
||||
|
||||
expectedPeek := stanza.UnAckedStz{
|
||||
Id: 1,
|
||||
Stz: `<iq type='set'
|
||||
from='romeo@montague.net/home'
|
||||
to='characters.shakespeare.lit'
|
||||
id='search2'
|
||||
xml:lang='en'>
|
||||
<query xmlns='jabber:iq:search'>
|
||||
<last>Capulet</last>
|
||||
</query>
|
||||
</iq>`,
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(expectedPeek, *uaq.Uslice[0]) {
|
||||
t.Fatalf("peek failed to return the correct stanza")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPeekNUnack(t *testing.T) {
|
||||
uaq := initUnAckQueue()
|
||||
initLen := len(uaq.Uslice)
|
||||
randPop := rand.Int31n(int32(initLen))
|
||||
|
||||
peeked := uaq.PeekN(int(randPop))
|
||||
|
||||
if len(uaq.Uslice) != initLen {
|
||||
t.Fatalf("queue length changed whith peek n operation : had %d found %d after peek", initLen, len(uaq.Uslice))
|
||||
}
|
||||
|
||||
if len(peeked) != int(randPop) {
|
||||
t.Fatalf("did not peek the correct number of element from queue. Expected %d got %d", randPop, len(peeked))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPeekNUnackTooLong(t *testing.T) {
|
||||
uaq := initUnAckQueue()
|
||||
initLen := len(uaq.Uslice)
|
||||
|
||||
// Have a random number of elements to peek that's greater than the queue size
|
||||
randPop := rand.Int31n(int32(initLen)) + 1 + int32(initLen)
|
||||
|
||||
peeked := uaq.PeekN(int(randPop))
|
||||
|
||||
if len(uaq.Uslice) != initLen {
|
||||
t.Fatalf("total length changed whith peek n operation : had %d found %d after pop", initLen, len(uaq.Uslice))
|
||||
}
|
||||
|
||||
if len(peeked) != initLen {
|
||||
t.Fatalf("did not peek the correct number of element from queue. Expected %d got %d", initLen, len(peeked))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPopNUnack(t *testing.T) {
|
||||
uaq := initUnAckQueue()
|
||||
initLen := len(uaq.Uslice)
|
||||
randPop := rand.Int31n(int32(initLen))
|
||||
|
||||
popped := uaq.PopN(int(randPop))
|
||||
|
||||
if len(uaq.Uslice)+len(popped) != initLen {
|
||||
t.Fatalf("total length changed whith pop n operation : had %d found %d after pop", initLen, len(uaq.Uslice)+len(popped))
|
||||
}
|
||||
|
||||
for _, elt := range popped {
|
||||
for _, oldElt := range uaq.Uslice {
|
||||
if reflect.DeepEqual(elt, oldElt) {
|
||||
t.Fatalf("pop n operation duplicated some elements")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPopNUnackTooLong(t *testing.T) {
|
||||
uaq := initUnAckQueue()
|
||||
initLen := len(uaq.Uslice)
|
||||
|
||||
// Have a random number of elements to pop that's greater than the queue size
|
||||
randPop := rand.Int31n(int32(initLen)) + 1 + int32(initLen)
|
||||
|
||||
popped := uaq.PopN(int(randPop))
|
||||
|
||||
if len(uaq.Uslice)+len(popped) != initLen {
|
||||
t.Fatalf("total length changed whith pop n operation : had %d found %d after pop", initLen, len(uaq.Uslice)+len(popped))
|
||||
}
|
||||
|
||||
for _, elt := range popped {
|
||||
for _, oldElt := range uaq.Uslice {
|
||||
if reflect.DeepEqual(elt, oldElt) {
|
||||
t.Fatalf("pop n operation duplicated some elements")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPopUnack(t *testing.T) {
|
||||
uaq := initUnAckQueue()
|
||||
initLen := len(uaq.Uslice)
|
||||
|
||||
popped := uaq.Pop()
|
||||
|
||||
if len(uaq.Uslice)+1 != initLen {
|
||||
t.Fatalf("total length changed whith pop operation : had %d found %d after pop", initLen, len(uaq.Uslice)+1)
|
||||
}
|
||||
for _, oldElt := range uaq.Uslice {
|
||||
if reflect.DeepEqual(popped, oldElt) {
|
||||
t.Fatalf("pop n operation duplicated some elements")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func initUnAckQueue() stanza.UnAckQueue {
|
||||
q := []*stanza.UnAckedStz{
|
||||
{
|
||||
Id: 1,
|
||||
Stz: `<iq type='set'
|
||||
from='romeo@montague.net/home'
|
||||
to='characters.shakespeare.lit'
|
||||
id='search2'
|
||||
xml:lang='en'>
|
||||
<query xmlns='jabber:iq:search'>
|
||||
<last>Capulet</last>
|
||||
</query>
|
||||
</iq>`,
|
||||
},
|
||||
{Id: 2,
|
||||
Stz: `<iq type='get'
|
||||
from='juliet@capulet.com/balcony'
|
||||
to='characters.shakespeare.lit'
|
||||
id='search3'
|
||||
xml:lang='en'>
|
||||
<query xmlns='jabber:iq:search'/>
|
||||
</iq>`},
|
||||
{Id: 3,
|
||||
Stz: `<iq type='set'
|
||||
from='juliet@capulet.com/balcony'
|
||||
to='characters.shakespeare.lit'
|
||||
id='search4'
|
||||
xml:lang='en'>
|
||||
<query xmlns='jabber:iq:search'>
|
||||
<x xmlns='jabber:x:data' type='submit'>
|
||||
<field type='hidden' var='FORM_TYPE'>
|
||||
<value>jabber:iq:search</value>
|
||||
</field>
|
||||
<field var='x-gender'>
|
||||
<value>male</value>
|
||||
</field>
|
||||
</x>
|
||||
</query>
|
||||
</iq>`},
|
||||
}
|
||||
|
||||
return stanza.UnAckQueue{Uslice: q}
|
||||
|
||||
}
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UTC().UnixNano())
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue