Ejemplo n.º 1
0
 public void OnMessageStatusChanged(Im im)
 {
     if (_onMessageStatusChanged != null)
     {
         _onMessageStatusChanged(im);
     }
     Trace(im.uin + "->" + im.Status);
 }
Ejemplo n.º 2
0
 private void ReadMsg2(byte[] _bytes, string uin, Flap _Flap)
 {
     using (MemoryStream _MemoryStream = new MemoryStream(_bytes))
     {
         ushort charset  = _MemoryStream.ReadUInt16();
         ushort language = _MemoryStream.ReadUInt16();
         _bytes = _MemoryStream.Read();
         Encoding incoming = charset == 2 ? Encoding.BigEndianUnicode : Encoding.ASCII;
         incoming = Encoding.GetEncoding(incoming.CodePage, new EncoderExceptionFallback(), new DecoderExceptionFallback());
         string msg;
         try { msg = incoming.GetString(_bytes); }
         catch (DecoderFallbackException) { msg = Encoding.Default.GetString(_bytes); }
         Im _IM = new Im {
             msg = msg, uin = uin, Status = MessageStatus.Received
         };
         if (_onMessage != null)
         {
             _onMessage(_IM);
         }
     }
 }
Ejemplo n.º 3
0
 public void SendMessage(Im im)
 {
     try
     {
         if (_ConnectionStatus != ConnectionStatus.Connected)
         {
             throw new Exception("Not Connected");
         }
         Flap _Flap = new Flap {
             _ICQ = this
         };
         _Flap.ch        = 2;
         _Flap._Snac.ID1 = 4;
         _Flap._Snac.ID2 = 6;
         _Flap.WriteSnac();
         _Dictionary.Add(_Flap._Snac.req, im);
         _Flap._data.Write("00 00 00 00 00 00 00 00 00 01".Hex());
         byte[] sendto = ASCIIEncoding.ASCII.GetBytes(im.uin);
         _Flap._data.WriteByte((byte)sendto.Length);
         _Flap._data.Write(sendto);
         _Flap._data.Write(new byte[] { 0, 6, 0, 0 });
         Tvl _Tvl02 = new Tvl()
         {
             data = new byte[] { 05, 01, 00, 02, 01, 01 }, type = 2
         };
         Tvl _Tvl0101 = new Tvl()
         {
             type = 257, data = ("\0\0\0\0" + im.msg).ToBytes()
         };
         _Tvl02.data = _Tvl02.data.Join(_Tvl0101.ToBytes());
         _Flap._Tvls.Add(_Tvl02);
         _Flap.WriteTvl().Send();
         im.Status    = MessageStatus.Sending;
         im._DateTime = DateTime.Now;
     }
     catch (IOException) { Trace2(_uin + "Error msg send failed"); }
 }
Ejemplo n.º 4
0
        private void StartListen()
        {
            try
            {
                try
                {
                    Connect();
                }
                catch (IOException) { throw new ExceptionB("conn error2"); }
                catch (SocketException) { throw new ExceptionB("conn error2"); }
                Thread.Sleep(2000);
                _ConnectionStatus = ConnectionStatus.Connected;
                Trace2(Trace("Connected " + _uin));
                while (true)
                {
                    Flap _Flap = new Flap {
                        _ICQ = this
                    };
                    _Flap.Receive();
                    Trace("FlapReceived");
                    Trace(_Flap._data.ToArray().ToHex());
                    Trace("Snac Type Of" + _Flap.ch);
                    switch (_Flap.ch)
                    {
                    default:
                        break;

                    case 2:
                    {
                        _Flap.ReadSnac();
                        if (_Flap._Snac.ID1 == 4)
                        {
                            Im _Im = null;
                            if (_Dictionary.Keys.Contains(_Flap._Snac.req))
                            {
                                _Im = _Dictionary[_Flap._Snac.req];
                            }

                            if (_Flap._Snac.ID2 == 7)         //message
                            {
                                ReadMsg1(_Flap);
                            }
                            if (_Flap._Snac.ID2 == 12 && _Im != null)         //accepted
                            {
                                _Im.Status = MessageStatus.AcceptedForDelivery;
                                OnMessageStatusChanged(_Im);
                                _Dictionary.Remove(_Flap._Snac.req);
                            }
                            if (_Flap._Snac.ID2 == 1 && _Im != null)
                            {
                                _Im.Status = MessageStatus.UnknownError;
                                _Dictionary.Remove(_Flap._Snac.req);
                                int errorcode = _Flap._data.ReadUInt16();
                                _Flap.ReadTvl();
                                int subcode = 0;
                                Tvl _Tvl    = _Flap.GetTvl(0x0008);
                                if (_Tvl != null)
                                {
                                    subcode = BitConverter.ToUInt16(_Tvl.data.ReverseA(2), 0);
                                }
                                switch (errorcode)
                                {
                                case 0x0004:
                                    _Im.Status = MessageStatus.BuddyOffline;
                                    break;

                                case 0x0010:
                                    _Im.Status = MessageStatus.BuddyBlocked;
                                    break;
                                }
                                switch (subcode)
                                {
                                case 0x000E:
                                    _Im.Status = MessageStatus.OfflineMessagesNotSupported;
                                    break;

                                case 0x000F:
                                    _Im.Status = MessageStatus.OfflineStorageFull;
                                    break;
                                }
                                OnMessageStatusChanged(_Im);
                            }
                        }
                    }
                    break;
                    }
                }
            }
            catch (ExecutionEngineException) { }
            catch (IOException e)
            {
                Trace(e);
                Thread.Sleep(1000);
                _ConnectionStatus = ConnectionStatus.Kicked;
            }
            catch (SocketException e)
            {
                Trace(e);
                Thread.Sleep(1000);
                _ConnectionStatus = ConnectionStatus.Kicked;
            }
            catch (ExceptionB e)
            {
                Trace(e);
                Thread.Sleep(15000);
                if (ConnectionStatus.LoginError == _oldConnectionStatus)
                {
                    Thread.Sleep(16 * 1000 * 60);
                }
                _ConnectionStatus = ConnectionStatus.LoginError;
            }
            errors++;
        }