/usr/share/gocode/src/google.golang.org/appengine/xmpp/xmpp.go is in golang-google-appengine-dev 0.0~git20150606-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | // Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
/*
Package xmpp provides the means to send and receive instant messages
to and from users of XMPP-compatible services.
To send a message,
m := &xmpp.Message{
To: []string{"kaylee@example.com"},
Body: `Hi! How's the carrot?`,
}
err := m.Send(c)
To receive messages,
func init() {
xmpp.Handle(handleChat)
}
func handleChat(c context.Context, m *xmpp.Message) {
// ...
}
*/
package xmpp // import "google.golang.org/appengine/xmpp"
import (
"errors"
"fmt"
"net/http"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/internal"
pb "google.golang.org/appengine/internal/xmpp"
)
// Message represents an incoming chat message.
type Message struct {
// Sender is the JID of the sender.
// Optional for outgoing messages.
Sender string
// To is the intended recipients of the message.
// Incoming messages will have exactly one element.
To []string
// Body is the body of the message.
Body string
// Type is the message type, per RFC 3921.
// It defaults to "chat".
Type string
// RawXML is whether the body contains raw XML.
RawXML bool
}
// Presence represents an outgoing presence update.
type Presence struct {
// Sender is the JID (optional).
Sender string
// The intended recipient of the presence update.
To string
// Type, per RFC 3921 (optional). Defaults to "available".
Type string
// State of presence (optional).
// Valid values: "away", "chat", "xa", "dnd" (RFC 3921).
State string
// Free text status message (optional).
Status string
}
var (
ErrPresenceUnavailable = errors.New("xmpp: presence unavailable")
ErrInvalidJID = errors.New("xmpp: invalid JID")
)
// Handle arranges for f to be called for incoming XMPP messages.
// Only messages of type "chat" or "normal" will be handled.
func Handle(f func(c context.Context, m *Message)) {
http.HandleFunc("/_ah/xmpp/message/chat/", func(_ http.ResponseWriter, r *http.Request) {
f(appengine.NewContext(r), &Message{
Sender: r.FormValue("from"),
To: []string{r.FormValue("to")},
Body: r.FormValue("body"),
})
})
}
// Send sends a message.
// If any failures occur with specific recipients, the error will be an appengine.MultiError.
func (m *Message) Send(c context.Context) error {
req := &pb.XmppMessageRequest{
Jid: m.To,
Body: &m.Body,
RawXml: &m.RawXML,
}
if m.Type != "" && m.Type != "chat" {
req.Type = &m.Type
}
if m.Sender != "" {
req.FromJid = &m.Sender
}
res := &pb.XmppMessageResponse{}
if err := internal.Call(c, "xmpp", "SendMessage", req, res); err != nil {
return err
}
if len(res.Status) != len(req.Jid) {
return fmt.Errorf("xmpp: sent message to %d JIDs, but only got %d statuses back", len(req.Jid), len(res.Status))
}
me, any := make(appengine.MultiError, len(req.Jid)), false
for i, st := range res.Status {
if st != pb.XmppMessageResponse_NO_ERROR {
me[i] = errors.New(st.String())
any = true
}
}
if any {
return me
}
return nil
}
// Invite sends an invitation. If the from address is an empty string
// the default (yourapp@appspot.com/bot) will be used.
func Invite(c context.Context, to, from string) error {
req := &pb.XmppInviteRequest{
Jid: &to,
}
if from != "" {
req.FromJid = &from
}
res := &pb.XmppInviteResponse{}
return internal.Call(c, "xmpp", "SendInvite", req, res)
}
// Send sends a presence update.
func (p *Presence) Send(c context.Context) error {
req := &pb.XmppSendPresenceRequest{
Jid: &p.To,
}
if p.State != "" {
req.Show = &p.State
}
if p.Type != "" {
req.Type = &p.Type
}
if p.Sender != "" {
req.FromJid = &p.Sender
}
if p.Status != "" {
req.Status = &p.Status
}
res := &pb.XmppSendPresenceResponse{}
return internal.Call(c, "xmpp", "SendPresence", req, res)
}
var presenceMap = map[pb.PresenceResponse_SHOW]string{
pb.PresenceResponse_NORMAL: "",
pb.PresenceResponse_AWAY: "away",
pb.PresenceResponse_DO_NOT_DISTURB: "dnd",
pb.PresenceResponse_CHAT: "chat",
pb.PresenceResponse_EXTENDED_AWAY: "xa",
}
// GetPresence retrieves a user's presence.
// If the from address is an empty string the default
// (yourapp@appspot.com/bot) will be used.
// Possible return values are "", "away", "dnd", "chat", "xa".
// ErrPresenceUnavailable is returned if the presence is unavailable.
func GetPresence(c context.Context, to string, from string) (string, error) {
req := &pb.PresenceRequest{
Jid: &to,
}
if from != "" {
req.FromJid = &from
}
res := &pb.PresenceResponse{}
if err := internal.Call(c, "xmpp", "GetPresence", req, res); err != nil {
return "", err
}
if !*res.IsAvailable || res.Presence == nil {
return "", ErrPresenceUnavailable
}
presence, ok := presenceMap[*res.Presence]
if ok {
return presence, nil
}
return "", fmt.Errorf("xmpp: unknown presence %v", *res.Presence)
}
// GetPresenceMulti retrieves multiple users' presence.
// If the from address is an empty string the default
// (yourapp@appspot.com/bot) will be used.
// Possible return values are "", "away", "dnd", "chat", "xa".
// If any presence is unavailable, an appengine.MultiError is returned
func GetPresenceMulti(c context.Context, to []string, from string) ([]string, error) {
req := &pb.BulkPresenceRequest{
Jid: to,
}
if from != "" {
req.FromJid = &from
}
res := &pb.BulkPresenceResponse{}
if err := internal.Call(c, "xmpp", "BulkGetPresence", req, res); err != nil {
return nil, err
}
presences := make([]string, 0, len(res.PresenceResponse))
errs := appengine.MultiError{}
addResult := func(presence string, err error) {
presences = append(presences, presence)
errs = append(errs, err)
}
anyErr := false
for _, subres := range res.PresenceResponse {
if !subres.GetValid() {
anyErr = true
addResult("", ErrInvalidJID)
continue
}
if !*subres.IsAvailable || subres.Presence == nil {
anyErr = true
addResult("", ErrPresenceUnavailable)
continue
}
presence, ok := presenceMap[*subres.Presence]
if ok {
addResult(presence, nil)
} else {
anyErr = true
addResult("", fmt.Errorf("xmpp: unknown presence %q", *subres.Presence))
}
}
if anyErr {
return presences, errs
}
return presences, nil
}
func init() {
internal.RegisterErrorCodeMap("xmpp", pb.XmppServiceError_ErrorCode_name)
}
|