jsc 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.bnsignore +18 -0
- data/.gitignore +23 -3
- data/History.txt +14 -0
- data/LICENSE +20 -0
- data/README.rdoc +15 -27
- data/Rakefile +46 -20
- data/TODO.org +5 -11
- data/VERSION +1 -0
- data/bin/jsc +18 -6
- data/features/run_command.feature +67 -0
- data/features/support/env.rb +8 -0
- data/lib/jsc.rb +1 -1
- data/lib/jsc/closure_compiler.rb +48 -26
- data/lib/jsc/parser.rb +75 -0
- data/spec/jsc_spec.rb +7 -7
- metadata +39 -44
- data/js/extra/alcadialer_im.js +0 -559
- data/js/extra/strophe.js +0 -3543
- data/jsc.gemspec +0 -66
- data/rdoc/classes/JSCompiler.html +0 -649
- data/rdoc/classes/String.html +0 -118
- data/rdoc/created.rid +0 -1
- data/rdoc/files/README_rdoc.html +0 -234
- data/rdoc/files/lib/jsc/closure_compiler_rb.html +0 -151
- data/rdoc/files/lib/jsc_rb.html +0 -101
- data/rdoc/fr_class_index.html +0 -28
- data/rdoc/fr_file_index.html +0 -29
- data/rdoc/fr_method_index.html +0 -38
- data/rdoc/index.html +0 -26
- data/rdoc/rdoc-style.css +0 -208
- data/tmp/aruba/js/compiled_code.js +0 -4
- data/tmp/aruba/js/errors.js +0 -4
- data/tmp/aruba/js/warnings.js +0 -5
data/lib/jsc/parser.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
module JSCompiler
|
2
|
+
module Parser
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
# this methos calls the right parser for result
|
7
|
+
def parse(result)
|
8
|
+
return flymake_parser(result) if JSCompiler.format_type == "flymake"
|
9
|
+
default_parser(result)
|
10
|
+
end
|
11
|
+
|
12
|
+
# window_message_handlers.js:73: strict warning: trailing comma is not legal in ECMA-262 object initializers:
|
13
|
+
def flymake_parser(result)
|
14
|
+
if $debug
|
15
|
+
puts "#DEBUG flymake parser \n"
|
16
|
+
end
|
17
|
+
|
18
|
+
#TODO
|
19
|
+
out = ""
|
20
|
+
op = JSCompiler.op
|
21
|
+
unless result.nil?
|
22
|
+
file = JSCompiler.file
|
23
|
+
format_type = JSCompiler.format_type
|
24
|
+
|
25
|
+
num = result.size
|
26
|
+
result.each do |message|
|
27
|
+
out << "#{file}:#{message['lineno']}: #{op.singularize}: #{message['type']}: " + message[op.singularize] +"\n"
|
28
|
+
out << "#{file}:#{message['lineno']}: #{op.singularize}: #{message['line']} \n" unless message['line'].nil?
|
29
|
+
out << "#{file}:#{message['lineno']}: #{op.singularize}: " + print_under_character(message['charno'])
|
30
|
+
end
|
31
|
+
else
|
32
|
+
out = "No #{op}"
|
33
|
+
end
|
34
|
+
return out
|
35
|
+
end
|
36
|
+
|
37
|
+
def default_parser(result)
|
38
|
+
out = ""
|
39
|
+
|
40
|
+
if $debug
|
41
|
+
puts "#DEBUG default parser \n"
|
42
|
+
end
|
43
|
+
|
44
|
+
op = JSCompiler.op
|
45
|
+
unless result.nil?
|
46
|
+
num = result.size
|
47
|
+
out << "You've got #{result.size} #{op}\n"
|
48
|
+
i = 0
|
49
|
+
result.each do |message|
|
50
|
+
i += 1
|
51
|
+
out << "\n#{op.singularize.capitalize} n.#{i}\n"
|
52
|
+
out << "\t#{message['type']}: " + message[op.singularize] + " at line #{message['lineno']} character #{message['charno']}\n"
|
53
|
+
out << "\t" + message['line'] + "\n" unless message['line'].nil?
|
54
|
+
out << "\t" + print_under_character(message['charno'])
|
55
|
+
end
|
56
|
+
out
|
57
|
+
else
|
58
|
+
"No #{op}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def print_under_character(pos)
|
63
|
+
i = 0
|
64
|
+
auto_fill = ""
|
65
|
+
while i < pos -1
|
66
|
+
auto_fill << "."
|
67
|
+
i = i+1
|
68
|
+
end
|
69
|
+
|
70
|
+
auto_fill << " ^ \n"
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/spec/jsc_spec.rb
CHANGED
@@ -82,7 +82,7 @@ describe JSCompiler do
|
|
82
82
|
|
83
83
|
describe 'compile code and get compiled code' do
|
84
84
|
before do
|
85
|
-
@resp = JSCompiler.compile(COMPILE_CODE, false, "compiled_code", "SIMPLE_OPTIMIZATIONS")
|
85
|
+
@resp = JSCompiler.compile(COMPILE_CODE, false, "compiled_code", "SIMPLE_OPTIMIZATIONS", "default")
|
86
86
|
end
|
87
87
|
|
88
88
|
it 'should receive the compiled code' do
|
@@ -92,7 +92,7 @@ describe JSCompiler do
|
|
92
92
|
|
93
93
|
describe 'compile code and find errors' do
|
94
94
|
before do
|
95
|
-
@resp = JSCompiler.compile(ERROR_CODE, false, "errors", "SIMPLE_OPTIMIZATIONS")
|
95
|
+
@resp = JSCompiler.compile(ERROR_CODE, false, "errors", "SIMPLE_OPTIMIZATIONS", "default")
|
96
96
|
end
|
97
97
|
|
98
98
|
it 'should return the result string' do
|
@@ -102,7 +102,7 @@ describe JSCompiler do
|
|
102
102
|
|
103
103
|
describe 'compile code and find warnings' do
|
104
104
|
before do
|
105
|
-
@resp = JSCompiler.compile(WARNING_CODE, false, "warnings", "SIMPLE_OPTIMIZATIONS")
|
105
|
+
@resp = JSCompiler.compile(WARNING_CODE, false, "warnings", "SIMPLE_OPTIMIZATIONS", "default")
|
106
106
|
end
|
107
107
|
|
108
108
|
it 'should return the result string' do
|
@@ -112,7 +112,7 @@ describe JSCompiler do
|
|
112
112
|
|
113
113
|
describe 'compile code and obtain statistics' do
|
114
114
|
before do
|
115
|
-
@resp = JSCompiler.compile(COMPILE_CODE, false, "statistics", "SIMPLE_OPTIMIZATIONS")
|
115
|
+
@resp = JSCompiler.compile(COMPILE_CODE, false, "statistics", "SIMPLE_OPTIMIZATIONS", "")
|
116
116
|
end
|
117
117
|
|
118
118
|
it 'should return the result string' do
|
@@ -124,7 +124,7 @@ describe JSCompiler do
|
|
124
124
|
describe 'FULL code compile' do
|
125
125
|
describe 'without errors or warnings' do
|
126
126
|
before do
|
127
|
-
@resp = JSCompiler.full_compile(COMPILE_CODE, false, "SIMPLE_OPTIMIZATIONS")
|
127
|
+
@resp = JSCompiler.full_compile(COMPILE_CODE, false, "SIMPLE_OPTIMIZATIONS", "")
|
128
128
|
end
|
129
129
|
|
130
130
|
it 'should receive the compiled code' do
|
@@ -134,7 +134,7 @@ describe JSCompiler do
|
|
134
134
|
|
135
135
|
describe 'and get errors' do
|
136
136
|
before do
|
137
|
-
@resp = JSCompiler.full_compile(ERROR_CODE, false, "SIMPLE_OPTIMIZATIONS")
|
137
|
+
@resp = JSCompiler.full_compile(ERROR_CODE, false, "SIMPLE_OPTIMIZATIONS", "")
|
138
138
|
end
|
139
139
|
|
140
140
|
it 'should return the result string' do
|
@@ -144,7 +144,7 @@ describe JSCompiler do
|
|
144
144
|
|
145
145
|
describe 'and get warnings' do
|
146
146
|
before do
|
147
|
-
@resp = JSCompiler.full_compile(WARNING_CODE, false, "SIMPLE_OPTIMIZATIONS")
|
147
|
+
@resp = JSCompiler.full_compile(WARNING_CODE, false, "SIMPLE_OPTIMIZATIONS", "")
|
148
148
|
end
|
149
149
|
|
150
150
|
it 'should return the result string' do
|
metadata
CHANGED
@@ -1,104 +1,99 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 2
|
9
|
+
version: 0.2.2
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
|
-
-
|
12
|
+
- Davide Saurino
|
8
13
|
autorequire:
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-02-
|
13
|
-
default_executable:
|
17
|
+
date: 2010-02-27 00:00:00 +01:00
|
18
|
+
default_executable: jsc
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
name: term-ansicolor
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
|
-
- - "
|
25
|
+
- - "="
|
22
26
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 0
|
30
|
+
- 4
|
31
|
+
version: 1.0.4
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description:
|
35
|
+
email: davide.saurino@gmail.com
|
27
36
|
executables:
|
28
|
-
- "#jsc#"
|
29
37
|
- jsc
|
30
38
|
extensions: []
|
31
39
|
|
32
40
|
extra_rdoc_files:
|
33
|
-
-
|
41
|
+
- LICENSE
|
34
42
|
- README.rdoc
|
35
|
-
- bin/#jsc#
|
36
|
-
- bin/jsc
|
37
43
|
files:
|
44
|
+
- .bnsignore
|
38
45
|
- .gitignore
|
39
46
|
- History.txt
|
47
|
+
- LICENSE
|
40
48
|
- README.rdoc
|
41
49
|
- Rakefile
|
42
50
|
- TODO.org
|
43
|
-
-
|
51
|
+
- VERSION
|
44
52
|
- bin/jsc
|
53
|
+
- features/run_command.feature
|
54
|
+
- features/support/env.rb
|
45
55
|
- js/compiled_code.js
|
46
56
|
- js/errors.js
|
47
|
-
- js/extra/alcadialer_im.js
|
48
|
-
- js/extra/strophe.js
|
49
57
|
- js/warnings.js
|
50
|
-
- jsc.gemspec
|
51
58
|
- lib/jsc.rb
|
52
59
|
- lib/jsc/closure_compiler.rb
|
60
|
+
- lib/jsc/parser.rb
|
53
61
|
- plugins/jsc.el
|
54
|
-
- rdoc/classes/JSCompiler.html
|
55
|
-
- rdoc/classes/String.html
|
56
|
-
- rdoc/created.rid
|
57
|
-
- rdoc/files/README_rdoc.html
|
58
|
-
- rdoc/files/lib/jsc/closure_compiler_rb.html
|
59
|
-
- rdoc/files/lib/jsc_rb.html
|
60
|
-
- rdoc/fr_class_index.html
|
61
|
-
- rdoc/fr_file_index.html
|
62
|
-
- rdoc/fr_method_index.html
|
63
|
-
- rdoc/index.html
|
64
|
-
- rdoc/rdoc-style.css
|
65
|
-
- spec/#wip_spec.rb#
|
66
62
|
- spec/jsc_spec.rb
|
67
63
|
- spec/spec_helper.rb
|
68
|
-
- spec/wip_spec.rb
|
69
64
|
- tasks/jsc.rake
|
70
65
|
- test/test_jsc.rb
|
71
|
-
- tmp/aruba/js/compiled_code.js
|
72
|
-
- tmp/aruba/js/errors.js
|
73
|
-
- tmp/aruba/js/warnings.js
|
74
66
|
has_rdoc: true
|
75
67
|
homepage: http://github.com/sub/jsc
|
76
68
|
licenses: []
|
77
69
|
|
78
70
|
post_install_message:
|
79
71
|
rdoc_options:
|
80
|
-
- --
|
81
|
-
- README.rdoc
|
72
|
+
- --charset=UTF-8
|
82
73
|
require_paths:
|
83
74
|
- lib
|
84
75
|
required_ruby_version: !ruby/object:Gem::Requirement
|
85
76
|
requirements:
|
86
77
|
- - ">="
|
87
78
|
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 0
|
88
81
|
version: "0"
|
89
|
-
version:
|
90
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
83
|
requirements:
|
92
84
|
- - ">="
|
93
85
|
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 0
|
94
88
|
version: "0"
|
95
|
-
version:
|
96
89
|
requirements: []
|
97
90
|
|
98
|
-
rubyforge_project:
|
99
|
-
rubygems_version: 1.3.
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.3.6
|
100
93
|
signing_key:
|
101
94
|
specification_version: 3
|
102
|
-
summary:
|
95
|
+
summary: Ruby API to Google Closure Compiler Web service
|
103
96
|
test_files:
|
97
|
+
- spec/jsc_spec.rb
|
98
|
+
- spec/spec_helper.rb
|
104
99
|
- test/test_jsc.rb
|
data/js/extra/alcadialer_im.js
DELETED
@@ -1,559 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
AlcaDialer_IM: Modulo di messagistica istantanea
|
3
|
-
|
4
|
-
Configurazione dei parametri di una connessione,
|
5
|
-
configurazione degli handlers,
|
6
|
-
connessione e disconnessione,
|
7
|
-
invio di dati,
|
8
|
-
amministrazione pubsub,
|
9
|
-
iscrizione e ricezione pubsub
|
10
|
-
*/
|
11
|
-
|
12
|
-
// REQUIRE: jQuery e StropheJS
|
13
|
-
//
|
14
|
-
// jQuery: XML parsing
|
15
|
-
// StropheJS: XMPP
|
16
|
-
|
17
|
-
// NOTE: create a module private context
|
18
|
-
(function(){
|
19
|
-
var base_im_config = {
|
20
|
-
max_conn_retry: 0,
|
21
|
-
username: null,
|
22
|
-
password: null,
|
23
|
-
jid: null,
|
24
|
-
fulljid: null,
|
25
|
-
hostname: null,
|
26
|
-
boshurl: null,
|
27
|
-
resource: "AlcaBosh"
|
28
|
-
};
|
29
|
-
|
30
|
-
var dummy_handler = function() { };
|
31
|
-
|
32
|
-
var base_call_callbacks = {
|
33
|
-
ok: dummy_handler,
|
34
|
-
fail: dummy_handler
|
35
|
-
};
|
36
|
-
|
37
|
-
var base_global_callbacks = {
|
38
|
-
connected: dummy_handler,
|
39
|
-
connection_error: dummy_handler,
|
40
|
-
disconnecting: dummy_handler,
|
41
|
-
disconnected: dummy_handler,
|
42
|
-
receive_message: dummy_handler,
|
43
|
-
receive_pubsub_item: dummy_handler
|
44
|
-
};
|
45
|
-
|
46
|
-
var DEBUG = false;
|
47
|
-
|
48
|
-
var NS_PUBSUB = 'http://jabber.org/protocol/pubsub';
|
49
|
-
|
50
|
-
// NOTE: current configuration
|
51
|
-
var im_config = null;
|
52
|
-
// NOTE: current connection
|
53
|
-
var im_conn = null;
|
54
|
-
// NOTE: current persistent exported handlers
|
55
|
-
var im_handlers = null;
|
56
|
-
|
57
|
-
// AlcaDialer_IM constructor
|
58
|
-
var AlcaDialer_IM = mixin({}, {
|
59
|
-
init: IM_init,
|
60
|
-
connect: IM_connect,
|
61
|
-
disconnect: IM_disconnect,
|
62
|
-
send_message: IM_send_message,
|
63
|
-
ping: IM_ping,
|
64
|
-
create_user: IM_create_user,
|
65
|
-
flush: IM_flush,
|
66
|
-
pubsub: {
|
67
|
-
create_node: PUBSUB_create_node,
|
68
|
-
remove_node: PUBSUB_remove_node,
|
69
|
-
list_nodes: PUBSUB_list_nodes,
|
70
|
-
publish_item: PUBSUB_publish_item,
|
71
|
-
subscribe: PUBSUB_subscribe,
|
72
|
-
list_subscribed: PUBSUB_list_subscribed,
|
73
|
-
unsubscribe: PUBSUB_unsubscribe,
|
74
|
-
unsubscribe_all: PUBSUB_unsubscribe_all
|
75
|
-
},
|
76
|
-
get_connection: function() { return im_conn; },
|
77
|
-
jid: jid,
|
78
|
-
fulljid: fulljid,
|
79
|
-
STATUS: Strophe.Status // Export Strophe Status consts
|
80
|
-
});
|
81
|
-
|
82
|
-
// NOTE: Export AlcaDialer_IM
|
83
|
-
window.AlcaDialer_IM = AlcaDialer_IM;
|
84
|
-
|
85
|
-
// PUBLIC NETHODS IMPLEMENTATIONS
|
86
|
-
function jid(config) {
|
87
|
-
var cfg = config ? config : im_config;
|
88
|
-
|
89
|
-
return cfg.username+"@"+cfg.hostname;
|
90
|
-
}
|
91
|
-
|
92
|
-
function fulljid(config) {
|
93
|
-
var cfg = config ? config : im_config;
|
94
|
-
|
95
|
-
return jid(cfg)+'/'+cfg.resource;
|
96
|
-
}
|
97
|
-
|
98
|
-
function IM_init(config) {
|
99
|
-
// Mix configuration parameters
|
100
|
-
im_config = mixin({}, base_im_config, config);
|
101
|
-
im_config.fulljid = fulljid(im_config);
|
102
|
-
im_config.jid = jid(im_config);
|
103
|
-
im_handlers = mixin({}, base_global_callbacks, config.handlers);
|
104
|
-
|
105
|
-
return true;
|
106
|
-
}
|
107
|
-
|
108
|
-
function IM_connect() {
|
109
|
-
var fulljid = im_config.fulljid;
|
110
|
-
|
111
|
-
im_conn = new Strophe.Connection(im_config.boshurl);
|
112
|
-
// AlcaBosh.conn.rawInput = AlcaBosh._raw_input;
|
113
|
-
// AlcaBosh.conn.rawOutput = AlcaBosh._raw_output;
|
114
|
-
im_conn.connect(fulljid, im_config.password, _IM_connection_manager);
|
115
|
-
}
|
116
|
-
|
117
|
-
function IM_disconnect(reason) {
|
118
|
-
im_conn.flush();
|
119
|
-
im_conn.disconnect(reason);
|
120
|
-
im_config.conn_retry = 0;
|
121
|
-
}
|
122
|
-
|
123
|
-
function IM_create_user(callbacks) {
|
124
|
-
var cb = mixin({},base_call_callbacks,callbacks);
|
125
|
-
var xmpp_id = im_conn.getUniqueId('reg');
|
126
|
-
|
127
|
-
var xmpp = $iq(
|
128
|
-
{
|
129
|
-
"id": xmpp_id,
|
130
|
-
"type": "set",
|
131
|
-
"to": im_config.hostname
|
132
|
-
}).
|
133
|
-
c("query", {"xmlns": "jabber:iq:register"}).
|
134
|
-
c("username").t(im_config.username).
|
135
|
-
up().c("password").t(im_config.password);
|
136
|
-
|
137
|
-
_IM_sendSysIQ(xmpp, cb.ok, cb.fail);
|
138
|
-
im_conn.flush();
|
139
|
-
}
|
140
|
-
|
141
|
-
function IM_ping() {
|
142
|
-
_IM_send_data($iq({
|
143
|
-
type: 'get',
|
144
|
-
id: im_conn.getUniqueId('ping')
|
145
|
-
}).c('ping', { xmlns: 'urn:xmpp:ping' }));
|
146
|
-
|
147
|
-
im_conn.flush();
|
148
|
-
// don't remove the time handler
|
149
|
-
return true;
|
150
|
-
}
|
151
|
-
|
152
|
-
function IM_flush() {
|
153
|
-
im_conn.flush();
|
154
|
-
}
|
155
|
-
|
156
|
-
function IM_send_message(to, text) {
|
157
|
-
_IM_send_data(
|
158
|
-
$msg({ to: to, type: 'chat'}).
|
159
|
-
c('body').t(text)
|
160
|
-
);
|
161
|
-
}
|
162
|
-
|
163
|
-
function PUBSUB_list_nodes(rootnode, callbacks) {
|
164
|
-
var cb = mixin({},base_call_callbacks,callbacks);
|
165
|
-
var xmpp_id = im_conn.getUniqueId('list');
|
166
|
-
var xmpp = $iq(
|
167
|
-
{
|
168
|
-
id: xmpp_id,
|
169
|
-
type: 'get',
|
170
|
-
from: im_config.fulljid,
|
171
|
-
to: im_config.pubsub
|
172
|
-
}).
|
173
|
-
c('query',
|
174
|
-
{
|
175
|
-
xmlns: Strophe.NS.DISCO_ITEMS,
|
176
|
-
node: rootnode
|
177
|
-
});
|
178
|
-
|
179
|
-
// TODO: import from first prototype
|
180
|
-
_IM_sendIQ(xmpp, ok, fail);
|
181
|
-
function ok(data) {
|
182
|
-
var content = [];
|
183
|
-
$(data).find('item').each(function() {
|
184
|
-
content.push($(this).attr('node'));
|
185
|
-
});
|
186
|
-
cb.ok(content);
|
187
|
-
}
|
188
|
-
function fail(data) {
|
189
|
-
cb.fail(data);
|
190
|
-
}
|
191
|
-
}
|
192
|
-
|
193
|
-
/*
|
194
|
-
NOTE: xml fragment generation con jquery
|
195
|
-
|
196
|
-
var iq = $iq({ id: "12312312", type: "set"});
|
197
|
-
$(iq.tree()).append( $("<c xmlns='test'>"));
|
198
|
-
$('c', iq.tree()).append($("<value pippo='34'/>"));
|
199
|
-
iq.toString();
|
200
|
-
|
201
|
-
====> <iq id='12312312' type='set' xmlns='jabber:client'><c xmlns='test'><value pippo='34'/></c></iq>
|
202
|
-
*/
|
203
|
-
|
204
|
-
function PUBSUB_create_node(node, callbacks) {
|
205
|
-
var cb = mixin({},base_call_callbacks,callbacks);
|
206
|
-
var xmpp_id = im_conn.getUniqueId('list');
|
207
|
-
/* TODO: TOOOOOOO BIG */
|
208
|
-
var xmpp_iq = $iq(
|
209
|
-
{
|
210
|
-
id: xmpp_id,
|
211
|
-
type: 'set',
|
212
|
-
from: im_config.fulljid,
|
213
|
-
to: im_config.pubsub,
|
214
|
-
xmlns: Strophe.NS.CLIENT
|
215
|
-
}).
|
216
|
-
c('pubsub',{xmlns: NS_PUBSUB}).
|
217
|
-
c('create',{node: node});
|
218
|
-
|
219
|
-
// NOTE: Crea i fragment xml con gli helper strophe
|
220
|
-
var xmpp_configure = $build("configure", {node: node}).
|
221
|
-
c('x', {'type': 'submit', 'xmlns': 'jabber:x:data'});
|
222
|
-
|
223
|
-
var xmpp_field_formtype = $build('field', {'type': 'hidden', 'var': 'FORM_TYPE'}).
|
224
|
-
c('value').t('http://jabber.org/protocol/pubsub#node_config');
|
225
|
-
|
226
|
-
var xmpp_field_lastpub = $build('field', {'var': 'pubsub#send_last_published_item'}).
|
227
|
-
c('value').t('never');
|
228
|
-
|
229
|
-
var xmpp_field_presencedelivery = $build('field', {'var': 'pubsub#presence_based_delivery'}).
|
230
|
-
c('value').t('1');
|
231
|
-
|
232
|
-
var xmpp_field_publishmodel = $build('field', {'var': 'pubsub#publish_model'}).
|
233
|
-
c('value').t('open');
|
234
|
-
|
235
|
-
// NOTE: Assembla il pacchetto xml dai fragment mediante jQuery
|
236
|
-
$('pubsub', xmpp_iq.tree()).append(xmpp_configure.tree());
|
237
|
-
$('x', xmpp_configure.tree()).
|
238
|
-
append(xmpp_field_formtype.tree()).
|
239
|
-
append(xmpp_field_lastpub.tree()).
|
240
|
-
append(xmpp_field_presencedelivery.tree()).
|
241
|
-
append(xmpp_field_publishmodel.tree());
|
242
|
-
|
243
|
-
_IM_sendIQ(xmpp_iq, cb.ok, cb.fail);
|
244
|
-
|
245
|
-
return xmpp_id;
|
246
|
-
}
|
247
|
-
|
248
|
-
function PUBSUB_remove_node(node, callbacks) {
|
249
|
-
var cb = mixin({},base_call_callbacks,callbacks);
|
250
|
-
var xmpp_id = im_conn.getUniqueId('del');
|
251
|
-
var xmpp_iq = $iq(
|
252
|
-
{
|
253
|
-
'type':'set',
|
254
|
-
'from': im_config.fulljid,
|
255
|
-
'to': im_config.pubsub,
|
256
|
-
'id': xmpp_id
|
257
|
-
}).
|
258
|
-
c('pubsub', {'xmlns': 'http://jabber.org/protocol/pubsub#owner'}).
|
259
|
-
c('delete', {'node': node});
|
260
|
-
|
261
|
-
_IM_sendIQ(xmpp_iq, cb.ok, cb.fail);
|
262
|
-
}
|
263
|
-
|
264
|
-
function PUBSUB_publish_item(node, text, callbacks) {
|
265
|
-
var cb = mixin({},base_call_callbacks,callbacks);
|
266
|
-
var xmpp_id = im_conn.getUniqueId('pub');
|
267
|
-
var xmpp_iq = $iq(
|
268
|
-
{
|
269
|
-
'type':'set',
|
270
|
-
'from': im_config.fulljid,
|
271
|
-
'to': im_config.pubsub,
|
272
|
-
'id': xmpp_id
|
273
|
-
}).
|
274
|
-
c('pubsub', {'xmlns': 'http://jabber.org/protocol/pubsub'}).
|
275
|
-
c('publish', {'node': node }).
|
276
|
-
c('item').t(text);
|
277
|
-
|
278
|
-
_IM_sendIQ(xmpp_iq, cb.ok, cb.fail);
|
279
|
-
}
|
280
|
-
|
281
|
-
|
282
|
-
function PUBSUB_subscribe(node, callbacks) {
|
283
|
-
var cb = mixin({},base_call_callbacks,callbacks);
|
284
|
-
var xmpp_id = im_conn.getUniqueId('sub');
|
285
|
-
var bare_jid = Strophe.getBareJidFromJid(im_config.fulljid);
|
286
|
-
var xmpp_iq = $iq(
|
287
|
-
{
|
288
|
-
'type':'set',
|
289
|
-
'from': im_config.fulljid,
|
290
|
-
'to': im_config.pubsub,
|
291
|
-
'id': xmpp_id
|
292
|
-
}).
|
293
|
-
c('pubsub', {'xmlns': 'http://jabber.org/protocol/pubsub'}).
|
294
|
-
c('subscribe', {'node': node, 'jid': bare_jid});
|
295
|
-
|
296
|
-
_IM_sendIQ(xmpp_iq, parse_subscribe_ok, parse_subscribe_fail);
|
297
|
-
|
298
|
-
var result = { node: node, success: false };
|
299
|
-
|
300
|
-
function parse_subscribe_ok(data) {
|
301
|
-
result.success = true;
|
302
|
-
cb.ok(result);
|
303
|
-
}
|
304
|
-
function parse_subscribe_fail(data) {
|
305
|
-
//NOTE: result.success is already false
|
306
|
-
cb.ok(result);
|
307
|
-
}
|
308
|
-
}
|
309
|
-
|
310
|
-
function PUBSUB_unsubscribe(node, callbacks) {
|
311
|
-
var cb = mixin({},base_call_callbacks,callbacks);
|
312
|
-
var xmpp_id = im_conn.getUniqueId('unsub');
|
313
|
-
var bare_jid = Strophe.getBareJidFromJid(im_config.fulljid);
|
314
|
-
var xmpp_iq = $iq(
|
315
|
-
{
|
316
|
-
'type':'set',
|
317
|
-
'from': im_config.fulljid,
|
318
|
-
'to': im_config.pubsub,
|
319
|
-
'id': xmpp_id
|
320
|
-
}).
|
321
|
-
c('pubsub', {'xmlns': 'http://jabber.org/protocol/pubsub'}).
|
322
|
-
c('unsubscribe', {'node': node, 'jid': bare_jid});
|
323
|
-
|
324
|
-
_IM_sendIQ(xmpp_iq, parse_unsubscribe_ok, parse_unsubscribe_fail);
|
325
|
-
|
326
|
-
var result = { node: node, success: false };
|
327
|
-
|
328
|
-
function parse_unsubscribe_ok(data) {
|
329
|
-
result.success = true;
|
330
|
-
cb.ok(result);
|
331
|
-
}
|
332
|
-
function parse_unsubscribe_fail(data) {
|
333
|
-
//NOTE: result.success is already false
|
334
|
-
cb.ok(result);
|
335
|
-
}
|
336
|
-
return xmpp_id;
|
337
|
-
}
|
338
|
-
|
339
|
-
function PUBSUB_list_subscribed(callbacks) {
|
340
|
-
var cb = mixin({},base_call_callbacks,callbacks);
|
341
|
-
var xmpp_id = im_conn.getUniqueId('list');
|
342
|
-
var bare_jid = Strophe.getBareJidFromJid(im_config.fulljid);
|
343
|
-
var xmpp_iq = $iq(
|
344
|
-
{
|
345
|
-
'type':'get',
|
346
|
-
'from': im_config.fulljid,
|
347
|
-
'to': im_config.pubsub,
|
348
|
-
'id': xmpp_id
|
349
|
-
}).
|
350
|
-
c('pubsub', {'xmlns': 'http://jabber.org/protocol/pubsub'}).
|
351
|
-
c('subscriptions');
|
352
|
-
|
353
|
-
_IM_sendIQ(xmpp_iq, parse_subscriptions, cb.fail);
|
354
|
-
function parse_subscriptions(data) {
|
355
|
-
var content = [];
|
356
|
-
|
357
|
-
$(data).find('subscription').each(function () {
|
358
|
-
content.push($(this).attr('node'));
|
359
|
-
});
|
360
|
-
|
361
|
-
cb.ok(content);
|
362
|
-
}
|
363
|
-
|
364
|
-
}
|
365
|
-
|
366
|
-
// TODO: dovrebbe controllare l'effetivo esito delle desottoscrizioni richieste
|
367
|
-
function PUBSUB_unsubscribe_all(callbacks) {
|
368
|
-
var cb = mixin({},base_call_callbacks,callbacks);
|
369
|
-
|
370
|
-
PUBSUB_list_subscribed({
|
371
|
-
ok: function(data) {
|
372
|
-
var nodes = [];
|
373
|
-
var result = true;
|
374
|
-
$(data).each(function() {
|
375
|
-
PUBSUB_unsubscribe(this, {
|
376
|
-
ok: function(unsub_reply) {
|
377
|
-
nodes.push(unsub_reply);
|
378
|
-
if(nodes.length === data.length) {
|
379
|
-
if(result === true)
|
380
|
-
cb.ok(nodes);
|
381
|
-
else
|
382
|
-
cb.fail(nodes);
|
383
|
-
}
|
384
|
-
},
|
385
|
-
fail: function(unsub_reply) {
|
386
|
-
result = false;
|
387
|
-
nodes.push(unsub_reply);
|
388
|
-
if(nodes.length === data.length)
|
389
|
-
cb.fail(nodes);
|
390
|
-
}
|
391
|
-
});
|
392
|
-
im_conn.flush();
|
393
|
-
});
|
394
|
-
|
395
|
-
|
396
|
-
im_conn.flush();
|
397
|
-
}
|
398
|
-
});
|
399
|
-
}
|
400
|
-
|
401
|
-
// PRIVATE FUNCTIONS
|
402
|
-
|
403
|
-
function _IM_sendIQ(data, cb_ok, cb_fail) {
|
404
|
-
im_conn.sendIQ(data, cb_ok, cb_fail);
|
405
|
-
}
|
406
|
-
|
407
|
-
// NOTE: this helper send an IQ with attached system handlers which don't require
|
408
|
-
// an authenticated session
|
409
|
-
function _IM_sendSysIQ(elem, callback, errback, timeout) {
|
410
|
-
var timeoutHandler = null;
|
411
|
-
var that = im_conn;
|
412
|
-
|
413
|
-
if (typeof(elem.tree) === "function") {
|
414
|
-
elem = elem.tree();
|
415
|
-
}
|
416
|
-
var id = elem.getAttribute('id');
|
417
|
-
|
418
|
-
// inject id if not found
|
419
|
-
if (!id) {
|
420
|
-
id = im_conn.getUniqueId("sendIQ");
|
421
|
-
elem.setAttribute("id", id);
|
422
|
-
}
|
423
|
-
|
424
|
-
var handler = im_conn._addSysHandler(function (stanza) {
|
425
|
-
|
426
|
-
// remove timeout handler if there is one
|
427
|
-
if (timeoutHandler) {
|
428
|
-
that.deleteTimedHandler(timeoutHandler);
|
429
|
-
}
|
430
|
-
|
431
|
-
var iqtype = stanza.getAttribute('type');
|
432
|
-
|
433
|
-
if (iqtype === 'result') {
|
434
|
-
if (callback) {
|
435
|
-
callback(stanza);
|
436
|
-
}
|
437
|
-
} else if (iqtype === 'error') {
|
438
|
-
if (errback) {
|
439
|
-
errback(stanza);
|
440
|
-
}
|
441
|
-
} else {
|
442
|
-
throw {
|
443
|
-
name: "StropheError",
|
444
|
-
message: "Got bad IQ type of " + iqtype
|
445
|
-
};
|
446
|
-
}
|
447
|
-
}, null, 'iq', null, id);
|
448
|
-
|
449
|
-
// if timeout specified, setup timeout handler.
|
450
|
-
if (timeout) {
|
451
|
-
timeoutHandler = im_conn.addTimedHandler(timeout, function () {
|
452
|
-
// get rid of normal handler
|
453
|
-
that.deleteHandler(handler);
|
454
|
-
|
455
|
-
// call errback on timeout with null stanza
|
456
|
-
if (errback) {
|
457
|
-
errback(null);
|
458
|
-
}
|
459
|
-
return false;
|
460
|
-
});
|
461
|
-
}
|
462
|
-
|
463
|
-
im_conn.send(elem);
|
464
|
-
|
465
|
-
return id;
|
466
|
-
}
|
467
|
-
|
468
|
-
function _IM_connection_manager(status, reason) {
|
469
|
-
var fulljid = im_config.fulljid;
|
470
|
-
var fail_event = {
|
471
|
-
fulljid: fulljid,
|
472
|
-
status: status,
|
473
|
-
error: reason
|
474
|
-
};
|
475
|
-
|
476
|
-
switch(status) {
|
477
|
-
case Strophe.Status.ERROR:
|
478
|
-
im_handlers.connection_error(fail_event);
|
479
|
-
break;
|
480
|
-
case Strophe.Status.CONNFAIL:
|
481
|
-
im_handlers.connection_error(fail_event);
|
482
|
-
break;
|
483
|
-
case Strophe.Status.AUTHFAIL:
|
484
|
-
//im_conn.flush();
|
485
|
-
fail_event.error = "Authentication Error";
|
486
|
-
im_handlers.connection_error(fail_event);
|
487
|
-
break;
|
488
|
-
case Strophe.Status.CONNECTED:
|
489
|
-
_IM_connected();
|
490
|
-
im_handlers.connected({fulljid: fulljid});
|
491
|
-
break;
|
492
|
-
case Strophe.Status.DISCONNECTING:
|
493
|
-
im_handlers.disconnecting({
|
494
|
-
fulljid: fulljid,
|
495
|
-
reason: reason
|
496
|
-
});
|
497
|
-
break;
|
498
|
-
case Strophe.Status.DISCONNECTED:
|
499
|
-
im_handlers.disconnected({
|
500
|
-
fulljid: fulljid,
|
501
|
-
reason: reason
|
502
|
-
});
|
503
|
-
break;
|
504
|
-
}
|
505
|
-
}
|
506
|
-
|
507
|
-
function _IM_connected() {
|
508
|
-
// Install Message XMPP Stanzas receiver
|
509
|
-
im_conn.addHandler(_IM_receive_message, null, 'message',null,null,null,null);
|
510
|
-
// Send presence XMPP Stanzas
|
511
|
-
_IM_send_data($pres());
|
512
|
-
// Set periodic 5 min ping to confirm our presence
|
513
|
-
im_conn.addTimedHandler(5*60*1000, IM_ping);
|
514
|
-
}
|
515
|
-
|
516
|
-
function _IM_send_data(data) {
|
517
|
-
im_conn.send(data);
|
518
|
-
}
|
519
|
-
|
520
|
-
function _IM_receive_message(data) {
|
521
|
-
var message = $(data);
|
522
|
-
var from = message.attr('from');
|
523
|
-
var to = message.attr('to');
|
524
|
-
var type = message.attr('type');
|
525
|
-
|
526
|
-
var content = [];
|
527
|
-
|
528
|
-
if (from.match(/pubsub/i)) {
|
529
|
-
var items = $(message.find('items')[0]);
|
530
|
-
var node = items.attr('node');
|
531
|
-
message.find('item').each(function () {
|
532
|
-
content.push($(this).text());
|
533
|
-
});
|
534
|
-
|
535
|
-
if (content.length > 0)
|
536
|
-
im_handlers.receive_pubsub_item({node: node, content: content});
|
537
|
-
}
|
538
|
-
else if (type === "chat") {
|
539
|
-
content.push($(message.find('body')[0]).text());
|
540
|
-
im_handlers.receive_message({from: from, content: content});
|
541
|
-
}
|
542
|
-
|
543
|
-
return true;
|
544
|
-
}
|
545
|
-
|
546
|
-
// NOTE: simple (non-deep) mixin utility function
|
547
|
-
function mixin(target) {
|
548
|
-
var src=null;
|
549
|
-
for (var i=1; i<arguments.length; i++) {
|
550
|
-
src = arguments[i];
|
551
|
-
for (var j in src) {
|
552
|
-
target[j] = src[j];
|
553
|
-
}
|
554
|
-
}
|
555
|
-
|
556
|
-
return target;
|
557
|
-
}
|
558
|
-
|
559
|
-
})();
|