forked from fabito/grails-xmpp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
XmppGrailsPlugin.groovy
336 lines (282 loc) · 11.9 KB
/
XmppGrailsPlugin.groovy
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import org.jivesoftware.smack.RosterListener
import org.codehaus.groovy.grails.commons.GrailsClassUtils
import grails.util.GrailsUtil
import grails.util.GrailsNameUtils
import org.jivesoftware.smack.packet.Packet
import org.jivesoftware.smack.packet.Message
import org.jivesoftware.smack.PacketListener
import org.jivesoftware.smack.RosterListener
import org.jivesoftware.smack.filter.PacketFilter
import org.jivesoftware.smack.Chat
import org.jivesoftware.smack.packet.Presence
import org.codehaus.groovy.grails.commons.ServiceArtefactHandler
import org.apache.commons.lang.StringUtils
class XmppGrailsPlugin {
// the plugin version
def version = "0.4.3"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.1 > *"
// resources that are excluded from plugin packaging
def pluginExcludes = [
"grails-app/views/error.gsp",
"grails-app/services/org/grails/xmpp/*Service.groovy",
"grails-app/controllers/org/grails/xmpp/*Controller.groovy",
"grails-app/conf/XmppBootStrap.groovy",
"src/groovy/org/grails/xmpp/ClosureMessageListenerAdapter.groovy",
"scripts/*.groovy"
]
def author = "Fábio Franco Uechi"
def authorEmail = "[email protected]"
def title = "Grails XMPP Plugin"
def description = '''\\
Provides XMPP/Jabber based services to grails applications. Built upon the Smack API this plugin allows
an application to send and receive xmpp packets (presence, message, etc). Basically it creates a Roster
which automatically connects, to a pre configured host, at application startup. All the mentioned xmpp
services rely upon this Roster. Eases the development of real-time web applications.
One of the main features is the ability to expose methods as commands (method name = command) making it
easier to develop XmppBots, for example.
Another feature is auto-detection of PacketListeners and RosterListeners.
'''
// URL to the plugin's documentation
def documentation = "http://grails.org/plugin/xmpp"
def loadAfter = ['services', 'controllers']
def observe = ['services', 'controllers']
def doWithSpring = {
registerXmppBaseBeans(delegate, log)
xmppAgent (org.grails.xmpp.XmppAgent){ bean ->
bean.destroyMethod = "destroy"
connection = xmppConnection
username = application.config.xmpp.username
password = application.config.xmpp.password
}
/*
* For each service containing the expose xmpp attribute
* creates and registers the respectice MessageListenerAdapter bean
* in the spring context
*/
application.serviceClasses?.each { service ->
registerXmppMessageListenerBean(service, delegate, log)
}
}
def registerXmppBaseBeans (beanBuilder, log) {
beanBuilder.with {
connectionConfiguration (org.jivesoftware.smack.ConnectionConfiguration, application.config.xmpp.connection.host, application.config.xmpp.connection.port, application.config.xmpp.connection.service ) {
SASLAuthenticationEnabled = application.config.xmpp.connection.SASLAuthenticationEnabled
reconnectionAllowed = true
}
xmppConnection (org.jivesoftware.smack.XMPPConnection, ref(connectionConfiguration)) {
}
}
}
def registerXmppMessageListenerBean(service, beanBuilder, log) {
def serviceClass = service.getClazz()
def exposeList = GrailsClassUtils.getStaticPropertyValue(serviceClass, 'expose')
if (exposeList!=null && exposeList.contains('xmpp')) {
def listenerMethod = GrailsClassUtils.getStaticPropertyValue(serviceClass, 'listenerMethod')
if (!listenerMethod)
listenerMethod = org.grails.xmpp.MessageListenerAdapter.ORIGINAL_DEFAULT_LISTENER_METHOD
def xmppCommandPrefix = GrailsClassUtils.getStaticPropertyValue(serviceClass, 'xmppCommandPrefix')
// comparing to null to allow "prefixless" commands
if (xmppCommandPrefix == null)
xmppCommandPrefix = org.grails.xmpp.MessageListenerAdapter.ORIGINAL_DEFAULT_COMMAND_PREFIX
def xmppCommandMethodSuffix = GrailsClassUtils.getStaticPropertyValue(serviceClass, 'xmppCommandMethodSuffix')
// comparing to null to allow "suffixless" commands
if (xmppCommandMethodSuffix == null)
xmppCommandMethodSuffix = org.grails.xmpp.MessageListenerAdapter.ORIGINAL_DEFAULT_COMMAND_METHOD_SUFFIX
beanBuilder.with {
"${service.shortName}XmppMessageListener"(org.grails.xmpp.MessageListenerAdapter, ref("${service.propertyName}")) {
defaultListenerMethod = listenerMethod
defaultXmppCommandPrefix = xmppCommandPrefix
defaultXmppCommandMethodSuffix = xmppCommandMethodSuffix
}
}
}
}
def doWithApplicationContext = { applicationContext ->
// Checks all services and controllers which implement
// PackageListener or MessageListener and add to xmppAgent
application.serviceClasses?.each { service ->
registerXmppMessageListener(service, applicationContext)
}
//initializes xmppAgent
if (application.config.xmpp.autoStartup)
applicationContext.xmppAgent.connect()
}
def registerXmppMessageListener(service, applicationContext) {
def serviceClass = service.getClazz()
def exposeList = GrailsClassUtils.getStaticPropertyValue(serviceClass, 'expose')
if (exposeList!=null && exposeList.contains('xmpp')) {
//println ">>>> Adding XMPP listener for ${service.shortName} to xmppAgent"
applicationContext.xmppAgent.packetListeners.add(applicationContext.getBean("${service.shortName}XmppMessageListener"))
}
if(PacketListener.class.isAssignableFrom(serviceClass)) {
def svc = applicationContext.getBean("${service.propertyName}")
applicationContext.xmppAgent.packetListeners.add(svc)
}
if(RosterListener.class.isAssignableFrom(serviceClass)) {
def svc = applicationContext.getBean("${service.propertyName}")
applicationContext.xmppAgent.rosterListeners.add(svc)
}
}
def doWithDynamicMethods = { ctx ->
def xmppAgent = ctx.getBean("xmppAgent")
[application.controllerClasses, application.serviceClasses].each {
it.each {
if (it.clazz.name != "XmppService") {
addXmppMethodsToClass(xmppAgent, it.clazz)
}
}
}
}
def addXmppMethodsToClass(xmppAgent, clazz) {
[
sendXmppMessage: "sendXmppMessage",
sendXmppPacket: "sendXmppPacket",
changeXmppRosterStatus: "changeXmppRosterStatus",
addXmppContact: "addXmppContact",
removeXmppContact: "removeXmppContact",
// blockXmppContact: "blockXmppContact",
getAllXmppContacts: "getAllXmppContacts"
].each { m, i ->
clazz.metaClass."$m" << this."$i".curry(xmppAgent)
}
}
def onChange = { event ->
// Code that is executed when any artefact that this plugin is
// watching is modified and reloaded. The event contains: event.source,
// event.application, event.manager, event.ctx, and event.plugin.
if (event.source && event.ctx) {
def xmppAgent = event.ctx.getBean('xmppAgent')
if (application.isControllerClass(event.source)) {
def controllerClass = event.application.getControllerClass(event.source?.name)
addXmppMethodsToClass(xmppAgent, controllerClass.clazz)
} else if (application.isServiceClass(event.source)) {
boolean isNew = event.application.getServiceClass(event.source?.name) == null
def serviceClass = application.addArtefact(ServiceArtefactHandler.TYPE, event.source).clazz
def tgt = event.ctx.getBean(GrailsNameUtils.getPropertyName(event.source))
if (!isNew) {
def exposeList = GrailsClassUtils.getStaticPropertyValue(event.source, 'expose')
if (exposeList!=null && exposeList.contains('xmpp')) {
xmppAgent.connection.removePacketListener(event.ctx.getBean("${GrailsNameUtils.getShortName(event.source)}XmppMessageListener"))
event.ctx.removeBeanDefinition("${GrailsNameUtils.getShortName(event.source)}XmppMessageListener")
}
if(PacketListener.class.isAssignableFrom(event.source)) {
xmppAgent.connection.removePacketListener(tgt)
}
if(RosterListener.class.isAssignableFrom(event.source)) {
xmppAgent.roster.removeRosterListener(tgt)
}
}
serviceClass = event.application.getServiceClass(event.source?.name)
def newBeans = beans {
registerXmppMessageListenerBean(serviceClass, delegate, log)
}
newBeans.beanDefinitions.each { n,d ->
event.ctx.registerBeanDefinition(n, d)
}
addXmppMethodsToClass(xmppAgent, serviceClass.clazz)
xmppAgent.unregisterListeners()
doWithApplicationContext(event.ctx)
xmppAgent.registerListeners()
}
}
}
def onConfigChange = { event ->
def wasConnectedBeforeConfigChange = applicationContext.xmppAgent.connection?.isConnected()
def pList = applicationContext.xmppAgent.packetListeners
def rList = applicationContext.xmppAgent.rosterListeners
if (wasConnectedBeforeConfigChange) {
applicationContext.xmppAgent.disconnect()
}
event.ctx.removeBeanDefinition("connectionConfiguration")
event.ctx.removeBeanDefinition("xmppConnection")
def newBeans = beans {
registerXmppBaseBeans(delegate, log)
}
newBeans.beanDefinitions.each { n,d ->
event.ctx.registerBeanDefinition(n, d)
}
applicationContext.xmppAgent.connection = applicationContext.xmppConnection
applicationContext.xmppAgent.username = application.config.xmpp.username
applicationContext.xmppAgent.password = application.config.xmpp.password
applicationContext.xmppAgent.packetListeners = pList
applicationContext.xmppAgent.rosterListeners = rList
doWithDynamicMethods(event.ctx)
if (application.config.xmpp.autoStartup || wasConnectedBeforeConfigChange) {
applicationContext.xmppAgent.connect()
}
}
// xmpp utility closures
def addXmppContact = { xmppAgent, to ->
if (to instanceof List) {
to.each { user ->
xmppAgent.roster.createEntry(user, user, null)
}
} else {
xmppAgent.roster.createEntry(to, to, null)
}
}
def removeXmppContact = { xmppAgent, to ->
if (to instanceof List) {
to.each { user ->
user = StringUtils.substringBefore(user,"/")
log.debug "removing ${user}"
xmppAgent.roster.removeEntry(xmppAgent.roster.getEntry(user))
}
} else {
to = StringUtils.substringBefore(to,"/")
log.debug "looking for entry ${to}"
def entry = xmppAgent.roster.getEntry(to)
if (entry) {
log.debug "found ${to}"
xmppAgent.roster.removeEntry(entry)
}
else {
log.debug "not found entry ${to}"
}
}
}
def sendXmppMessage = { xmppAgent, to, text ->
try{
def chat
def msgObj
if (to instanceof List) {
to.each { addr ->
chat = xmppAgent.connection.chatManager.createChat(addr, null)
msgObj = new Message(addr, Message.Type.chat)
msgObj.body = text
chat.sendMessage(msgObj)
}
} else {
chat = xmppAgent.connection.chatManager.createChat(to, null)
msgObj = new Message(to, Message.Type.chat)
msgObj.body = text
chat.sendMessage(msgObj)
}
} catch (Exception e) {
log.error "Failed to send message", e
}
}
def sendXmppPacket = { xmppAgent, pkg ->
try{
xmppAgent.connection.sendPacket(pkg)
} catch (Exception e) {
log.error "Failed to send packet", e
}
}
def getAllXmppContacts = { xmppAgent ->
return xmppAgent.roster.entries
}
def changeXmppRosterStatus = { xmppAgent, newStatus, presenceType ->
if (!presenceType)
changeXmppRosterStatus2(xmppAgent, newStatus)
Presence presence = new Presence(presenceType);
presence.setStatus(newStatus);
xmppAgent.connection.sendPacket(presence);
}
def changeXmppRosterStatus2 = { xmppAgent, newStatus ->
Presence presence = new Presence(org.jivesoftware.smack.packet.Presence.Type.available);
presence.setStatus(newStatus);
xmppAgent.connection.sendPacket(presence);
}
}