-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiChat.py
More file actions
executable file
·1009 lines (949 loc) · 45.4 KB
/
multiChat.py
File metadata and controls
executable file
·1009 lines (949 loc) · 45.4 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Used to display date and time for messages.
from datetime import date, datetime
# Used to set home directory for chat logs.
import platform
# Used to make sure chat directory exists.
import os
from pathlib import Path
# Proper arrow key scrolling on Un*x
try:
if platform.system() != "Windows": import readline
except:
print("Could not import readline.")
# Used for dice rolling
import random
# Used to save/load users
import pickle
# Check if installed. If not, warn user.
COLORS = False
try:
# Used to color names/prompts.
from termcolor import colored;
COLORS = True
except:
print("Warning: termcolor library required for color output.")
print("It can be installed in your terminal with 'pip install termcolor'.")
print("Continuing without color.")
PROMPT_INSTALLED = False
try:
# Fixes text wrap bugs by allowing input wrapping
from prompt_toolkit import prompt, print_formatted_text, ANSI
PROMPT_INSTALLED = True
except:
print("Warning: prompt_toolkit library required for handling long messages.")
print("It can be installed in your terminal with 'pip install prompt_toolkit'.")
print("Continuing. Visual errors may occur when entering long lines of text.")
# Decide where to look for the settings file
# The default option will be one of the following on Linux systems:
# - $XDG_CONFIG_HOME/multichat
# - $HOME/.config/multichat
# - $HOME/Documents/multichat
# - $HOME/multichat
# On Windows:
# $APPDATA\multichat
# C:\Program Files (x86)\multichat
def get_settings_dir():
if platform.system() == "Windows":
try:
basepath = os.getenv('APPDATA')
except:
basepath = "C:/Program Files (x86)"
else:
try:
basepath = os.environ['XDG_CONFIG_HOME']
except:
basepath = os.environ['HOME'] + "/.config"
if os.path.isdir(basepath) == False: # ...Documents, then?
basepath = os.environ['HOME'] + "/Documents"
if os.path.isdir(basepath) == False: # Fine, home directory it is
basepath = os.environ['HOME']
settingpath = basepath + "/multichat"
if os.path.isdir(settingpath) == False: Path(settingpath).mkdir(parents=True, exist_ok=True)
# Deal with permission errors
while not os.access(settingpath, os.W_OK):
print(f"Default settings path ({settingpath}) is not accessible: permission denied.")
settingpath = input("Please enter a save location for Multichat settings files: ").rstrip() + "/multichat"
if os.path.isdir(settingpath) == False: Path(settingpath).mkdir(parents=True, exist_ok=True)
return settingpath
# Make the default settings
def build_default_settings():
# Figure out default chatlog file location- distinct from settings location!
log_dir = get_log_dir()
settings_dir = get_settings_dir()
# Build the settings dict
default_settings = {"savedir": log_dir, "timestamps": True, "timestamp_format": "%H:%M:%S", "backread_linecount": 100, "case_sensitive_proxies": True}
return default_settings
# Either get the user's existing settings, or assign the defaults
def retrieve_settings():
settings_dir = get_settings_dir()
if os.path.isfile(settings_dir + "/settings.pkl") == False:
settings = build_default_settings()
else:
with open(settings_dir + "/settings.pkl", "rb") as settingsfile:
settings = pickle.load(settingsfile)
# Deal with backwards compatibility for new settings
try:
test = settings["backread_linecount"]
except:
settings["backread_linecount"] = 100
settings_dir = get_settings_dir()
save_settings(settings, settings_dir)
try:
casesensitive = settings["case_sensitive_proxies"]
except:
settings["case_sensitive_proxies"] = True
settings_dir = get_settings_dir()
save_settings(settings, settings_dir)
try:
timestamp_format = settings["timestamp_format"]
now.strftime(settings["timestamp_format"])
except:
print("Invalid or missing timestamp format setting; reverting to base format (%H:%M:%S).")
settings["timestamp_format"] = "%H:%M:%S"
settings_dir = get_settings_dir()
save_settings(settings, settings_dir)
# Add random flavortext
settings["random_flavortext"] = get_flavortext(settings_dir)
return settings
# Save user settings to file
def save_settings(settings, settings_dir):
if os.path.isdir(settings_dir) == False: Path(settings).mkdir(parents=True, exist_ok=True)
# Remove options that we don't want to store in the settings file
if "random_flavortext" in settings.keys():
del settings["random_flavortext"]
with open (settings_dir + "/settings.pkl", "wb") as settingfile:
pickle.dump(settings, settingfile)
return True
# We need this directory to exist yesterday
def make_dir_exist(dir: str) -> bool:
if os.path.isdir(dir) == False: # Doesn't exist
try: # Try to make it
Path(dir).mkdir(parents=True, exist_ok=True)
except:
return False # Doesn't exist, can't make it
if os.path.isdir(dir) == True:
return True # Does exist, one way or another
return False # Something has gone terribly wrong
# Decide where the (default) chatlog save directory should be
# The default locations will be one of the following depending on the file system and environment variable setup.
# On Linux:
# - $XDG_DATA_HOME/multichat
# - $HOME/.local/share/multichat
# - $HOME/Documents/multichat
# - $HOME/multichat
# On Windows:
# $APPDATA\multichat
def get_log_dir() -> str:
env_home = None # Nothing default
if platform.system() == "Windows":
env_home = os.getenv('APPDATA')
else:
for place in [os.environ['XDG_DATA_HOME'], os.environ['HOME'] + "/.local/share", os.environ['HOME'] + "/Documents", os.environ['HOME']]:
if make_dir_exist(place) == True:
env_home = place
break
# Something has gone very wrong if you get here
if env_home is None:
print("Cannot find a valid log save location.")
log_dir = input("Enter valid log save location: ").rstrip() + "/multichat"
log_dir = env_home + "/multichat"
log_dir_exists = make_dir_exist(log_dir)
# Likewise: previously fine directory is somehow not fine to write to
while not os.access(log_dir, os.W_OK) or not log_dir_exists:
print(f"Cannot access log save location ({log_dir}). Permission error?")
log_dir = input("Enter valid log save location: ").rstrip() + "/multichat"
log_dir_exists = make_dir_exist(log_dir)
return log_dir
def change_log_dir(settings, log_file, log_file_name, user_list):
save_dir = input("Enter new chatlog save location (absolute path): ").rstrip() # Remove trailing slash, if present
# if save_dir[-1] in ["/", "\\"]: save_dir = save_dir[:-1]
# DO SAFETY CHECKS
try: # Does it exist?
if os.path.isdir(save_dir) == False: # Nope, fix it
Path(save_dir + "/multichat").mkdir(parents=True, exist_ok=True)
except Exception as ex: # Nope, failed to fix it
print("Could not create save location.")
if os.path.isdir(save_dir) == True: # It exists!
# Can we write to it?
if os.access(save_dir, os.W_OK):
settings["savedir"] = save_dir + "/multichat"
settings_dir = get_settings_dir()
save_settings(settings, settings_dir)
clear()
print(f"Saved. MultiChat's chat logs will now be saved to {save_dir}.")
print(log_file)
log_file.write("\n\n")
log_file.close() # Kill the old file, on to the new!
log_file = open_log(save_dir, log_file_name)
chat(user_list, save_dir, log_file, log_file_name, settings)
else:
print(f"Cannot access log file: permission denied. Do you have permission to write to files in {save_dir}?")
# Get list of flavortext used by /random
def get_flavortext(settings_dir):
# Check for a custom flavor text file
if os.path.isfile(settings_dir + "/random_flavortext.txt"):
with open(settings_dir + "/random_flavortext.txt", "r") as flavor_file:
flavor_options = flavor_file.readlines()
elif os.path.isfile("random_flavortext.txt"):
with open("random_flavortext.txt", "r") as flavor_file:
flavor_options = flavor_file.readlines()
else:
# If not found, use a short default list of flavor text
flavor_options = [
"NAME's turn!",
"NAME has been chosen by the gods of randomness!",
"NAME was hand-picked by the algorithm!",
"NAME has been chosen!",
"NAME won the lottery!",
"NAME spawned in!",
"NAME, I choose you!",
"NAME joins the game!",
"NAME gets the talking stick!"
]
return flavor_options
# Add a new user, updating the user list
def add_user(user, user_list):
if user:
new_user_number = len(user_list) + 1
user_list.update({str(new_user_number): {"username": str(user), 'color': 'default'}})
print()
print(user + " added!")
print("Type " + str(new_user_number) + " to send messages as " + user + ".")
print()
else:
print("Please enter a username when adding a new user.")
return user_list
# Load existing users
def load_users(output: bool):
settings_dir = get_settings_dir()
if os.path.isfile(settings_dir + "/saved-users.pkl") == False:
print("No saved users found. Save current users by sending /save\nwhile in chat.")
return {}
else:
with open(settings_dir + "/saved-users.pkl", "rb") as savefile:
user_list = pickle.load(savefile)
print("Loaded users from file.")
if output == True:
for user in user_list.keys():
print("Type " + str(user) + " to send messages as " + str(user_list[user]["username"]))
return user_list
# List out all users in list- output for the user to figure out who's there
def list_users(user_list):
for user in user_list.keys():
# Fix old data format
if not isinstance(user_list[user], dict):
user_list[user] = {"username": str(user_list[user]), "color": "default"}
# Information for the user :D
print("Type " + str(user) + " to send messages as " + user_list[user]["username"])
# Clears the terminal.
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
# Get initial users.
def get_users():
# Get at least one user. Dictionary format.
user_list = {}
# Variable is used to control a while loop that decides whether
# to enter more users.
continue_entry = True
# Set up first user label
user_number = 1
clear()
print("Welcome to MultiChat!")
print()
print("If you'd like to load saved users, enter /load.")
user_name = input("Otherwise, enter the name of user " + str(user_number) + " or q to quit: ")
# Get user name and add to dictionary
# Dictionary format:
# Number: Name
while continue_entry == True:
# Allow for insta-quitting.
if user_name == "q":
clear()
raise SystemExit
# We're done here- chat time
elif user_name =="n" and user_number > 1:
user_number -= 1
return user_list
# Fast bypass if users already are saved to file- else, complain
if user_name == "":
user_name = "/load"
# Load existing users if they exist
if user_name == "/load" and user_list == {}:
user_list = load_users(False)
if user_list != {}:
user_number = len(user_list)
continue_entry = False
# Deal with lack of color support
if not COLORS:
for user in user_list:
user_list[user]["color"] = "default"
return user_list
elif user_name == "/load":
overwrite = input("Warning: loading saved users will overwrite current user list. Continue? y/n: ")
if overwrite.lower == "y" or overwrite.lower == "yes":
user_list = load_users(False)
# We have a username!
elif user_name:
# Add user.
user_list[str(user_number)] = {"username": str(user_name), "color": "default"}
user_number += 1
print("If you're done, enter n to stop adding users.")
# Bogus inputs
else:
print("Please enter a username.")
continue_entry = True
user_name = input("Enter the name of user " + str(user_number) + " or q to quit: ")
# Separated into its own function to enable changing log dir in settings
def open_log(log_dir, log_file_name):
try:
log_file_name = log_dir + "/" + log_file_name + ".txt"
print(log_file_name)
# Make sure the file exists so r+ mode won't throw a fit
# Yes, this is clumsy and a horrible way to do this
# But it's 5 pm and I've been at this all day, and it's
# not really intended for capitalism or anything, so.
# As long as it works!
log_file = open(log_file_name, "a")
log_file.close()
# Now that we are 100% certain the file exists:
# Attempt to open the log file as read-append.
log_file = open(log_file_name, 'r+')
# If something goes wrong, retry the loop.
except Exception as error:
print("Error:", error)
print("Please try again.")
# Stop the loop and return the needed information.
else:
return log_file
def get_log_file(log_dir):
# Get or create log file.
print()
print("Enter a name for this chat, or hit enter to")
log_file_name = input("select the default (chat): ")
# Check for empty input.
if log_file_name.isspace() == True or not log_file_name:
log_file_name = "chat"
# Check for quitting.
if log_file_name == "q":
raise SystemExit
log_file = open_log(log_dir, log_file_name)
return log_file, log_file_name
# Test if a message is a switch. If it is, return the key to switch with.
def check_for_switch(message: str, user_list: dict, case_sensitivity: bool):
try:
proxies = user_list.keys()
if case_sensitivity == False:
insensitive_proxies = [proxy.casefold() for proxy in proxies]
if message.casefold() in insensitive_proxies: # Removes case differences
return True
else:
return False
elif message in proxies:
return True
else:
return False
except Exception as ex:
print("Please report the following error:", "check for switch failed,", ex)
# If all else fails, don't switch
return False
# CHANGE ACTIVE USER
def switch(user):
active_user = user["username"]
# Handle color/noncolor support
if COLORS:
active_color = user["color"]
else:
active_color = "default"
chat_message = ""
return active_user, active_color, chat_message
# The main chat sequence
def chat(user_list, log_dir, log_file, log_file_name, settings):
# Setup
chat_message = ""
# Deal with any goofs
if isinstance(user_list, tuple): # If I missed any old user_counter passes
user_list = user_list[0]
while user_list == {}:
user_list = get_users()
# Set first active user to be user 1, as this is the
# most expected behavior and prevents sending messages
# as no one.
# Also fix the old /save format to match the new format
# (so color can happen!)
active_user = next(iter(user_list.keys()))
if not isinstance(user_list[active_user], dict):
username = user_list[active_user]
user_list[active_user] = {"username": str(username), "color": "default"}
active_user = username
active_color = "default"
else:
user_info = user_list[active_user]
print(user_info)
active_user = str(user_info["username"])
active_color = str(user_info["color"])
# Add a date marker to the top of the log file
# (or if appending to an existing file, to the end of it).
try:
# Clear the terminal to make it look nicer.
clear()
# Setup
read_line_count = int(settings["backread_linecount"])
# Get the old chat logs
oldchat = log_file.readlines()
# If the user saved this as a setting, get it
# Deal with improper values
if read_line_count > len(oldchat) or read_line_count < 0:
read_line_count = len(oldchat) - 1
# Print up to N lines of any existing chat to terminal.
for line in (oldchat[-read_line_count:]):
print(line, end ='')
#for line in log_file:
# print(line, end="")
# Get the date and add formatting.
now = date.today()
todayDate = "-----" + str(now.strftime("%A, %B %d, %Y")) + "-----"
# Display the date and add it to the log file.
print(todayDate)
log_file.write(todayDate + "\n\n")
# Notify the user if an exception occurs while getting the date.
except Exception as error:
print("Error getting chat file or date.")
print(error)
# Print instructions for user
print("Welcome to MultiChat!")
list_users(user_list)
print("Or type /quit to quit (case sensitive).")
print()
print("Type /help to view a help message,")
print("/commands to view a list of commands,")
print("or /users to see a list of all users.")
print()
# Check for special inputs and handle accordingly
while chat_message not in ["/quit", "/exit"]:
# Get the time.
now = datetime.now()
current_time = now.strftime(settings["timestamp_format"])
# Set up message preface (used to identify messages)
if settings["timestamps"] == True:
preface_contents = str(active_user) + ", " + current_time + ": "
else:
preface_contents = str(active_user) + ": "
if active_color == "default": # No color set
preface = preface_contents
else: # Color set
preface = colored(preface_contents, active_color)
# Get chat message.
if PROMPT_INSTALLED:
chat_message = prompt(ANSI(preface))
else:
chat_message = input(preface)
# SWITCH ACTIVE USER
# Do not record the number in the log file.
try: # Try switching to the user indicated by the number.
# We have a switch:
if check_for_switch(chat_message, user_list, settings["case_sensitive_proxies"]) == True:
# Handle the actual switch
if settings["case_sensitive_proxies"] == False:
# Find the match
for user in user_list:
# Case-insensitive matching and switching
if user.casefold() == chat_message.casefold():
active_user, active_color, chat_message = switch(user_list[user])
# Fix old user formats
if not isinstance(user_list[user], dict): # If user format is outdated
user_list[user] = {"username": user_list[user], "color": "default"}
else: # Case sensitive is simpler
active_user, active_color, chat_message = switch(user_list[chat_message])
else: # We're not switching right now
chat_message = chat_message
except Exception as ex:
print(ex)
chat_message = chat_message
else:
# If it doesn't work, tell the user it's not working.
# Then make sure that chat_message is a string so as
# not to mess up other checks.
chat_message = str(chat_message)
# If we're quitting, add space in the text file, and notify user.
if chat_message == "/quit" or chat_message == "/exit":
clear()
print("Chat saved.")
try:
log_file.write("\n\n")
except Exception as error:
print("Error adding text separator to end of file.")
print("Your log should still be okay, but any later")
print("additions will not be separated by a line.")
raise SystemExit
# Help message
elif chat_message == "/help":
print()
print("Welcome to MultiChat!")
print("To view a list of users, type /users")
print("To add a new user to this session, type /add")
print("To view more commands, type /commands")
print()
# Add a new user
elif chat_message == "/add":
print()
new_user = input("Enter the name of the user to add: ")
user_list = add_user(new_user, user_list)
elif chat_message.startswith("/add ") == True:
new_user = chat_message.removeprefix("/add ")
if new_user == "help":
print("-------------")
print("/add: Add a new user to the list. Will prompt for username.")
print("Alternatively, /add <user> will add <user> to the list.")
print("A default proxy will be assigned based on the number of users.")
print("Examples:")
print(" /add Alice: Add a user named Alice.")
print(" /add Bob: Add a user named Bob.")
print(" /add Eve: Add a user named Eve.")
print("-------------")
else:
user_list = add_user(new_user, user_list)
# Remove users from the list
elif chat_message.startswith("/remove"):
del_user = chat_message.removeprefix("/remove ")
# Help message
if del_user == "help":
print("-------------")
print("/remove <proxy>: delete the user with proxy <proxy>.")
print("Past messages sent as this user will still be under their name, but they will be removed \nfrom the user list and you will not be able to send messages as them.")
print("You will be asked to confirm deletion by typing their full name.")
print("If you're not sure what a user's <proxy> is, see /users")
print("-------------")
# Delete the user, if they exist
elif del_user in user_list:
username = del_user["username"]
print(f"Are you sure you'd like to delete {username}?")
confirm = input(f"Type their name to confirm (case-sensitive): ").lower()
if confirm == username:
user_list[del_user]
print(f"{username} deleted.")
list_users(user_list)
else:
print("Name does not match. Did you make a typo?")
# Oops, no user!
else:
print(f"Cannot find user with proxy {del_user}. Did you typo?")
# Clear the screen
elif chat_message == "/clear":
clear()
# List all users
elif chat_message == "/users" or chat_message == "/switch":
print("Users:")
list_users(user_list)
# Quote saving and retrieval
elif chat_message.startswith("/quote") == True:
quote_path = log_dir + "/quotes.txt"
if chat_message == "/quote help":
print("-------------")
print("/quote: save chat messages or read a random saved message.")
print("/quote <message> will store <message> as a quote that can be retrieved later.")
print("This quote will be attributed to the current user and the current time.")
print("/quote on its own will retrieve a random saved quote, if any exist.")
print("-------------")
elif chat_message == "/quote":
# Read from quote file
try:
quote_file = open(quote_path, "r")
except: # Quotes broke, sorry. Start over.
# Save the broken one, if it exists
try:
from shutil import copyfile
shutil.copy(quote_path, quote_path + ".bak")
print("Error: quotes file missing or corrupt. \nSaving old file as backup, creating new file.")
except: # Can't save backup- file probably doesn't exist yet
pass # we'll just replace it.
# Make a new quotes file
quote_file = open(quote_path, "w")
quote_file.close()
quote_file = open(quote_path, "r")
# Put all lines in a list
quote_list = []
for line in quote_file:
quote_list.append(line)
# Pick random line
random.seed()
try:
quote_count = len(quote_list)
random_quote = quote_list[random.randrange(0, quote_count)]
print(random_quote, end="")
log_file.write(preface + chat_message + "\n")
# Write to log file so it's not confusing later
chat_message = "MultiChat: " + random_quote
log_file.write(chat_message)
except:
print("Unable to access quotes; did you save any?")
# Close the file
quote_file.close()
elif "/quotes" in chat_message:
print("MultiChat: Did you mean /quote?")
elif chat_message.startswith("/quote ") == True:
# Open quote file
quote_file = open(quote_path, "a")
# Remove /quote from message
try:
chat_message = chat_message.removeprefix("/quote ")
# Add quote
today = str(now.strftime("%A, %B %d, %Y"))
quote_text = "On " + today + ", " + active_user + " said: " + chat_message + "\n"
quote_file.write(quote_text)
# Write to log file so it's not confusing later
chat_message = chat_message
log_file.write(active_user + ' added: "' + chat_message + '" to the quotes!' + "\n")
print("Multichat: Quote added!")
except:
print("Error: Could not remove /quote from message.")
# Close file
finally:
quote_file.close()
else:
print("MultiChat: Did you mean /quote?")
# Save users to file
elif chat_message.startswith("/save"):
if chat_message.endswith("help"):
print("-------------")
print("/save: Save the names, colors, and proxies of current users.")
print("This allows these users to be loaded in a different chat with /load.")
print("Saved users can be found in \n %APPDATA/multichat/saved-users.pkl (Windows)")
print(" ~/.config/multichat/saved-users.pkl (Linux, MacOS).")
print("-------------")
else:
settings_dir = get_settings_dir()
with open(settings_dir + "/saved-users.pkl", "wb") as savefile:
pickle.dump(user_list, savefile)
print("Saved users to file.")
# Load users from file
elif chat_message.startswith("/load"):
if chat_message.endswith("help"):
print("-------------")
print("/load: Retrieve saved user names, colors, and proxies.")
print("Warning: old user list will be overwritten!")
print("/save must have been used previously to create the file that /load looks for:")
print(" %APPDATA/multichat/saved-users.pkl (Windows)")
print(" ~/.config/multichat/saved-users.pkl (Linux, MacOS)")
print("-------------")
else:
user_list = load_users(True)
# Change settings
elif chat_message == "/settings":
# Retrieve settings
print(f"Settings (saved at {get_settings_dir()}):")
loc = settings["savedir"]
timestat = settings["timestamps"]
timestat_format = settings["timestamp_format"]
backread_linecount = settings["backread_linecount"]
case_sensitivity = settings["case_sensitive_proxies"]
print(f"1: Change chatlog save location (currently {loc})")
print(f"2: Toggle timestamps (currently {timestat})")
print(f"3: Change timestamp format (currently {timestat_format})")
print(f"4: Set lines of old chatlog to display (currently {backread_linecount})")
print(f"5: Toggle case-sensitivity for switch proxies (currently {case_sensitivity})")
setnum = input("Enter number of setting to change: ")
match setnum:
# Changing where chatlogs are saved
case "1":
print("Old files will not be copied over-")
print("please move these yourself if you'd like to access them.")
change_log_dir(settings, log_file, log_file_name, user_list)
# Toggle timestamps
case "2":
settings["timestamps"] = not settings["timestamps"]
settings_dir = get_settings_dir()
save_settings(settings, settings_dir)
print("Timestamps toggled. Currently:", settings["timestamps"])
# Change format of timestamps
case "3":
settings["timestamp_format"] = input("Please enter a valid datetime formatting string (e.g. %H:%M): ")
invalid = True
while invalid:
try:
now.strftime(settings["timestamp_format"])
invalid = False
except:
print("Invalid datetime format string.")
invalid = True
settings["timestamp_format"] = input("Please enter a valid datetime formatting string (e.g. %H:%M): ")
settings_dir = get_settings_dir()
save_settings(settings, settings_dir)
print("Timestamp format changed. Currently:", settings["timestamp_format"])
case "4":
backread_linecount = input("Enter number of lines to display when loading saved chats: ")
settings["backread_linecount"] = backread_linecount
settings_dir = get_settings_dir()
save_settings(settings, settings_dir)
print(f"Line count set to {backread_linecount}.")
case "5":
settings["case_sensitive_proxies"] = not settings["case_sensitive_proxies"]
settings_dir = get_settings_dir()
save_settings(settings, settings_dir)
print("Case sensitivity toggled. Currently:", settings["case_sensitive_proxies"])
# Change the user's prefix color
elif chat_message.startswith("/color") == True:
if not COLORS:
print("Colors not supported: termcolor library not installed.")
else:
color = chat_message.removeprefix("/color ")
# Valid colors
color_list = ["red", "yellow", "green", "cyan", "blue", "magenta", "light_grey", "dark_grey", "black", "white", "light_red", "light_yellow", "light_green", "light_cyan", "light_blue", "light_magenta"]
# Set up sample color display, accounting for availability differences
color_samples = "\ndefault"
for color_option in color_list:
try: # Every color colored with itself
color_samples += ", \n" + colored(color_option, color_option)
except:
pass # Color not available on this system
# Help texts
if color == "/color":
print("\nTo set a color for the current user, run: /color (color name)")
print("For example: /color red")
color = "nonsense"
if color == "help":
print("-------------")
print("/color <colorname>: set the color of the current user's name/timestamp.")
print("<color> may be any standard ANSII terminal color name.")
print("The following colors are allowed:")
print(color_samples)
print("Examples:")
print(" /color red: set the current user's color to red.")
print(" /color default: set the current user's color to the default text color.")
print("-------------")
# If we have a valid color
if color in color_list and color != "default":
# Get key for current user. I'm sorry. This is overkill.
reverse_user_lookup = {}
for user in user_list:
try:
reverse_user_lookup[user_list[user]["username"]] = user
except TypeError:
# String indices must be integers, not 'str'
print("Oops! Please let the maintainer know that you encountered an error: user lookup searching for wrong type.")
tag = reverse_user_lookup[active_user]
# Set new color for current user
user_list[tag]["color"] = color
active_color = color
elif color != "help": # Invalid choice
print("Please enter a valid color from the following:", color_samples, "\n")
# Load users from file
elif chat_message.startswith("/proxy") == True:
tag = chat_message.removeprefix("/proxy ")
if tag == "help":
print("-------------")
print("/proxy <tag>: Change the current user's proxy to <tag>.")
print("This allows you to set the text used to switch to the current user.")
print("If a message exactly matches a user's proxy text, then the next message is set \nas coming from their user. Matching is case-sensitive!")
print("It's a good idea to choose proxies that you don't tend to type in \nnormal conversation.")
print("Examples: ")
print(' /proxy w>: Sets switch text for current user to "w>".')
print(' /proxy Will: Sets switch text for current user to "Will".')
print(' /proxy sudo: Sets switch text for current user to "sudo".')
print("-------------")
elif len(tag) > 0 and len(chat_message) > 6:
if not tag.startswith("/"):
# Get key for current user. I'm sorry
reverse_user_lookup = {}
for user in user_list:
reverse_user_lookup[user_list[user]["username"]] = user
old_tag = reverse_user_lookup[active_user]
user_list[tag] = active_user
del user_list[old_tag]
print(active_user + "'s proxy changed to " + tag)
list_users(user_list)
else:
print("Proxies cannot start with / (sorry!).")
else:
print("Please supply a new tag: /proxy <tag>")
# List commands
elif chat_message == "/commands":
print()
print("Commands:")
print("/add <username>: Add new user.")
print("/clear: Clear the screen.")
print("/color <color name>: Set a prefix color for this user.")
print("/commands: View this message.")
print("/dice <number>: Roll a die with <number> faces.")
print("/help: View a help message.")
print("/load: Load saved users from file. Overwrites current user list!")
print("/nolog: Do not save the next message.")
print("/proxy <text>: Change the current user's switch text to <text>.")
print("/quit: Save and quit MultiChat.")
print("/quote: View random quotes you've added.")
print("/quote <text>: Add text to the quotes list.")
print("/random: Change to a random user.")
print("/remove: Delete a user.")
print("/save: Save list of current users to file.")
print("/settings: Change settings for MultiChat.")
print("/shrug: Send a shrug emote.")
print("/users: List users in session.")
print()
# Dice rolling
elif chat_message == "/dice":
print("MultiChat: /dice syntax: /dice <number>")
print("MultiChat: For more information, see /dice help")
elif chat_message.startswith("/dice ") == True:
dice_sides = chat_message.removeprefix("/dice ")
if dice_sides == "help":
print("-------------")
print("/dice <number>: roll a die with a set number of sides and shows the results.")
print("<number> may be any integer greater than zero.")
print("Examples:")
print(" /dice 6: rolls a 6-sided die.")
print(" /dice 20: rolls a 20-sided die.")
print(" /dice 8675309: rolls a 8,675,309-sided die.")
print("-------------")
try:
dice_sides = int(dice_sides)
if dice_sides == 1:
dice_roll = 1
else:
random.seed()
dice_roll = str(random.randrange(1, dice_sides))
print("You rolled a " + dice_roll + "!")
log_file.write(active_user + " rolled a " + str(dice_sides) + "-sided die and rolled a " + dice_roll + "!\n")
except:
if dice_sides != "help":
print("Can't roll die! " + str(dice_sides) + " is not a valid number for rolling!")
# Change to random user
elif chat_message == "/random":
# Pick the silly flavortext
random_flavor = random.choice(settings["random_flavortext"]).strip()
# Pick the random user
random_user = random.choice(list(user_list.keys()))
# Change to the random user only if it's different than the current active_user. Otherwise choose a random user again. Repeat until a different user is found.
if active_user != random_user:
active_user, active_color, chat_message = switch(user_list[random_user])
# Let the user know who got picked
print("Multichat: " + random_flavor.replace("NAME", active_user))
# Easter eggs and references
# Table flipping
elif chat_message.lower() == "flips table" or chat_message.lower() == "tableflip" or chat_message.lower() == "table flip":
tableflip = preface + "(╯°□°)╯︵ ┻━┻"
print(tableflip)
log_file.write(tableflip + "\n")
# Shrug
elif chat_message.lower() == "shrug" or chat_message.lower() == "shrugs" or chat_message == "/shrug":
try:
shrug = preface + "¯\\_('u')_/¯"
print(shrug)
log_file.write(active_user + " shrugs.")
except:
print("Inexplicably, your shoulders fail to rise. The power of /shrug is beyond you.")
# Losing the Game (sorry)
elif chat_message.lower() == "the game":
print("\nMultiChat: !!! THE GAME HAS BEEN LOST! !!!")
print("MultiChat: Days since last incident: 0\n")
you_lost = active_user + " has unleashed an infohazard!\n"
log_file.write(you_lost)
# Eyes emoji
elif chat_message.lower() == "eyes":
eyes = """
wWWWWWWWww.
WWW'''::::::''WWw
wWWW" .,wWWWWWWw.. WWw.
` ` wWW' W888888888888W 'WXX.
. `. wWW' M88888i#####888"8M 'WWX.
` wWWW' M88888##d###'w8oo88M WWMX.
` wWWW" :W88888####* #88888M; WWIZ.
- -- wWWWW" W88888####42##88888W WWWXx
"WIZ W8n889######98888W WWXx.
' ' 'Wm, W88888999988888W >WWR'
' "WMm. "WW88888888WW" mmMM'
'Wmm. "WWWWWW" ,whAT?'
''MMMmm.. _,mMMMM
MMMMMMMMMMMMMM
"""
print(preface + eyes)
log_file.write(preface + "Eyes emoji\n")
# Beetlejuice
elif "beetlejuice" in chat_message.lower():
print("Say it again!")
log_file.write(preface + chat_message)
# Not an easter egg, too lazy to move it
# Allows for not saving a message upon request
elif "/nolog" in chat_message:
if chat_message.removeprefix("/nolog") == " help":
print("-------------")
print("/nolog: hide a single message from the chatlog.")
print("The following message will not be saved in any chatlogs or records (not even with /quote).")
print("Useful if you want to make sure something isn't saved in your records.")
print("WARNING: the message is really, truly gone once MultiChat closes!")
print("Don't use this for any messages that you want to read later.")
print("-------------")
else:
print("/nolog: The next message will not be saved in the chatlog.")
# Set up message preface (used to identify messages)
preface = colored(active_user + ", " + current_time + ": ", "dark_grey")
# Get chat message.
chat_message = input(preface)
print("/nolog: Back to normal logging.")
elif chat_message.lower() == "thumbsupper":
emote = """
´´´´´´´´´´´´´´´´´´´´´´@@@@@@@@@
´´´´´´´´´´´´´´´´´´´´@@´´´´´´´´´´@@
´´´´´´@@@@@´´´´´´´@@´´´´´´´´´´´´´´@@
´´´´´@´´´´´@´´´´@@´´´´´@@´´´´@@´´´´´@@
´´´´´@´´´´´@´´´@@´´´´´´@@´´´´@@´´´´´´´@@
´´´´´@´´´´@´´@@´´´´´´´´@@´´´´@@´´´´´´´´@@
´´´´´´@´´´@´´´@´´´´´´´´´´´´´´´´´´´´´´´´´@@
´´´´@@@@@@@@@@@@´´´´´´´´´´´´´´´´´´´´´´´´@@
´´´@´´´´´´´´´´´´@´@@´´´´´´´´´´´´´@@´´´´´@@
´´@@´´´´´´´´´´´´@´´@@´´´´´´´´´´´´@@´´´´´@@
´@@´´´@@@@@@@@@@@´´´´@@´´´´´´´´@@´´´´´´´@@
´@´´´´´´´´´´´´´´´@´´´´´@@@@@@@´´´´´´´´´@@
´@@´´´´´´´´´´´´´´@´´´´´´´´´´´´´´´´´´´´@@
´´@´´´@@@@@@@@@@@@´´´´´´´´´´´´´´´´´´´@@
´´@@´´´´´´´´´´´@´´@@´´´´´´´´´´´´´´´´@@
´´´@@@@@@@@@@@@´´´´´@@´´´´´´´´´´´´@@
´´´´´´´´´´´´´´´´´´´´´´´@@@@@@@@@@@"""
print(preface + emote + "\n")
log_file.write(preface + emote + "\n")
elif "thumbsup" in chat_message.lower():
print(preface + "👍")
log_file.write(preface + "👍" + "\n")
# Done with easter eggs, back to regular code.
# If there are no special cases, try to append the
# new message to the log file. Report and handle
# errors if this doesn't succeed, and notify the
# user that their message may not have saved.
elif chat_message != "":
try:
# Append to file.
log_file.write(preface + chat_message + "\n")
except PermissionError:
print("Cannot save to file: permission denied. Do you have permission to write to your log save location?")
change_loc = input("Would you like to change your log save location? y/n: ")
if change_loc.casefold() == "y":
pass
except Exception as error:
print("Error:", error)
print("Your message may not have been saved.")
# Set up main function
def main():
# Get user settings, including log directory location
settings = retrieve_settings()
# Set up a log directory
log_dir = settings["savedir"]
try:
if os.path.isdir(log_dir) == False: Path(log_dir).mkdir(parents=True, exist_ok=True)
os.chdir(log_dir)
except Exception as error:
print("Error creating or accessing chatlogs folder.")
print("Please report this error to the creator.")
print("Error code:", error)
# Get user names and the file to log to.
user_list = get_users()
log_file, log_file_name = get_log_file(log_dir)
# Chat and log to file.
chat(user_list, log_dir, log_file, log_file_name, settings)
# Close file and finish up.