-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
420 lines (338 loc) · 13.5 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import os
import logging
import time
import sleekxmpp
from getpass import getpass
from prettytable import PrettyTable
from sleekxmpp.exceptions import IqError, IqTimeout
import client
from consts import *
close_login = False
# logging.basicConfig(level=logging.DEBUG,
# format='%(levelname)-8s %(message)s')
# With tkinter, opens a windows for the user to select a file.
def get_file_path():
file_path = filedialog.askopenfilename()
return file_path
# Prints a table with all of the groups the user is part of
def print_groups(group_dict):
table = PrettyTable()
table.field_names = [f'{BOLD}No.{ENDC}',
f'{BOLD}ROOM{ENDC}',
f'{BOLD}NICK{ENDC}']
table.align = 'l'
counter = 1
for url, group in group_dict.items():
group_info = group.get_data()
table.add_row([counter, group_info[0], group_info[1]])
counter += 1
print(table)
# Prints a table with every user and its index
def print_contact_index(user_dict):
table = PrettyTable()
table.field_names = [f'{BOLD}No. {ENDC}',
f'{BOLD}USERNAME{ENDC}',
f'{BOLD}SHOW{ENDC}',
f'{BOLD}JID{ENDC}']
table.align = 'l'
counter = 1
for jid, user in user_dict.items():
user_info = user.get_connection_data()
table.add_row([counter, user_info[0], user_info[1], jid])
counter += 1
print(table)
# Prints all users through a table
def print_all_users(user_dict):
table = PrettyTable()
table.field_names = [f'{BOLD}USERNAME{ENDC}',
f'{BOLD}NAME{ENDC}',
f'{BOLD}EMAIL{ENDC}',
f'{BOLD}JID{ENDC}']
table.align = 'l'
for jid, user_info in user_dict.items():
table.add_row([user_info[0], user_info[1], user_info[2], jid])
print(table)
# Prints a table with every contact and its connection data
def print_contacts(user_dict):
table = PrettyTable()
table.field_names = [f'{BOLD}USER{ENDC}',
f'{BOLD}SHOW{ENDC}',
f'{BOLD}STATUS{ENDC}',
f'{BOLD}SUBSCRIPTION{ENDC}',
f'{BOLD}JID{ENDC}']
table.align = 'l'
for jid, user in user_dict.items():
user_data = user.get_connection_data()
user_data.append(jid)
table.add_row(user_data)
table.sortby = f'{BOLD}SHOW{ENDC}'
print(table)
# Prints a table with every user and its connection data
def print_user_data(users, amount):
table = PrettyTable(border=False)
table.field_names = [f'{BOLD}EMAIL{ENDC}',
f'{BOLD}JID{ENDC}',
f'{BOLD}USERNAME{ENDC}',
f'{BOLD}NAME{ENDC}']
table.align = 'l'
counter = 0
user_data = []
for data in users:
counter += 1
user_data.append(data)
if counter % 4 == 0:
table.add_row(user_data)
user_data = []
print(table)
# Hanldes the client once the user logged in
def handle_session(event):
close_session = False
xmpp.session_start()
option = ''
print(f'{OKGREEN}Logged in as {xmpp.boundjid.bare}{ENDC}')
while not close_session:
print(main_menu)
option = input('Enter an option: ')
# OPTION 1: Show connected users
if option == '1':
print(f'\n{BOLD}Every user on this server:{ENDC}\n')
users = xmpp.get_all_online()
print_all_users(users)
print(
f'\n{BLUE}|================================================|{ENDC}')
print(f'\n{BOLD}My roster:{ENDC}\n')
roster = xmpp.get_user_dict()
print_contacts(roster)
# OPTION 2: Add a user to my contact list
elif option == '2':
print(f'\n{BOLD}Add user to contact list{ENDC}')
user_jid = input('Enter user jid: ')
if user_jid and '@' in user_jid:
xmpp.add_user(user_jid)
else:
print(invalid_option)
# OPTION 3: Get user details
elif option == '3':
username = input('Enter a username to search: ')
user_data, amount = xmpp.get_user_data(username)
if not user_data:
print(f'{RED}User not found...{ENDC}')
else:
print_user_data(user_data, amount)
# OPTION 4: Private session
elif option == '4':
print(f'\n{BOLD}Send a private message{ENDC}\n')
# Get updated roster
roster = xmpp.get_user_dict()
# Get all users as list
users = list(roster.keys())
# Print table of users with their index
print_contact_index(roster)
recipient = input('\nEnter recipient index or jid: ')
if '@' in recipient:
dest = recipient
else:
try:
recipient = int(recipient)
# Check if user index was correct
dest = users[recipient-1]
except ValueError:
# Else, repeat
print(invalid_option)
continue
received_messages = roster[dest].get_messages()
if received_messages:
print(
f'\n{YELLOW}The message(s) with {dest} are:{ENDC}')
for sender, msg in received_messages:
print(f'\t{sender}: {msg}')
new_message = input('\nEnter a message: ')
xmpp.send_session_message(dest, new_message)
# OPTION 5: Group chat options
elif option == '5':
print(group_options)
group_option = input('\tEnter an option: ')
# 1. Create a group
if group_option == '1':
print(f'\n{BOLD}Create a group chat{ENDC}')
group_name = input('Room URL: ')
nick = input('Nick: ')
if nick and group_name:
xmpp.create_new_room(group_name, nick)
print(f'{OKGREEN}{group_name} created!{ENDC}')
else:
print(f'{FAIL}Please set a group name and a nick{ENDC}')
continue
# 2. Join a group
elif group_option == '2':
print(f'\n{BOLD}Join a group chat{ENDC}')
room = input('Room URL: ')
nick = input('Nick: ')
if nick and room:
xmpp.join_room(room, nick)
print(f'{OKGREEN}Joined {room}{ENDC}')
else:
print(f'{FAIL}Please set a group name and a nick{ENDC}')
continue
# 3. Send message to a group
elif group_option == '3':
print(f'\n{BOLD}Send a message to a group{ENDC}')
room_dict = xmpp.get_group_dict()
if room_dict:
# Print table of groups with their index
print_groups(room_dict)
recipient = input('\nEnter recipient index or jid: ')
if '@' in recipient:
dest = recipient
else:
try:
recipient = int(recipient)
# Check if user index was correct
dest = list(room_dict.keys())[recipient-1]
except ValueError:
# Else, repeat
print(invalid_option)
continue
received_messages = room_dict[dest].get_messages()
if received_messages:
print(
f'\n{YELLOW}The message(s) from {dest} are:{ENDC}')
for sender, msg in received_messages:
print(f'\t{sender} --> {msg}')
new_message = input('\nEnter a message: ')
if new_message:
if xmpp.send_groupchat_message(dest, new_message):
print(f'{OKGREEN}Message sent!{ENDC}')
else:
print(error_msg)
else:
print(f'{FAIL}You havent joined any room yet.{ENDC}')
# 4. Leave group
elif group_option == '4':
print(f'\n{BOLD}Leave room{ENDC}')
room = input('Room URL: ')
nick = input('Nick: ')
if room and nick:
xmpp.leave_room(room, nick)
print(f'{OKGREEN}You left the group.{ENDC}')
else:
print(f'{FAIL}You must provide a nick and a room!{ENDC}')
continue
# 5. Cancel
elif group_option == '5':
continue
# Invalid option
else:
print(invalid_option)
# OPTION 6: Presence message
elif option == '6':
print(f'\n{BOLD}Presence message{ENDC}')
print(show_options)
# Let user decide his options
show_input = input('Show: ')
status = input('Status: ')
try:
# Validate if user selected a valid option
show = show_array[int(show_input)-1]
except:
# If not, go to the default one.
print(
f'{WARNING}Incorrect show option selected... Seting show to "available".')
show = 'available'
# Send the presence message and inform the user about it.
xmpp.presence_message(show, status)
print(f'{OKGREEN}Presence message sent!{ENDC}')
# OPTION 7: Send a file
elif option == '7':
print(f'{BOLD}Send a file{ENDC}')
# Get updated roster
roster = xmpp.get_user_dict()
# Get all users as list
users = list(roster.keys())
# Print table of users with their index
print_contact_index(roster)
# Ask for input
recipient = input('\nEnter recipient index: ')
try:
# Check if user index was correct
dest = users[int(recipient)-1]
except:
# Else, repeat
print(invalid_option)
continue
try:
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.mainloop()
file_path = get_file_path()
except:
file_path = str(input(
'Enter the path of the file you want to send: '))
if file_path:
xmpp.request_si(dest, file_path)
else:
print(invalid_option)
continue
# OPTION 8: Log out.
elif option == '8':
print(f'\n{BOLD}Logging out of {xmpp.boundjid.bare}{ENDC}')
xmpp.disconnect()
close_session = True
# OPTION 9: Delete account from server.
elif option == '9':
print(f'\n{WARNING}Deleting account: {xmpp.boundjid.bare}{ENDC}')
xmpp.delete_account()
xmpp.disconnect()
close_session = True
else:
print(invalid_option)
if __name__ == "__main__":
while not close_login:
print(login_menu)
option = input('Enter an option: ')
# Register a new user
if option == '1':
print('\nRegister a new account')
username = input('Enter your jid: ')
password = getpass('Enter your password: ')
if username and password:
print(
f'{WARNING}Initiating registration process... please wait.{ENDC}')
xmpp = client.RegisterBot(username, password)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0004') # Data forms
xmpp.register_plugin('xep_0066') # Out-of-band Data
xmpp.register_plugin('xep_0077') # In-band Registration
xmpp.register_plugin('xep_0045') # Groupchat
xmpp.register_plugin('xep_0199') # XMPP Ping
xmpp['xep_0077'].force_registration = True
if xmpp.connect():
xmpp.process(block=True)
else:
print(f'{FAIL}Unable to connect to the server{ENDC}')
else:
print(f'{FAIL}Input is incorrect.')
# Login with credentials
elif option == '2':
print(f'\n{BOLD}Login in to your account{ENDC}')
username = input('Enter your username: ')
password = getpass('Enter your password: ')
if not username or not password:
print(f'{FAIL} please enter a username and a password!{ENDC}')
continue
xmpp = client.Client(username, password)
xmpp.add_event_handler(
"session_start", handle_session, threaded=True)
if xmpp.connect():
xmpp.process(block=False)
close_login = True
else:
print(f'{FAIL}Unable to connect to the server{ENDC}')
xmpp.disconnect()
continue
elif option == '3':
print('Exiting the program...')
close_login = True
else:
print(invalid_option)