Esempio n. 1
0
File: Client.cs Progetto: dio85/DGP
 private void OnLineRecived(LineRecivedEventArgs e)
 {
     // We must do a check here to see if anyone has hooked up the event. Because
     // if none has, it's no point in raising the event.
     if (LineRecived != null)
     {
         LineRecived(this, e);
     }
 }
Esempio n. 2
0
 private void OnLineRecived(LineRecivedEventArgs e)
 {
     // We must do a check here to see if anyone has hooked up the event. Because
     // if none has, it's no point in raising the event.
     if (LineRecived != null)
     {
         LineRecived(this, e);
     }
 }
Esempio n. 3
0
        private void ClientLineRecived(object sender, LineRecivedEventArgs e)
        {
            // Print the client's message in the console just so we can se what's happening
            //Logg("Message: " + e.Message);

            // Check the message prefix to see what kind of message the client sent
            if (e.Message.StartsWith("LOGGIN"))
            {
                string messageBuffer = e.Message.Substring("LOGGIN ".Length);
                int    pos           = messageBuffer.IndexOf("/");
                if (pos != -1)
                {
                    string name     = messageBuffer.Substring(0, pos);
                    string password = messageBuffer.Remove(0, pos + 1);

                    if (password != "")
                    {
                        e.Client.Send("LOGGIN FALSE");
                        Logg("LOGGIN ERROR: Ej chatbehhörig");
                    }


                    e.Client.DisplayName = name;
                    e.Client.Id          = getNewClientId().ToString();
                    e.Client.Status      = 'C';
                    e.Client.Send("LOGGIN TRUE " + e.Client.DisplayName + "\r" + e.Client.Id);

                    Logg("Client Id: \'" + e.Client.Id + "\' -  Name: \'" + name + "\'  -   Password: \'password\'  Loggin TRUE");

                    UpdateUserLists();
                }
                else
                {
                    e.Client.Send("LOGGIN FALSE");
                    Logg("LOGGIN ERROR: Inget Namn");
                }
            }
            else if (e.Message.StartsWith("GETUSERLIST"))
            {
                string message = "USERLIST ";

                message += "0\nAll\nC";                 // Server User, Send messages to everybody

                for (int i = 0; i < this.clientList.Count; i++)
                {
                    message += "\n";
                    Client client = (Client)clientList[i];
                    message += client.Id;
                    message += "\n";
                    message += client.DisplayName;
                    message += "\n";
                    message += client.Status;
                }
                //	Logg("GetUserList  -  Id: " + e.Client.Id);
                e.Client.Send(message);
            }
            else if (e.Message.StartsWith("LOST") || e.Message.StartsWith("QUIT"))
            {
                e.Client.Close();
                clientList.Remove(e.Client);
                Logg("Client Removed: " + e.Client.DisplayName + "  -  Clients Left: " + clientList.Count);
                UpdateUserLists();
            }
            else if (e.Message.StartsWith("MSG"))
            {
                string messageBuffer   = e.Message.Substring("MSG ".Length);
                int    endMessageIndex = messageBuffer.IndexOf(" END_MESSAGE\r", 0, messageBuffer.Length);
                string message         = messageBuffer.Substring(0, endMessageIndex);
                messageBuffer = messageBuffer.Remove(0, endMessageIndex + " END_MESSAGE\r".Length);
                string [] messageBuffers = messageBuffer.Split('\r');
                string    contactId      = messageBuffers[0].Trim();


                //	Logg("\'Contact ID: " + messageBuffer + "\'");

                if (SendMessageToClient(e.Message, contactId) == false)
                {                       // If not delivered
                                        // Return the message
                    SendMessageToClient("MSG UNDELIVERED " + e.Message.Substring("MSG ".Length), e.Client.Id);
                }
            }
            else if (e.Message.StartsWith("UPDATENAME"))
            {
                string messageBuffer = string.Copy(e.Message.Substring("UPDATENAME ".Length));
                e.Client.DisplayName = string.Copy(messageBuffer);
                UpdateUserLists();
            }

            /*else if(e.Message.StartsWith("SEND_FILE_TO"))
             * {
             *      string id = e.Message.Substring("SEND_FILE_TO".Length).Trim();
             *      string clientIp = GetClientIp(id);
             *      if(clientIp != "")
             *      {
             *              SendMessageToClient("PREPARE_RECIVE_FILE " + e.Client.Id + '\r' + fileTransferId, id);
             *              SendMessageToClient("SEND_FILE_TO_IP " + clientIp + '\r' + id + '\r' + fileTransferId, e.Client.Id);
             *              fileTransferId++;
             *      }
             *      else
             *      {
             *              SendMessageToClient("SEND_FILE_TO_IP_ERROR " + '\r' + id, e.Client.Id);
             *      }
             *      if(SendMessageToClient(e.Message, id) != true)
             *      {
             *              SendMessageToClient("MSG UNDELIVERED Kunde inte skicka inbjudan om fildelning", e.Client.Id);
             *      }
             * }
             * else if(e.Message.StartsWith("SEND_FILE_VIA_SERVER"))
             * {
             *      string [] stringBuffer = e.Message.Substring("SEND_FILE_VIA_SERVER".Length).Trim().Split('\r');
             *      string id = stringBuffer[0];
             *      string fileTransid = stringBuffer[1];
             *      SendMessageToClient("RECIVEFILE_VIA_SERVER" + e.Client.Id + '\r' + fileTransid, id);
             * }*/
        }
