Esempio n. 1
0
	def load(self):
		log.msg(self, 'load')

		if os.path.exists(self.file):
			# Load
			with open(self.file, 'rb') as fp:
				next, data = mload(fp)

				log.msg(self, 'load', len(data[0]), len(data[1]), len(data[2]))

				self.next = self.counter(next)
				self.data = ((
					deque(tuple(To.fromDict(to) for to in data[0])), 
					[(priority, To.fromDict(to)) for priority, to in data[1]],
					[(after, To.fromDict(to)) for after, to in data[2]],
				))

				# Transform list into a heap
				heapify(self.data[1])
				heapify(self.data[2])

		log.msg(self, 'load ok')
Esempio n. 2
0
	def commands_mail(self, id, item):
		itemType = item.get('type', 'single')
		itemGroup = int(item.get('group')) if (item.has_key('group') and item['group']) else None

		response = (dict(
			counts = (dict(
				all=0,
				queued=0
			)),
			id=id
		))

		if itemType == 'single' or itemType == 'multiple':
			# Check
			if not 'to' in item:
				raise ReceiverError('Value "to" missing')

			if not isinstance(item['to'], (ListType, TupleType)):
				item['to'] = [item['to']]

			for to in item['to']:
				if not isinstance(to, DictType):
					raise ReceiverError('Value "to" must be dictonary type')

				if (not to['email']) or (not to['name']) or (int(to.get('priority', 0)) < 0):
					raise ReceiverError('Value "to" must have "email" and "name" fields')

				to['priority'] = min(QUEUE_MAX_PRIORITY, int(to.get('priority', 0)))
				to['priority'] = (-(to['priority'] - QUEUE_MAX_PRIORITY) + 1) if to['priority'] > 0 else 0

				# Create parts
				if not 'parts' in to or not to['parts']:
					to['parts'] = dict()

				if not isinstance(to['parts'], DictType):
					raise ReceiverError('Value "parts" in "to" must be dictonary type')

				if 'delay' in to:
					if to['delay']:
						to['after'] = int(reactor.seconds() + to['delay'])

					# Clean
					del to['delay']

			if not isinstance(item['message'], DictType):
				raise ReceiverError('Value "message" must be dictonary type')

			if not 'id' in item['message']:
				if (not item['message'].get('subject')):
					raise ReceiverError('Value "message" must be contains "subject" field')

				if (not item['message'].get('html')) and (not item['message'].get('text')):
					raise ReceiverError('Value "message" must be contains "html" and/or "text" fields')

				if (not item['message'].get('sender')):
					raise ReceiverError('Value "message" must be contains "sender" field')

				if not isinstance(item['message']['sender'], DictType):
					raise ReceiverError('Value "message" field "sender" must be dictonary type')

				if item['message'].get('headers') and not isinstance(item['message']['headers'], DictType):
					raise ReceiverError('Value "message" field "headers" must be dictonary type')

			messageId = item['message']['id'] if 'id' in item['message'] else None
			messageId = int(messageId) if messageId is not None else None

			# Create message
			if messageId is None:
				messageParams = dict((

				))

				# Add params
				if item['message'].get('headers'):
					messageParams['headers'] = item['message']['headers']
				else:
					messageParams['headers'] = dict()

				# Clean
				if 'headers' in item['message']:
					del item['message']['headers']

				message = Message.fromDict(dict(id=messages.id, **item['message']))
				message.params = messageParams

				# Add to database
				messageId = messages.add(message)
			else:
				message = messages.get(messageId)
				if message is None:
					raise ReceiverError('Value "message" {0} not found'.format(messageId))

			if itemGroup:
				# Check group
				group = groups.get(itemGroup)
				if group is None or group.status == GROUP_STATUS_INACTIVE:
					raise ReceiverError('Value "group" {0} not found or inactive'.format(itemGroup))
			else:
				group = None

			for to in item['to']:
				to = To.fromDict(dict(id=tos.id, **to))
				to.message = messageId

				if group is not None:
					to.group = group.id

				# Add to database
				toId = tos.add(to)

				# Counts
				response['counts']['all'] += 1
				response['counts']['queued'] += 1

			if response['counts']['queued'] > 0:
				# Update group
				if group is not None:
					group.all += response['counts']['queued']
					group.wait += response['counts']['queued']

				# Update message
				message.tos += response['counts']['queued']

			self.send(response)
		else:
			self.send(dict(
				error='Unknown type "{0}"'.format(itemType),
				id=itemId,
			))