Debian Codegraph @

xmpp_muc.go

raw
1 // Copyright 2013 Flo Lauber <[email protected]>. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // TODO(flo):
6 // - support password protected MUC rooms
7 // - cleanup signatures of join/leave functions
8 package xmpp
9
10 import (
11 "errors"
12 "fmt"
13 "time"
14 )
15
16 const (
17 NoHistory = 0
18 CharHistory = 1
19 StanzaHistory = 2
20 SecondsHistory = 3
21 SinceHistory = 4
22 )
23
24 // Send sends room topic wrapped inside an XMPP message stanza body.
25 func (c *Client) SendTopic(chat Chat) (n int, err error) {
26 return fmt.Fprintf(c.stanzaWriter, "<message to='%s' type='%s' xml:lang='en'>"+"<subject>%s</subject></message>\n",
27 xmlEscape(chat.Remote), xmlEscape(chat.Type), xmlEscape(chat.Text))
28 }
29
30 func (c *Client) JoinMUCNoHistory(jid, nick string) (n int, err error) {
31 if nick == "" {
32 nick = c.jid
33 }
34 return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
35 "<x xmlns='%s'>"+
36 "<history maxchars='0'/></x>"+
37 "</presence>\n",
38 xmlEscape(jid), xmlEscape(nick), XMPPNS_MUC)
39 }
40
41 // xep-0045 7.2
42 func (c *Client) JoinMUC(jid, nick string, history_type, history int, history_date *time.Time) (n int, err error) {
43 if nick == "" {
44 nick = c.jid
45 }
46 switch history_type {
47 case NoHistory:
48 return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
49 "<x xmlns='%s' />"+
50 "</presence>\n",
51 xmlEscape(jid), xmlEscape(nick), XMPPNS_MUC)
52 case CharHistory:
53 return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
54 "<x xmlns='%s'>"+
55 "<history maxchars='%d'/></x>"+
56 "</presence>\n",
57 xmlEscape(jid), xmlEscape(nick), XMPPNS_MUC, history)
58 case StanzaHistory:
59 return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
60 "<x xmlns='%s'>"+
61 "<history maxstanzas='%d'/></x>"+
62 "</presence>\n",
63 xmlEscape(jid), xmlEscape(nick), XMPPNS_MUC, history)
64 case SecondsHistory:
65 return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
66 "<x xmlns='%s'>"+
67 "<history seconds='%d'/></x>"+
68 "</presence>\n",
69 xmlEscape(jid), xmlEscape(nick), XMPPNS_MUC, history)
70 case SinceHistory:
71 if history_date != nil {
72 return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
73 "<x xmlns='%s'>"+
74 "<history since='%s'/></x>"+
75 "</presence>\n",
76 xmlEscape(jid), xmlEscape(nick), XMPPNS_MUC, history_date.Format(time.RFC3339))
77 }
78 }
79 return 0, errors.New("unknown history option")
80 }
81
82 // xep-0045 7.2.6
83 func (c *Client) JoinProtectedMUC(jid, nick string, password string, history_type, history int, history_date *time.Time) (n int, err error) {
84 if nick == "" {
85 nick = c.jid
86 }
87 switch history_type {
88 case NoHistory:
89 return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
90 "<x xmlns='%s'>"+
91 "<password>%s</password>"+
92 "</x>"+
93 "</presence>\n",
94 xmlEscape(jid), xmlEscape(nick), XMPPNS_MUC, xmlEscape(password))
95 case CharHistory:
96 return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
97 "<x xmlns='%s'>"+
98 "<password>%s</password>"+
99 "<history maxchars='%d'/></x>"+
100 "</presence>\n",
101 xmlEscape(jid), xmlEscape(nick), XMPPNS_MUC, xmlEscape(password), history)
102 case StanzaHistory:
103 return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
104 "<x xmlns='%s'>"+
105 "<password>%s</password>"+
106 "<history maxstanzas='%d'/></x>"+
107 "</presence>\n",
108 xmlEscape(jid), xmlEscape(nick), XMPPNS_MUC, xmlEscape(password), history)
109 case SecondsHistory:
110 return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
111 "<x xmlns='%s'>"+
112 "<password>%s</password>"+
113 "<history seconds='%d'/></x>"+
114 "</presence>\n",
115 xmlEscape(jid), xmlEscape(nick), XMPPNS_MUC, xmlEscape(password), history)
116 case SinceHistory:
117 if history_date != nil {
118 return fmt.Fprintf(c.stanzaWriter, "<presence to='%s/%s'>"+
119 "<x xmlns='%s'>"+
120 "<password>%s</password>"+
121 "<history since='%s'/></x>"+
122 "</presence>\n",
123 xmlEscape(jid), xmlEscape(nick), XMPPNS_MUC, xmlEscape(password), history_date.Format(time.RFC3339))
124 }
125 }
126 return 0, errors.New("unknown history option")
127 }
128
129 // xep-0045 7.14
130 func (c *Client) LeaveMUC(jid string) (n int, err error) {
131 return fmt.Fprintf(c.stanzaWriter, "<presence from='%s' to='%s' type='unavailable' />\n",
132 c.jid, xmlEscape(jid))
133 }