Esempio n. 4
0
        private void ClientLineRecived(object sender, LineRecivedEventArgs e)
        {
            // Print the client's message in the console just so we can se what's happening
            Logg("Recived Message: " + e.Message);

            // Check the message prefix to see what kind of message the client sent
            /*if (e.Message.StartsWith("LOGGIN"))
            {
                string messageBuffer = e.Message.Substring("LOGGIN ".Length);
                int pos = messageBuffer.IndexOf("/");
                if(pos != -1)
                {
                    string name		= messageBuffer.Substring(0, pos);
                    string password = messageBuffer.Remove(0, pos+1);

                    Logg("Name: \'" + name + "\'     Password: \'password\'");
                    Database db		= new Database(DB_SERVER, DB_DATABASE, DB_USERNAME, DB_PASSWORD);
                    if(db.testNameAndPassword(name, password, e.Client))
                    {
                        SetUserOnlineStatus('A', e.Client);
                        e.Client.Send("LOGGIN TRUE " + e.Client.DisplayName + "\r" + e.Client.Id);
                        Logg("Loggin TRUE");
                    }
                    else
                    {
                        e.Client.Send("LOGGIN FALSE");
                        Logg("Loggin FALSE");
                    }
                    db.close();
                }
                else
                {
                    e.Client.Send("LOGGIN FALSE");
                    Logg("LOGGIN ERROR");
                }
            }
            else if (e.Message.StartsWith("GETUSERLIST"))
            {
                Database db		= new Database(DB_SERVER, DB_DATABASE, DB_USERNAME, DB_PASSWORD);
                string [] contactIds = (string []) db.getDGPContacts(e.Client.Id).ToArray(typeof(string));

                string message = "USERLIST ";

                for( int i = 0; i < contactIds.Length; i++)
                {
                    if(i != 0)
                    {
                        message += "\n";
                    }
                    message += contactIds[i].ToString();
                    message += "\n";
                    message += db.getDisplayName(contactIds[i].ToString());
                    message += "\n";

                    message += db.getUserOnlineStatus(contactIds[i]);
                }
                e.Client.Send(message);
                db.close();
            }
            else if (e.Message.StartsWith("LOST") || e.Message.StartsWith("QUIT"))
            {
                SetUserOnlineStatus('Z', e.Client);

                e.Client.Close();
                clientList.Remove(e.Client);
                Logg("Client Removed: " + e.Client.DisplayName);
                Logg("Clients Left: " + clientList.Count);

                UpdateDGPContactsUserList(e.Client);
            }
            else if (e.Message.StartsWith("MSG"))
            {
                string messageBuffer	= e.Message.Substring("MSG ".Length);
                int endMessageIndex		= messageBuffer.IndexOf(" END_MESSAGE\r", 0, messageBuffer.Length);
                string message			= messageBuffer.Substring(0, endMessageIndex);
                messageBuffer			= messageBuffer.Remove(0,  endMessageIndex + " END_MESSAGE\r".Length);
                string [] messageBuffers = messageBuffer.Split('\r');
                string contactId			= messageBuffers[0].Trim();

                if(SendMessageToClient(e.Message, contactId) == false)
                {	// If not delivered
                    // Return the message
                    SendMessageToClient("MSG UNDELIVERED " + e.Message.Substring("MSG ".Length), e.Client.Id);
                }
            }
            else if(e.Message.StartsWith("UPDATENAME"))
            {
                string messageBuffer = string.Copy(e.Message.Substring("UPDATENAME ".Length));
                Database db		= new Database(DB_SERVER, DB_DATABASE, DB_USERNAME, DB_PASSWORD);
                db.setUserDisplayName(e.Client.Id.ToString(), messageBuffer);
                e.Client.DisplayName = string.Copy(messageBuffer);
                db.close();
                UpdateDGPContactsUserList(e.Client);
            }
            else if(e.Message.StartsWith("UPDATE_FRIEND_USERLISTS"))
            {
                UpdateDGPContactsUserList(e.Client);
            }
            else if(e.Message.StartsWith("UPDATE_ONLINE_STATUS "))
            {
                string stringBuffer = e.Message.Substring("UPDATE_ONLINE_STATUS ".Length).Trim();
                char onlineStatus = stringBuffer[0];
                SetUserOnlineStatus(onlineStatus, e.Client);
                UpdateDGPContactsUserList(e.Client);
            }
            else if(e.Message.StartsWith("SEND_FILE_TO"))
            {
                string id = e.Message.Substring("SEND_FILE_TO".Length).Trim();
                string clientIp = GetClientIp(id);
                if(clientIp != "")
                {
                    SendMessageToClient("PREPARE_RECIVE_FILE " + e.Client.Id + '\r' + fileTransferId, id);
                    SendMessageToClient("SEND_FILE_TO_IP " + clientIp + '\r' + id + '\r' + fileTransferId, e.Client.Id);
                    fileTransferId++;
                }
                else
                {
                    SendMessageToClient("SEND_FILE_TO_IP_ERROR " + '\r' + id, e.Client.Id);
                }
                /*if(SendMessageToClient(e.Message, id) != true)
                {
                    SendMessageToClient("MSG UNDELIVERED Kunde inte skicka inbjudan om fildelning", e.Client.Id);
                }
            }
            else if(e.Message.StartsWith("SEND_FILE_VIA_SERVER"))
            {
                string [] stringBuffer = e.Message.Substring("SEND_FILE_VIA_SERVER".Length).Trim().Split('\r');
                string id = stringBuffer[0];
                string fileTransid = stringBuffer[1];
                SendMessageToClient("RECIVEFILE_VIA_SERVER" + e.Client.Id + '\r' + fileTransid, id);
            }*/
        }
