social_stream-presence 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/app/assets/javascripts/chat_interface_manager.js.erb +43 -5
  2. data/app/assets/javascripts/chat_utilities.js +26 -0
  3. data/app/assets/javascripts/xmpp_client_management.js.erb +5 -1
  4. data/app/assets/stylesheets/chat.css +24 -0
  5. data/app/views/chat/_off.html.erb +0 -3
  6. data/app/views/xmpp/active_users.html.erb +1 -1
  7. data/config/locales/en.yml +4 -2
  8. data/config/locales/es.yml +3 -1
  9. data/config/routes.rb +3 -3
  10. data/ejabberd/conf/ssconfig_example.cfg +14 -20
  11. data/ejabberd/ejabberd_files.zip +0 -0
  12. data/ejabberd/ejabberd_scripts/authentication_script +3 -13
  13. data/ejabberd/ejabberd_scripts/development_scripts/show_config.sh +13 -9
  14. data/ejabberd/ejabberd_scripts/emanagement +65 -22
  15. data/ejabberd/ejabberd_scripts/reset_connection_script +1 -1
  16. data/ejabberd/ejabberd_scripts/set_connection_script +1 -1
  17. data/ejabberd/ejabberd_scripts/set_presence_script +1 -1
  18. data/ejabberd/ejabberd_scripts/set_script_header.sh +112 -0
  19. data/ejabberd/ejabberd_scripts/synchronize_presence_script +4 -2
  20. data/ejabberd/ejabberd_scripts/unset_connection_script +1 -1
  21. data/ejabberd/ejabberd_scripts/unset_presence_script +48 -0
  22. data/ejabberd/installer.sh +267 -0
  23. data/ejabberd/mod_sspresence/mod_sspresence.beam +0 -0
  24. data/ejabberd/mod_sspresence/mod_sspresence.erl +31 -99
  25. data/lib/generators/social_stream/presence/install_generator.rb +1 -1
  26. data/lib/generators/social_stream/presence/templates/initializer.rb +6 -4
  27. data/lib/social_stream-presence.rb +2 -1
  28. data/lib/social_stream/presence/models/buddy_manager.rb +1 -1
  29. data/lib/social_stream/presence/version.rb +1 -1
  30. data/lib/social_stream/presence/xmpp_server_order.rb +128 -28
  31. data/lib/tasks/presence/installer.rake +100 -0
  32. data/social_stream-presence.gemspec +2 -0
  33. data/vendor/assets/javascripts/jquery.ui.chatbox.js +18 -2
  34. metadata +23 -5
@@ -1,10 +1,31 @@
1
+ %%%-------------------------------------------------------------------
2
+ %%% File : mod_sspresence.erl
3
+ %%% Author : Aldo
4
+ %%% Contact: < social-stream@dit.upm.es >
5
+ %%% Purpose : Process events and hooks for Social Stream Presence: http://social-stream.dit.upm.es/
6
+ %%% Created : 1 Oct 2011
7
+ %%%
8
+ %%%
9
+ %%% http://social-stream.dit.upm.es/
10
+ %%% Copyright © 2011 Social Stream
11
+ %%%
12
+ %%%-------------------------------------------------------------------
13
+
1
14
  -module(mod_sspresence).
15
+ -author('aldo').
2
16
 
3
17
  -behavior(gen_mod).
4
18
 
5
19
  -include("ejabberd.hrl").
6
20
 
7
- -export([start/2, stop/1, on_register_connection/3, on_remove_connection/3, on_presence/4, on_unset_presence/4, isConnected/1, on_packet_send/3]).
21
+ -export([start/2, stop/1,
22
+ on_register_connection/3,
23
+ on_remove_connection/3,
24
+ on_presence/4,
25
+ on_unset_presence/4,
26
+ on_packet_send/3,
27
+ isConnected/1
28
+ ]).
8
29
 
9
30
  start(Host, _Opts) ->
10
31
  ?INFO_MSG("mod_sspresence starting", []),
@@ -52,30 +73,25 @@ on_presence(User, _Server, _Resource, Packet) ->
52
73
 
53
74
  case Type of
54
75
  "presence" -> Status = getStatusFromSubel(Subel),
55
- Login_path = string:concat(getOptionValue("scripts_path="), "/set_presence_script "),
76
+ Presence_path = string:concat(getOptionValue("scripts_path="), "/set_presence_script "),
56
77
  ?INFO_MSG("mod_sspresence: set_presence_script call with user (~p) and status (~p)", [User,Status]),
