chainsol

evennia_decryption_channel.py

Mar 6th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.40 KB | None | 0 0
  1. # encrypted_channels.py
  2. from evennia import DefaultChannel
  3.  
  4. class EncryptedChannel(DefaultChannel):
  5.     def msg(self, msgobj, header=None, senders=None, sender_strings=None,
  6.             keep_log=None, online=False, emit=False, external=False):
  7.         """
  8.        Send the given message to all accounts connected to channel. Note that
  9.        no permission-checking is done here; it is assumed to have been
  10.        done before calling this method. The optional keywords are not used if
  11.        persistent is False.
  12.        Args:
  13.            msgobj (Msg, TempMsg or str): If a Msg/TempMsg, the remaining
  14.                keywords will be ignored (since the Msg/TempMsg object already
  15.                has all the data). If a string, this will either be sent as-is
  16.                (if persistent=False) or it will be used together with `header`
  17.                and `senders` keywords to create a Msg instance on the fly.
  18.            header (str, optional): A header for building the message.
  19.            senders (Object, Account or list, optional): Optional if persistent=False, used
  20.                to build senders for the message.
  21.            sender_strings (list, optional): Name strings of senders. Used for external
  22.                connections where the sender is not an account or object.
  23.                When this is defined, external will be assumed.
  24.            keep_log (bool or None, optional): This allows to temporarily change the logging status of
  25.                this channel message. If `None`, the Channel's `keep_log` Attribute will
  26.                be used. If `True` or `False`, that logging status will be used for this
  27.                message only (note that for unlogged channels, a `True` value here will
  28.                create a new log file only for this message).
  29.            online (bool, optional) - If this is set true, only messages people who are
  30.                online. Otherwise, messages all accounts connected. This can
  31.                make things faster, but may not trigger listeners on accounts
  32.                that are offline.
  33.            emit (bool, optional) - Signals to the message formatter that this message is
  34.                not to be directly associated with a name.
  35.            external (bool, optional): Treat this message as being
  36.                agnostic of its sender.
  37.        Returns:
  38.            success (bool): Returns `True` if message sending was
  39.                successful, `False` otherwise.
  40.        """
  41.         senders = make_iter(senders) if senders else []
  42.         if isinstance(msgobj, basestring):
  43.             # given msgobj is a string - convert to msgobject (always TempMsg)
  44.             if senders and hasattr(senders[0], "db"):
  45.                 __channel_passwords = senders[0].db.__channel_passwords
  46.                 if type(__channel_passwords) is dict:
  47.                     password = __channel_passwords.get(self)
  48.                     lockstring = "read:decrypt({})".format(password) if password else "read:true()"
  49.             msgobj = TempMsg(senders=senders, header=header, message=msgobj, channels=[self], lockstring=lockstring or "read:true()")
  50.         # we store the logging setting for use in distribute_message()
  51.         msgobj.keep_log = keep_log if keep_log is not None else self.db.keep_log
  52.  
  53.         # start the sending
  54.         msgobj = self.pre_send_message(msgobj)
  55.         if not msgobj:
  56.             return False
  57.         msgobj = self.message_transform(msgobj, emit=emit,
  58.                                         sender_strings=sender_strings,
  59.                                         external=external)
  60.         self.distribute_message(msgobj, online=online)
  61.         self.post_send_message(msgobj)
  62.         return True
  63.  
  64.     def distribute_message(self, msgobj, online=False, **kwargs):
  65.         """
  66.        Method for grabbing all listeners that a message should be
  67.        sent to on this channel, and sending them a message.
  68.        Args:
  69.            msgobj (Msg or TempMsg): Message to distribute.
  70.            online (bool): Only send to receivers who are actually online
  71.                (not currently used):
  72.            **kwargs (dict): Arbitrary, optional arguments for users
  73.                overriding the call (unused by default).
  74.        Notes:
  75.            This is also where logging happens, if enabled.
  76.        """
  77.         # get all accounts or objects connected to this channel and send to them
  78.         if online:
  79.             subs = self.subscriptions.online()
  80.         else:
  81.             subs = self.subscriptions.all()
  82.         for entity in subs:
  83.             # if the entity is muted, we don't send them a message
  84.             if entity in self.mutelist:
  85.                 continue
  86.             try:
  87.                 # note our addition of the from_channel keyword here. This could be checked
  88.                 # by a custom account.msg() to treat channel-receives differently.
  89.                 if msgobj.access(entity, "read"):
  90.                     entity.msg(msgobj.message, from_obj=msgobj.senders, options={"from_channel": self.id})
  91.                 else:
  92.                     entity.msg("[scrambled message]", from_obj=msgobj.senders, options={"from_channel": self.id})
  93.             except AttributeError as e:
  94.                 logger.log_trace("%s\nCannot send msg to '%s'." % (e, entity))
  95.  
  96.         if msgobj.keep_log:
  97.             # log to file
  98.             logger.log_file(msgobj.message, self.attributes.get("log_file") or "channel_%s.log" % self.key)
Advertisement
Add Comment
Please, Sign In to add comment