/
XMPPModel.as
executable file
·1329 lines (1112 loc) · 38.1 KB
/
XMPPModel.as
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.cleartext.esm.models
{
import com.cleartext.esm.events.ApplicationEvent;
import com.cleartext.esm.events.FormEvent;
import com.cleartext.esm.events.UserAccountEvent;
import com.cleartext.esm.events.XmppErrorEvent;
import com.cleartext.esm.models.types.ChatStateTypes;
import com.cleartext.esm.models.types.IQTypes;
import com.cleartext.esm.models.types.MicroBloggingServiceTypes;
import com.cleartext.esm.models.types.SubscriptionTypes;
import com.cleartext.esm.models.utils.LinkUitls;
import com.cleartext.esm.models.valueObjects.Buddy;
import com.cleartext.esm.models.valueObjects.BuddyGroup;
import com.cleartext.esm.models.valueObjects.ChatRoom;
import com.cleartext.esm.models.valueObjects.Contact;
import com.cleartext.esm.models.valueObjects.FormField;
import com.cleartext.esm.models.valueObjects.FormObject;
import com.cleartext.esm.models.valueObjects.Message;
import com.cleartext.esm.models.valueObjects.Status;
import com.cleartext.esm.models.valueObjects.UserAccount;
import com.hurlant.crypto.tls.TLSConfig;
import com.hurlant.crypto.tls.TLSEngine;
import com.hurlant.crypto.tls.TLSEvent;
import com.hurlant.crypto.tls.TLSSocket;
import com.seesmic.as3.xmpp.IdHandler;
import com.seesmic.as3.xmpp.IqStanza;
import com.seesmic.as3.xmpp.MessageStanza;
import com.seesmic.as3.xmpp.PresenceStanza;
import com.seesmic.as3.xmpp.StreamEvent;
import com.seesmic.as3.xmpp.XMPP;
import com.seesmic.as3.xmpp.XMPPEvent;
import flash.utils.Dictionary;
import mx.controls.Alert;
import mx.states.AddChild;
import org.swizframework.Swiz;
public class XMPPModel
{
//------------------------------------------------------------------
//
// NAMESPACES
//
//------------------------------------------------------------------
namespace discoInfo = "http://jabber.org/protocol/disco#info";
public var DISCO_INFO_NS:String = "http://jabber.org/protocol/disco#info";
namespace discoItems = "http://jabber.org/protocol/disco#items";
public var DISCO_ITEMS_NS:String = "http://jabber.org/protocol/disco#items";
namespace jabberRegister = "jabber:iq:register";
public var JABBER_REGISTER_NS:String = "jabber:iq:register";
namespace muc = "http://jabber.org/protocol/muc";
public var MUC_NS:String = "http://jabber.org/protocol/muc";
namespace mucOwner = "http://jabber.org/protocol/muc#owner";
public var MUC_OWNER_NS:String = "http://jabber.org/protocol/muc#owner";
namespace mucUser = "http://jabber.org/protocol/muc#user";
public var MUC_USER_NS:String = "http://jabber.org/protocol/muc#user";
namespace vCardTemp = "vcard-temp";
public var V_CARD_TEMP_NS:String = "vcard-temp";
namespace jabberRoster = "jabber:iq:roster";
public var JABBER_ROSTER_NS:String = "jabber:iq:roster";
namespace jabberData = "jabber:x:data";
public var JABBER_DATA_NS:String = "jabber:x:data";
//------------------------------------------------------------------
//
// MODELS
//
//------------------------------------------------------------------
[Autowire(bean="appModel")]
[Bindable]
public var appModel:ApplicationModel;
private function get settings():SettingsModel
{
return appModel.settings;
}
private function get database():DatabaseModel
{
return appModel.database;
}
private function get buddies():ContactModel
{
return appModel.buddies;
}
private function get requests():BuddyRequestModel
{
return appModel.requests;
}
private function get chats():ChatModel
{
return appModel.chats;
}
private function get chatRooms():ChatRoomModel
{
return appModel.chatRooms;
}
private function get soundColor():SoundAndColorModel
{
return appModel.soundColor;
}
private function get avatarModel():AvatarModel
{
return appModel.avatarModel;
}
//------------------------------------------------------------------
//
// VARIABLES
//
//------------------------------------------------------------------
[Bindable]
public var connected:Boolean = false;
// a dictionary to store the nicknames that we have used to
// log into various chat rooms the key of the dictonary is the
// jid of the chat room
// private var chatRoomNicknames:Dictionary = new Dictionary();
// a dictionary to store the variables that we want to associate
// with iq queries the key is the id of the iq stanza the we
// are sending out
private var iqVariables:Dictionary = new Dictionary();
// flags to track progress during connecting
private var gotAvatar:Boolean = false;
private var gotRosterList:Boolean = false;
// the seesmic xmpp object
private var xmpp:XMPP;
public static const cleartextComponentPrefix:String = "cleartext.";
public var cleartextComponentJid:String;
public function get cleartextComponentHost():String
{
return cleartextComponentJid ? cleartextComponentJid.substr(cleartextComponentPrefix.length) : null;
}
public var twitterGatewayJid:String;
//------------------------------------------------------------------
//
// CONSTRUCTOR
//
//------------------------------------------------------------------
public function XMPPModel()
{
xmpp = new XMPP();
// stream event listeners
xmpp.addEventListener(StreamEvent.COMM_IN, consoleHandler);
xmpp.addEventListener(StreamEvent.COMM_OUT, consoleHandler);
// event listeners for starting session (this is the order in which
// the events are dispatched
xmpp.addEventListener(StreamEvent.CONNECTED, logHandler);
xmpp.addEventListener(XMPPEvent.SECURE, logHandler);
xmpp.addEventListener(XMPPEvent.AUTH_SUCCEEDED, logHandler);
xmpp.addEventListener(XMPPEvent.SESSION, sessionHandler);
// fail handlers
xmpp.addEventListener(StreamEvent.CONNECT_FAILED, failHandler);
xmpp.addEventListener(XMPPEvent.AUTH_FAILED, failHandler);
xmpp.addEventListener(StreamEvent.DISCONNECTED, failHandler);
// event listeners for messages, presance and changes to the roster
xmpp.addEventListener(XMPPEvent.MESSAGE, messageHandler);
xmpp.addEventListener(XMPPEvent.CHAT_STATE, chatStateHandler);
xmpp.addEventListener(XMPPEvent.PRESENCE, presenceHandler);
xmpp.addEventListener(XMPPEvent.ROSTER_ITEM, rosterListChangeHandler);
xmpp.addEventListener(XMPPEvent.MESSAGE_MUC, messageHandler);
}
//------------------------------------------------------------------
//
// CONNECTION / DISCONNECTION HANDLERS
//
//------------------------------------------------------------------
//-------------------------------
// CONNECT
//-------------------------------
public function connect():void
{
if(connected || appModel.serverSideStatus.value == Status.CONNECTING)
return;
var account:UserAccount = settings.userAccount;
if(account.jid && account.password)
{
gotAvatar = false;
xmpp.auto_reconnect = true;
appModel.serverSideStatus.value = Status.CONNECTING;
xmpp.setJID(account.jid);
xmpp.setPassword(account.password);
xmpp.setServer(account.server, account.port);
xmpp.setupTLS(TLSEvent, TLSConfig, TLSEngine, TLSSocket, true, true, true);
xmpp.connect();
}
}
//-------------------------------
// DISCONNECT
//-------------------------------
public function disconnect():void
{
if (connected)
{
appModel.log("Disconnecting from XMPP server");
xmpp.send("<presence from='" + xmpp.fulljid.toString() + "' type='unavailable' status='Logged out' />");
connected = false;
}
xmpp.disconnect();
xmpp.auto_reconnect = false;
appModel.serverSideStatus.value =
(appModel.localStatus.value == Status.OFFLINE) ?
Status.OFFLINE : Status.ERROR;
for each(var contact:Contact in buddies.buddies.source)
contact.status.value = Status.OFFLINE;
}
//-------------------------------
// SESSION HANDLER
//-------------------------------
/**
* We have sucessfully initiated a session, now we can
* say the server knows our status and we can get the
* list of buddies and send our presence.
*/
private function sessionHandler(event:XMPPEvent):void
{
appModel.log(event);
appModel.serverSideStatus.value = appModel.localStatus.value;
connected = true;
// get the roster list
gotRosterList = false;
sendIq(settings.userAccount.jid,
IQTypes.GET,
<query xmlns={JABBER_ROSTER_NS} ver='329eg'/>,
getRosterHandler);
// get the vCard stored on the server
getVCard(settings.userAccount.jid);
sendPresence();
}
//------------------------------------------------------------------
//
// LOG / CONSOLE / FAIL HANDLERS
//
//------------------------------------------------------------------
//-------------------------------
// LOG HANDLER
//-------------------------------
public function logHandler(event:Event):void
{
appModel.log(event);
}
//-------------------------------
// CONSOLE HANDLER
//-------------------------------
public function consoleHandler(event:StreamEvent):void
{
appModel.xmlStreamHandler(event);
}
//-------------------------------
// FAIL HANDLER
//-------------------------------
public function failHandler(event:Event):void
{
appModel.log(event);
connected = false;
disconnect();
}
//------------------------------------------------------------------
//
// MESSAGE / CHATSTATE HANDLER
//
//------------------------------------------------------------------
//-------------------------------
// MESSAGE HANDLER
//-------------------------------
/**
* receive a message stanza, find the associated sender and
* receiver buddies, save to the database and add to the correct
* chat
*/
private function messageHandler(event:XMPPEvent):void
{
var messageStanza:MessageStanza = event.stanza as MessageStanza;
// ignore "The message has been sent." messages
if(messageStanza.body == "The message has been sent.")
return;
var message:Message = appModel.createFromStanza(messageStanza);
var contact:Contact = appModel.getContactByJid(message.sender);
// if the buddy does not exist in our buddy list, then treat it as a buddy
// request
if(!contact)
{
requests.receiving(message.sender, messageStanza.nick, message.plainMessage);
database.saveMessage(message);
return;
}
// if this is a message from a groupchat, then work out the
// sender's nickname
if(contact is ChatRoom)
{
message.groupChatSender = event.stanza.from.resource;
}
// otherwise set the buddy's resource, we only save the message
// if it isn't from a chatRoom
else
{
if(contact is Buddy)
(contact as Buddy).resource = messageStanza.from.resource;
contact.status.isTyping = false;
database.saveMessage(message);
}
contact.lastSeen = message.receivedTimestamp.time;
if(!chats.selectedChat || chats.selectedChat.contact != contact)
contact.unreadMessages++;
chats.addMessage(contact, message);
// play sounds & increment unread messages for the mblog buddy
if(contact.isMicroBlogging)
{
if(chats.selectedChat && chats.selectedChat.contact != Buddy.ALL_MICRO_BLOGGING_BUDDY)
Buddy.ALL_MICRO_BLOGGING_BUDDY.unreadMessages++;
chats.addMessage(Buddy.ALL_MICRO_BLOGGING_BUDDY, message);
soundColor.play(SoundAndColorModel.NEW_SOCIAL);
}
else
{
soundColor.play(SoundAndColorModel.NEW_MESSAGE);
}
// notify the user (bounce the dock icon etc.)
Swiz.dispatchEvent(new ApplicationEvent(ApplicationEvent.NOTIFY));
}
//-------------------------------
// CHAT STATE HANDLER
//-------------------------------
private function chatStateHandler(event:XMPPEvent):void
{
var chatState:String = event.stanza.chatState;
var fromJid:String = event.stanza.from.getBareJID();
var buddy:Buddy = appModel.getContactByJid(fromJid) as Buddy;
if(buddy)
buddy.status.isTyping = (chatState == ChatStateTypes.COMPOSING);
}
//------------------------------------------------------------------
//
// PRESENCE HANDLER
//
//------------------------------------------------------------------
private function presenceHandler(event:XMPPEvent):void
{
appModel.log(event);
var stanza:PresenceStanza = event.stanza as PresenceStanza;
var fromJid:String = stanza.from.getBareJID();
var contact:Contact = appModel.getContactByJid(fromJid);
// if this is our own presence we don't care
if(fromJid == settings.userAccount.jid)
return;
var chatRoom:ChatRoom = contact as ChatRoom;
// if this is from a chat room that we have asked to join
if(chatRoom)
{
try
{
var x:Object = stanza.getXML().mucUser::x[0];
if(x)
{
// if status code is 201, then send an iq telling the server
// to create the room with default settings
var status:Object = x.mucUser::status;
if(status && status[0] && status[0].@code=='201')
{
sendIq(fromJid,
IQTypes.SET,
<query xmlns={MUC_OWNER_NS}><x xmlns={JABBER_DATA_NS} type="submit" /></query>,
createDefaultChatRoomHandler,
chatRoom);
return;
}
var jid:String = x.mucUser::item[0].@jid;
var index:int = jid.indexOf("/");
if(index != -1)
jid = jid.substr(0,index);
chatRoom.setPresence(jid, stanza.type, stanza.from.resource, "");
}
}
catch(e:Error)
{
appModel.log(e);
}
return;
}
// if this is a subscription request
if(stanza.type == SubscriptionTypes.SUBSCRIBE)
{
// if there are in our rosterlist, then auto-subscirbe
if(contact)
{
sendSubscribe(contact.jid, SubscriptionTypes.SUBSCRIBED);
if((contact is Buddy) && !(contact as Buddy).subscribedTo)
sendSubscribe(contact.jid, SubscriptionTypes.SUBSCRIBE);
}
// if they aren't in the roster list, then alert the user
else
{
requests.receiving(fromJid, stanza.nick);
// notify the user - bounce the dock icon etc
Swiz.dispatchEvent(new ApplicationEvent(ApplicationEvent.NOTIFY));
}
}
// all other presence types (including subscribed and unsubscribed can
// be handled here - using Status.setFromStanzaType()
else
{
if(!contact)
{
// we don't care about you if you aren't in our roster list
return;
}
var wasOffline:Boolean = contact.status.isOffline();
if(contact is Buddy)
(contact as Buddy).resource = stanza.from.resource;
// setFromStanzaType also handles "unsubscribed" and "subscribed"
// if there is an error, then reset the customStatus, otherwise
// set the customStatus from the stanza
var error:Boolean = contact.status.setFromStanzaType(stanza.type);
contact.customStatus = (error) ? "" : stanza.status;
// only set lastSeen if the buddy was offline or we are now
// explicitly going online
if(wasOffline || contact.status.value == Status.AVAILABLE)
contact.lastSeen = new Date().time;
var avatarHash:String = stanza.avatarHash;
if(avatarHash)
avatarModel.setUrlOrHash(contact.jid, avatarHash);
buddies.buddies.refresh();
database.saveBuddy(contact);
}
}
//------------------------------------------------------------------
//
// ROSTER HANDLERS
//
//------------------------------------------------------------------
//-------------------------------
// GET ROSTER HANDLER - CALLED TO GET INITAL ROSTER LIST AFTER CONNECTION
//-------------------------------
private function getRosterHandler(stanza:IqStanza):void
{
appModel.log("getRosterHandler");
var contact:Contact;
var buddiesToDelete:Dictionary = new Dictionary();
for each(contact in buddies.buddies.source)
if(!(contact is ChatRoom) && !(contact is BuddyGroup))
buddiesToDelete[contact.jid] = contact;
for each(var item:XML in stanza.query.jabberRoster::item)
{
var jid:String = item.@jid;
// if we already have the buddy, then we just want to
// update the values, otherwise create a new buddy
contact = appModel.getContactByJid(jid);
if(!contact)
contact = new Buddy(jid);
var b:Buddy = contact as Buddy;
if(b)
{
var groups:Array = new Array();
for each(var group:XML in item.jabberRoster::group)
{
var gString:String = String(group.text());
if(gString != "")
groups.push(gString);
}
b.groups = groups;
b.subscription = item.@subscription;
requests.setSubscription(jid, b.nickname, b.subscription);
}
contact.nickname = item.@name;
avatarModel.getAvatar(jid).displayName = item.@name;
// flag used to delete buddies that are no longer in
// the roster list
delete buddiesToDelete[contact.jid];
// this adds, saves and adds an event listener to the buddy
buddies.addBuddy(contact);
}
for each(contact in buddiesToDelete)
buddies.removeBuddy(contact);
gotRosterList = true;
// discoveryItems() gives us a list of jids available on the server
discoveryItems(settings.userAccount.host,
function(jids:Array):void
{
for each(var j:String in jids)
discoveryInfo(j, testForMicroBloggingComponents);
});
}
//-------------------------------
// ROSTER CHANGED HANDLER
//
// called whenever the roster list changes
//-------------------------------
private function rosterListChangeHandler(event:XMPPEvent):void
{
// if we haven't already got the roster list, then ignore
if(connected && !gotRosterList)
return;
appModel.log("rosterListChangeHandler");
var jid:String = event.stanza["jid"];
// if we already have the buddy, then we just want to
// update the values, otherwise create a new buddy
var buddy:Buddy = appModel.getContactByJid(jid) as Buddy;
// NOTE we want a Buddy - a roster item here, this could
// lead to duplicates if someone adds a group or a chatroom
// to their roster list
if(!buddy)
{
buddy = new Buddy(jid);
buddies.addBuddy(buddy);
}
var subscription:String = event.stanza["subscription"];
if(subscription == SubscriptionTypes.REMOVE)
{
buddies.removeBuddy(buddy);
}
else
{
buddy.groups = event.stanza["groups"];
buddy.nickname = event.stanza["name"];
buddy.subscription = subscription;
avatarModel.getAvatar(jid).displayName = event.stanza["name"];
discoveryInfo(buddy.jid, testForMicroBloggingComponents);
requests.setSubscription(jid, buddy.nickname, buddy.subscription);
}
buddies.refresh();
}
//-------------------------------
// TEST FOR MICRO BLOGGING COMPONENTS
//
// Every time we get the roster list we get a list of transports on the server
// and do a discovery info on them with this function as a handler. When
// the server tells us that a new item is added to the roster we also do a
// discovery info with this function as a handler.
//-------------------------------
private function testForMicroBloggingComponents(iqStanza:IqStanza):void
{
// if this is a cleartext microblogging component
// then check it is in the roster list
if(iqStanza.from == cleartextComponentPrefix + settings.userAccount.host)
{
cleartextComponentJid = iqStanza.from;
var cleartextComponent:Contact = buddies.getBuddyByJid(cleartextComponentJid);
// if this is in the roster list, then check we have the right
// microBloggingServiceType
if(cleartextComponent)
{
cleartextComponent.microBloggingServiceType = MicroBloggingServiceTypes.CLEARTEXT_MICROBLOGGING;
var c:Boolean = false;
if(cleartextComponent.nickname == cleartextComponent.jid)
{
cleartextComponent.nickname = "Cleartext MicroBlogging";
avatarModel.getAvatar(cleartextComponentJid).displayName = "Cleartext MicroBlogging";
c = true;
}
if(!cleartextComponent.isMicroBlogging)
{
cleartextComponent.isMicroBlogging = true;
c = true;
}
if(c)
{
modifyRosterItem(cleartextComponent);
}
}
// otherwise add it to the roster
else
{
cleartextComponent = new Buddy(cleartextComponentJid);
cleartextComponent.nickname = "Cleartext MicroBlogging";
cleartextComponent.microBloggingServiceType = MicroBloggingServiceTypes.CLEARTEXT_MICROBLOGGING;
cleartextComponent.isMicroBlogging = true;
buddies.addBuddy(cleartextComponent);
addToRoster(cleartextComponent);
avatarModel.getAvatar(cleartextComponentJid).displayName = "Cleartext MicroBlogging";
}
}
else if(iqStanza.query.discoInfo::identity)
{
// if this is the status one twitter gateway, then set
// the twitterGatewayJid
var identity:XML = iqStanza.query.discoInfo::identity[0];
if(identity && identity.@category == "gateway" && identity.@type == "twitter")
{
twitterGatewayJid = iqStanza.from;
// if we have the twitter gataway in the roster
// then make sure it has the right settings
if(buddies.containsJid(twitterGatewayJid))
{
var twitterGateway:Contact = buddies.getBuddyByJid(twitterGatewayJid);
twitterGateway.microBloggingServiceType = MicroBloggingServiceTypes.STATUS_ONE_TWITTER;
var change:Boolean = false;
if(twitterGateway.nickname == twitterGateway.jid)
{
twitterGateway.nickname = "Twitter";
avatarModel.getAvatar(twitterGatewayJid).displayName = "Twitter";
change = true;
}
if(!twitterGateway.isMicroBlogging)
{
twitterGateway.isMicroBlogging = true;
change = true;
}
if(change)
{
modifyRosterItem(twitterGateway);
}
}
}
}
}
//-------------------------------
// ADD TO ROSTER
//-------------------------------
public function addToRoster(buddy:Contact):void
{
var query:XML = <query xmlns={JABBER_ROSTER_NS} />;
var item:XML = <item jid={buddy.jid} />;
if(buddy.nickname != buddy.jid)
item.@name = buddy.nickname;
if(buddy is Buddy)
{
var groups:Array = (buddy as Buddy).groups;
for each(var group:String in groups)
item.appendChild(<group>{group}</group>);
}
query.appendChild(item);
sendIq(settings.userAccount.jid,
IQTypes.SET,
query,
addToRosterHandler,
buddy.jid);
}
//-------------------------------
// ADD TO ROSTER HANDLER
//-------------------------------
private function addToRosterHandler(stanza:IqStanza):void
{
var id:String = stanza.id;
if(iqVariables.hasOwnProperty(id))
{
var toJid:String = iqVariables[id] as String;
sendSubscribe(toJid, SubscriptionTypes.SUBSCRIBE);
chats.getChat(appModel.getContactByJid(toJid), true);
delete iqVariables[id];
}
appModel.log("addToRosterHandler");
}
//-------------------------------
// REMOVE FROM ROSTER
//-------------------------------
public function removeFromRoster(toJid:String):void
{
chats.removeChat(toJid);
sendIq(settings.userAccount.jid,
IQTypes.SET,
<query xmlns={JABBER_ROSTER_NS}><item jid={toJid} subscription={SubscriptionTypes.REMOVE} /></query>,
removeFromRosterHandler);
}
//-------------------------------
// REMOVE FROM ROSTER HANDLER
//-------------------------------
private function removeFromRosterHandler(stanza:IqStanza):void
{
appModel.log("removeFromRosterHandler");
}
//-------------------------------
// MODIFY ROSTER ITEM
//-------------------------------
public function modifyRosterItem(buddy:Contact):void
{
var query:XML = <query xmlns={JABBER_ROSTER_NS} />;
var item:XML = <item jid={buddy.jid}/>
if(buddy is Buddy)
item.@subscription=(buddy as Buddy).subscription;
if(buddy.nickname != buddy.jid)
item.@name = buddy.nickname;
if(buddy is Buddy)
{
var groups:Array = (buddy as Buddy).groups;
for each(var group:String in groups)
item.appendChild(<group>{group}</group>);
}
query.appendChild(item);
sendIq(settings.userAccount.jid,
IQTypes.SET,
query,
modifyRosterHandler);
}
//-------------------------------
// MODIFY ROSTER HANDLER
//-------------------------------
private function modifyRosterHandler(stanza:IqStanza):void
{
appModel.log("modifyRosterHandler");
}
//-------------------------------
// IS TWITTER GATEWAY
//-------------------------------
private function isTwitterGateway(jid:String):Boolean
{
return false;
}
//------------------------------------------------------------------
//
// VCARD HANDLERS
//
//------------------------------------------------------------------
//-------------------------------
// GET VCARD
//-------------------------------
public function getVCard(jid:String):void
{
sendIq(jid,
IQTypes.GET,
<vCard xmlns='vcard-temp'/>,
vCardHandler);
}
//-------------------------------
// VCARD HANDLER
//-------------------------------
private function vCardHandler(stanza:IqStanza):void
{
var xml:XML = stanza.getXML();
var buddyJid:String = stanza.from;
// if we are downloading our v-card from the server
if(buddyJid == settings.userAccount.jid)
{
gotAvatar = true;
var vCard:XMLList = xml.vCardTemp::vCard;
var serverAvatar:String = vCard.vCardTemp::PHOTO.vCardTemp::BINVAL;
var localAvatar:String = avatarModel.userAccountAvatar.bitmapString;
// if we have a different avatar to the one on the server, then send a
// vcard back to the server
if(serverAvatar != localAvatar && localAvatar != "" && localAvatar != "null")
{
vCard.vCardTemp::PHOTO.vCardTemp::BINVAL = localAvatar;
sendIq(settings.userAccount.jid, IQTypes.SET, vCard[0]);
}
else if(localAvatar == "" || localAvatar == "null")
{
// make sure the hash is also set !!
avatarModel.setBitmapString("userAccount", serverAvatar);
}
// now we have the avatar, resend the presence to make sure the
// avatar hash is sent to our buddies
sendPresence();
}
else
{
var avatarString:String = xml.vCardTemp::vCard.vCardTemp::PHOTO.vCardTemp::BINVAL;
avatarModel.setBitmapString(buddyJid, avatarString);
}
}
//-------------------------------
// GET VCARD AVATAR FOR MBLOG BUDDY
//-------------------------------
public function getAvatarForMBlogBuddy(jid:String):void
{
sendIq(jid,
IQTypes.GET,
<vCard xmlns='vcard-temp'/>,
mBlogVCardHandler);
}
//-------------------------------
// MBLOG BUDDY VCARD HANDLER
//-------------------------------
private function mBlogVCardHandler(stanza:IqStanza):void
{
var xml:XML = stanza.getXML();
var buddyJid:String = stanza.from;
var avatarString:String = xml.vCardTemp::vCard.vCardTemp::PHOTO.vCardTemp::BINVAL;
avatarModel.setBitmapString(buddyJid, avatarString);
}
//------------------------------------------------------------------
//
// SEND IQ / PRESENCE / MESSAGE
//
//------------------------------------------------------------------
//-------------------------------
// SEND IQ
//-------------------------------
public function sendIq(tojid:String, type:String, payload:XML, responseHandler:Function=null, responseVariables:Object=null):void
{
var iqStanza:IqStanza = new IqStanza(xmpp);
var id:String = iqStanza.setID();
if(responseHandler != null)
xmpp.addHandler(new IdHandler(id, responseHandler));
if(responseVariables != null)
iqVariables[id] = responseVariables;
iqStanza.setTo(tojid);
iqStanza.setType(type);
iqStanza.setQuery(payload);
iqStanza.send();
}
//-------------------------------
// SEND PRESENCE
//-------------------------------
public function sendPresence(toJid:String=""):void
{
var status:Status = appModel.localStatus;
var customStatus:String = settings.userAccount.customStatus;
// if we want to go offline, then disconnect
if(status.value == Status.OFFLINE)
{
disconnect();
}
// if we are already connected, then send the presence
else if(connected)
{
var priority:String = (status.value == Status.AWAY || status.value == Status.EXTENDED_AWAY) ? "0" : "5";
xmpp.sendPresence(customStatus, status.toShow(), priority, toJid, (gotAvatar) ? avatarModel.userAccountAvatar.urlOrHash : "");
appModel.serverSideStatus.value = status.value;
}
// if we want to connect, then try to connect, if we are successful, then
// we will send the presence once the session is established
else
{
connect();
}
}
//-------------------------------
// SEND MESSAGE
//-------------------------------
public function sendMessage(toJid:String, body:String, subject:String=null, type:String='chat', chatState:String=null, customTags:Array=null):MessageStanza
{
var msg:MessageStanza = xmpp.sendMessage(toJid, body, subject, type, chatState, customTags);
msg.body = LinkUitls.escapeHTML(msg.body);
return msg;
}
//------------------------------------------------------------------
//
// SEND SUBSCRIBE / BLOCK
//
//------------------------------------------------------------------
public function sendSubscribe(toJid:String, type:String):void
{
var xmlString:String = '<presence to="' + toJid + '" type="' + type;
if(type == SubscriptionTypes.SUBSCRIBE)
{
requests.sending(toJid);
xmlString += '"><nick xmlns="http://jabber.org/protocol/nick">' + settings.userAccount.nickname + '</nick></presence>';
}
else
{
xmlString += '" />';