/
adhoc.js
320 lines (280 loc) · 12.7 KB
/
adhoc.js
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
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is OneWeb.
*
* The Initial Developer of the Original Code is
* ProcessOne.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
var adhocCmdsSessions = {};
var adhocCmds = {
"openedTabs": ["Get all opened tabs", function(pkt, query) {
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
getService(Components.interfaces.nsIWindowMediator);
var numWindows = 0, msg = "", msgHtml = "";
var e = wm.getEnumerator("navigator:browser");
while (e.hasMoreElements()) {
var w = e.getNext();
if (numWindows++ > 0) {
msg += "\nTabs opened in window "+numWindows+":\n";
msgHtml += "<br/>Tabs opened in window "+numWindows+":<br/>";
}
var browsers = w.gBrowser ? w.gBrowser.browsers : w.Browser.browsers;
msgHtml += "<ul>"
for (var i = 0; i < browsers.length; i++) {
var b = browsers[i];
msg += " "+b.contentTitle+" [ "+b.currentURI.spec+" ]\n";
msgHtml += "<li><a href=\""+xmlEscape(b.currentURI.spec)+"\">"+
xmlEscape(b.contentTitle||"(empty title)")+"</a></li>";
}
msgHtml += "</ul>"
}
if (numWindows == 0) {
yield (
<command xmlns="http://jabber.org/protocol/commands" status="completed">
<note>
You have no browser windows currently opened.
</note>
</command>);
} else {
if (numWindows == 1) {
msg = "All currently opened tabs:\n" + msg;
msgHtml = "All currently opened tabs:<br/>" + msgHtml;
} else {
msg = "Tabs opened in window 1:\n" + msg;
msgHtml = "Tabs opened in window 1:<br/>" + msgHtml;
}
var msgPkt = new JSJaCMessage();
msgPkt.setTo(pkt.getFrom());
msgPkt.setType("chat");
msgPkt.setBody(msg);
var dp = new DOMParser();
var doc = dp.parseFromString("<html xmlns='http://jabber.org/protocol/xhtml-im'>"+
"<body xmlns='http://www.w3.org/1999/xhtml'>"+
msgHtml+"</body></html>", "text/xml");
msgPkt.appendNode(doc.documentElement);
msgPkt.appendNode("opened-tabs", {xmlns: "http://oneweb.im/command"}, []);
account.connection.send(msgPkt);
yield (
<command xmlns="http://jabber.org/protocol/commands" status="completed">
<note>
Message with opened tabs was sent.
</note>
</command>);
}
}],
"openUrl": ["Open URL in new tab", function(pkt, query) {
[pkt, query] = yield (
<command xmlns="http://jabber.org/protocol/commands" status="executing">
<action execute="complete">
<complete/>
</action>
<x xmlns="jabber:x:data" type="form">
<instruction>Please enter URL which should be opened in new tab</instruction>
<field var="url" label="URL" type="text-single"/>
</x>
</command>);
if (query.@action == "cancel")
yield (<command xmlns="http://jabber.org/protocol/commands" status="canceled"/>);
else {
var ns = new Namespace("jabber:x:data");
var url = query..ns::field.(@var == "url").ns::value.toString();
if (url)
openLink(url);
yield (
<command xmlns="http://jabber.org/protocol/commands" status="completed">
<note>
Url was opened.
</note>
</command>);
}
}],
"searchBookmarks": ["Search bookmarks", function(pkt, query) {
[pkt, query] = yield (
<command xmlns="http://jabber.org/protocol/commands" status="executing">
<action execute="complete">
<complete/>
</action>
<x xmlns="jabber:x:data" type="form">
<instruction>Please enter term which should be searched in your bookmarks</instruction>
<field var="terms" label="Search Term" type="text-single"/>
</x>
</command>);
if (query.@action == "cancel")
yield (<command xmlns="http://jabber.org/protocol/commands" status="canceled"/>);
else {
var ns = new Namespace("jabber:x:data");
var terms = query..ns::field.(@var == "terms").ns::value.toString();
var msg, msgHtml;
var bs = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"].
getService(Components.interfaces.nsINavBookmarksService);
var hs = Components.classes["@mozilla.org/browser/nav-history-service;1"].
getService(Components.interfaces.nsINavHistoryService);
var options = hs.getNewQueryOptions();
var queryObj = hs.getNewQuery();
options.queryType = options.QUERY_TYPE_BOOKMARKS;
queryObj.setFolders([bs.placesRoot], 1);
queryObj.searchTerms = terms;
var result = hs.executeQuery(queryObj, options);
result.root.containerOpen = true;
if (result.root.childCount) {
msg = "List of bookmarks matching: '"+terms+"':\n";
msgHtml = "List of bookmarks matching: '"+xmlEscape(terms)+"'<br/><ul>";
for (var i = 0; i < result.root.childCount; i++) {
var c = result.root.getChild(i);
msg += " "+c.title+" [ "+c.uri+" ]\n";
msgHtml += "<li><a href=\""+xmlEscape(c.uri)+"\">"+
xmlEscape(c.title||"(empty title)")+"</a></li>";
}
msgHtml += "</ul>"
}
result.root.containerOpen = false;
if (!msg) {
yield (
<command xmlns="http://jabber.org/protocol/commands" status="completed">
<note>
Your search returned no results.
</note>
</command>);
} else {
var msgPkt = new JSJaCMessage();
msgPkt.setTo(pkt.getFrom());
msgPkt.setType("chat");
msgPkt.setBody(msg);
var dp = new DOMParser();
var doc = dp.parseFromString("<html xmlns='http://jabber.org/protocol/xhtml-im'>"+
"<body xmlns='http://www.w3.org/1999/xhtml'>"+
msgHtml+"</body></html>", "text/xml");
msgPkt.appendNode(doc.documentElement);
msgPkt.appendNode("bookmarks-search", {xmlns: "http://oneweb.im/command"}, []);
account.connection.send(msgPkt);
yield (
<command xmlns="http://jabber.org/protocol/commands" status="completed">
<note>
Message with list of matching bookmarks was sent.
</note>
</command>);
}
}
}]
};
function commandAllowed(jid, node) {
var permission = account.permissions[node.toLowerCase()] || 0;
var shortJID = jid.normalizedJID.shortJID;
return permission == 2 || shortJID == account.myJID.normalizedJID.shortJID ||
permission == 1 && account.contacts[shortJID];
}
servicesManager.addIQService("http://jabber.org/protocol/commands", function (pkt, query, queryDOM) {
var jid = new JID(pkt.getFrom());
var result;
if (pkt.getType() != "set" || query.name().localName != "command")
return 0;
var node = query.@node.toString();
if (!commandAllowed(jid, node))
return {
type: "error",
dom: queryDOM,
e4x: <error xmlns="jabber:client" type="cancel" code="500">
<forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
</error>
};
var sessionid = query.@sessionid.toString();
try {
if (sessionid) {
if (!adhocCmdsSessions[sessionid])
return {
type: "error",
dom: queryDOM,
e4x: <error xmlns="jabber:client" type="modify" code="400">
<bad-sessionid xmlns="http://jabber.org/protocol/commands"/>
</error>
};
result = adhocCmdsSessions[sessionid].send([pkt, query, queryDOM]);
} else {
if (!adhocCmds[node])
return {
type: "error",
dom: queryDOM,
e4x: <error xmlns="jabber:client" type="cancel" code="404">
<item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
</error>
};
sessionid = generateUniqueId();
adhocCmdsSessions[sessionid] = adhocCmds[node][1](pkt, query, queryDOM);
result = adhocCmdsSessions[sessionid].next();
}
} catch (ex) {
alert(ex);
return {
type: "error",
dom: queryDOM,
e4x: <error xmlns="jabber:client" type="cancel" code="404">
<session-expired xmlns="http://jabber.org/protocol/commands"/>
</error>
};
}
if (result.@status == "completed" || result.@status == "canceled")
delete adhocCmdsSessions[sessionid];
result.@sessionid = sessionid;
result.@node = node;
return result;
});
for (var i in adhocCmds) {
servicesManager.publishDiscoItems("http://jabber.org/protocol/commands",
i, adhocCmds[i][0], commandAllowed);
servicesManager.publishDiscoInfo("http://jabber.org/protocol/commands",
i, {name: adhocCmds[i][0],
category: "automation",
type: "command-node"});
servicesManager.publishDiscoInfo("jabber:x:data", i);
}
servicesManager.addMessageService("http://oneweb.im/command", function(pkt, node, jid, nodeE4X) {
var body = pkt.getNode().getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "body")[0];
if (!body)
return 2;
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
getService(Components.interfaces.nsIWindowMediator);
var browser = wm.getMostRecentWindow("navigator:browser");
browser = browser && browser.getBrowser();
if (!browser)
return 2;
var tab = browser.addTab("chrome://oneweb/content/result.xul", null, null);
browser.selectedTab = tab;
tab.linkedBrowser.addEventListener("load", {
handleEvent: function(ev) {
this.browser.removeEventListener("load", this, true);
this.win.data = this.val;
},
win: tab.linkedBrowser.contentWindow.wrappedJSObject,
browser: tab.linkedBrowser,
val: body.innerHTML
}, true);
return 2;
});