File: send.js

package info (click to toggle)
syncplaces 4.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 924 kB
  • sloc: makefile: 2
file content (500 lines) | stat: -rw-r--r-- 17,594 bytes parent folder | download
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* ***** 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 the SyncPlaces extension.
 *
 * The Initial Developer of the Original Code is Andy Halford.
 * Portions created by the Initial Developer are Copyright (C) 2008-2011
 * 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 SyncPlacesSend = {
	jsonSendFile: "syncplaces_send.json",
	htmlSendFile: "syncplaces_send.html",
	xbelSendFile: "syncplaces_send.xml",
	tmpBookmarks: null,
	tmpPasswords: null,
	Cc: Components.classes,
	Ci: Components.interfaces,

	//Launch the transfer dialog to send to server
	manualSend: function() {
		SyncPlacesOptions.prefs.setBoolPref("startManualSend", true);
		window.openDialog('chrome://syncplaces/content/transfer.xul', '_blank', 'chrome,resizable,modal,centerscreen', null);
		SyncPlacesOptions.lastTransferTimes();
	},

	//Send to server, taking a local backup first
	send: function(startupShutdown, checkSendSafe) {
		//If sending safely then do a receive first
		if (checkSendSafe && SyncPlacesOptions.prefs.getBoolPref("send_safe")) {
			//Display warning if last_sent is blank (ie no backup to compare when merging)
			if (SyncPlacesOptions.prefs.getCharPref("lastSend") != 0 ||
					SyncPlacesOptions.alert2(null, 'last_send', null, false,
						"http://www.andyhalford.com/syncplaces/use.html#merging_tip", true))
			{
				SyncPlacesReceive.receive(startupShutdown, checkSendSafe);
			}
			else {
				SyncPlacesNetworking.closeSPDialog();
			}
			return;
		}

		//if sending passwords then send them first
		if (SyncPlacesOptions.prefs.getBoolPref("sync_passwords")) {
			this.sendPasswords(startupShutdown);
		}
		//Else send JSON or XBEL format file
		else {
			this.sendSyncFile();
		}
	},

	sendPasswords: function(startupShutdown) {
		//Has password been set
		if (!SyncPlacesOptions.getPassword(SyncPlacesOptions.passwordUser, false)) {
			SyncPlaces.timedStatus('missing_ppassword', false, true);
			SyncPlacesOptions.alert2(null, 'missing_ppassword', null, false,
									"http://www.andyhalford.com/syncplaces/advanced.html#encryption");
			return;
		}

		//Get the passwords
		var passwords = SyncPlacesPasswords.getPasswords(true);
		if (!passwords) {
			return;
		}

		//Compare with cached version
		if (SyncPlacesOptions.prefs.getBoolPref("cache_pwd")) {
			//If hash not changed then send the bookmarks
			var sendHash = SyncPlacesOptions.prefs.getCharPref("sendPwdHash");
			if (SyncPlaces.computeHash(passwords) == sendHash) {
				this.sendSyncFile();
				return;
			}
			//Save for caching later on
			else {
				this.tmpPasswords = passwords;
			}
		}

		//Encrypt
		SyncPlaces.timedStatus('encrypting_passwords', false, false);
		var main = this.Cc["@mozilla.org/thread-manager;1"].getService(this.Ci.nsIThreadManager).mainThread;
		main.dispatch(new SyncPlacesEncryptThread(SyncPlaces.PWD, passwords, this.sendPasswordsCallBack, startupShutdown), main.DISPATCH_NORMAL);
	},

	sendPasswordsCallBack: function(errorData, channel, startupShutdown, ignoreMe, ignoreMe2, type) {
		//Check it worked
		if (!SyncPlacesSend.successfulSend(errorData, channel, startupShutdown, type)) {
			return;
		}

		//Save the hashes for next time
		try {
			var hash = SyncPlaces.computeHash(SyncPlacesSend.tmpPasswords);
			SyncPlacesSend.tmpPasswords = null;
			SyncPlacesOptions.prefs.setCharPref("sendPwdHash", hash);
			SyncPlacesOptions.prefs.setCharPref("receivePwdHash", hash);

		} catch (exception) {
			SyncPlacesOptions.alert2(exception, 'cant_save_cache', null, false);
			return;
		}

		//Only send hash to server if required to prevent comms issues
		if (SyncPlacesOptions.prefs.getBoolPref("cache_pwd"))
			SyncPlacesSend.sendPwdHash(false, startupShutdown, false);

		//Send JSON or XBEL format file
		else
			SyncPlacesSend.sendSyncFile();
	},

	/*
	 * Send the pwd hash to the server
	 *
	 * @param sendSafe - if true then do a full send() afterwards
	 * but it should only possibly be true if sendSafe did a receive which then called this cos it was missing
	 */
	sendPwdHash: function(sendSafe, startupShutdown, onlySendHash) {
		//Overwrite any existing success message
		SyncPlaces.timedStatus('in_progress', false, false);

		//Read the hash file
		var hash = SyncPlacesOptions.prefs.getCharPref("receivePwdHash");
		if (!hash) return;

		//Get the URL to send to
		var uri = SyncPlaces.getURI(SyncPlaces.PWD_HASH, false);
		if (!uri) {
			SyncPlaces.timedStatus('missing_path', false, true);
			return;
		}
		var fulluri = SyncPlaces.getURI(SyncPlaces.PWD_HASH, true);

		//Start send to server (asynchronous)
		try {
			SyncPlacesComms.init(true, uri, fulluri, hash, SyncPlaces.PWD_HASH, this.sendPwdHashCallBack, startupShutdown, sendSafe, onlySendHash);

		} catch (exception) {
			SyncPlacesOptions.alert2(exception, 'cant_send_hash', null, false);
		}
	},

	sendPwdHashCallBack: function(errorData, channel, startupShutdown, sendSafe, onlySendHash, type) {
		//Check it worked
		if (!SyncPlacesSend.successfulSend(errorData, channel, startupShutdown, type)) {
			return;
		}

		//If receive kicked this off because hash was missing then carry on with the rest of the receive
		if (onlySendHash) {
			//Receive hash before bookmarks
			//if okay this will go on to receive bookmarks as well
			if (SyncPlacesOptions.prefs.getBoolPref("cache"))
				SyncPlacesReceive.receiveHash(startupShutdown, sendSafe);
			else
				SyncPlacesReceive.receiveSyncFile(startupShutdown, sendSafe, false);
		}

		//Send JSON or XBEL format file
		else
			SyncPlacesSend.sendSyncFile();
	},

	sendSyncFile: function(startupShutdown) {
		SyncPlaces.timedStatus('in_progress', false, false);

		//Sort first?
		if (SyncPlacesOptions.prefs.getBoolPref("auto_sort")) {
			try {
				SortPlacesSort.sortBookmarks(false);
			} catch(e) {
			}
		}

		//What type?
		var json = SyncPlacesOptions.prefs.getCharPref("sync_type") == 'sync_json';

		var bookmarks = this.getBookmarks(json ? this.jsonSendFile : this.xbelSendFile, json ? SyncPlaces.JSON : SyncPlaces.XBEL)
		if (!bookmarks) {
			return;
		}

		//Compare with cached version
		if (SyncPlacesOptions.prefs.getBoolPref("cache")) {
			var sendHash = SyncPlacesOptions.prefs.getCharPref("sendHash");
			if (SyncPlaces.computeHash(bookmarks) == sendHash) {
				SyncPlaces.timedStatus('nothing_to_do', false, false);
				//Save the timestamp for merge processing (no this is wrong)
//				SyncPlacesOptions.prefs.setCharPref("lastSend", new Date().getTime() * 1000);
				SyncPlacesNetworking.closeSPDialog();
				return;
			}
			//Save for caching later on
			else {
				this.tmpBookmarks = bookmarks;
			}
		}

		//Encrypt first
		if (SyncPlacesOptions.prefs.getBoolPref("encrypt") && json && SyncPlacesOptions.getPassword(SyncPlacesOptions.passwordUser, false, null, 'send')) {
			SyncPlaces.timedStatus('encrypting_bookmarks', false, false);
			var main = this.Cc["@mozilla.org/thread-manager;1"].getService(this.Ci.nsIThreadManager).mainThread;
			main.dispatch(new SyncPlacesEncryptThread(json ? SyncPlaces.JSON : SyncPlaces.XBEL, bookmarks, this.sendSyncFileCallBack, startupShutdown), main.DISPATCH_NORMAL);
		}
		else {
			this.sendData(json ? SyncPlaces.JSON : SyncPlaces.XBEL, bookmarks, this.sendSyncFileCallBack, startupShutdown);
		}
	},

	getBookmarks: function(sendFile, type) {
		//Create the backup file in the profile folder
		var sendFilePath = SyncPlaces.saveBookmarks(false, type, true, sendFile);
		if (!sendFilePath) return null;

		//Read the backup file
		var bookmarks = null;
		try {
			bookmarks = SyncPlacesIO.readFile(sendFilePath);

		} catch (exception) {
			SyncPlacesOptions.alert2(exception, 'cant_read_bookmarks', null, false);
		}
		return bookmarks;
	},

	sendData: function(type, data, callBack, startupShutdown) {
		//Get the URL to send to
		var uri = SyncPlaces.getURI(type, false);
		if (!uri) {
			SyncPlaces.timedStatus('missing_path', false, true);
			return;
		}
		var fulluri = SyncPlaces.getURI(type, true);

		//Start send to server (asynchronous)
		try {
			SyncPlacesComms.init(true, uri, fulluri, data, type, callBack, startupShutdown, false, false);

		} catch (exception) {
			SyncPlacesOptions.alert2(exception, (type == SyncPlaces.PWD ? 'cant_send_passwords' : 'cant_send_bookmarks'), null, false);
		}
		return;
	},

	//Let them know it's done
	sendSyncFileCallBack: function(errorData, channel, startupShutdown, ignoreMe, ignoreMe2, type) {
		//Check it worked
		if (!SyncPlacesSend.successfulSend(errorData, channel, startupShutdown, type)) {
			return;
		}

		//If successful send then save the timestamp for merge processing
		SyncPlacesOptions.prefs.setCharPref("lastSend", new Date().getTime() * 1000);

		//Take backup for merge processing
		SyncPlaces.saveBookmarks(false, SyncPlaces.JSON, true, SyncPlaces.mergeFile);

		//Save the hashes for next time
		try {
			var hash = SyncPlaces.computeHash(SyncPlacesSend.tmpBookmarks);
			SyncPlacesSend.tmpBookmarks = null;
			SyncPlacesOptions.prefs.setCharPref("sendHash", hash);
			SyncPlacesOptions.prefs.setCharPref("receiveHash", hash);

		} catch (exception) {
			SyncPlacesOptions.alert2(exception, 'cant_save_cache', null, false);
			return;
		}

		//Only send hash to server if required to prevent comms issues
		if (SyncPlacesOptions.prefs.getBoolPref("cache"))
			SyncPlacesSend.sendHash(false, startupShutdown, false);
		else {
			//If sending html as well then send it now
			//This will check for and send XBEL if required
			if (SyncPlacesOptions.prefs.getBoolPref("sendhtml")) {
				SyncPlacesSend.sendHTML(startupShutdown);
				return;
			}
			//Else if sending XBEL then send it now
			else if (SyncPlacesOptions.prefs.getBoolPref("sendxbel") && SyncPlacesOptions.prefs.getCharPref("sync_type") == 'sync_json') {
				SyncPlacesSend.sendXBEL(startupShutdown);
				return;
			}

			SyncPlacesNetworking.closeSPDialog();
		}
	},

	successfulSend: function(errorData, channel, startupShutdown, type) {
		//Did the connection fail?
		if (channel == null) {
			SyncPlaces.displayError(errorData, false);
			return false;
		}

		//Check the http response code
		var success = true;
		var protocol = SyncPlacesOptions.prefs.getCharPref("protocol");
		if ((protocol != "ftp") && (protocol != "file")) {
			try {
				var resCode = channel.QueryInterface(this.Ci.nsIHttpChannel).responseStatus;
				success = false;
				switch(resCode) {
					case 200:
					case 204:
						var message = 'sent_bookmarks';
						if (type == SyncPlaces.PWD) message = 'sent_passwords';
						else if (type == SyncPlaces.HASH || type == SyncPlaces.PWD_HASH) message = 'sent_hash';
						SyncPlaces.timedStatus(message, false, false);
						success = true;
						break;
					case 201:
						var message = 'new_bookmarks';
						if (type == SyncPlaces.PWD) message = 'new_passwords';
						else if (type == SyncPlaces.HASH || type == SyncPlaces.PWD_HASH) message = 'new_hash';
						SyncPlaces.timedStatus(message, false, false);
						success = true;
						break;
					case 401:
						SyncPlaces.timedStatus('unauthorised', false, true);
						break;
					case 405:
						SyncPlaces.timedStatus('server_cant_accept', false, true);
						break;
					case 407:
						SyncPlaces.timedStatus('proxy_unauthorised', false, true);
						break;
					case 502:
						SyncPlaces.timedStatus('bad_gateway', false, true);
						break;
					default:
						var message = 'cant_send_bookmarks';
						if (type == SyncPlaces.PWD) message = 'cant_send_passwords';
						else if (type == SyncPlaces.HASH || type == SyncPlaces.PWD_HASH) message = 'cant_send_hash';
						SyncPlacesOptions.alert2(null, message, resCode, false,
									"http://www.andyhalford.com/syncplaces/support.html#sending");
						break;
				}
			} catch(exception) {
				SyncPlacesOptions.alert2(exception, 'error_sending', null, false);
				return false;
			}
		}
		//For FTP some proxy servers (eg Microsoft's) may send back a web page when can't get through
		else if (errorData && SyncPlacesOptions.trim(errorData).length > 0 && SyncPlacesOptions.trim(errorData).charAt(0) == '<') {
			SyncPlaces.timedStatus('bad_gateway', false, true);
			success = false;
		}
		else {
			SyncPlaces.timedStatus('sent_bookmarks', false, false);
			//NB Aways pretend it's bookmarks even if sending a hash otherwise user confused by messages
		}

		//If not successful then bail out
		if (!success) {
			SyncPlaces.displayWebPage(errorData, startupShutdown);
			return false;
		}

		return true;
	},

	/*
	 * Send the hash to the server
	 *
	 * @param sendSafe - if true then do a full send() afterwards
	 * but it should only possibly be true if sendSafe did a receive which then called this cos it was missing
	 */
	sendHash: function(sendSafe, startupShutdown, onlySendHash) {
		//Overwrite any existing success message
		SyncPlaces.timedStatus('in_progress', false, false);

		//Read the hash file
		var hash = SyncPlacesOptions.prefs.getCharPref("receiveHash");
		if (!hash) return;

		//Get the URL to send to
		var uri = SyncPlaces.getURI(SyncPlaces.HASH, false);
		if (!uri) {
			SyncPlaces.timedStatus('missing_path', false, true);
			return;
		}
		var fulluri = SyncPlaces.getURI(SyncPlaces.HASH, true);

		//Start send to server (asynchronous)
		try {
			SyncPlacesComms.init(true, uri, fulluri, hash, SyncPlaces.HASH, this.sendHashCallBack, startupShutdown, sendSafe, onlySendHash);

		} catch (exception) {
			SyncPlacesOptions.alert2(exception, 'cant_send_hash', null, false);
		}
	},

	sendHashCallBack: function(errorData, channel, startupShutdown, sendSafe, onlySendHash, type) {
		//Check it worked
		if (!SyncPlacesSend.successfulSend(errorData, channel, startupShutdown, type)) {
			return;
		}

		//If send-safe kicked off a receive that then sent the hash cos it was missing then get to here!
		//So now do the send from the top
		if (sendSafe) {
			SyncPlacesSend.send(startupShutdown, false);
			return;
		}

		//If sending html as well then send it now
		//This will check for and send XBEL if required
		if (!onlySendHash) {
			if (SyncPlacesOptions.prefs.getBoolPref("sendhtml")) {
				SyncPlacesSend.sendHTML(startupShutdown);
				return;
			}
			//If just sending XBEL then send it now
			else if (SyncPlacesOptions.prefs.getBoolPref("sendxbel") && SyncPlacesOptions.prefs.getCharPref("sync_type") == 'sync_json') {
				SyncPlacesSend.sendXBEL(startupShutdown);
				return;
			}
		}

		SyncPlacesNetworking.closeSPDialog();
	},

	sendHTML: function(startupShutdown) {
		SyncPlaces.timedStatus('in_progress', false, false);

		var bookmarks = this.getBookmarks(this.htmlSendFile, SyncPlaces.HTML)
		if (!bookmarks) {
			return;
		}

		this.sendData(SyncPlaces.HTML, bookmarks, this.sendHTMLCallBack, startupShutdown);
	},

	sendHTMLCallBack: function(errorData, channel, startupShutdown, ignoreMe, ignoreMe2, type) {
		//Check it worked
		if (!SyncPlacesSend.successfulSend(errorData, channel, startupShutdown, type)) {
			return;
		}

		//If sending XBEL then send it now
		if (SyncPlacesOptions.prefs.getBoolPref("sendxbel") && SyncPlacesOptions.prefs.getCharPref("sync_type") == 'sync_json') {
			SyncPlacesSend.sendXBEL(startupShutdown);
			return;
		}

		SyncPlacesNetworking.closeSPDialog();
	},

	sendXBEL: function(startupShutdown) {
		SyncPlaces.timedStatus('in_progress', false, false);

		var bookmarks = this.getBookmarks(this.xbelSendFile, SyncPlaces.XBEL)
		if (!bookmarks) {
			return;
		}

		this.sendData(SyncPlaces.XBEL, bookmarks, this.sendXBELCallBack, startupShutdown);
	},

	sendXBELCallBack: function(errorData, channel, startupShutdown, ignoreMe, ignoreMe2, type) {
		//Check it worked
		if (!SyncPlacesSend.successfulSend(errorData, channel, startupShutdown, type)) {
			return;
		}

		SyncPlacesNetworking.closeSPDialog();
	}
};