Esempio n. 5
0
        private void ClientLineRecived(object sender, LineRecivedEventArgs e)
        {
            // Print the client's message in the console just so we can se what's happening
            //Logg("Message: " + e.Message);

            // Check the message prefix to see what kind of message the client sent
            if (e.Message.StartsWith("LOGGIN"))
            {
                string messageBuffer = e.Message.Substring("LOGGIN ".Length);
                int pos = messageBuffer.IndexOf("/");
                if(pos != -1)
                {
                    string name		= messageBuffer.Substring(0, pos);
                    string password = messageBuffer.Remove(0, pos+1);

                    if(password != "")
                    {
                        e.Client.Send("LOGGIN FALSE");
                        Logg("LOGGIN ERROR: Ej chatbehhörig");
                    }

                    e.Client.DisplayName = name;
                    e.Client.Id = getNewClientId().ToString();
                    e.Client.Status = 'C';
                    e.Client.Send("LOGGIN TRUE " + e.Client.DisplayName + "\r" + e.Client.Id);

                    Logg("Client Id: \'" + e.Client.Id + "\' -  Name: \'" + name + "\'  -   Password: \'password\'  Loggin TRUE");

                    UpdateUserLists();
                }
                else
                {
                    e.Client.Send("LOGGIN FALSE");
                    Logg("LOGGIN ERROR: Inget Namn");
                }
            }
            else if (e.Message.StartsWith("GETUSERLIST"))
            {
                string message = "USERLIST ";

                message += "0\nAll\nC";	// Server User, Send messages to everybody

                for( int i = 0; i < this.clientList.Count; i++)
                {
                    message += "\n";
                    Client client = (Client)clientList[i];
                    message += client.Id;
                    message += "\n";
                    message += client.DisplayName;
                    message += "\n";
                    message += client.Status;
                }
            //	Logg("GetUserList  -  Id: " + e.Client.Id);
                e.Client.Send(message);
            }
            else if (e.Message.StartsWith("LOST") || e.Message.StartsWith("QUIT"))
            {
                e.Client.Close();
                clientList.Remove(e.Client);
                Logg("Client Removed: " + e.Client.DisplayName + "  -  Clients Left: " + clientList.Count);
                UpdateUserLists();
            }
            else if (e.Message.StartsWith("MSG"))
            {
                string messageBuffer	= e.Message.Substring("MSG ".Length);
                int endMessageIndex		= messageBuffer.IndexOf(" END_MESSAGE\r", 0, messageBuffer.Length);
                string message			= messageBuffer.Substring(0, endMessageIndex);
                messageBuffer			= messageBuffer.Remove(0,  endMessageIndex + " END_MESSAGE\r".Length);
                string [] messageBuffers = messageBuffer.Split('\r');
                string contactId			= messageBuffers[0].Trim();

            //	Logg("\'Contact ID: " + messageBuffer + "\'");

                if(SendMessageToClient(e.Message, contactId) == false)
                {	// If not delivered
                    // Return the message
                    SendMessageToClient("MSG UNDELIVERED " + e.Message.Substring("MSG ".Length), e.Client.Id);
                }
            }
            else if(e.Message.StartsWith("UPDATENAME"))
            {
                string messageBuffer = string.Copy(e.Message.Substring("UPDATENAME ".Length));
                e.Client.DisplayName = string.Copy(messageBuffer);
                UpdateUserLists();
            }
            /*else if(e.Message.StartsWith("SEND_FILE_TO"))
            {
                string id = e.Message.Substring("SEND_FILE_TO".Length).Trim();
                string clientIp = GetClientIp(id);
                if(clientIp != "")
                {
                    SendMessageToClient("PREPARE_RECIVE_FILE " + e.Client.Id + '\r' + fileTransferId, id);
                    SendMessageToClient("SEND_FILE_TO_IP " + clientIp + '\r' + id + '\r' + fileTransferId, e.Client.Id);
                    fileTransferId++;
                }
                else
                {
                    SendMessageToClient("SEND_FILE_TO_IP_ERROR " + '\r' + id, e.Client.Id);
                }
                if(SendMessageToClient(e.Message, id) != true)
                {
                    SendMessageToClient("MSG UNDELIVERED Kunde inte skicka inbjudan om fildelning", e.Client.Id);
                }
            }
            else if(e.Message.StartsWith("SEND_FILE_VIA_SERVER"))
            {
                string [] stringBuffer = e.Message.Substring("SEND_FILE_VIA_SERVER".Length).Trim().Split('\r');
                string id = stringBuffer[0];
                string fileTransid = stringBuffer[1];
                SendMessageToClient("RECIVEFILE_VIA_SERVER" + e.Client.Id + '\r' + fileTransid, id);
            }*/
        }
