~samwhited/xmpp

xmpp/muc/types.go -rw-r--r-- 4.4 KiB
c9743d9fSam Whited docs: update changelog to include deprecations 4 days ago
                                                                                
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
// Copyright 2021 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

//go:generate go run -tags=tools golang.org/x/tools/cmd/stringer -type=Affiliation,Role,Privileges -linecomment

package muc

import (
	"encoding/xml"
	"errors"

	"mellium.im/xmpp/jid"
)

// Item represents a user in the channel.
// Various fields will be set when a user joins the channel or when the channel
// informs us of upates to the users information.
type Item struct {
	JID         jid.JID     `xml:"jid,attr,omitempty"`
	Affiliation Affiliation `xml:"affiliation,attr,omitempty"`
	Nick        string      `xml:"nick,attr,omitempty"`
	Role        Role        `xml:"role,attr,omitempty"`
	Reason      string      `xml:"reason"`
}

// Affiliation indicates a users affiliation to the room.
type Affiliation uint8

// A list of room affiliations.
const (
	AffiliationNone Affiliation = iota // none

	// Support for the owner affiliation is required.
	AffiliationOwner // owner

	// Support for these affiliations is recommended, but optional.
	AffiliationAdmin   // admin
	AffiliationMember  // member
	AffiliationOutcast // outcast
)

// UnmarshalXMLAttr satisfies xml.UnmarshalerAttr.
func (a *Affiliation) UnmarshalXMLAttr(attr xml.Attr) error {
	switch attr.Value {
	case AffiliationNone.String():
		*a = AffiliationNone
	case AffiliationOwner.String():
		*a = AffiliationOwner
	case AffiliationAdmin.String():
		*a = AffiliationAdmin
	case AffiliationMember.String():
		*a = AffiliationMember
	case AffiliationOutcast.String():
		*a = AffiliationOutcast
	default:
		return errors.New("muc: unrecognized affiliation")
	}
	return nil
}

// MarshalXMLAttr satisfies xml.MarshalerAttr.
func (a *Affiliation) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
	return xml.Attr{Name: name, Value: a.String()}, nil
}

// Role indicates a users role in the room.
type Role uint8

// A list of user roles.
const (
	RoleNone Role = iota // none

	// Support for these roles is required.
	RoleModerator   // moderator
	RoleParticipant // participant

	// Support for these roles is recommended, but optional.
	RoleVisitor // visitor
)

// UnmarshalXMLAttr satisfies xml.UnmarshalerAttr.
func (r *Role) UnmarshalXMLAttr(attr xml.Attr) error {
	switch attr.Value {
	case RoleNone.String():
		*r = RoleNone
	case RoleModerator.String():
		*r = RoleModerator
	case RoleParticipant.String():
		*r = RoleParticipant
	case RoleVisitor.String():
		*r = RoleVisitor
	default:
		return errors.New("muc: unrecognized role")
	}
	return nil
}

// MarshalXMLAttr satisfies xml.MarshalerAttr.
func (r *Role) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
	if r == nil {
		return xml.Attr{}, nil
	}
	return xml.Attr{Name: name, Value: r.String()}, nil
}

// Privileges is a bit mask indicating the various privileges assigned to a room
// user.
type Privileges uint16

// A list of possible privileges.
const (
	PrivilegePresent            Privileges = 1 << iota // present
	PrivilegeReceiveMessages                           // receive-messages
	PrivilegeReceivePresence                           // receive-presence
	PrivilegeBroadcastPresence                         // broadcast-presence
	PrivilegeChangeAvailability                        // change-availability
	PrivilegeChangeNick                                // change-nick
	PrivilegePrivateMessage                            // send-private-message
	PrivilegeSendInvites                               // send-invites
	PrivilegeSendMessages                              // send-messages
	PrivilegeModifySubject                             // modify-subject
	PrivilegeKick                                      // kick
	PrivilegeGrantVoice                                // grant-voice
	PrivilegeRevokeVoice                               // revoke-voice

	// Common default privilages for each role.
	// These are just common defaults provided as a convenience, it is not
	// guaranteed that a user of a given role has this set of privileges.
	PrivilegesVisitor     = PrivilegePresent | PrivilegeReceiveMessages | PrivilegeReceivePresence | PrivilegeBroadcastPresence | PrivilegeChangeAvailability | PrivilegeChangeNick | PrivilegePrivateMessage | PrivilegeSendInvites
	PrivilegesParticipant = PrivilegesVisitor | PrivilegeSendMessages | PrivilegeModifySubject
	PrivilegesModerator   = PrivilegesParticipant | PrivilegeKick | PrivilegeGrantVoice | PrivilegeRevokeVoice
)