From e0e253ce4bab759c8fca73f7afa37fbdff16f1c5 Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Wed, 20 Apr 2016 22:19:59 +0200 Subject: [PATCH] Add Ad-Hoc command --- src/xmpp/ad-hoc.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++ src/xmpp/uuid.go | 13 +++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/xmpp/ad-hoc.go diff --git a/src/xmpp/ad-hoc.go b/src/xmpp/ad-hoc.go new file mode 100644 index 0000000..bf05109 --- /dev/null +++ b/src/xmpp/ad-hoc.go @@ -0,0 +1,68 @@ +package xmpp + +import ( + "encoding/xml" +) + +const ( + NodeAdHocCommand = "http://jabber.org/protocol/commands" + + ActionAdHocExecute = "execute" + ActionAdHocNext = "next" + ActionAdHocCancel = "cancel" + + StatusAdHocExecute = "execute" + StatusAdHocCompleted = "completed" + StatusAdHocCanceled = "canceled" + + TypeAdHocForm = "form" + TypeAdHocResult = "result" + TypeAdHocSubmit = "submit" + + TypeAdHocListSingle = "list-single" + TypeAdHocListMulti = "list-multi" + + TypeAdHocNoteInfo = "info" + TypeAdHocNoteWarning = "warn" + TypeAdHocNoteError = "error" + + TypeAdHocFieldListMulti = "list-multi" + TypeAdHocFieldListSingle = "list-single" + TypeAdHocFieldTextSingle = "text-single" + TypeAdHocFieldJidSingle = "jid-single" +) + +type AdHocCommand struct { + XMLName xml.Name `xml:"http://jabber.org/protocol/commands command"` + Node string `xml:"node,attr"` + Action string `xml:"action,attr"` + SessionId string `xml:"sessionid,attr"` + Status string `xml:"status,attr"` + XForm AdHocXForm `xml:"x"` + Note AdHocNote `xml:"note,omitempty"` +} + +type AdHocXForm struct { + XMLName xml.Name `xml:"jabber:x:data x"` + Type string `xml:"type,attr"` + Title string `xml:"title"` + Instructions string `xml:"instructions"` + Fields []AdHocField `xml:"field"` +} + +type AdHocField struct { + Var string `xml:"var,attr"` + Label string `xml:"label,attr"` + Type string `xml:"type,attr"` + Options []AdHocFieldOption `xml:"option"` + Value string `xml:"value,omitempty"` +} + +type AdHocFieldOption struct { + Value string `xml:"value"` +} + +type AdHocNote struct { + Type string `xml:"type,attr"` + Value string `xml:",innerxml"` +} diff --git a/src/xmpp/uuid.go b/src/xmpp/uuid.go index 14d866f..2d8779d 100644 --- a/src/xmpp/uuid.go +++ b/src/xmpp/uuid.go @@ -15,3 +15,16 @@ func UUID4() string { uuid[8] = (uuid[8] &^ 0x40) | 0x80 return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]) } + +func SessionId() string { + var strSize = 15 + var dictionary string + dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + + var bytes = make([]byte, strSize) + rand.Read(bytes) + for k, v := range bytes { + bytes[k] = dictionary[v%byte(len(dictionary))] + } + return string(bytes) +}