57
- os:cmd(string:join([Login_path, User , Status], " "));
78
+ os:cmd(string:join([Presence_path, User , Status], " "));
58
79
  _ -> ok
59
80
  end,
60
81
  ok.
61
82
 
62
83
  on_unset_presence(User, _Server, _Resource, _Status) ->
63
84
  ?INFO_MSG("mod_sspresence: on_unset_presence (~p)", [User]),
85
+ _UPresence_path = string:concat(getOptionValue("scripts_path="), "/unset_presence_script "),
86
+ %% Wait for on_remove_connection
87
+ %% ?INFO_MSG("mod_sspresence: unset_presence_script call with user (~p)", [User]),
88
+ %%os:cmd(string:join([UPresence_path, User , Status], " "));
64
89
  ok.
65
90
 
66
- on_packet_send(From, _To, {xmlelement, Type, _Attr, Subel} = _Packet) ->
91
+ on_packet_send(_From, _To, {xmlelement, Type, _Attr, _Subel} = _Packet) ->
67
92
  case Type of
68
- "message" ->
69
- {_SenderJID,Sender,_SenderDomain,_A,_B,_C,_D} = From,
70
-
71
- SSlogin = getOptionValue("ss_login="),
72
-
73
- case Sender of
74
- SSlogin ->
75
- Message = getMessageFromSubel(Subel),
76
- execute(Message);
77
- _ -> ok
78
- end;
93
+ "message" ->
94
+ ok;
79
95
  _ -> ok
80
96
  end.
81
97
 
@@ -101,82 +117,11 @@ getStatusFromSubel(Subel) ->
101
117
  end.
102
118
 
103
119
 
104
- getMessageFromSubel(Subel) ->
105
- [{_I,_J,_K,[{_L,MessageData}]}] = Subel,
106
- parseXmlCdata(MessageData).
107
-
108
-
109
120
  parseXmlCdata(Msg) ->
110
121
  MessageString = binary_to_list(list_to_binary(io_lib:format("~p", [Msg]))),
111
122
  lists:sublist(MessageString, 4, string:len(MessageString)-6).
112
123
 
113
124
 
