o
    Sxh+                     @   s   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
 ddlZG dd dZedkrYzddlZW n eyO   ed ed	 ed
 Y nw e Ze  dS dS )zU
BB10 Chat CLI Application
Handles encrypted messaging with PIN-based authentication
    N)Fernet)	b64encode	b64decodec                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zd ddZd!ddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )"BB10ChatCLIc                 C   s6   d| _ d | _d | _d| _tj| j s|   d S d S )Nz	chat.jsonzhttp://localhost/chat.php)	chat_filepinencryption_keyphp_endpointospathexistsinitialize_chat_fileself r   >/var/www/vhosts/berrystore.sw7ft.com/httpdocs/apps/chat_cli.py__init__   s   zBB10ChatCLI.__init__c                 C   sN   g i g d}t | jd}tj||dd W d   dS 1 s w   Y  dS )z2Initialize the chat.json file with empty structure)messagesuserspublic_messagesw   indentN)openr   jsondump)r   initial_datafr   r   r   r      s   "z BB10ChatCLI.initialize_chat_filec                 C   sv   | j r| j S td td td  }|rt|dkr$td dS || _ t| 	 }t
|dd | _|S )z"Get user's BB10 PIN for encryptionu   🔐 BB10 Chat - PIN Requiredz,Enter your BB10 device PIN (e.g., 2B11B48C):zPIN:    uA   ❌ Invalid PIN format. PIN must be 8 characters (e.g., 2B11B48C)N    )r   printinputstripupperlenhashlibsha256encodedigestr   r   )r   r   pin_hashr   r   r   get_pin%   s   zBB10ChatCLI.get_pinc                 C   s&   | j sdS t| j }||  S )z&Encrypt a message using the user's PINN)r   r   encryptr(   decode)r   messager   r   r   r   encrypt_message8   s   
zBB10ChatCLI.encrypt_messagec                 C   s6   | j sdS zt| j }||  W S    Y dS )z&Decrypt a message using the user's PINNz[ENCRYPTED - PIN REQUIRED])r   r   decryptr(   r-   )r   encrypted_messager   r   r   r   decrypt_message?   s   
zBB10ChatCLI.decrypt_messagepublicNc              
   C   sX  zt | jd}t|}W d   n1 sw   Y  | |}tt }t|d d |||||p5g d}	|d |	 |dkrJ|d |	 ||d vr[||dd	|d |< n||d | d
< |d | d  d7  < t | jd}tj	||dd W d   W dS 1 sw   Y  W dS  t
y }
 ztd|
  W Y d}
~
dS d}
~
ww )zAdd a new message to chat.jsonrNr      )idsenderr.   type	timestamp
recipientsr3   r   r   )
first_seen	last_seenmessage_countr<   r=   r   r   r   Tu   ❌ Error adding message: F)r   r   r   loadr/   inttimer%   appendr   	Exceptionr!   )r   
sender_pinr.   message_typer:   r   datar1   r9   new_messageer   r   r   add_messageI   sD   
	
zBB10ChatCLI.add_message
   c           	   
   C   s   zYt | jd}t|}W d   n1 sw   Y  g }|dkr.|d | d }|W S |dkrW|d D ]}|d dkrO|d |ksJ||d	 v rO|| q6|| d }|W S  tyt } ztd
|  g W  Y d}~S d}~ww )zGet recent messagesr4   Nr3   r   dmr   r8   r7   r:   u   ❌ Error getting messages: )r   r   r   r>   rA   rB   r!   )	r   limitrD   rC   r   rE   r   msgrG   r   r   r   get_messagest   s*   	
zBB10ChatCLI.get_messagesc              
   C   s   |st d dS t dt| d t d |D ]C}tdt|d }|d }| |d	 }|d
 dkrNd|d }t d| d| d| d|  qt d| d| d|  qt d dS )z#Display messages in a formatted wayu   📭 No messages foundNu   
📨 Last z
 messages:z2--------------------------------------------------%H:%M:%Sr9   r7   r.   r8   rJ   , r:   [z] u    → : )r!   r%   r@   strftime	localtimer2   join)r   r   rL   r9   r7   decrypted_messager:   r   r   r   display_messages   s   "zBB10ChatCLI.display_messagesc                 C   s\  t |dk rtd dS |  }|sdS t |dkr0|d }| ||dr.td|  dS dS t |dkrc|d  }|d }t |dkrLtd	 dS | ||d
