Ejemplo n.º 1
0
 void server_Incoming(Nexus.Client c, byte[] d)
 {
     try
     {
         object[] data = (object[])Nexus.Deserialize(d);
         switch ((int)data[0])
         {
             case (int)Nexus.Headers.SendLogin:
                 GetQuery("select * from users");
                 c.LoggedIn = CheckLogin(data[1].ToString().ToLower().Trim(), data[2].ToString());
                 if (c.LoggedIn)
                 {
                     c.Username = data[1].ToString();
                     c.Send(Nexus.Serialize(new object[] { (int)Nexus.Headers.LoginSuccess }));
                     c.Status += c_Status;
                     UpdateList(lvUsers, data[1].ToString(), c);
                     c.Channel = (int)Nexus.Channels.Lounge;
                     SendUsers(c.Channel);
                     Thread.Sleep(10);
                     //TODO: Fix that the right way.
                     //c.Send(Nexus.Serialize(new object[] { (int)Nexus.Headers.NewUser, c.Username }));
                     Invoke(new SendNewUserDelegate(SendNewUser), c);
                     Thread.Sleep(10);
                     c.Send(Nexus.Serialize(new object[] { (int)Nexus.Headers.Channels, GetChannels() }));
                     if ((bool)MOTD[0])
                         c.Send(Nexus.Serialize(new object[] { 2, MOTD[1] }));
                 }
                 else
                 {
                     c.Send(Nexus.Serialize(new object[] { (int)Nexus.Headers.LoginFail }));
                 }
                 break;
             case (int)Nexus.Headers.Message:
                 if (c.LoggedIn)
                 {
                     if (!(string.IsNullOrEmpty(data[1].ToString().Trim())))
                     {
                         UpdateList(lvMessages, new string[] { c.Username, data[1].ToString() });
                         foreach (Nexus.Client client in server.Connections)
                             if (client != c && client.Channel == c.Channel)
                                 client.Send(Nexus.Serialize(new object[] { 1, c.Username, data[1].ToString() }));
                     }
                 }
                 break;
             case (int)Nexus.Headers.SendRegister:
                 if (!CheckRegister(data[1].ToString()) && data[1].ToString().Length <= 24)
                 {
                     c.Send(Nexus.Serialize(new object[] { (int)Nexus.Headers.RegisterSuccess }));
                     InsertUser(data[1].ToString(), data[2].ToString());
                 }
                 else
                     c.Send(Nexus.Serialize(new object[] { (int)Nexus.Headers.RegisterFail }));
                 break;
             case (int)Nexus.Headers.Channels:
                 c.Send(Nexus.Serialize(new object[] { (int)Nexus.Headers.Channels, GetChannels() }));
                 break;
             case (int)Nexus.Headers.SelectChannel:
                 int oldChannel = c.Channel;
                 string chanName = Enum.GetName(typeof(Nexus.Channels), (int)data[1]).Replace("VB_NET", "VB.NET").Replace("CSharp", "C#");
                 c.Channel = (int)data[1];
                 SendUsers(c.Channel, new string[] { c.Username, "has joined this channel." });
                 SendUsers(oldChannel, new string[] { c.Username, "has moved to " + chanName });
                 break;
         }
     }
     catch (Exception ex) { MessageBox.Show(ex.ToString()); c.Disconnect(); /* Get the f**k out. */ }
 }