def send_message(self, from_name, recipients, subject, body): """ Create a Message object from the mailbox owner and save it. :param from_name: The name to use for the sender. :type from_name: str :param recipients: Who is to receive the message. Keys are recipient id numbers and values are strings identifying them (or None to allow Message object to obtain name itself). :type recipients: dict of (int, str) :param subject: The subject of the message. :type subject: str :param body: The body of the message. :type body: str :rtype: twisted.internet.defer.Deferred """ message = Message( self.mail_db, timestamp=unixtime(), from_id=self.recipient_id, from_name=from_name, subject=subject, body=body ) for recipient_id, recipient_name in recipients.iteritems(): message.add_recipient(recipient_id, recipient_name) d = self.mail_db.save_message(message) d.addCallback(self._notify_recipients) return d
def send_message(self, from_name, recipients, subject, body): """ Create a Message object from the mailbox owner and save it. :param from_name: The name to use for the sender. :type from_name: str :param recipients: Who is to receive the message. Keys are recipient id numbers and values are strings identifying them (or None to allow Message object to obtain name itself). :type recipients: dict of (int, str) :param subject: The subject of the message. :type subject: str :param body: The body of the message. :type body: str :rtype: twisted.internet.defer.Deferred """ message = Message(self.mail_db, timestamp=unixtime(), from_id=self.recipient_id, from_name=from_name, subject=subject, body=body) for recipient_id, recipient_name in recipients.iteritems(): message.add_recipient(recipient_id, recipient_name) d = self.mail_db.save_message(message) d.addCallback(self._notify_recipients) return d
def follow_invite(self, charlist): self._prune_allow_follow() newly_allowed = [] for char in charlist: if char not in self.allow_follow: newly_allowed.append(char) self.allow_follow[char] = time_utils.unixtime() if newly_allowed: names = RenderList(newly_allowed, format='{g%s{n') self.emit_message('follow_invite', actor=self.ref(), charlist=names) return newly_allowed
def _prune_allow_follow(self): if 'allow_follow' not in self.__dict__: self.allow_follow = {} remove = [] now = time_utils.unixtime() for char, timestamp in self.allow_follow.iteritems(): if now - timestamp > 120: remove.append(char) for char in remove: del self.allow_follow[char] return remove
def authenticate_request(request, authorizations=()): """ Called for a request that requires authentication via API key signature. Calls will result in request having additional attributes: - authenticated: bool - apikey: None or Player :param request: The request to authenticate. :type request: Request :return: Whether or not the request has passed authentication. :rtype: bool """ request.authenticated = False request.apikey = None signature = request.getHeader('Signature') if signature is not None: (key_id, nonce, signed) = signature.split(':') try: #: :type: restserver.APIKey apikey = get_api_key(key_id) except InvalidAPIKey: pass # Fall through to Access Denied else: authorized = True for authorization in authorizations: if not apikey.is_authorized(authorization): authorized = False break if authorized: request.apikey = apikey date_header = request.getHeader('Date') if apikey.valid and date_header: timestamp = stringToDatetime(date_header) offset = abs(unixtime() - timestamp) if offset <= 600: tosign = '\n'.join(( request.method, request.uri, date_header, nonce )) hashed = hmac.new(apikey.secret, tosign, sha1) verify = hashed.digest().encode('base64').rstrip('\n') if verify == signed: request.authenticated = True return True return False
def authenticate_request(request, authorizations=()): """ Called for a request that requires authentication via API key signature. Calls will result in request having additional attributes: - authenticated: bool - apikey: None or Player :param request: The request to authenticate. :type request: Request :return: Whether or not the request has passed authentication. :rtype: bool """ request.authenticated = False request.apikey = None signature = request.getHeader('Signature') if signature is not None: (key_id, nonce, signed) = signature.split(':') try: #: :type: restserver.APIKey apikey = get_api_key(key_id) except InvalidAPIKey: pass # Fall through to Access Denied else: authorized = True for authorization in authorizations: if not apikey.is_authorized(authorization): authorized = False break if authorized: request.apikey = apikey date_header = request.getHeader('Date') if apikey.valid and date_header: timestamp = stringToDatetime(date_header) offset = abs(unixtime() - timestamp) if offset <= 600: tosign = '\n'.join( (request.method, request.uri, date_header, nonce)) hashed = hmac.new(apikey.secret, tosign, sha1) verify = hashed.digest().encode('base64').rstrip('\n') if verify == signed: request.authenticated = True return True return False
def log(self, source, text): if self._log_enabled: self._init_logfile() if hasattr(source, 'obj_id'): if hasattr(source, 'name'): source = '%s (#%d)' % (source.name, source.obj_id) else: source = '#%d' % source.obj_id else: source = str(source) lines = [''.join(l.split(self.prefix, 1)) for l in text.splitlines()] logfile = self._logfile for line in lines: logfile.execute('INSERT INTO log (timestamp, source, text)' 'VALUES (?, ?, ?)', (time_utils.unixtime(), source, line)) logfile.commit()
def log(self, source, text): if self._log_enabled: self._init_logfile() if hasattr(source, 'obj_id'): if hasattr(source, 'name'): source = '%s (#%d)' % (source.name, source.obj_id) else: source = '#%d' % source.obj_id else: source = str(source) lines = [ ''.join(l.split(self.prefix, 1)) for l in text.splitlines() ] logfile = self._logfile for line in lines: logfile.execute( 'INSERT INTO log (timestamp, source, text)' 'VALUES (?, ?, ?)', (time_utils.unixtime(), source, line)) logfile.commit()
def _parse_filter_date(self, input): return unixtime(parse_datetime(input))