social_stream-presence 0.8.0 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- #Ejabberd Management script
3
+ ####################################
4
+ # EMANAGEMENT
5
+ #Ejabberd Management script
4
6
  #New features for management and maintenance ejabberd
5
- #@author Aldo
7
+ #@author Aldo Gordillo < agordillos@gmail.com >
8
+ #@version 2.0 - 24-2-2012
9
+ ####################################
6
10
 
7
11
  require 'logger'
8
12
 
@@ -27,9 +31,7 @@ def getOption(option)
27
31
  end
28
32
 
29
33
 
30
-
31
34
  #Configuration variables
32
- $domain = getOption("server_domain=")
33
35
  $verbose = (getOption("verbose=")=="true")
34
36
  $ejabberd_user = getOption("ejabberd_server_user=")
35
37
  $checkEjabberdctlQuotedString = false
@@ -42,11 +44,12 @@ PARAMS_FOR_COMMANDS = {
42
44
  'unsetBidireccionalBuddys' => 4,
43
45
  'getRoster' => 1,
44
46
  'removeRoster' => 1,
47
+ 'removeAllRostersByDomain' => 1,
45
48
  'removeAllRosters' => 0,
46
- 'getAllUsersWithRoster' => 0,
47
- 'printAllRosters' => 0,
48
- 'printAllBidirecctionalBuddys' => 0,
49
- 'checkUser' => 1,
49
+ 'getAllUserJidsWithRosterByDomain' => 1,
50
+ 'printAllRostersByDomain' => 1,
51
+ 'printAllBidirecctionalBuddysByDomain' => 1,
52
+ 'checkUserJid' => 1,
50
53
  'checkBidirecctionalBuddys' => 2,
51
54
  'sendPresence' => 2,
52
55
  'setPresence' => 1,
@@ -55,39 +58,45 @@ PARAMS_FOR_COMMANDS = {
55
58
  'getUserResource' => 1,
56
59
  'isEjabberdNodeStarted' => 0,
57
60
  'broadcast' => 3,
58
- 'checkEjabberdctlQuotedString' => 0,
59
- 'getConnectedUsers' => 0,
61
+ 'broadcastToConnectedUsers' => 3,
62
+ 'getConnectedJidsByDomain' => 1,
63
+ 'getConnectedJids' => 0,
64
+ 'kickUserJid' => 1,
60
65
  'help' => 0,
61
66
  }
62
67
 
63
68
  SYNTAX_FOR_COMMANDS = {
64
- 'addBuddyToRoster' => 'addBuddyToRoster userSid buddySid buddyNick buddyGroup subscription_type',
65
- 'removeBuddyFromRoster' => 'removeBuddyFromRoster userSid buddySid',
66
- 'setBidireccionalBuddys' => 'setBidireccionalBuddys userASid userBSid userANick userBNick groupForA groupForB',
67
- 'unsetBidireccionalBuddys' => 'unsetBidireccionalBuddys userSid oldFriendSid oldFriendNick groupForOldFriend',
68
- 'getRoster' => 'getRoster username',
69
- 'removeRoster' => 'removeRoster username',
69
+ 'addBuddyToRoster' => 'addBuddyToRoster userJid buddyJid buddyNick buddyGroup subscription_type',
70
+ 'removeBuddyFromRoster' => 'removeBuddyFromRoster userJid buddyJid',
71
+ 'setBidireccionalBuddys' => 'setBidireccionalBuddys userAJid userBJid userANick userBNick groupForA groupForB',
72
+ 'unsetBidireccionalBuddys' => 'unsetBidireccionalBuddys userJid oldFriendJid oldFriendNick groupForOldFriend',
73
+ 'getRoster' => 'getRoster userJid',
74
+ 'removeRoster' => 'removeRoster userJid',
75
+ 'removeAllRostersByDomain' => 'removeAllRostersByDomain domain',
70
76
  'removeAllRosters' => 'removeAllRosters',
71
- 'getAllUsersWithRoster' => 'getAllUsersWithRoster',
72
- 'printAllRosters' => 'printAllRosters',
73
- 'printAllBidirecctionalBuddys' => 'printAllBidirecctionalBuddys',
74
- 'checkUser' => 'checkUser user',
75
- 'checkBidirecctionalBuddys' => 'checkBidirecctionalBuddys userASid userBSid',
76
- 'sendPresence' => 'sendPresence username show',
77
- 'setPresence' => 'setPresence username',
78
- 'unsetPresence' => 'unsetPresence username',
79
- 'sendMessageToUser' => 'sendMessageToUser from_name to_name msg',
80
- 'getUserResource' => 'getUserResource username',
77
+ 'getAllUserJidsWithRosterByDomain' => 'getAllUserJidsWithRosterByDomain domain',
78
+ 'printAllRostersByDomain' => 'printAllRostersByDomain domain',
79
+ 'printAllBidirecctionalBuddysByDomain' => 'printAllBidirecctionalBuddysByDomain domain',
80
+ 'checkUserJid' => 'checkUserJid userJid',
81
+ 'checkBidirecctionalBuddys' => 'checkBidirecctionalBuddys userAJid userBJid',
82
+ 'sendPresence' => 'sendPresence userJid show',
83
+ 'setPresence' => 'setPresence userJid',
84
+ 'unsetPresence' => 'unsetPresence userJid',
85
+ 'sendMessageToUser' => 'sendMessageToUser fromJid toJid msg',
86
+ 'getUserResource' => 'getUserResource userJid',
81
87
  'isEjabberdNodeStarted' => 'isEjabberdNodeStarted',
82
- 'broadcast' => 'broadcast admin users msg (users values: "all" or slugs array)',
83
- 'checkEjabberdctlQuotedString' => 'checkEjabberdctlQuotedString',
84
- 'getConnectedUsers' => 'getConnectedUsers',
88
+ 'broadcast' => 'broadcast admin userJids msg ',
89
+ 'broadcastToConnectedUsers' => 'broadcastToConnectedUsers admin userJids msg (userJids value: "all" or jids array [jid1,jid2,...,jidN])',
90
+ 'getConnectedJidsByDomain' => 'getConnectedJidsByDomain domain',
91
+ 'getConnectedJids' => 'getConnectedJids',
92
+ 'kickUserJid' => 'kickUserJid(userJid)',
85
93
  'help' => 'help',
86
94
  }
87
95
 
88
96
 
89
-
97
+ #########################
90
98
  #Debug methods
99
+ #########################
91
100
  def ejabberdLog(text)
92
101
  $logger.info "Ejabberd Management Script: " + text
93
102
  if $verbose
@@ -109,93 +118,99 @@ def logWithTitle(msg,title)
109
118
  end
110
119
 
111
120
 
112
-
121
+ #########################
113
122
  #Methods to manage rosters from Social Stream Rails App
114
-
115
- def setBidireccionalBuddys(userASid,userBSid,userANick,userBNick,groupForA,groupForB)
116
- addBuddyToRoster(userASid,userBSid,userBNick,groupForB,"both")
117
- addBuddyToRoster(userBSid,userASid,userANick,groupForA,"both")
123
+ #########################
124
+ def setBidireccionalBuddys(userAJid,userBJid,userANick,userBNick,groupForA,groupForB)
125
+ addBuddyToRoster(userAJid,userBJid,userBNick,groupForB,"both")
126
+ addBuddyToRoster(userBJid,userAJid,userANick,groupForA,"both")
118
127
  return "Done"
119
128
  end
120
129
 
121
- def unsetBidireccionalBuddys(userSid,oldFriendSid,oldFriendNick,groupForOldFriend)
122
- if checkBidirecctionalBuddys(userSid,oldFriendSid)
123
- removeBuddyFromRoster(userSid,oldFriendSid)
124
- removeBuddyFromRoster(oldFriendSid,userSid)
125
- addBuddyToRoster(userSid,oldFriendSid,oldFriendNick,groupForOldFriend,"to")
130
+ def unsetBidireccionalBuddys(userJid,oldFriendJid,oldFriendNick,groupForOldFriend)
131
+ if checkBidirecctionalBuddys(userJid,oldFriendJid)
132
+ removeBuddyFromRoster(userJid,oldFriendJid)
133
+ removeBuddyFromRoster(oldFriendJid,userJid)
134
+ addBuddyToRoster(userJid,oldFriendJid,oldFriendNick,groupForOldFriend,"to")
126
135
  return "Done"
127
136
  else
128
- return userSid + " and " + oldFriendSid + " aren't bidireccional buddys"
137
+ return userJid + " and " + oldFriendJid + " aren't bidireccional buddys"
129
138
  end
130
139
  end
131
140
 
132
- def addBuddyToRoster(userSid,buddySid,buddyNick,buddyGroup,subscription_type)
133
- user = userSid.split("@")[0]
134
- buddy = buddySid.split("@")[0]
135
- userDomain = userSid.split("@")[1]
136
- buddyDomain = buddySid.split("@")[1]
141
+ def addBuddyToRoster(userJid,buddyJid,buddyNick,buddyGroup,subscription_type)
142
+ user = getUsernameFromJid(userJid)
143
+ buddy = getUsernameFromJid(buddyJid)
144
+ userDomain = getDomainFromJid(userJid)
145
+ buddyDomain = getDomainFromJid(buddyJid)
137
146
  executeCommand("ejabberdctl add-rosteritem " + user + " " + userDomain + " " + buddy + " " + buddyDomain + " " + buddyNick + " " + buddyGroup + " " + subscription_type)
138
147
  return "Done"
139
148
  end
140
149
 
141
- def removeBuddyFromRoster(userSid,buddySid)
142
- user = userSid.split("@")[0]
143
- buddy = buddySid.split("@")[0]
144
- userDomain = userSid.split("@")[1]
145
- buddyDomain = buddySid.split("@")[1]
146
- if checkUserInRoster(buddy,getRoster(user))
150
+ def removeBuddyFromRoster(userJid,buddyJid)
151
+ user = getUsernameFromJid(userJid)
152
+ buddy = getUsernameFromJid(buddyJid)
153
+ userDomain = getDomainFromJid(userJid)
154
+ buddyDomain = getDomainFromJid(buddyJid)
155
+ if checkUserJidInRoster(buddyJid,getRoster(userJid))
147
156
  executeCommand("ejabberdctl delete_rosteritem " + user + " " + userDomain + " " + buddy + " " + buddyDomain)
148
157
  return "Done"
149
158
  else
150
- return "User " + buddy + " not found in " + user + " roster."
159
+ return "User " + buddyJid + " not found in " + userJid + " roster."
151
160
  end
152
161
  end
153
162
 
154
163
 
164
+ #########################
155
165
  #Roster Utilities
156
-
157
- def getRoster(username)
158
- if checkUser(username)
159
- executeCommand("ejabberdctl get_roster " + username + " " + $domain)
166
+ #########################
167
+ def getRoster(userJid)
168
+ if checkUserJid(userJid)
169
+ executeCommand("ejabberdctl get_roster " + getUsernameFromJid(userJid) + " " + getDomainFromJid(userJid))
160
170
  else
161
- return "Roster not found for user " + username
171
+ return "Roster not found for user " + userJid
162
172
  end
163
173
  end
164
174
 
165
- def getBuddysFromRoster(roster)
166
- buddys = []
175
+ def getBuddyJidsFromRoster(roster)
176
+ buddyJids = []
167
177
  lines = roster.split("\n")
168
178
  lines.each do |line|
169
- buddys << line.split("@")[0]
179
+ buddyJids << line.split(" ")[0]
170
180
  end
171
- buddys
181
+ buddyJids
172
182
  end
173
183
 
174
- def removeRoster(username)
175
- if checkUser(username)
176
- user_sid = username + "@" + $domain
177
- user_roster = getRoster(username);
178
- user_buddys = getBuddysFromRoster(user_roster);
179
- user_buddys.each do |buddy|
180
- buddy_sid = buddy + "@" + $domain
181
- removeBuddyFromRoster(user_sid,buddy_sid)
182
- end
184
+ def removeRoster(userJid)
185
+ if checkUserJid(userJid)
186
+ executeCommand("ejabberdctl process_rosteritems delete any any " + userJid + " any")
183
187
  return "Done"
184
188
  else
185
- return "Roster not found for user " + username
189
+ return "Roster not found for userJid " + userJid
186
190
  end
187
191
  end
188
192
 
189
- def removeAllRosters()
190
- executeCommand("ejabberdctl process_rosteritems delete any any any any")
193
+ def removeAllRostersByDomain(domain)
194
+ if(domain=="all")
195
+ executeCommand("ejabberdctl process_rosteritems delete any any any any")
196
+ else
197
+ executeCommand("ejabberdctl process_rosteritems delete any any *@" + domain + " any")
198
+ end
191
199
  return "Done";
192
200
  end
193
201
 
194
- def getAllUsersWithRoster()
195
- output = executeCommand("ejabberdctl process_rosteritems list any any any any")
202
+ def removeAllRosters()
203
+ return removeAllRostersByDomain("all")
204
+ end
196
205
 
197
- users = []
206
+ def getAllUserJidsWithRosterByDomain(domain)
207
+ if(domain=="all")
208
+ output = executeCommand("ejabberdctl process_rosteritems list any any any any")
209
+ else
210
+ output = executeCommand("ejabberdctl process_rosteritems list any any *@" + domain + " any")
211
+ end
198
212
 
213
+ userJids = []
199
214
  lines = output.split("\n");
200
215
  items = lines[0].split(" ")[2]
201
216
 
@@ -204,180 +219,178 @@ def getAllUsersWithRoster()
204
219
  if items.to_i > 0
205
220
  lines.each do |line|
206
221
  if line.split(":")[0]=="Matches"
207
- user = line.split(" ")[1].split("@")[0]
208
- #puts i.to_s() + " :" + line
209
- unless users.include?(user)
210
- users << user
222
+ userJid = line.split(" ")[1]
223
+ #puts "Line:" + line
224
+ unless userJids.include?(userJid)
225
+ userJids << userJid
211
226
  end
212
227
  end
213
228
  end
214
229
  end
215
230
  end
216
231
 
217
- return users;
232
+ return userJids;
218
233
  end
219
234
 
220
- def getAllRosters()
235
+ def getAllUserJidsWithRoster()
236
+ return getAllUserJidsWithRosterByDomain("all")
237
+ end
238
+
239
+ def getAllRostersByDomain(domain)
221
240
  rosterList = { }
222
241
 
223
- users = getAllUsersWithRoster()
242
+ userJids = getAllUserJidsWithRosterByDomain(domain)
224
243
 
225
- users.each do |user|
226
- roster = getRoster(user)
227
- rosterList.store(user,roster)
244
+ userJids.each do |userJid|
245
+ roster = getRoster(userJid)
246
+ rosterList.store(userJid,roster)
228
247
  end
229
248
  return rosterList
230
249
  end
231
250
 
232
- def printAllRosters()
233
- rosterList = getAllRosters()
234
- rosterList.keys.each do |user|
251
+ def getAllRosters()
252
+ return getAllRostersByDomain("all");
253
+ end
254
+
255
+ def printAllRostersByDomain(domain)
256
+ rosterList = getAllRostersByDomain(domain)
257
+ rosterList.keys.each do |userJid|
235
258
  puts "\n"
236
259
  puts "-------------------------------------"
237
- puts user + " Roster"
260
+ puts userJid + " Roster"
238
261
  puts "-------------------------------------"
239
- puts rosterList[user]
262
+ puts rosterList[userJid]
240
263
  puts "-------------------------------------"
241
264
  puts "\n"
242
265
  end
243
266
  return "Done"
244
267
  end
245
268
 
246
- def getAllBidirecctionalBuddys()
247
- b_buddys = []
248
- users = getAllUsersWithRoster()
249
- nonCheckedUsers = users
250
- users.each do |user|
251
- nonCheckedUsers.delete(user)
252
- nonCheckedUsers.each do |checkUser|
253
- if checkBidirecctionalBuddys(user + "@" + $domain,checkUser + "@" + $domain)
254
- b_buddys << [user,checkUser]
269
+ def printAllRosters()
270
+ return printAllRostersByDomain("all")
271
+ end
272
+
273
+ def getAllBidirecctionalBuddysByDomain(domain)
274
+ b_buddyJids = []
275
+ userJids = getAllUserJidsWithRosterByDomain(domain)
276
+ nonCheckedUsers = userJids
277
+ userJids.each do |userJid|
278
+ nonCheckedUsers.delete(userJid)
279
+ nonCheckedUsers.each do |checkUserJid|
280
+ if checkBidirecctionalBuddys(userJid,checkUserJid)
281
+ b_buddyJids << [userJid,checkUserJid]
255
282
  end
256
283
  end
257
284
  end
258
- return b_buddys
285
+ return b_buddyJids
259
286
  end
260
287
 
261
- def printAllBidirecctionalBuddys()
288
+ def getAllBidirecctionalBuddys()
289
+ return getAllBidirecctionalBuddysByDomain("all")
290
+ end
291
+
292
+ def printAllBidirecctionalBuddysByDomain(domain)
262
293
  puts "This may take a while..."
263
- b_buddys = getAllBidirecctionalBuddys
294
+ b_buddys = getAllBidirecctionalBuddysByDomain(domain)
264
295
  b_buddys.each do |contact|
265
296
  puts "[" + contact[0] + "," + contact[1] + "]"
266
297
  end
267
298
  return "Done"
268
299
  end
269
300
 
270
- #Check if the user have a roster
271
- def checkUser(user)
272
- return getAllUsersWithRoster().include?(user)
301
+ def printAllBidirecctionalBuddys()
302
+ return printAllBidirecctionalBuddysByDomain("all")
273
303
  end
274
304
 
275
- def checkUserInRoster(user,roster)
276
- return getBuddysFromRoster(roster).include?(user)
305
+ #Check if the user have a roster in his domain
306
+ def checkUserJid(userJid)
307
+ domain = getDomainFromJid(userJid)
308
+ return getAllUserJidsWithRosterByDomain(domain).include?(userJid)
277
309
  end
278
310
 
279
- def checkBidirecctionalBuddys(userASid,userBSid)
280
- userA = userASid.split("@")[0]
281
- userB = userBSid.split("@")[0]
282
- rosterA = getRoster(userA)
283
- rosterB = getRoster(userB)
284
- return (checkUserInRoster(userA,rosterB) and checkUserInRoster(userB,rosterA))
311
+ def checkUserJidInRoster(userJid,roster)
312
+ return getBuddyJidsFromRoster(roster).include?(userJid)
285
313
  end
286
314
 
315
+ def checkBidirecctionalBuddys(userAJid,userBJid)
316
+ rosterA = getRoster(userAJid)
317
+ rosterB = getRoster(userBJid)
318
+ return (checkUserJidInRoster(userAJid,rosterB) and checkUserJidInRoster(userBJid,rosterA))
319
+ end
287
320
 
288
- #Manage stanzas Utilities
289
321
 
290
- def setPresence(username)
291
- sendPresenceStanzaWithType(username,username,"available")
322
+ #########################
323
+ #Manage stanzas Utilities
324
+ #########################
325
+ def setPresence(userJid)
326
+ sendPresenceStanzaWithType(userJid,userJid,"available")
292
327
  end
293
328
 
294
- def unsetPresence(username)
295
- sendPresenceStanzaWithType(username,username,"unavailable")
329
+ def unsetPresence(userJid)
330
+ sendPresenceStanzaWithType(userJid,userJid,"unavailable")
296
331
  end
297
332
 
298
333
  def sendPresence(username,show)
299
334
  sendPresenceWithShow(username,username,show)
300
335
  end
301
336
 
302
- def sendPresenceWithShow(from_name,to_name,show)
303
- #puts from_name
304
- resource = getUserResource(from_name);
305
- #puts resource
306
- from_sid = from_name + "@" + $domain;
307
- to_sid = to_name + "@" + $domain;
308
- pres_stanza = "\\<" + buildQuotedString("presence from=") + "\\\"" + buildQuotedString(from_sid) + "\\\"" + buildQuotedString(" to=") + "\\\"" +
309
- buildQuotedString(to_sid) + "\\\"\\>\\<" + buildQuotedString("show") + "\\>" + buildQuotedString(show) + "\\<" +
337
+ def sendPresenceWithShow(fromJid,toJid,show)
338
+ resource = getUserResource(fromJid);
339
+ from_name = getUsernameFromJid(fromJid)
340
+ domain = getDomainFromJid(fromJid)
341
+ pres_stanza = "\\<" + buildQuotedString("presence from=") + "\\\"" + buildQuotedString(fromJid) + "\\\"" + buildQuotedString(" to=") + "\\\"" +
342
+ buildQuotedString(toJid) + "\\\"\\>\\<" + buildQuotedString("show") + "\\>" + buildQuotedString(show) + "\\<" +
310
343
  buildQuotedString("/show") + "\\>\\<" + buildQuotedString("/presence") + "\\>"
311
- executeCommand("ejabberdctl send_stanza_c2s " + from_name + " " + $domain + " " + resource + " " + pres_stanza)
344
+ executeCommand("ejabberdctl send_stanza_c2s " + from_name + " " + domain + " " + resource + " " + pres_stanza)
312
345
  return "Done"
313
346
  end
314
347
 
315
- def sendPresenceStanzaWithType(from_name,to_name,presence_type)
316
- resource = getUserResource(from_name);
317
- from_sid = from_name + "@" + $domain;
318
- to_sid = to_name + "@" + $domain;
348
+ def sendPresenceStanzaWithType(fromJid,toJid,presence_type)
349
+ resource = getUserResource(fromJid);
350
+ from_name = getUsernameFromJid(fromJid)
351
+ domain = getDomainFromJid(fromJid)
319
352
  pres_stanza = "\\<" + buildQuotedString("presence type=") + "\\\"" + buildQuotedString(presence_type) + "\\\"" + buildQuotedString(" from=") + "\\\"" +
320
- buildQuotedString(from_sid) + "\\\"" + buildQuotedString(" to=") + "\\\"" + buildQuotedString(to_sid) + "\\\"\\>\\<" + buildQuotedString("/presence") + "\\>"
321
- executeCommand("ejabberdctl send_stanza_c2s " + from_name + " " + $domain + " " + resource + " " + pres_stanza)
353
+ buildQuotedString(fromJid) + "\\\"" + buildQuotedString(" to=") + "\\\"" + buildQuotedString(toJid) + "\\\"\\>\\<" + buildQuotedString("/presence") + "\\>"
354
+ executeCommand("ejabberdctl send_stanza_c2s " + from_name + " " + domain + " " + resource + " " + pres_stanza)
322
355
  return "Done"
323
356
  end
324
357
 
325
- def sendMessageToUser(from_name,to_name,msg)
326
- from_sid = from_name + "@" + $domain;
327
- to_sid = to_name + "@" + $domain;
328
- executeCommand("ejabberdctl send_message_chat " + from_sid + " " + to_sid + " " + buildQuotedString(msg))
358
+ def sendMessageToUser(fromJid,toJid,msg)
359
+ executeCommand("ejabberdctl send_message_chat " + fromJid + " " + toJid + " " + buildQuotedString(msg))
329
360
  return "Done"
330
361
  end
331
362
 
332
- def getUserResource(username)
363
+ def getUserResource(userJid)
333
364
  output = executeCommand("ejabberdctl connected-users")
334
365
  lines = output.split("\n");
335
366
  lines.each do |line|
336
- if line.split("@")[0] == username
337
- #puts "Find " + username
338
- s = line.split("@")[1];
339
- resource = s.split("/")[1];
367
+ if line.split("/")[0] == userJid
368
+ #puts "Find " + userJid
369
+ resource = line.split("/")[1];
340
370
  return resource;
341
371
  end
342
372
  end
343
- return username + " no have any active session"
373
+ return userJid + " no have any active session"
344
374
  end
345
375
 
346
376
 
347
- #More utilities
348
-
349
- def isEjabberdNodeStarted
350
- output = executeCommand("ejabberdctl status")
351
- if firstLine = output.split("\n")[0]
352
- return ((firstLine.split(":")[1]).strip()=="started")
353
- end
354
- return false
377
+ #########################
378
+ #Utilities
379
+ #########################
380
+ def getUsernameFromJid(jid)
381
+ return jid.split("@")[0];
355
382
  end
356
383
 
357
- def getConnectedUsers
358
- users = []
359
- output = executeCommand("ejabberdctl connected-users")
360
- sessions = output.split("\n")
361
- sessions.each do |session|
362
- users << session.split("@")[0]
363
- end
364
- return users
384
+ def getDomainFromJid(jid)
385
+ return jid.split("@")[1];
365
386
  end
366
387
 
367
- def broadcast(admin,users,msg)
368
- output = executeCommand("ejabberdctl connected-users")
369
- lines = output.split("\n");
370
- lines.each do |line|
371
- username = line.split("@")[0]
372
- if (users == "all") or (users.length > 1 and users.include?(username))
373
- s = line.split("@")[1];
374
- resource = s.split("/")[1];
375
- sendMessageToUser(admin,username,msg)
376
- end
377
- end
378
- return "Done"
388
+ def getElementsFromStringArray(stringArray)
389
+ stringArray=stringArray[1,stringArray.length-2]
390
+ return stringArray.split(",")
379
391
  end
380
392
 
393
+
381
394
  #Determine how to scape characters for build quoted strings
382
395
  def checkEjabberdctlQuotedString
383
396
  puts "checkForSimpleSlash: " + checkForSimpleSlash.to_s()
@@ -461,8 +474,84 @@ def buildQuotedString(msg)
461
474
  end
462
475
 
463
476
 
464
- #Help & Support methods
477
+ #########################
478
+ #Connection Info
479
+ #########################
480
+ def isEjabberdNodeStarted
481
+ output = executeCommand("ejabberdctl status")
482
+ if firstLine = output.split("\n")[0]
483
+ return ((firstLine.split(":")[1]).strip()=="started")
484
+ end
485
+ return false
486
+ end
465
487
 
488
+ def getConnectedJidsByDomain(domain)
489
+ jids = []
490
+ if(domain=="all")
491
+ output = executeCommand("ejabberdctl connected-users")
492
+ else
493
+ output = executeCommand("ejabberdctl connected-users | grep @" + domain + "/")
494
+ end
495
+ sessions = output.split("\n")
496
+ sessions.each do |session|
497
+ jids << session.split("/")[0]
498
+ end
499
+ return jids
500
+ end
501
+
502
+ def getConnectedJids()
503
+ return getConnectedJidsByDomain("all")
504
+ end
505
+
506
+
507
+ #########################
508
+ #Advanced features
509
+ #########################
510
+ def broadcast(admin,userJids,msg)
511
+ getElementsFromStringArray(userJids).each do |userJid|
512
+ sendMessageToUser(admin,userJid,msg)
513
+ end
514
+ return "Done"
515
+ end
516
+
517
+ def broadcastToConnectedUsers(admin,userJids,msg)
518
+ connectedJids = getConnectedJids()
519
+ if(userJids=="all")
520
+ connectedJids.each do |userJid|
521
+ sendMessageToUser(admin,userJid,msg)
522
+ end
523
+ else
524
+ getElementsFromStringArray(userJids).each do |userJid|
525
+ if (connectedJids.include?(userJid))
526
+ sendMessageToUser(admin,userJid,msg)
527
+ end
528
+ end
529
+ end
530
+ return "Done"
531
+ end
532
+
533
+ def kickUserJid(userJid)
534
+ user = getUsernameFromJid(userJid)
535
+ userDomain = getDomainFromJid(userJid)
536
+ resource = getUserResource(userJid);
537
+ reason = "0"
538
+ executeCommand("ejabberdctl kick_session " + user + " " + userDomain + " " + resource + " " + reason)
539
+ return "Done"
540
+ end
541
+
542
+ def banUserJid(userJid)
543
+ kickUserJid(userJid)
544
+ #Notify Web server to ban user permanently
545
+ end
546
+
547
+
548
+
549
+
550
+
551
+
552
+ #########################
553
+ #Execution commands methods
554
+ #########################
466
555
  def executeCommand(command)
467
556
  #Building...
468
557
  command = buildCommand(command)
@@ -499,6 +588,10 @@ def execute_as_sudo
499
588
  end
500
589
  end
501
590
 
591
+
592
+ #########################
593
+ #Support
594
+ #########################
502
595
  def help
503
596
  log("Command list")
504
597
  SYNTAX_FOR_COMMANDS.values.each do |command|
@@ -509,8 +602,9 @@ end
509
602
 
510
603
 
511
604
 
605
+ #########################
512
606
  #Main thread
513
-
607
+ #########################
514
608
  #log("Init Ejabberd Maintenance script")
515
609
 
516
610
  begin
@@ -545,11 +639,14 @@ begin
545
639
  puts "Use 'help' to get information about command syntax"
546
640
  end
547
641
  else
548
- puts "Error: Command not recognized"
549
- puts "Use 'help' to get information about command syntax"
642
+ if (ARGV.length==0)
643
+ help
644
+ else
645
+ puts "Error: Command not recognized"
646
+ puts "Use 'help' to get information about command syntax"
647
+ end
550
648
  end
551
649
  rescue
552
650
  puts "Syntax error"
553
651
  puts "Use 'help' to get information about command syntax"
554
652
  end
555
-