Esempio n. 6
0
        private void ClientLineRecived(object sender, LineRecivedEventArgs e)
        {
            // Print the client's message in the console just so we can se what's happening
            Logg("Recived Message: " + e.Message);

            // Check the message prefix to see what kind of message the client sent

            /*if (e.Message.StartsWith("LOGGIN"))
             * {
             *      string messageBuffer = e.Message.Substring("LOGGIN ".Length);
             *      int pos = messageBuffer.IndexOf("/");
             *      if(pos != -1)
             *      {
             *              string name		= messageBuffer.Substring(0, pos);
             *              string password = messageBuffer.Remove(0, pos+1);
             *
             *              Logg("Name: \'" + name + "\'     Password: \'password\'");
             *              Database db		= new Database(DB_SERVER, DB_DATABASE, DB_USERNAME, DB_PASSWORD);
             *              if(db.testNameAndPassword(name, password, e.Client))
             *              {
             *                      SetUserOnlineStatus('A', e.Client);
             *                      e.Client.Send("LOGGIN TRUE " + e.Client.DisplayName + "\r" + e.Client.Id);
             *                      Logg("Loggin TRUE");
             *              }
             *              else
             *              {
             *                      e.Client.Send("LOGGIN FALSE");
             *                      Logg("Loggin FALSE");
             *              }
             *              db.close();
             *      }
             *      else
             *      {
             *              e.Client.Send("LOGGIN FALSE");
             *              Logg("LOGGIN ERROR");
             *      }
             * }
             * else if (e.Message.StartsWith("GETUSERLIST"))
             * {
             *      Database db		= new Database(DB_SERVER, DB_DATABASE, DB_USERNAME, DB_PASSWORD);
             *      string [] contactIds = (string []) db.getDGPContacts(e.Client.Id).ToArray(typeof(string));
             *
             *      string message = "USERLIST ";
             *
             *      for( int i = 0; i < contactIds.Length; i++)
             *      {
             *              if(i != 0)
             *              {
             *                      message += "\n";
             *              }
             *              message += contactIds[i].ToString();
             *              message += "\n";
             *              message += db.getDisplayName(contactIds[i].ToString());
             *              message += "\n";
             *
             *              message += db.getUserOnlineStatus(contactIds[i]);
             *      }
             *      e.Client.Send(message);
             *      db.close();
             * }
             * else if (e.Message.StartsWith("LOST") || e.Message.StartsWith("QUIT"))
             * {
             *      SetUserOnlineStatus('Z', e.Client);
             *
             *      e.Client.Close();
             *      clientList.Remove(e.Client);
             *      Logg("Client Removed: " + e.Client.DisplayName);
             *      Logg("Clients Left: " + clientList.Count);
             *
             *      UpdateDGPContactsUserList(e.Client);
             * }
             * else if (e.Message.StartsWith("MSG"))
             * {
             *      string messageBuffer	= e.Message.Substring("MSG ".Length);
             *      int endMessageIndex		= messageBuffer.IndexOf(" END_MESSAGE\r", 0, messageBuffer.Length);
             *      string message			= messageBuffer.Substring(0, endMessageIndex);
             *      messageBuffer			= messageBuffer.Remove(0,  endMessageIndex + " END_MESSAGE\r".Length);
             *      string [] messageBuffers = messageBuffer.Split('\r');
             *      string contactId			= messageBuffers[0].Trim();
             *
             *
             *      if(SendMessageToClient(e.Message, contactId) == false)
             *      {	// If not delivered
             *              // Return the message
             *              SendMessageToClient("MSG UNDELIVERED " + e.Message.Substring("MSG ".Length), e.Client.Id);
             *      }
             * }
             * else if(e.Message.StartsWith("UPDATENAME"))
             * {
             *      string messageBuffer = string.Copy(e.Message.Substring("UPDATENAME ".Length));
             *      Database db		= new Database(DB_SERVER, DB_DATABASE, DB_USERNAME, DB_PASSWORD);
             *      db.setUserDisplayName(e.Client.Id.ToString(), messageBuffer);
             *      e.Client.DisplayName = string.Copy(messageBuffer);
             *      db.close();
             *      UpdateDGPContactsUserList(e.Client);
             * }
             * else if(e.Message.StartsWith("UPDATE_FRIEND_USERLISTS"))
             * {
             *      UpdateDGPContactsUserList(e.Client);
             * }
             * else if(e.Message.StartsWith("UPDATE_ONLINE_STATUS "))
             * {
             *      string stringBuffer = e.Message.Substring("UPDATE_ONLINE_STATUS ".Length).Trim();
             *      char onlineStatus = stringBuffer[0];
             *      SetUserOnlineStatus(onlineStatus, e.Client);
             *      UpdateDGPContactsUserList(e.Client);
             * }
             * else if(e.Message.StartsWith("SEND_FILE_TO"))
             * {
             *      string id = e.Message.Substring("SEND_FILE_TO".Length).Trim();
             *      string clientIp = GetClientIp(id);
             *      if(clientIp != "")
             *      {
             *              SendMessageToClient("PREPARE_RECIVE_FILE " + e.Client.Id + '\r' + fileTransferId, id);
             *              SendMessageToClient("SEND_FILE_TO_IP " + clientIp + '\r' + id + '\r' + fileTransferId, e.Client.Id);
             *              fileTransferId++;
             *      }
             *      else
             *      {
             *              SendMessageToClient("SEND_FILE_TO_IP_ERROR " + '\r' + id, e.Client.Id);
             *      }
             *      /*if(SendMessageToClient(e.Message, id) != true)
             *      {
             *              SendMessageToClient("MSG UNDELIVERED Kunde inte skicka inbjudan om fildelning", e.Client.Id);
             *      }
             * }
             * else if(e.Message.StartsWith("SEND_FILE_VIA_SERVER"))
             * {
             *      string [] stringBuffer = e.Message.Substring("SEND_FILE_VIA_SERVER".Length).Trim().Split('\r');
             *      string id = stringBuffer[0];
             *      string fileTransid = stringBuffer[1];
             *      SendMessageToClient("RECIVEFILE_VIA_SERVER" + e.Client.Id + '\r' + fileTransid, id);
             * }*/
        }