114
- execute(Message) ->
115
- %?INFO_MSG("Message vale: ~p", [Message]),
116
- %?INFO_MSG("Order vale: ~p", [Order]),
117
- [Order|Params] = string:tokens(Message, "&"),
118
- case Order of
119
- "AddItemToRoster" ->
120
- case length(Params) of
121
- 4 -> [UserSID,BuddySID,BuddyName,Subscription_type] = Params,
122
- ?INFO_MSG("Execute: ~p with params ~p", [Order,Params]),
123
- [ContactName|_R] = string:tokens(BuddyName, " "),
124
- [UserSlug,UserDomain] = string:tokens(UserSID, "@"),
125
- [BuddySlug,BuddyDomain] = string:tokens(BuddySID, "@"),
126
- addItemToRoster(UserSlug,UserDomain,BuddySlug,BuddyDomain,ContactName,"SocialStream",Subscription_type),
127
- ok;
128
- _ -> ?INFO_MSG("Incorrect parameters in order ~p", [Order]),
129
- ok
130
- end,
131
- ok;
132
-
133
- "SetRosterForBidirectionalTie" ->
134
- case length(Params) of
135
- 4 -> [UserASID,UserBSID,UserAName,UserBName] = Params,
136
- ?INFO_MSG("Executing: ~p with params ~p", [Order,Params]),
137
- [UserASlug,UserADomain] = string:tokens(UserASID, "@"),
138
- [UserBSlug,UserBDomain] = string:tokens(UserBSID, "@"),
139
- addItemToRoster(UserASlug,UserADomain,UserBSlug,UserBDomain,UserBName,"SocialStream","both"),
140
- addItemToRoster(UserBSlug,UserBDomain,UserASlug,UserADomain,UserAName,"SocialStream","both"),
141
- ok;
142
- _ -> ?INFO_MSG("Incorrect parameters in order ~p", [Order]),
143
- ok
144
- end,
145
- ok;
146
-
147
- "Synchronize" ->
148
- synchronizePresence(),
149
- ok;
150
-
151
- "SynchronizeRosters" ->
152
- synchronizeRosters(),
153
- ok;
154
-
155
- _ -> ?INFO_MSG("Command not found", []),
156
- ok
157
- end.
158
-
159
-
160
-
161
- %%Call ejabberdctl command add_rosteritem
162
- %Command Name: add_rosteritem
163
- %Needs mod_admin_extra (http://www.ejabberd.im/ejabberd-modules)
164
- %ejabberdctl add_rosteritem localuser localserver buddy buddyserver nick group subs
165
- %subs= none, from, to or both,
166
-
167
- %Example
168
- %ejabberdctl add_rosteritem frank-williamson trapo demo trapo NickName SocialStream from
169
- %frank-williamson@trapo adds demo@trapo to its roster with subscription from, in the group SocialStream with the name Nickname
170
- %%
171
- addItemToRoster(UserSlug,UserDomain,BuddySlug,BuddyDomain,ContactName,Group,Subscription_type) ->
172
- %%Command = lists:concat(["ejabberdctl add_rosteritem ", UserSlug , " ", UserDomain, " ", BuddySlug, " ", BuddyDomain , " \\'", ContactName , "\\' ", Group , " ", Subscription_type]),
173
- %%Command = lists:concat(["ejabberdctl add_rosteritem ", UserSlug , " ", UserDomain, " ", BuddySlug, " ", BuddyDomain , " \'", ContactName , "\' ", Group , " ", Subscription_type]),
174
-
175
- [UserName|_R] = string:tokens(ContactName, " "),
176
- Command = lists:concat(["ejabberdctl add_rosteritem ", UserSlug , " ", UserDomain, " ", BuddySlug, " ", BuddyDomain , " ", UserName , " ", Group , " ", Subscription_type]),
177
- os:cmd(Command),
178
- ?INFO_MSG("Execute command: ~p", [Command]),
179
- ok.
180
125
 
181
126
  %%GETTERS CONFIG VALUES
182
127
  %%CONFIG FILE: /etc/ejabberd/ssconfig.cfg
@@ -237,19 +182,6 @@ case Output of
237
182
  end.
238
183
 
239
184
 
240
- %%Send all connected users to Social Stream Rails Application
241
- synchronizePresence() ->
242
- Synchronize_path = string:concat(getOptionValue("scripts_path="), "/synchronize_presence_script "),
243
- os:cmd(Synchronize_path),
244
- ok.
245
-
246
-
247
- %%Reset all rosters and wait to received data from Social Stream Rails Application
248
- synchronizeRosters() ->
249
- SynchronizeRosters_path = string:concat(getOptionValue("scripts_path="), "/emanagement removeAllRosters"),
250
- os:cmd(SynchronizeRosters_path),
251
- ok.
252
-
253
185
 
254
186
  %Reset all connections
255
187
  reset_connections() ->
@@ -4,7 +4,7 @@ class SocialStream::Presence::InstallGenerator < Rails::Generators::Base
4
4
  source_root File.expand_path('../templates', __FILE__)
5
5
 
6
6
  def create_initializer_file
7
- copy_file 'initializer.rb', 'config/initializers/social_stream-presence.rb'
7
+ template 'initializer.rb', 'config/initializers/social_stream_presence.rb'
8
8
  end
9
9
 
10
10
  def create_migration_file
@@ -6,13 +6,15 @@ SocialStream::Presence.setup do |config|
6
6
  #Configures Authentication Method: "cookie" or "password"
7
7
  config.auth_method = "cookie"
8
8
  #Configures XMPP Server Password
9
- config.xmpp_server_password = "password"
9
+ config.xmpp_server_password = <%= SecureRandom.hex(64).inspect %>
10
10
  #Remote or local mode
11
11
  config.remote_xmpp_server = false
12
12
  #Scripts path to execute ejabberd scripts: local or remote
13
13
  config.scripts_path = "/scripts_path"
14
- #False to disable Social Stream Presence
15
- #config.enable = false
14
+ #Ejabberd module path in the xmpp server
15
+ config.ejabberd_module_path = "/lib/ejabberd/ebin"
16
+ #Uncomment to enable Social Stream Presence
17
+ #config.enable = true
16
18
 
17
19
  #Parameters for remote mode
18
20
  #SSH Login
@@ -25,6 +27,6 @@ SocialStream::Presence.setup do |config|
25
27
  #Username of the the Social Stream Admin sid
26
28
  config.social_stream_presence_username = "social_stream-presence"
27
29
  #Configures Social Stream Rails App Password
28
- config.password = "password"
30
+ config.password = <%= SecureRandom.hex(64).inspect %>
29
31
 
30
32
  end
@@ -22,6 +22,7 @@ module SocialStream
22
22
  mattr_accessor :xmpp_server_password
23
23
  mattr_accessor :remote_xmpp_server
24
24
  mattr_accessor :scripts_path
25
+ mattr_accessor :ejabberd_module_path
25
26
  mattr_accessor :enable
26
27
 
27
28
  mattr_accessor :ssh_domain
@@ -33,7 +34,7 @@ module SocialStream
33
34
 
34
35
  @@auth_method = "cookie"
35
36
  @@remote_xmpp_server = false
36
- @@enable = true
37
+ @@enable = false
37
38
 
38
39
  class << self
39
40
  def setup
@@ -39,7 +39,7 @@ module SocialStream
39
39
  elsif self.positive?
40
40
  #Case: Possitive tie unidirectional
41
41
  #Execute addBuddyToRoster(userSID,buddySID,buddyNick,buddyGroup,subscription_type)
42
- subscription_type = "to"
42
+ subscription_type = "from"
43
43
  SocialStream::Presence::XmppServerOrder::addBuddyToRoster(user_sid,buddy_sid,buddy_name,"SocialStream",subscription_type)
44
44
  else
45
45
  #Negative Tie
@@ -1,5 +1,5 @@
1
1
  module Socialstream
2
2
  module Presence
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
@@ -4,6 +4,7 @@ require 'xmpp4r/roster'
4
4
  require 'xmpp4r/client'
5
5
  require 'xmpp4r/message'
6
6
  require 'net/ssh'
7
+ require 'net/sftp'
7
8
 
8
9
  module SocialStream
9
10
  module Presence
@@ -31,32 +32,16 @@ module SocialStream
31
32
  end
32
33
 
33
34
 
35
+
34
36
  def synchronizePresence
35
-
36
- if !isEjabberdNodeUp
37
+ if isEjabberdNodeUp
38
+ output = executeEmanagementCommand("getConnectedUsers",[])
39
+ user_slugs = output.split("\n")
40
+ synchronizePresenceForSlugs(user_slugs)
41
+ else
37
42
  resetPresence
38
43
  return "Xmpp Server Down: Reset Connected Users"
39
- end
40
-
41
- if SocialStream::Presence.remote_xmpp_server
42
- command = buildCommand("synchronize_presence_script","",[])
43
- executeCommand(command)
44
- else
45
- #SocialStream::Presence.remote_xmpp_server=false
46
-
47
- #Get connected users locally
48
- users = []
49
- output = %x[ejabberdctl connected-users]
50
- sessions = output.split("\n")
51
-
52
- sessions.each do |session|
53
- users << session.split("@")[0]
54
- puts session.split("@")[0]
55
- end
56
-
57
- synchronizePresenceForSlugs(users)
58
-
59
- end
44
+ end
60
45
  end
61
46
 
62
47
 
@@ -93,6 +78,7 @@ module SocialStream
93
78
 
94
79
 
95
80
  def synchronizePresenceForSlugs(user_slugs)
81
+
96
82
  #Check connected users
97
83
  users = User.find_all_by_connected(true)
98
84
 
@@ -123,6 +109,117 @@ module SocialStream
123
109
  end
124
110
 
125
111
 
112
+ #Installation methods
113
+
114
+ def copyFolder(oPath,dPath)
115
+ if SocialStream::Presence.remote_xmpp_server
116
+ #Remote mode
117
+ copyRemoteFolder(oPath,dPath)
118
+ else
119
+ #Local mode
120
+ SocialStream::Presence::XmppServerOrder::executeCommand("cp -r " + oPath + "/* " + dPath)
121
+ end
122
+ end
123
+
124
+
125
+ def copyRemoteFolder(localPath,remotePath)
126
+ begin
127
+ if SocialStream::Presence.ssh_password
128
+
129
+ Net::SFTP.start(SocialStream::Presence.ssh_domain, SocialStream::Presence.ssh_user, :password => SocialStream::Presence.ssh_password, :auth_methods => ["password"] ) do |sftp|
130
+ recursiveCopyFolder(localPath,remotePath,sftp)
131
+ end
132
+
133
+ else
134
+ #SSH with authentication key instead of password
135
+ Net::SFTP.start(SocialStream::Presence.ssh_domain, SocialStream::Presence.ssh_user) do |sftp|
136
+ recursiveCopyFolder(localPath,remotePath,sftp)
137
+ end
138
+ end
139
+ output="Ok"
140
+ rescue Exception => e
141
+ case e
142
+ when Net::SSH::AuthenticationFailed
143
+ output = "AuthenticationFailed on remote access"
144
+ else
145
+ output = "Unknown exception in copyRemoteFolder method: #{e.to_s}"
146
+ end
147
+ end
148
+
149
+ return output
150
+ end
151
+
152
+
153
+ def recursiveCopyFolder(localPath,remotePath,sftp)
154
+ # Create directory if not exits
155
+ sftp.mkdir(remotePath)
156
+ # Upload files to the remote host
157
+ Dir.foreach(localPath) do |f|
158
+ file_path = localPath + "/#{f}"
159
+ if File.file?(file_path)
160
+ sftp.upload(file_path, remotePath + "/#{f}")
161
+ elsif File.directory?(file_path) and f!="." and f!=".."
162
+ recursiveCopyFolder(file_path,remotePath + "/#{f}",sftp)
163
+ end
164
+ end
165
+ end
166
+
167
+ def autoconf(options)
168
+ autoconf=[]
169
+
170
+ #Add autoconfiguration options
171
+ #autoconf.push("key=value")
172
+
173
+ autoconf.push("scripts_path=" + SocialStream::Presence.scripts_path)
174
+ autoconf.push("ejabberd_password=" + SocialStream::Presence.xmpp_server_password)
175
+ autoconf.push("server_domain=" + SocialStream::Presence.domain)
176
+ autoconf.push("cookie_name=" + Rails.application.config.session_options[:key])
177
+
178
+ #Param options
179
+ if options
180
+ options.each do |option|
181
+ autoconf = addManualOption(autoconf,option)
182
+ end
183
+ end
184
+
185
+ #return "[key1=value1,key2=value2,key3=value3]"
186
+ return "[" + autoconf.join(',') + "]"
187
+ end
188
+
189
+
190
+ def addManualOption(array,option)
191
+
192
+ optionSplit = option.split("=")
193
+ unless optionSplit.length == 2
194
+ return array
195
+ end
196
+
197
+ key = optionSplit[0];
198
+ array.each do |element|
199
+ if element.split("=")[0]==key
200
+ #Replace element
201
+ array[array.index(element)]=option
202
+ return array
203
+ end
204
+ end
205
+ #Add option
206
+ array.push(option)
207
+ return array
208
+ end
209
+
210
+
211
+ def getExecutorUser
212
+ if SocialStream::Presence.remote_xmpp_server
213
+ if SocialStream::Presence.ssh_user
214
+ return SocialStream::Presence.ssh_user
215
+ else
216
+ return nil
217
+ end
218
+ else
219
+ return %x["whoami"].gsub(/\n/,"");
220
+ end
221
+ end
222
+
126
223
 
127
224
  #Execution commands manage
128
225
 
@@ -148,11 +245,11 @@ module SocialStream
148
245
  if commands.length > 1
149
246
  puts "Executing the following commands:"
150
247
  commands.each do |command|
151
- puts command
248
+ puts parsingCommand(command)
152
249
  end
153
250
  puts "Command list finish"
154
251
  elsif commands.length == 1
155
- puts "Executing " + commands[0]
252
+ puts "Executing " + parsingCommand(commands[0])
156
253
  else
157
254
  puts "No command to execute"
158
255
  return
@@ -204,18 +301,21 @@ module SocialStream
204
301
 
205
302
  return output
206
303
  end
207
-
304
+
208
305
 
209
306
 
210
307
  #Help methods
211
308
 
212
309
  def isEjabberdNodeUp
213
310
  output = executeEmanagementCommand("isEjabberdNodeStarted",[])
214
- nodeUp = output.split("\n")[3]
311
+ nodeUp = output.split("\n")[0]
215
312
  return (nodeUp and nodeUp.strip()=="true")
216
313
  end
217
314
 
218
-
315
+ def parsingCommand(command)
316
+ #Hide passwords on sudo commands: command pattern = "echo password | sudo -S order"
317
+ return command.gsub(/echo ([aA-zZ]+) [|] sudo -S [.]*/,"echo ****** | sudo -S \\2")
318
+ end
219
319
 
220
320
  #Xmpp client manage methods
221
321