-
Notifications
You must be signed in to change notification settings - Fork 3
/
multi-user-chat.lisp
240 lines (206 loc) · 8.26 KB
/
multi-user-chat.lisp
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
;;;; $Id: multi-user-chat.lisp,v 1.6 2007/12/17 09:17:22 kcrosbie Exp $
;;;; $Source: /project/cl-xmpp/cvsroot/cl-xmpp/multi-user-chat.lisp,v $
(in-package :cl-xmpp)
;;
;; Multi User Chat
;;
;;<presence
;; from='[email protected]/desktop'
;; to='[email protected]/firstwitch'>
;; <x xmlns='http://jabber.org/protocol/muc'/>
;;</presence>
(defmethod create-chatroom ((connection connection) &key from room priority)
(with-xml-output (connection)
(cxml:with-element "presence"
(cxml:attribute "to" room)
(cxml:attribute "from" from)
(when priority
(cxml:with-element "priority"
(cxml:text "0")))
(cxml:with-element "x"
(cxml:attribute "xmlns" "http://jabber.org/protocol/muc")))))
;;<presence
;; from='[email protected]/pda'
;; to='[email protected]/thirdwitch'/>
(defmethod join-chatroom ((connection connection) &key from room password)
(with-xml-output (connection)
(cxml:with-element "presence"
(cxml:attribute "to" room)
(cxml:attribute "from" from)
(when password
(cxml:with-element "x"
(cxml:attribute "xmlns" "http://jabber.org/protocol/muc")
(cxml:with-element "password"
(cxml:text password)))))))
;;<presence
;; from='[email protected]/pda'
;; to='[email protected]/thirdwitch'
;; type='unavailable'/>
(defmethod leave-chatroom ((connection connection) &key from room)
(with-xml-output (connection)
(cxml:with-element "presence"
(cxml:attribute "to" room)
(cxml:attribute "from" from)
(cxml:attribute "type" "unavailable"))))
;;<message
;; from='[email protected]/desktop'
;; to='[email protected]'>
;; <x xmlns='http://jabber.org/protocol/muc#user'>
;; <invite to='[email protected]'>
;; <reason>
;; Hey Hecate, this is the place for all good witches!
;; </reason>
;; </invite>
;; </x>
;;</message>
(defmethod invite-to-chatroom ((connection connection)
&key from room to reason)
(with-xml-output (connection)
(cxml:with-element "message"
(cxml:attribute "to" room)
(cxml:attribute "from" from)
(cxml:with-element "x"
(cxml:attribute "xmlns" "http://jabber.org/protocol/muc#user")
(cxml:with-element "invite"
(cxml:attribute "to" to)
(cxml:with-element "reason"
(cxml:text reason)))))))
;;<iq from='[email protected]/pda'
;; id='kick1'
;; to='[email protected]'
;; type='set'>
;; <query xmlns=''>
;; <item nick='pistol' role='none'>
;; <reason>Avaunt, you cullion!</reason>
;; </item>
;; </query>
;;</iq>
(defmethod kick-from-chatroom ((connection connection)
&key to room reason)
(with-xml-output (connection)
(with-iq-query (connection :to room :type "set"
:xmlns "http://jabber.org/protocol/muc#admin")
(cxml:with-element "item"
(cxml:attribute "nick" to)
(cxml:attribute "role" "none")
(cxml:with-element "reason"
(cxml:text reason))))))
;;<iq from='[email protected]/desktop'
;; id='member1'
;; to='[email protected]'
;; type='set'>
;; <query xmlns='http://jabber.org/protocol/muc#admin'>
;; <item affiliation='member'
;; jid='[email protected]'/>
;; </query>
;;</iq>
(defmethod set-room-affiliation ((connection connection)
&key room to affiliation)
(with-xml-output (connection)
(with-iq-query (connection :to room :type "set"
:xmlns "http://jabber.org/protocol/muc#admin")
(cxml:with-element "item"
(cxml:attribute "affiliation" affiliation)
(cxml:attribute "jid" to)))))
(defmethod grant-room-membership ((connection connection) &key room to)
(set-room-affiliation connection :room room :to to :affiliation "member"))
(defmethod revoke-room-membership ((connection connection) &key room to)
(set-room-affiliation connection :room room :to to :affiliation "none"))
;;<message
;; from='[email protected]/pda'
;; to='[email protected]'
;; type='groupchat'>
;; <body>Harpier cries: 'tis time, 'tis time.</body>
;;</message>
(defmethod broadcast-room ((connection connection) &key from room message)
(with-xml-output (connection)
(cxml:with-element "message"
(cxml:attribute "from" from)
(cxml:attribute "to" room)
(cxml:attribute "type" "groupchat")
(cxml:with-element "body"
(cxml:text message)))))
(defmacro with-form-field (type var &optional (value ""))
`(cxml:with-element "field"
,(when type
`(cxml:attribute "type" ,type))
,(when var
`(cxml:attribute "var" ,var))
(cxml:with-element "value"
(cxml:text ,(or value "")))))
;; For now these are just the settings that I want to use. It would be easy
;; to change this method so that it takes a list of arguments and looks up
;; nodes/data-types in some structure.
(defmethod default-room-config ((connection connection) &key room)
(with-xml-output (connection)
(with-iq-query (connection :type "set" :to room
:xmlns "http://jabber.org/protocol/muc#owner")
(cxml:with-element "x"
(cxml:attribute "xmlns" "jabber:x:data")
(cxml:attribute "type" "submit")
(with-form-field "hidden" "FORM_TYPE"
"http://jabber.org/protocol/muc#roomconfig")
(with-form-field "text-single" "muc#roomconfig_roomname")
(with-form-field "boolean" "muc#roomconfig_persistentroom" "0")
(with-form-field "boolean" "muc#roomconfig_publicroom" "0")
(with-form-field "boolean" "public_list" "0")
(with-form-field "boolean" "muc#roomconfig_passwordprotectedroom" "0")
(with-form-field "text-private" "muc#roomconfig_roomsecret")
(with-form-field "list-single" "muc#roomconfig_whois" "moderators")
(with-form-field "boolean" "muc#roomconfig_membersonly" "1")
(with-form-field "boolean" "muc#roomconfig_moderatedroom" "0")
(with-form-field "boolean" "members_by_default" "0")
(with-form-field "boolean" "muc#roomconfig_changesubject" "0")
(with-form-field "boolean" "allow_private_messages" "0")
(with-form-field "boolean" "allow_query_users" "0")
(with-form-field "boolean" "muc#roomconfig_allowinvites" "0")))))
;;<message
;; from='[email protected]/laptop'
;; to='[email protected]'
;; type='groupchat'>
;; <subject>Fire Burn and Cauldron Bubble!</subject>
;;</message>
(defmethod set-chatroom-subject ((connection connection)
&key from room subject)
(with-xml-output (connection)
(cxml:with-element "message"
(cxml:attribute "from" from)
(cxml:attribute "to" room)
(cxml:attribute "type" "groupchat")
(cxml:with-element "subject"
(cxml:text subject)))))
;;<iq from='[email protected]/desktop'
;; id='begone'
;; to='[email protected]'
;; type='set'>
;; <query xmlns='http://jabber.org/protocol/muc#owner'>
;; <destroy jid='[email protected]'>
;; <reason>Macbeth doth come.</reason>
;; </destroy>
;; </query>
;;</iq>
(defmethod destroy-chatroom ((connection connection) &key room reason)
(with-xml-output (connection)
(with-iq-query (connection :type "set" :to room
:xmlns "http://jabber.org/protocol/muc#owner")
(cxml:with-element "destroy"
(cxml:attribute "jid" room)
(when reason
(cxml:with-element "reason"
(cxml:text reason)))))))
;;<iq from='[email protected]/desktop'
;; id='voice2'
;; to='[email protected]'
;; type='set'>
;; <query xmlns='http://jabber.org/protocol/muc#admin'>
;; <item nick='thirdwitch'
;; role='visitor'/>
;; </query>
;;</iq>
(defmethod revoke-voice ((connection connection) &key room nickname)
(with-xml-output (connection)
(with-iq-query (connection :type "set" :to room
:xmlns "http://jabber.org/protocol/muc#admin")
(cxml:with-element "item"
(cxml:attribute "nick" nickname)
(cxml:attribute "role" "visitor")))))