@@ -216,6 +216,50 @@ def track_idleness_and_update_status() -> None:
216
216
def update_recipient_emails (self , write_box : ReadlineEdit ) -> None :
217
217
self .recipient_emails = re .findall (r"[\w\.-]+@[\w\.-]+" , write_box .edit_text )
218
218
219
+ def tidy_valid_recipients_and_notify_invalid_ones (
220
+ self , write_box : ReadlineEdit
221
+ ) -> bool :
222
+ tidied_recipients = list ()
223
+ invalid_recipients = list ()
224
+
225
+ recipients = [
226
+ recipient .strip ()
227
+ for recipient in write_box .edit_text .split ("," )
228
+ if recipient .strip () # This condition avoids whitespace recipients (", ,")
229
+ ]
230
+
231
+ for recipient in recipients :
232
+ cleaned_recipient_list = re .findall (
233
+ r"^(.*?)(?:\s*?<?([\w\.-]+@[\w\.-]+)>?(.*))?$" , recipient
234
+ )
235
+ recipient_name , recipient_email , invalid_text = cleaned_recipient_list [0 ]
236
+ # Discard invalid_text as part of tidying up the recipient.
237
+
238
+ if recipient_email :
239
+ is_valid = self .model .is_valid_private_recipient (
240
+ recipient_name , recipient_email
241
+ )
242
+ else :
243
+ is_valid = False
244
+
245
+ if is_valid :
246
+ tidied_recipients .append (f"{ recipient_name } <{ recipient_email } >" )
247
+ else :
248
+ invalid_recipients .append (recipient )
249
+ tidied_recipients .append (recipient )
250
+
251
+ write_box .edit_text = ", " .join (tidied_recipients )
252
+ write_box .edit_pos = len (write_box .edit_text )
253
+
254
+ if invalid_recipients :
255
+ invalid_recipients_error = (
256
+ f"Invalid recipient(s) - { ', ' .join (invalid_recipients )} "
257
+ )
258
+ self .view .controller .report_error (invalid_recipients_error )
259
+ return False
260
+
261
+ return True
262
+
219
263
def stream_box_view (
220
264
self , stream_id : int , caption : str = "" , title : str = ""
221
265
) -> None :
@@ -561,6 +605,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
561
605
msg_id = self .msg_edit_id ,
562
606
)
563
607
else :
608
+ all_valid = self .tidy_valid_recipients_and_notify_invalid_ones (
609
+ self .to_write_box
610
+ )
611
+ if not all_valid :
612
+ return key
564
613
self .update_recipient_emails (self .to_write_box )
565
614
if self .recipient_emails :
566
615
success = self .model .send_private_message (
@@ -587,6 +636,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
587
636
elif is_command_key ("SAVE_AS_DRAFT" , key ):
588
637
if not self .msg_edit_id :
589
638
if self .to_write_box :
639
+ all_valid = self .tidy_valid_recipients_and_notify_invalid_ones (
640
+ self .to_write_box
641
+ )
642
+ if not all_valid :
643
+ return key
590
644
self .update_recipient_emails (self .to_write_box )
591
645
this_draft : Composition = PrivateComposition (
592
646
type = "private" ,
@@ -650,22 +704,12 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
650
704
else :
651
705
header .focus_col = self .FOCUS_HEADER_BOX_STREAM
652
706
else :
653
- self .update_recipient_emails (self .to_write_box )
654
- invalid_emails = list ()
655
-
656
- for recipient_email in self .recipient_emails :
657
- is_valid = self .model .is_valid_private_recipient (
658
- recipient_email
659
- )
660
- if not is_valid :
661
- invalid_emails .append (recipient_email )
662
-
663
- if invalid_emails :
664
- invalid_emails_error = (
665
- f"Invalid recipient(s) - { ', ' .join (invalid_emails )} "
666
- )
667
- self .view .controller .report_error (invalid_emails_error )
707
+ all_valid = self .tidy_valid_recipients_and_notify_invalid_ones (
708
+ self .to_write_box
709
+ )
710
+ if not all_valid :
668
711
return key
712
+ self .update_recipient_emails (self .to_write_box )
669
713
users = self .model .user_dict
670
714
self .recipient_user_ids = [
671
715
users [email ]["user_id" ] for email in self .recipient_emails
0 commit comments