File: xmpp_muc.go

package info (click to toggle)
golang-github-mattn-go-xmpp 0.2.16-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 196 kB
  • sloc: makefile: 2
file content (187 lines) | stat: -rw-r--r-- 5,961 bytes parent folder | download
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
// Copyright 2013 Flo Lauber <dev@qatfy.at>.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// TODO(flo):
//   - support password protected MUC rooms
//   - cleanup signatures of join/leave functions
package xmpp

import (
	"errors"
	"fmt"
	"time"
)

const (
	nsMUC          = "http://jabber.org/protocol/muc"
	nsMUCUser      = "http://jabber.org/protocol/muc#user"
	NoHistory      = 0
	CharHistory    = 1
	StanzaHistory  = 2
	SecondsHistory = 3
	SinceHistory   = 4
)

// Send sends room topic wrapped inside an XMPP message stanza body.
func (c *Client) SendTopic(chat Chat) (n int, err error) {
	// Reset ticker for periodic pings if configured.
	if c.periodicPings {
		c.periodicPingTicker.Reset(c.periodicPingPeriod)
	}
	return fmt.Fprintf(c.stanzaWriter, "<message to='%s' type='%s' xml:lang='en'>"+"<subject>%s</subject></message>\n",
		xmlEscape(chat.Remote), xmlEscape(chat.Type), xmlEscape(chat.Text))
}

func (c *Client) JoinMUCNoHistory(jid, nick string) (n int, err error) {
	if nick == "" {
		nick = c.jid
	}
	// Reset ticker for periodic pings if configured.
	if c.periodicPings {
		c.periodicPingTicker.Reset(c.periodicPingPeriod)
	}
	return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
		"<x xmlns='%s'>"+
		"<history maxchars='0'/></x>"+
		"</presence>\n",
		xmlEscape(jid), xmlEscape(nick), nsMUC)
}

// xep-0045 7.2
func (c *Client) JoinMUC(jid, nick string, history_type, history int, history_date *time.Time) (n int, err error) {
	if nick == "" {
		nick = c.jid
	}
	switch history_type {
	case NoHistory:
		// Reset ticker for periodic pings if configured.
		if c.periodicPings {
			c.periodicPingTicker.Reset(c.periodicPingPeriod)
		}
		return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
			"<x xmlns='%s' />"+
			"</presence>\n",
			xmlEscape(jid), xmlEscape(nick), nsMUC)
	case CharHistory:
		// Reset ticker for periodic pings if configured.
		if c.periodicPings {
			c.periodicPingTicker.Reset(c.periodicPingPeriod)
		}
		return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
			"<x xmlns='%s'>"+
			"<history maxchars='%d'/></x>"+
			"</presence>\n",
			xmlEscape(jid), xmlEscape(nick), nsMUC, history)
	case StanzaHistory:
		// Reset ticker for periodic pings if configured.
		if c.periodicPings {
			c.periodicPingTicker.Reset(c.periodicPingPeriod)
		}
		return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
			"<x xmlns='%s'>"+
			"<history maxstanzas='%d'/></x>"+
			"</presence>\n",
			xmlEscape(jid), xmlEscape(nick), nsMUC, history)
	case SecondsHistory:
		// Reset ticker for periodic pings if configured.
		if c.periodicPings {
			c.periodicPingTicker.Reset(c.periodicPingPeriod)
		}
		return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
			"<x xmlns='%s'>"+
			"<history seconds='%d'/></x>"+
			"</presence>\n",
			xmlEscape(jid), xmlEscape(nick), nsMUC, history)
	case SinceHistory:
		if history_date != nil {
			// Reset ticker for periodic pings if configured.
			if c.periodicPings {
				c.periodicPingTicker.Reset(c.periodicPingPeriod)
			}
			return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
				"<x xmlns='%s'>"+
				"<history since='%s'/></x>"+
				"</presence>\n",
				xmlEscape(jid), xmlEscape(nick), nsMUC, history_date.Format(time.RFC3339))
		}
	}
	return 0, errors.New("unknown history option")
}

// xep-0045 7.2.6
func (c *Client) JoinProtectedMUC(jid, nick string, password string, history_type, history int, history_date *time.Time) (n int, err error) {
	if nick == "" {
		nick = c.jid
	}
	switch history_type {
	case NoHistory:
		// Reset ticker for periodic pings if configured.
		if c.periodicPings {
			c.periodicPingTicker.Reset(c.periodicPingPeriod)
		}
		return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
			"<x xmlns='%s'>"+
			"<password>%s</password>"+
			"</x>"+
			"</presence>\n",
			xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password))
	case CharHistory:
		// Reset ticker for periodic pings if configured.
		if c.periodicPings {
			c.periodicPingTicker.Reset(c.periodicPingPeriod)
		}
		return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
			"<x xmlns='%s'>"+
			"<password>%s</password>"+
			"<history maxchars='%d'/></x>"+
			"</presence>\n",
			xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password), history)
	case StanzaHistory:
		// Reset ticker for periodic pings if configured.
		if c.periodicPings {
			c.periodicPingTicker.Reset(c.periodicPingPeriod)
		}
		return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
			"<x xmlns='%s'>"+
			"<password>%s</password>"+
			"<history maxstanzas='%d'/></x>"+
			"</presence>\n",
			xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password), history)
	case SecondsHistory:
		// Reset ticker for periodic pings if configured.
		if c.periodicPings {
			c.periodicPingTicker.Reset(c.periodicPingPeriod)
		}
		return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
			"<x xmlns='%s'>"+
			"<password>%s</password>"+
			"<history seconds='%d'/></x>"+
			"</presence>\n",
			xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password), history)
	case SinceHistory:
		if history_date != nil {
			// Reset ticker for periodic pings if configured.
			if c.periodicPings {
				c.periodicPingTicker.Reset(c.periodicPingPeriod)
			}
			return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
				"<x xmlns='%s'>"+
				"<password>%s</password>"+
				"<history since='%s'/></x>"+
				"</presence>\n",
				xmlEscape(jid), xmlEscape(nick), nsMUC, xmlEscape(password), history_date.Format(time.RFC3339))
		}
	}
	return 0, errors.New("unknown history option")
}

// xep-0045 7.14
func (c *Client) LeaveMUC(jid string) (n int, err error) {
	// Reset ticker for periodic pings if configured.
	if c.periodicPings {
		c.periodicPingTicker.Reset(c.periodicPingPeriod)
	}
	return fmt.Fprintf(c.stanzaWriter, "<presence from='%s' to='%s' type='unavailable' />\n",
		c.jid, xmlEscape(jid))
}