Advertisement
AntonPaly4

client

Aug 19th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.50 KB | None | 0 0
  1. import socket
  2. from time import sleep
  3.  
  4. from JIM.JIMs import Jim, MessageConverter
  5. from JIM.jim_config import *
  6. from client_src.client_storage_handler import ClientStorageHandler
  7. from crypto.crypto import *
  8.  
  9. BUF_SIZE = 1024
  10. ADDR = 'localhost'
  11. PORT = 7777
  12.  
  13.  
  14. class Client:
  15.     def __init__(self, name, addr='localhost', port=7777):
  16.         """ Initialize User-class
  17.           :param: name: - name of messenger's user, :type: string,
  18.           :param: addr: - ip-address of messenger's server, :type: string, :default: 'localhost',
  19.           :param: port: - port of messenger's server, :type: integer, :default: 7777. """    
  20.         self.name = name
  21.         self.address = addr, port
  22.         self.soc = None
  23.         # TODO: при регистрации генерировать соль и записывать в базу или файл
  24.         self.salt = b'e690a758702ee2f78e4ae5d1327f52d246c82a6eda3648d25c3806142717e5d3'
  25.         self.message_key = '40tisachobezianvzhopusunulibanan'  # (с) С.Лукьяненко - Лабиринт Отражений.
  26.         self.message_maker = Jim(self.name)
  27.         self.converter = MessageConverter()
  28.         self.storage_handler = ClientStorageHandler(self.name)
  29.  
  30.     def connect_to_server(self):
  31.         """ Making socket and connecting to server, setting timeout = 1 sec"""
  32.         self.soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  33.         self.soc.connect(self.address)
  34.         self.soc.settimeout(1)
  35.  
  36.     def send_authorisation(self, password):
  37.         """ Sending authorization-message to server, returns received response.
  38.           :param: password: user's password :type: str. """
  39.         self.connect_to_server()
  40.         authorisation_ = self.message_maker.create(action=AUTHORISE)
  41.         authorisation = self.converter(authorisation_)
  42.         self.soc.send(authorisation)
  43.         serv_answ = self.receive()[ALERT]
  44.         key = make_password(self.salt, password)
  45.         answ = crypt_message(key, serv_answ)
  46.         answer = self.message_maker.create(action=AUTHORISE, answer=answ)
  47.         answer = self.converter(answer)
  48.         self.soc.send(answer)
  49.         resp = self.receive()
  50.         return resp
  51.  
  52.     def send_registration(self, password):
  53.         """ Sending registration-message to server, returns received response.
  54.           :param: password: user's password :type: str. """
  55.         passw = make_password(self.salt, password)
  56.         self.connect_to_server()
  57.         registration = self.message_maker.create(action=REGISTER, password=passw)
  58.         registration = self.converter(registration)
  59.         self.soc.send(registration)
  60.         resp = self.receive()
  61.         return resp
  62.  
  63.     def add_contact(self, contact_name):
  64.         """ Sending action-message with contact name to add.
  65.           :param: contact_name: name of contact to add :type: string """
  66.         action = self.message_maker.create(action=ADD_CONTACT, contact=contact_name)
  67.         action = self.converter(action)
  68.         self.soc.send(action)
  69.         self.storage_handler.add_contact(contact_name)
  70.  
  71.     def del_contact(self, contact_name):
  72.         """ Sending action-message with contact name to remove from user's contact list.
  73.           :param: contact_name: name of contact to remove :type: string """
  74.         action = self.message_maker.create(action=DEL_CONTACT, contact=contact_name)
  75.         action = self.converter(action)
  76.         self.soc.send(action)
  77.         self.storage_handler.del_contact(contact_name)
  78.  
  79.     def get_contacts(self):
  80.         """ Sending action-message to get contact-list. """
  81.         action = self.message_maker.create(action=GET_CONTACTS)
  82.         action = self.converter(action)
  83.         self.soc.send(action)
  84.  
  85.     def quit_server(self):
  86.         """ Sending action-message to log-out from server. """
  87.         quit_msg = self.message_maker.create(action=QUIT)
  88.         quit_msg = self.converter(quit_msg)
  89.         self.soc.send(quit_msg)
  90.         sleep(0.2)
  91.         self.soc.close()
  92.  
  93.     def send_presence(self):
  94.         msg = self.message_maker.create(action=PRESENCE)
  95.         msg = self.converter(msg)
  96.         self.soc.send(msg)
  97.  
  98.     def receive(self):
  99.         """ Receiving message from server_src, checking for message-action,
  100.         if so - returns  string in format SENDER'S_NAME: MESSAGE."""
  101.         try:
  102.             mess = self.soc.recv(BUF_SIZE)
  103.         except socket.timeout:
  104.             pass
  105.         else:
  106.             if mess:
  107.                 dmess = self.converter(mess)
  108.                 if dmess.get(MESSAGE):
  109.                     encrypted_message = decrypt_message(self.message_key, dmess[MESSAGE])
  110.                     dmess[MESSAGE] = encrypted_message
  111.                     self.storage_handler.log_incoming_message(dmess)
  112.                 return dmess
  113.  
  114.     def log_message(self, message):
  115.         self.storage_handler.log_incoming_message(message[MESSAGE])
  116.  
  117.     def send_message(self, message, to):
  118.         """ Making dict message-action and sending to server_src
  119.        """
  120.         message_ = self.message_maker.create(action=MSG, message=message, to=to)
  121.         self.storage_handler.log_outgoing_message(message_)
  122.         crypted_message = crypt_message(self.message_key, message)
  123.         message_[MESSAGE] = crypted_message
  124.         message_to_send = self.converter(message_)
  125.         self.soc.send(message_to_send)
  126.  
  127.     # TODO: заменить количество на период
  128.     def get_messages_history(self, contact_name, count=5):
  129.         return self.storage_handler.messages_history(contact_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement