1
0
Fork 0
This commit is contained in:
Florian RICHER (MrDev023) 2022-04-14 22:48:15 +02:00
parent f2d808f5d3
commit 41b191bdd3
6 changed files with 80 additions and 30 deletions

View file

@ -5,9 +5,11 @@ enum StreamElementType { log, alert }
class StreamElement {
StreamElement(this.type, this.message) {
uuid = const Uuid().v4();
date = DateTime.now();
}
late final String uuid;
late final DateTime date;
final String message;
final StreamElementType type;
}

View file

@ -1,10 +1,17 @@
import 'dart:async';
import 'dart:math';
import 'package:desktopapp/classes/stream_element.dart';
import 'package:desktopapp/utils/logger.dart';
import 'package:desktopapp/utils/requests.dart';
import 'package:enough_mail/enough_mail.dart';
class Mailer {
static final List<String> customMessages = [
'Un gueux vient de commander de la HOLY',
'Un nouveau gueux vient de commander de la HOLY'
];
static final List<String> whiteListEmail = ['louis@holyenergy.fr'];
static MailClient? _mailClient;
/// High level mail API example
@ -32,29 +39,11 @@ class Mailer {
await _mailClient!.selectInbox();
streamController.add(
StreamElement(StreamElementType.log, 'INBOX selected for $email'));
List<String> whitelist = [email, ...whiteListEmail];
streamController.add(StreamElement(
StreamElementType.log, 'Listen for emails: ${whitelist.join(', ')}'));
_mailClient!.eventBus.on<ImapEvent>().listen((ImapEvent event) async {
if (event.eventType == ImapEventType.exists) {
final evt = event as ImapMessagesExistEvent;
if (evt.newMessagesExists <= evt.oldMessagesExists) return;
final sequence = MessageSequence();
if (evt.newMessagesExists - evt.oldMessagesExists > 1) {
sequence.addRange(evt.oldMessagesExists, evt.newMessagesExists);
} else {
sequence.add(evt.newMessagesExists);
}
final messages = await _mailClient!.fetchMessageSequence(sequence,
fetchPreference: FetchPreference.envelope);
for (final message in messages) {
var mailEvt = MailLoadEvent(message, _mailClient!);
streamController.add(StreamElement(StreamElementType.alert,
mailEvt.message.decodeSubject() ?? ''));
// var content = mailEvt.message.decodeSubject();
// var mess = mailEvt.message.decodeTextPlainPart();
// Logger.log(LoggerType.info, Mailer, 'New message $content : $mess');
// Requests.sendAlert('*$content* $mess');
}
}
await _onMessage(event, streamController, email, whitelist);
});
await _mailClient!.startPolling();
return true;
@ -65,12 +54,53 @@ class Mailer {
}
}
static Future<void> disconnect() async {
static Future<void> disconnect({
required StreamController<StreamElement> streamController,
}) async {
if (_mailClient == null) return;
Logger.log(LoggerType.info, Mailer, 'Disconnecting from mail');
await _mailClient!.stopPolling();
await _mailClient!.disconnect();
Logger.log(LoggerType.info, Mailer, 'Disconnected from mail');
_mailClient = null;
streamController
.add(StreamElement(StreamElementType.log, 'Disconnected from mail'));
}
static Future<void> _onMessage(
ImapEvent event,
StreamController streamController,
String email,
List<String> whitelist) async {
if (event.eventType == ImapEventType.exists) {
final evt = event as ImapMessagesExistEvent;
if (evt.newMessagesExists <= evt.oldMessagesExists) return;
final sequence = MessageSequence();
if (evt.newMessagesExists - evt.oldMessagesExists > 1) {
sequence.addRange(evt.oldMessagesExists, evt.newMessagesExists);
} else {
sequence.add(evt.newMessagesExists);
}
final messages = await _mailClient!.fetchMessageSequence(sequence,
fetchPreference: FetchPreference.envelope);
for (final message in messages) {
MailAddress? mail = message.from?.cast<MailAddress?>().firstWhere(
(mEmail) => whitelist.contains(mEmail!.email),
orElse: () => null);
if (mail == null) continue;
var mailEvt = MailLoadEvent(message, _mailClient!);
_onMailEvent(mailEvt, streamController);
}
}
}
static Future<void> _onMailEvent(
MailLoadEvent mailLoadEvent, StreamController streamController) async {
final message =
customMessages[Random.secure().nextInt(customMessages.length)];
await Requests.sendAlert(message);
streamController.add(StreamElement(StreamElementType.alert, message));
}
}

View file

@ -1,8 +1,11 @@
import 'package:desktopapp/classes/stream_element.dart';
import 'package:desktopapp/utils/requests.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class StreamElementCard extends StatelessWidget {
StreamElementCard({Key? key, required this.streamElement}) : super(key: key);
const StreamElementCard({Key? key, required this.streamElement})
: super(key: key);
final StreamElement streamElement;
@ -25,15 +28,29 @@ class StreamElementCard extends StatelessWidget {
children: [
Row(
children: [
Text(
DateFormat.jms().format(streamElement.date),
style: const TextStyle(fontSize: 16),
),
const SizedBox(width: 8),
Icon(
icon,
size: 30,
),
SizedBox(width: 8),
Text(
streamElement.message,
style: TextStyle(fontSize: 16),
const SizedBox(width: 8),
Expanded(
child: Text(
streamElement.message,
style: const TextStyle(fontSize: 16),
),
),
if (streamElement.type == StreamElementType.alert)
IconButton(
icon: const Icon(Icons.send),
onPressed: () async {
await Requests.sendAlert(streamElement.message);
},
),
],
)
],

View file

@ -43,7 +43,7 @@ class _HomePageState extends State<HomePage> {
children: [
TextButton(
onPressed: () async {
await Mailer.disconnect();
await Mailer.disconnect(streamController: controller);
setState(() {
connected = false;
});

View file

@ -206,7 +206,7 @@ packages:
source: hosted
version: "4.0.0"
intl:
dependency: transitive
dependency: "direct main"
description:
name: intl
url: "https://pub.dartlang.org"

View file

@ -41,6 +41,7 @@ dependencies:
dio: ^4.0.6
enough_mail: ^1.3.6
uuid: ^3.0.6
intl: ^0.17.0
dev_dependencies:
flutter_test: