Update mail.py

This commit is contained in:
grungies1138 2016-12-30 09:04:30 -06:00 committed by GitHub
parent ddd3cd0888
commit b0ad1c06f4

View file

@ -22,24 +22,21 @@ class CmdMail(default_cmds.MuxCommand):
""" """
Commands that allow either IC or OOC communications Commands that allow either IC or OOC communications
Usage: Usage:
{w@mail{n @mail - Displays all the mail a player has in their mailbox
- Displays all the mail a player has in their mailbox
{w@mail <#>{n @mail <#> - Displays a specific message
- Displays a specific message
{w@mail <players>=<subject>/<message>{n @mail <players>=<subject>/<message>
- Sends a message to the comma separated list of players. - Sends a message to the comma separated list of players.
{w@mail/delete <#>{n @mail/delete <#> - Deletes a specific message
- Deletes a specific message
{w@mail/forward <player list>=<#>[/<Message>]{n @mail/forward <player list>=<#>[/<Message>]
- Forwards an existing message to the specified list of players, - Forwards an existing message to the specified list of players,
original message is delivered with optional Message prepended. original message is delivered with optional Message prepended.
{w@mail/reply <#>=<message>{n @mail/reply <#>=<message>
- Replies to a message #. Prepends message to the original - Replies to a message #. Prepends message to the original
message text. message text.
Switches: Switches:
@ -47,12 +44,6 @@ class CmdMail(default_cmds.MuxCommand):
forward - forward a received message to another object with an optional message attached. forward - forward a received message to another object with an optional message attached.
reply - Replies to a received message, appending the original message to the bottom. reply - Replies to a received message, appending the original message to the bottom.
Status Legend:
'|wU|n' = Unread/New message
'|wO|n' = Read/Old message
'|wR|n' = Replied to
'|wF|n' = Forwarded
Examples: Examples:
@mail 2 @mail 2
@mail Griatch=New mail/Hey man, I am sending you a message! @mail Griatch=New mail/Hey man, I am sending you a message!
@ -70,66 +61,74 @@ class CmdMail(default_cmds.MuxCommand):
body = "" body = ""
if self.switches or self.args: if self.switches or self.args:
if "delete" in self.switches: if "delete" in self.switches:
if not self.lhs: try:
self.caller.msg("No Message ID given. Unable to delete.") if not self.lhs:
return self.caller.msg("No Message ID given. Unable to delete.")
else:
if self.get_all_mail()[int(self.lhs) - 1]:
self.get_all_mail()[int(self.lhs) - 1].delete()
self.caller.msg("Message %s deleted" % self.lhs)
else:
self.caller.msg("That message does not exist.")
return return
else:
if self.get_all_mail()[int(self.lhs) - 1]:
self.get_all_mail()[int(self.lhs) - 1].delete()
self.caller.msg("Message %s deleted" % self.lhs)
else:
self.caller.msg("That message does not exist.")
return
except ValueError:
self.caller.msg("Usage: @mail/delete <message ID>")
elif "forward" in self.switches: elif "forward" in self.switches:
if not self.rhs: try:
self.caller.msg("Cannot forward a message without a player list. Please try again.") if not self.rhs:
return self.caller.msg("Cannot forward a message without a player list. Please try again.")
elif not self.lhs: return
self.caller.msg("You must define a message to forward.") elif not self.lhs:
return self.caller.msg("You must define a message to forward.")
else: return
if "/" in self.rhs:
message_number, message = self.rhs.split("/")
if self.get_all_mail()[int(message_number) - 1]:
old_message = self.get_all_mail()[int(message_number) - 1]
self.send_mail(self.lhslist, "FWD: " + old_message.header,
message + "\n---- Original Message ----\n" + old_message.message,
self.caller)
self.caller.msg("Message forwarded.")
else:
self.caller.msg("Message does not exist.")
return
else: else:
if self.get_all_mail()[int(self.rhs) - 1]: if "/" in self.rhs:
old_message = self.get_all_mail()[int(self.rhs) - 1] message_number, message = self.rhs.split("/")
self.send_mail(self.lhslist, "FWD: " + old_message.header, if self.get_all_mail()[int(message_number) - 1]:
"\n---- Original Message ----\n" + old_message.message, self.caller) old_message = self.get_all_mail()[int(message_number) - 1]
self.caller.msg("Message forwarded.")
old_message.tags.remove("u", category="mail") self.send_mail(self.lhslist, "FWD: " + old_message.header,
old_message.tags.add("f", category="mail") message + "\n---- Original Message ----\n" + old_message.message,
self.caller)
self.caller.msg("Message forwarded.")
else:
self.caller.msg("Message does not exist.")
return
else: else:
self.caller.msg("Message does not exist.") if self.get_all_mail()[int(self.rhs) - 1]:
return old_message = self.get_all_mail()[int(self.rhs) - 1]
self.send_mail(self.lhslist, "FWD: " + old_message.header,
"\n---- Original Message ----\n" + old_message.message, self.caller)
self.caller.msg("Message forwarded.")
old_message.tags.remove("u", category="mail")
old_message.tags.add("f", category="mail")
else:
self.caller.msg("Message does not exist.")
return
except ValueError:
self.caller.msg("Usage: @mail/forward <player list>=<#>[/<Message>]")
elif "reply" in self.switches: elif "reply" in self.switches:
if not self.rhs: try:
self.caller.msg("You must define a message to reply to.") if not self.rhs:
return self.caller.msg("You must define a message to reply to.")
elif not self.lhs: return
self.caller.msg("You must supply a reply message") elif not self.lhs:
return self.caller.msg("You must supply a reply message")
else:
if self.get_all_mail()[int(self.lhs) - 1]:
old_message = self.get_all_mail()[int(self.lhs) - 1]
self.send_mail(old_message.senders, "RE: " + old_message.header,
self.rhs + "\n---- Original Message ----\n" + old_message.message, self.caller)
old_message.tags.remove("u", category="mail")
old_message.tags.add("r", category="mail")
return return
else: else:
self.caller.msg("Message does not exist.") if self.get_all_mail()[int(self.lhs) - 1]:
return old_message = self.get_all_mail()[int(self.lhs) - 1]
self.send_mail(old_message.senders, "RE: " + old_message.header,
self.rhs + "\n---- Original Message ----\n" + old_message.message, self.caller)
old_message.tags.remove("u", category="mail")
old_message.tags.add("r", category="mail")
return
else:
self.caller.msg("Message does not exist.")
return
except ValueError:
self.caller.msg("Usage: @mail/reply <#>=<message>")
else: else:
if self.rhs: if self.rhs:
if "/" in self.rhs: if "/" in self.rhs:
@ -142,13 +141,13 @@ class CmdMail(default_cmds.MuxCommand):
messageForm = [] messageForm = []
if message: if message:
messageForm.append(HEAD_CHAR * _WIDTH) messageForm.append(_HEAD_CHAR * _WIDTH)
messageForm.append("|wFrom:|n %s" % (message.senders[0].key)) messageForm.append("|wFrom:|n %s" % (message.senders[0].key))
messageForm.append("|wSent:|n %s" % message.db_date_created.strftime("%m/%d/%Y %H:%M:%S")) messageForm.append("|wSent:|n %s" % message.db_date_created.strftime("%m/%d/%Y %H:%M:%S"))
messageForm.append("|wSubject:|n %s" % message.header) messageForm.append("|wSubject:|n %s" % message.header)
messageForm.append(SUB_HEAD_CHAR * _WIDTH) messageForm.append(_SUB_HEAD_CHAR * _WIDTH)
messageForm.append(message.message) messageForm.append(message.message)
messageForm.append(HEAD_CHAR * _WIDTH) messageForm.append(_HEAD_CHAR * _WIDTH)
self.caller.msg("\n".join(messageForm)) self.caller.msg("\n".join(messageForm))
message.tags.remove("u", category="mail") message.tags.remove("u", category="mail")
message.tags.add("o", category="mail") message.tags.add("o", category="mail")
@ -158,7 +157,7 @@ class CmdMail(default_cmds.MuxCommand):
if messages: if messages:
table = evtable.EvTable("|wID:|n", "|wFrom:|n", "|wSubject:|n", "|wDate:|n", "|wSta:|n", table = evtable.EvTable("|wID:|n", "|wFrom:|n", "|wSubject:|n", "|wDate:|n", "|wSta:|n",
table=None, border="header", header_line_char=SUB_HEAD_CHAR, width=_WIDTH) table=None, border="header", header_line_char=_SUB_HEAD_CHAR, width=_WIDTH)
index = 1 index = 1
for message in messages: for message in messages:
table.add_row(index, message.senders[0], message.header, table.add_row(index, message.senders[0], message.header,
@ -172,28 +171,28 @@ class CmdMail(default_cmds.MuxCommand):
table.reformat_column(3, width=13) table.reformat_column(3, width=13)
table.reformat_column(4, width=7) table.reformat_column(4, width=7)
self.caller.msg(HEAD_CHAR * _WIDTH) self.caller.msg(_HEAD_CHAR * _WIDTH)
self.caller.msg(table) self.caller.msg(table)
self.caller.msg(HEAD_CHAR * _WIDTH) self.caller.msg(_HEAD_CHAR * _WIDTH)
else: else:
self.caller.msg("Sorry, you don't have any messages. What a pathetic loser!") self.caller.msg("Sorry, you don't have any messages. What a pathetic loser!")
def get_all_mail(self): def get_all_mail(self):
""" """
Returns a list of all the messages where the caller is a recipient. Returns a list of all the messages where the caller is a recipient.
Returns: Returns:
messages (list): list of Msg objects. messages (list): list of Msg objects.
""" """
mail_messages = Msg.objects.get_by_tag(category="mail") # mail_messages = Msg.objects.get_by_tag(category="mail")
messages = [] # messages = []
messages = [m for m in mail_messages if self.caller.player in m.receivers] messages = Msg.objects.get_by_tag(category="mail", raw_queryset=True).filter(db_receivers_players=self.caller.player)
return messages return messages
def send_mail(self, recipients, subject, message, caller): def send_mail(self, recipients, subject, message, caller):
""" """
Function for sending new mail. Also useful for sending notifications from objects or systems. Function for sending new mail. Also useful for sending notifications from objects or systems.
Args: Args:
recipients (list): list of Player or character objects to receive the newly created mails. recipients (list): list of Player or character objects to receive the newly created mails.
subject (str): The header or subject of the message to be delivered. subject (str): The header or subject of the message to be delivered.