|gratd| d|  dS dS t |dkr|d  |d  g}d|dd }|D ]}t |dkrtd|   dS q| ||d
|rtdd| d|  dS dS dS )zHandle the 'msg' commandr5   uN   ❌ Usage: msg <message> or msg <pin> <message> or msg <pin1> <pin2> <message>Nr   r3   u   ✅ Public message sent: r   r   u    ❌ Invalid recipient PIN formatrJ   u   ✅ DM sent to rQ       u   ❌ Invalid PIN format: u   ✅ Group DM sent to rO   )r%   r!   r+   rH   r$   rT   )r   argsr   r.   recipient_pinrecipient_pinsrpinr   r   r   handle_msg_command   s@   
zBB10ChatCLI.handle_msg_commandc                 C   L   |   }|sdS d}|r|d  rt|d }| |d|}| | dS )zHandle the 'msgs' commandNrI   r   r3   r+   isdigitr?   rM   rV   r   rY   r   rK   r   r   r   r   handle_msgs_command      zBB10ChatCLI.handle_msgs_commandc                 C   r^   )z,Handle the 'dms' command for direct messagesNrI   r   rJ   r_   ra   r   r   r   handle_dms_command   rc   zBB10ChatCLI.handle_dms_commandc              
   C   s   zOt | jd}t|}W d   n1 sw   Y  td td |d  D ]\}}tdt|d }t| d|d	  d
| d q*td W dS  t	yi } ztd|  W Y d}~dS d}~ww )z3Handle the 'users' command to show registered usersr4   Nu   
👥 Registered Users:z------------------------------r   rN   r<   rQ   r=   z messages (last: )u   ❌ Error getting users: )
r   r   r   r>   r!   itemsr@   rR   rS   rB   )r   r   rE   user_pin	user_infor<   rG   r   r   r   handle_users_command   s    z BB10ChatCLI.handle_users_commandc                 C   s   t d t d t d t d t d t d t d t d t d	 t d
 t d t d t d t d t d t d t d t d dS )zShow help informationu   
💬 BB10 Chat CLI - Helpz(========================================z	Commands:z2  msg <message>              - Send public messagez.  msg <pin> <message>        - Send DM to userz,  msg <pin1> <pin2> <msg>    - Send group DMzF  msgs [count]               - Show last public messages (default: 10)z:  dms [count]                - Show your DMs (default: 10)z4  users                      - Show registered usersz-  help                       - Show this helpz(  quit                       - Exit chatzExample:z-  msg Hello world!           - Public messagez2  msg 2B11B48C Hi there!     - DM to user 2B11B48Cz'  msg 2B11B48C 1A2B3C4D Hey! - Group DMz5  msgs 5                     - Last 5 public messagesN)r!   r   r   r   r   	show_help   s$   zBB10ChatCLI.show_helpc              
   C   s@  t d t d 	 zotd }|sW q| dv r!t d W d
S | dkr-|   W q| }|d  }t|d	krC|d	d
 ng }|dkrO| | n(|dkrY| | n|dkrc| 	| n|dkrl| 
  nt d|  t d W n& ty   t d Y d
S  ty } zt d|  W Y d
}~nd
}~ww q	)zMain CLI loopu   💬 BB10 Chat CLIz(Type 'help' for commands, 'quit' to exitTz
chat> )quitexitqu   👋 Goodbye!helpr   r5   NrL   msgsdmsr   u   ❌ Unknown command: z"Type 'help' for available commandsu   
👋 Goodbye!u   ❌ Error: )r!   r"   r#   lowerrj   splitr%   r]   rb   rd   ri   KeyboardInterruptrB   )r   commandpartscmdrY   rG   r   r   r   run  sF   
zBB10ChatCLI.run)r3   N)rI   r3   N)__name__
__module____qualname__r   r   r+   r/   r2   rH   rM   rV   r]   rb   rd   ri   rj   rw   r   r   r   r   r      s    




+)r   __main__u)   ❌ Error: cryptography library not foundz&Install with: pip install cryptographyr5   )__doc__r   r
   sysrequestsr&   cryptography.fernetr   base64r   r   r@   r   rx   cryptographyImportErrorr!   rl   chatrw   r   r   r   r   <module>   s.     $