-
Notifications
You must be signed in to change notification settings - Fork 0
/
contacts.py
159 lines (132 loc) · 5.03 KB
/
contacts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import socket
import sys
import threading
from scapy.all import *
import binascii
import ast
import time
import contacts
def addContact(ip, name):
# Save the new contact to the contacts file
with open('contacts.txt', 'a') as contactsFile:
contactsFile.write(name + ' | ' + ip + ' | Offline\n')
def removeContact(ip):
# Remove the contact from the contacts file
with open('contacts.txt', 'r') as contactsFile:
lines = contactsFile.readlines()
with open('contacts.txt', 'w') as contactsFile:
for line in lines:
if not line.split(' | ')[1].strip() == ip.strip():
contactsFile.write(line)
def savePublicKey(ip, public_key):
# Save the public key to the public_keys file
# Remove all newlines from the public key
public_key = public_key.replace('\n', 'NEWLINE')
with open('public_keys.txt', 'a') as publicKeysFile:
publicKeysFile.write(ip + ' | ' + public_key + '\n')
def setOnlineStatus(ip, status):
# Set the online status of the contact
with open('contacts.txt', 'r') as contactsFile:
lines = contactsFile.readlines()
with open('contacts.txt', 'w') as contactsFile:
for line in lines:
if line.split(' | ')[1].strip() == ip.strip():
contactsFile.write(line.split(' | ')[0] + ' | ' + ip + ' | ' + status + '\n')
else:
contactsFile.write(line)
def getPublicKey(ip):
try:
# Get the public key from the public_keys file
with open('public_keys.txt', 'r') as publicKeysFile:
for line in publicKeysFile:
line = line.strip()
if line.split(' | ')[0] == ip:
return line.split(' | ')[1].replace('NEWLINE', '\n')
except:
pass
# If the public key is not in the public_keys file, return 'Unknown'
return 'Unknown'
def getContactName(ip):
if ip == 'You':
return 'You'
try:
# Get the name of the contact from the contacts file
with open('contacts.txt', 'r') as contactsFile:
for line in contactsFile:
line = line.strip()
if line.split(' | ')[1] == ip:
return line.split(' | ')[0]
except:
pass
# If the contact is not in the contacts file, return 'Unknown'
return 'Unknown'
def getContactIP(name):
try:
# Get the IP address of the contact from the contacts file
with open('contacts.txt', 'r') as contactsFile:
for line in contactsFile:
line = line.strip()
if line.split(' | ')[0] == name:
return line.split(' | ')[1]
except:
pass
# If the contact is not in the contacts file, return 'Unknown'
return 'Unknown'
def getContactOnlineStatus(ip):
try:
# Get the online status of the contact from the contacts file
with open('contacts.txt', 'r') as contactsFile:
for line in contactsFile:
line = line.strip()
if line.split(' | ')[1].strip() == ip.strip():
return line.split(' | ')[2].strip()
except:
pass
# If the contact is not in the contacts file, return 'Unknown'
return 'Unknown'
def getContactList():
try:
# Get the list of contacts from the contacts file
contactList = []
with open('contacts.txt', 'r') as contactsFile:
for line in contactsFile:
line = line.strip()
contactList.append(line.split(' | '))
except:
pass
return contactList
def getContactListIP():
try:
# Get the list of contacts from the contacts file
contactList = []
with open('contacts.txt', 'r') as contactsFile:
for line in contactsFile:
line = line.strip()
contactList.append(line.split(' | ')[1])
except:
pass
return contactList
def saveMessage(message, ip):
# Save the message to a messages file for the ip
currentDatetime = time.strftime('%Y-%m-%d %H:%M:%S')
with open(str(ip)+'_messages.txt', 'a') as messagesFile:
messagesFile.write(ip + " | " + currentDatetime + ' | ' + message + '\n')
def saveOutgoingMessage(message, ip):
# Save the message to the messages file
currentDatetime = time.strftime('%Y-%m-%d %H:%M:%S')
with open(str(ip)+'_messages.txt', 'a') as messagesFile:
messagesFile.write("You | " + currentDatetime + ' | ' + message + '\n')
def getMessages(ip):
# Get the messages from the messages file
messages = []
try:
with open(str(ip)+'_messages.txt', 'r') as messagesFile:
for line in messagesFile:
try:
line = line.strip().split(' | ')
messages.append([line[0], line[1], line[2]])
except:
pass
except:
pass
return messages