package FlashChat { import flash.events.Event; import flash.events.EventDispatcher; import flash.external.ExternalInterface; import flash.net.SharedObject; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLRequestMethod; import flash.net.URLVariables; import flash.utils.Timer; import flash.utils.setInterval; import flash.xml.XMLDocument; import flash.xml.XMLNode; public class ChatManager extends EventDispatcher { private static var instance:EventDispatcher = new EventDispatcher(); public static var tmrFlood: Timer; public static var connectionId:String = ""; public static var messageId:uint = 0; //public static var Users:Array = new Array(); //public static var Rooms:Array = new Array(); public static function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void { instance.addEventListener(type,listener,useCapture,priority,useWeakReference); } private static function localEventListener(e:ChatEvent):void { } public function ChatManager():void { } public static function ignoreUser(ignoredUserID: int, txt: String):void { //ChatManager.getUserByID(ignoredUserID).ignored = true; Settings.getUserById(ignoredUserID).ignored = true; Settings.dispatchEvent(new Event('updateUsers')); var req: URLVariables = new URLVariables(); req.c = 'ignu'; req.u = ignoredUserID; req.t = txt; send(req); } public static function unignoreUser(unignoreUserID: int, txt: String):void { //ChatManager.getUserByID(unignoreUserID).ignored = false; Settings.getUserById(unignoreUserID).ignored = false; Settings.dispatchEvent(new Event('updateUsers')); var req: URLVariables = new URLVariables(); req.c = 'nignu'; req.u = unignoreUserID; req.t = txt; send(req); } public static function createRoom(label: String, isPublic: Boolean, password:String):void { var req: URLVariables = new URLVariables(); req.c = 'adr'; req.l = label; req.p = (isPublic)?1:0; req.ps = password; send(req); } public static function requestUserProfileText(userId: int): void { var req: URLVariables = new URLVariables(); req.c = 'usrp'; req.u = userId; send(req); } public static function inviteUserTo(invitedUserID: int, toRoomID: uint, txt: String):void { var req: URLVariables = new URLVariables(); req.c = 'invu'; req.u = invitedUserID; req.r = toRoomID; req.t = txt; send(req); } public static function acceptInvitationTo(invitedByUserID:int, toRoomID:uint, txt:String):void { var req: URLVariables = new URLVariables(); req.c = 'inva'; req.u = invitedByUserID; req.r = toRoomID; req.t = txt; send(req); changeRoom(toRoomID, null, true); } public static function sendTimeZone():void { var req: URLVariables = new URLVariables(); req.c = 'tzset'; req.tz = new Date().getTimezoneOffset(); send(req); } public static function logout(isExpected: Boolean):void { Settings.isLoggedOut = isExpected; var req: URLVariables = new URLVariables(); req.c = 'lout'; send(req); } public static function banUser(bannedUserID: int, banType: uint, banRoomID: uint, txt: String, sup: int): void { var req: URLVariables = new URLVariables(); req.c = 'banu'; req.u = bannedUserID; req.b = banType; req.r = banRoomID; req.t = txt; if(sup) req.s = sup; send(req); } public static function unbanUser(bannedUserID: int, txt: String, sup: int): void { var req: URLVariables = new URLVariables(); req.c = 'nbanu'; req.u = bannedUserID; req.t = txt; if (sup) req.s = sup; send(req); } public static function requestHelpText(): void { var req: URLVariables = new URLVariables(); req.c = 'help'; send(req); } public static function ringBell(): void { var req: URLVariables = new URLVariables(); req.c = 'ring'; send(req); } public static function back(numb: uint): void { var req: URLVariables = new URLVariables(); req.c = 'back'; req.n = numb; send(req); } public static function backtime(numb: uint): void { var req: URLVariables = new URLVariables(); req.c = 'backt'; req.n = numb; send(req); } public static function alert(userID: int, txt: String, sup: int): void { var req: URLVariables = new URLVariables(); req.c = 'alrt'; req.u = userID; req.t = txt; if(sup) req.s = sup; send(req); } public static function roomAlert(roomID: uint, txt: String, sup: int): void { var req: URLVariables = new URLVariables(); req.c = 'ralrt'; req.r = roomID; req.t = txt; if(sup) req.s = sup; send(req); } public static function chatAlert(txt: String, sup: int): void { var req: URLVariables = new URLVariables(); req.c = 'calrt'; req.t = txt; if(sup) req.s = sup; send(req); } public static function gag(userid: int, minutes: uint, sup: int): void { var req : URLVariables = new URLVariables(); req.c = 'gag'; req.u = userid; req.t = minutes; if(sup) req.s = sup; send(req); } public static function ungag(userid: int, sup: int): void{ var req: URLVariables = new URLVariables(); req.c = 'ngag'; req.u = userid; if(sup) req.s = sup; send(req); } //inArgs = {alrt|gag} public static function confirm(userid: int, inData: String, inArgs: String): void{ var req : URLVariables = new URLVariables(); req.c = 'cfrm'; req.u = userid; req.t = inData; req.a = inArgs; send(req); } public static function startBot(userName: String, roomId: uint, sup: int): void{ var req: URLVariables = new URLVariables(); req.c = 'srtbt'; req.lg = userName; req.r = roomId; if(sup) req.s = sup; send(req); } public static function killBot(userName: String, sup: int): void { var req : URLVariables = new URLVariables(); req.c = 'klbt'; req.lg = userName; if(sup) req.s = sup; send(req); } public static function addBot(login: String, bot: String, sup: int): void { var req: URLVariables = new URLVariables(); req.c = 'adbt'; req.lg = login; req.a = bot; if(sup) req.s = sup; send(req); } public static function teachBot(userName: String, args: String, sup: int): void { var req : URLVariables = new URLVariables(); req.c = 'tchbt'; req.lg = userName; req.a = args; if(sup) req.s = sup; send(req); } public static function unTeachBot(userName: String, args: String, sup: int): void { var req : URLVariables = new URLVariables(); req.c = 'utbt'; req.lg = userName; req.a = args; if(sup) req.s = sup; send(req); } public static function removeBot(userName: String, sup: int): void{ var req : URLVariables = new URLVariables(); req.c = 'rmbt'; req.lg = userName; if(sup) req.s = sup; send(req); } public static function showBots(sup: int): void { var req : URLVariables = new URLVariables(); req.c = 'swbt'; if(sup) req.s = sup; send(req); } public static function getPhoto(inUserID: int): void { var req : URLVariables = new URLVariables(); req.c = 'gpht'; req.u = inUserID; send(req); } public static function declineInvitationTo(invitedByUserID:int, toRoomID:uint, txt:String):void { var req : URLVariables = new URLVariables(); req.c = 'invd'; req.u = invitedByUserID; req.r = toRoomID; req.t = txt; send(req); } public static function setPhoto(path:String, setEmpty: Boolean = false):void { if ('' == path && !setEmpty) { return; } var v:URLVariables = new URLVariables(); v.c = 'spht'; v.a = path; send(v); } public static function changeRoom(id:uint, ps:String = null, isInvite: Boolean = false):void { if (!Settings.getRoomById(id)) return; var v:URLVariables = new URLVariables(); v.c = (isInvite) ? "imvu" : "mvu"; if (ps) { v.ps = ps; } v.r = id; send(v); } public static function changeAvt(s:String, t:String, uId: int = 0, saveChanges: Boolean = true):void { if (saveChanges) { if ('mavt' == t) { Settings.options.mAvt = s; } else { Settings.options.rAvt = s; } } // if ('' == s && !setEmpty) { // return; // } var v:URLVariables = new URLVariables(); v.c = t; v.u = uId;// 0 - Send to all v.a = s; send(v); } /*public static function getRoomByID(id:uint):Object { if (Rooms[id]) return Rooms[id]; else return null; }*/ /*public static function getUserByID(id:uint):Object { if (Users[id]) return Users[id]; else return null; }*/ public static function load(e:Object):void { var xml:XMLDocument = new XMLDocument(); xml.ignoreWhite = true; trace(e.target.data); //Alert.show(e.target.data); try { xml.parseXML(new XML(e.target.data)); } catch (err: Error) { return; } if (xml.firstChild) { if ('' == connectionId) { connectionId = xml.firstChild.attributes["id"]; ExternalInterface.call('setConnId', connectionId); } for (var i:int = 0; i < xml.firstChild.childNodes.length; i++) { var node:XMLNode = xml.firstChild.childNodes[i]; if (parseInt(node.attributes['id']) > messageId || !node.attributes.hasOwnProperty('id') || Settings.enableSocket) { messageId = parseInt(node.attributes['id']); var ce:ChatEvent = new ChatEvent(ChatEvent.EVENT); ce.data = node.attributes; ce.name = node.nodeName.toString(); ce.xml = node.toString(); if (node.childNodes.length != 0) ce.value = node.firstChild.toString(); instance.dispatchEvent(ce); } } } } public static var socket:*; public static function loadMessages():void { if (!Settings.isLoggedOut) { var v:URLVariables = new URLVariables(); //var v:Object = new Object(); v.c = "msgl"; if (connectionId != "") { send(v); } } } public static function login(login:String, password:String, lang:String, room:String):void { if (!Settings.enableSocket) { setInterval(ChatManager.loadMessages, parseInt(Settings.common['msgRequestInterval'])*1000); } else { socket = new socketServer(Settings.socketServer['host'], Settings.socketServer['port']); socket.doConnect(); socket.sendFirst(); } instance.addEventListener(ChatEvent.EVENT,localEventListener); var v:URLVariables = new URLVariables(); v.c = "lin"; v.lg = login; v.ps = password; v.l = lang; v.r = room; var so: SharedObject = SharedObject.getLocal('fc6_ban_info'); if (so.data.hasOwnProperty('banId')) { v.a = so.data.banId; } else { v.a = 0; } so.close(); v.tz = new Date().getTimezoneOffset(); send(v); } public static function send(variables:* = null):void { //exception for banUser (b is used) if (!variables.hasOwnProperty('b')) { variables.b = messageId+1; } if (variables == null) variables = new URLVariables(); var request:URLRequest = new URLRequest(Settings.path+"getxml.php"); variables.session_inst = 1; // TODO variables.id = connectionId; if (Settings.enableSocket) { var v: Object = new Object(); for (var i: String in variables) { v[i] = variables[i]; } if (socket) { socket.sendRequest(v); } } else { request.data = variables; request.method = URLRequestMethod.POST; if ('msgl' != variables.c) { trace('sending......'+request.data); } var loader:URLLoader = new URLLoader(request); loader.addEventListener(Event.COMPLETE,load); } } public static function banMe(banId: uint) : void { //for ban by PC - set cookie form me var v:URLVariables = new URLVariables(); v.c = "banme"; v.a = banId; send(v); } public static function sendMessageTo(toUserID:int, toRoomID:uint, txt:String, args:* = undefined, sup:* = undefined):Boolean { var allow: Boolean = !tmrFlood.running; if (allow) { var v:URLVariables = new URLVariables(); v.c = "msg"; v.u = toUserID; v.r = toRoomID; v.t = txt; v.a = args; v.s = sup; send(v); tmrFlood.start(); } return allow; } public static function setLanguage(lang:String):void { Settings.lang = lang; var v:URLVariables = new URLVariables(); v.c = "glan"; v.l = lang; send(v); } public static function statusChange(statusid:uint):void { var v:URLVariables = new URLVariables(); v.c = "sst"; v.u = Settings.myid; v.t = statusid; send(v); } public static function colorChange(color:uint):void { var v:URLVariables = new URLVariables(); v.c = "scl"; v.t = color; send(v); } } }