Welcome to mirror list, hosted at ThFree Co, Russian Federation.

action.js « core « src - github.com/candy-chat/candy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 966107b4a6dca716140d5baec643c3f496a8f919 (plain)
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
/** File: action.js
 * Candy - Chats are not dead yet.
 *
 * Legal: See the LICENSE file at the top-level directory of this distribution and at https://github.com/candy-chat/candy/blob/master/LICENSE
 */
'use strict';

/* global Candy, $iq, navigator, Candy, $pres, Strophe, jQuery, $msg */

/** Class: Candy.Core.Action
 * Chat Actions (basicly a abstraction of Jabber commands)
 *
 * Parameters:
 *   (Candy.Core.Action) self - itself
 *   (Strophe) Strophe - Strophe
 *   (jQuery) $ - jQuery
 */
Candy.Core.Action = (function(self, Strophe, $) {
	/** Class: Candy.Core.Action.Jabber
	 * Jabber actions
	 */
	self.Jabber = {
		/** Function: Version
		 * Replies to a version request
		 *
		 * Parameters:
		 *   (jQuery.element) msg - jQuery element
		 */
		Version: function(msg) {
			Candy.Core.getConnection().sendIQ($iq({
				type: 'result',
				to: Candy.Util.escapeJid(msg.attr('from')),
				from: Candy.Util.escapeJid(msg.attr('to')),
				id: msg.attr('id')
			}).c('query', {
				xmlns: Strophe.NS.VERSION
			})
			.c('name', Candy.about.name).up()
			.c('version', Candy.about.version).up()
			.c('os', navigator.userAgent));
		},

		/** Function: SetNickname
		 * Sets the supplied nickname for all rooms (if parameter "room" is not specified) or
		 * sets it only for the specified rooms
		 *
		 * Parameters:
		 *   (String) nickname - New nickname
		 *   (Array) rooms - Rooms
		 */
		SetNickname: function(nickname, rooms) {
			rooms = rooms instanceof Array ? rooms : Candy.Core.getRooms();
			var roomNick, presence,
				conn = Candy.Core.getConnection();
			$.each(rooms, function(roomJid) {
				roomNick = Candy.Util.escapeJid(roomJid + '/' + nickname);
				presence = $pres({
					to: roomNick,
					from: conn.jid,
					id: 'pres:' + conn.getUniqueId()
				});
				Candy.Core.getConnection().send(presence);
			});
		},

		/** Function: Roster
		 * Sends a request for a roster
		 */
		Roster: function() {
			var roster = Candy.Core.getConnection().roster,
				options = Candy.Core.getOptions();
			roster.registerCallback(Candy.Core.Event.Jabber.RosterPush);
			$.each(options.initialRosterItems, function (i, item) {
				// Blank out resources because their cached value is not relevant
				item.resources = {};
			});
			roster.get(
				Candy.Core.Event.Jabber.RosterFetch,
				options.initialRosterVersion,
				options.initialRosterItems
			);
			// Bootstrap our roster with cached items
			Candy.Core.Event.Jabber.RosterLoad(roster.items);
		},

		/** Function: Presence
		 * Sends a request for presence
		 *
		 * Parameters:
		 *   (Object) attr - Optional attributes
		 *   (Strophe.Builder) el - Optional element to include in presence stanza
		 */
		Presence: function(attr, el) {
			var conn = Candy.Core.getConnection();
			attr = attr || {};
			if(!attr.id) {
				attr.id = 'pres:' + conn.getUniqueId();
			}
			var pres = $pres(attr).c('priority').t(Candy.Core.getOptions().presencePriority.toString())
				.up().c('c', conn.caps.generateCapsAttrs())
				.up();
			if(el) {
				pres.node.appendChild(el.node);
			}
			conn.send(pres.tree());
		},

		/** Function: Services
		 * Sends a request for disco items
		 */
		Services: function() {
			Candy.Core.getConnection().sendIQ($iq({
				type: 'get',
				xmlns: Strophe.NS.CLIENT
			}).c('query', {xmlns: Strophe.NS.DISCO_ITEMS}).tree());
		},

		/** Function: Autojoin
		 * When Candy.Core.getOptions().autojoin is true, request autojoin bookmarks (OpenFire)
		 *
		 * Otherwise, if Candy.Core.getOptions().autojoin is an array, join each channel specified.
		 * Channel can be in jid:password format to pass room password if needed.

		 * Triggers:
		 *   candy:core.autojoin-missing in case no autojoin info has been found
		 */
		Autojoin: function() {
			// Request bookmarks
			if(Candy.Core.getOptions().autojoin === true) {
				Candy.Core.getConnection().sendIQ($iq({
					type: 'get',
					xmlns: Strophe.NS.CLIENT
				})
				.c('query', {xmlns: Strophe.NS.PRIVATE})
				.c('storage', {xmlns: Strophe.NS.BOOKMARKS})
				.tree());

				var pubsubBookmarkRequest = Candy.Core.getConnection().getUniqueId('pubsub');
				Candy.Core.addHandler(Candy.Core.Event.Jabber.Bookmarks, Strophe.NS.PUBSUB, 'iq', 'result', pubsubBookmarkRequest);

				Candy.Core.getConnection().sendIQ($iq({
					type: 'get',
					id: pubsubBookmarkRequest
				})
				.c('pubsub', { xmlns: Strophe.NS.PUBSUB })
				.c('items', { node: Strophe.NS.BOOKMARKS })
				.tree());
			// Join defined rooms
			} else if($.isArray(Candy.Core.getOptions().autojoin)) {
				$.each(Candy.Core.getOptions().autojoin, function() {
					self.Jabber.Room.Join.apply(null, this.valueOf().split(':',2));
				});
			} else {
				/** Event: candy:core.autojoin-missing
				 * Triggered when no autojoin information has been found
				 */
				$(Candy).triggerHandler('candy:core.autojoin-missing');
			}
		},

		/** Function: EnableCarbons
		 * Enable message carbons (XEP-0280)
		 */
		EnableCarbons: function() {
			Candy.Core.getConnection().sendIQ($iq({
				type: 'set'
			})
			.c('enable', {xmlns: Strophe.NS.CARBONS })
			.tree());
		},

		/** Function: ResetIgnoreList
		 * Create new ignore privacy list (and reset the previous one, if it exists).
		 */
		ResetIgnoreList: function() {
			Candy.Core.getConnection().sendIQ($iq({
					type: 'set',
					from: Candy.Core.getUser().getEscapedJid()
				})
				.c('query', {xmlns: Strophe.NS.PRIVACY })
				.c('list', {name: 'ignore'})
				.c('item', {'action': 'allow', 'order': '0'})
				.tree());
		},

		/** Function: RemoveIgnoreList
		 * Remove an existing ignore list.
		 */
		RemoveIgnoreList: function() {
			Candy.Core.getConnection().sendIQ($iq({
					type: 'set',
					from: Candy.Core.getUser().getEscapedJid()
				})
				.c('query', {xmlns: Strophe.NS.PRIVACY })
				.c('list', {name: 'ignore'}).tree());
		},

		/** Function: GetIgnoreList
		 * Get existing ignore privacy list when connecting.
		 */
		GetIgnoreList: function() {
			var iq = $iq({
					type: 'get',
					from: Candy.Core.getUser().getEscapedJid()
				})
				.c('query', {xmlns: Strophe.NS.PRIVACY})
				.c('list', {name: 'ignore'}).tree();
			var iqId = Candy.Core.getConnection().sendIQ(iq);
			// add handler (<#200 at https://github.com/candy-chat/candy/issues/200>)
			Candy.Core.addHandler(Candy.Core.Event.Jabber.PrivacyList, null, 'iq', null, iqId);
		},

		/** Function: SetIgnoreListActive
		 * Set ignore privacy list active
		 */
		SetIgnoreListActive: function() {
			Candy.Core.getConnection().sendIQ($iq({
					type: 'set',
					from: Candy.Core.getUser().getEscapedJid()})
				.c('query', {xmlns: Strophe.NS.PRIVACY })
				.c('active', {name:'ignore'}).tree());
		},

		/** Function: GetJidIfAnonymous
		 * On anonymous login, initially we don't know the jid and as a result, Candy.Core._user doesn't have a jid.
		 * Check if user doesn't have a jid and get it if necessary from the connection.
		 */
		GetJidIfAnonymous: function() {
			if (!Candy.Core.getUser().getJid()) {
				Candy.Core.log("[Jabber] Anonymous login");
				Candy.Core.getUser().data.jid = Candy.Core.getConnection().jid;
			}
		},

		/** Class: Candy.Core.Action.Jabber.Room
		 * Room-specific commands
		 */
		Room: {
			/** Function: Join
			 * Requests disco of specified room and joins afterwards.
			 *
			 * TODO:
			 *   maybe we should wait for disco and later join the room?
			 *   but what if we send disco but don't want/can join the room
			 *
			 * Parameters:
			 *   (String) roomJid - Room to join
			 *   (String) password - [optional] Password for the room
			 */
			Join: function(roomJid, password) {
				self.Jabber.Room.Disco(roomJid);
				roomJid = Candy.Util.escapeJid(roomJid);
				var conn = Candy.Core.getConnection(),
					roomNick = roomJid + '/' + Candy.Core.getUser().getNick(),
					pres = $pres({ to: roomNick, id: 'pres:' + conn.getUniqueId() })
						.c('x', {xmlns: Strophe.NS.MUC});
				if (password) {
					pres.c('password').t(password);
				}
				pres.up().c('c', conn.caps.generateCapsAttrs());
				conn.send(pres.tree());
			},

			/** Function: Leave
			 * Leaves a room.
			 *
			 * Parameters:
			 *   (String) roomJid - Room to leave
			 */
			Leave: function(roomJid) {
				var user = Candy.Core.getRoom(roomJid).getUser();
				if (user) {
					Candy.Core.getConnection().muc.leave(roomJid, user.getNick(), function() {});
				}
			},

			/** Function: Disco
			 * Requests <disco info of a room at http://xmpp.org/extensions/xep-0045.html#disco-roominfo>.
			 *
			 * Parameters:
			 *   (String) roomJid - Room to get info for
			 */
			Disco: function(roomJid) {
				Candy.Core.getConnection().sendIQ($iq({
					type: 'get',
					from: Candy.Core.getUser().getEscapedJid(),
					to: Candy.Util.escapeJid(roomJid)
				}).c('query', {xmlns: Strophe.NS.DISCO_INFO}).tree());
			},

			/** Function: Message
			 * Send message
			 *
			 * Parameters:
			 *   (String) roomJid - Room to which send the message into
			 *   (String) msg - Message
			 *   (String) type - "groupchat" or "chat" ("chat" is for private messages)
			 *   (String) xhtmlMsg - XHTML formatted message [optional]
			 *
			 * Returns:
			 *   (Boolean) - true if message is not empty after trimming, false otherwise.
			 */
			Message: function(roomJid, msg, type, xhtmlMsg) {
				// Trim message
				msg = $.trim(msg);
				if(msg === '') {
					return false;
				}
				var nick = null;
				if(type === 'chat') {
					nick = Strophe.getResourceFromJid(roomJid);
					roomJid = Strophe.getBareJidFromJid(roomJid);
				}
				// muc takes care of the escaping now.
				Candy.Core.getConnection().muc.message(roomJid, nick, msg, xhtmlMsg, type);
				return true;
			},

			/** Function: Invite
			 * Sends an invite stanza to multiple JIDs
			 *
			 * Parameters:
			 *   (String) roomJid - Room to which send the message into
			 *   (Array)  invitees - Array of JIDs to be invited to the room
			 *   (String) reason - Message to include with the invitation [optional]
			 *   (String) password - Password for the MUC, if required [optional]
			 */
			Invite: function(roomJid, invitees, reason, password) {
				reason = $.trim(reason);
				var message = $msg({to: roomJid});
				var x = message.c('x', {xmlns: Strophe.NS.MUC_USER});
				$.each(invitees, function(i, invitee) {
					invitee = Strophe.getBareJidFromJid(invitee);
					x.c('invite', {to: invitee});
					if (typeof reason !== 'undefined' && reason !== '') {
						x.c('reason', reason);
					}
				});

				if (typeof password !== 'undefined' && password !== '') {
					x.c('password', password);
				}

				Candy.Core.getConnection().send(message);
			},

			/** Function: IgnoreUnignore
			 * Checks if the user is already ignoring the target user, if yes: unignore him, if no: ignore him.
			 *
			 * Uses the ignore privacy list set on connecting.
			 *
			 * Parameters:
			 *   (String) userJid - Target user jid
			 */
			IgnoreUnignore: function(userJid) {
				Candy.Core.getUser().addToOrRemoveFromPrivacyList('ignore', userJid);
				Candy.Core.Action.Jabber.Room.UpdatePrivacyList();
			},

			/** Function: UpdatePrivacyList
			 * Updates privacy list according to the privacylist in the currentUser
			 */
			UpdatePrivacyList: function() {
				var currentUser = Candy.Core.getUser(),
					iq = $iq({type: 'set', from: currentUser.getEscapedJid()})
						.c('query', {xmlns: 'jabber:iq:privacy' })
							.c('list', {name: 'ignore'}),
					privacyList = currentUser.getPrivacyList('ignore');
				if (privacyList.length > 0) {
					$.each(privacyList, function(index, jid) {
						iq.c('item', {type:'jid', value: Candy.Util.escapeJid(jid), action: 'deny', order : index})
							.c('message').up().up();
					});
				} else {
					iq.c('item', {action: 'allow', order : '0'});
				}
				Candy.Core.getConnection().sendIQ(iq.tree());
			},

			/** Class: Candy.Core.Action.Jabber.Room.Admin
			 * Room administration commands
			 */
			Admin: {
				/** Function: UserAction
				 * Kick or ban a user
				 *
				 * Parameters:
				 *   (String) roomJid - Room in which the kick/ban should be done
				 *   (String) userJid - Victim
				 *   (String) type - "kick" or "ban"
				 *   (String) msg - Reason
				 *
				 * Returns:
				 *   (Boolean) - true if sent successfully, false if type is not one of "kick" or "ban".
				 */
				UserAction: function(roomJid, userJid, type, reason) {
					roomJid = Candy.Util.escapeJid(roomJid);
					userJid = Candy.Util.escapeJid(userJid);
					var itemObj = {nick: Strophe.getResourceFromJid(userJid)};
					switch(type) {
						case 'kick':
							itemObj.role = 'none';
							break;
						case 'ban':
							itemObj.affiliation = 'outcast';
							break;
						default:
							return false;
					}
					Candy.Core.getConnection().sendIQ($iq({
						type: 'set',
						from: Candy.Core.getUser().getEscapedJid(),
						to: roomJid
					}).c('query', {xmlns: Strophe.NS.MUC_ADMIN })
						.c('item', itemObj).c('reason').t(reason).tree());
					return true;
				},

				/** Function: SetSubject
				 * Sets subject (topic) of a room.
				 *
				 * Parameters:
				 *   (String) roomJid - Room
				 *   (String) subject - Subject to set
				 */
				SetSubject: function(roomJid, subject) {
					Candy.Core.getConnection().muc.setTopic(Candy.Util.escapeJid(roomJid), subject);
				}
			}
		}
	};

	return self;
}(Candy.Core.Action || {}, Strophe, jQuery));