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 { class StreamElement {
StreamElement(this.type, this.message) { StreamElement(this.type, this.message) {
uuid = const Uuid().v4(); uuid = const Uuid().v4();
date = DateTime.now();
} }
late final String uuid; late final String uuid;
late final DateTime date;
final String message; final String message;
final StreamElementType type; final StreamElementType type;
} }

View file

@ -1,10 +1,17 @@
import 'dart:async'; import 'dart:async';
import 'dart:math';
import 'package:desktopapp/classes/stream_element.dart'; import 'package:desktopapp/classes/stream_element.dart';
import 'package:desktopapp/utils/logger.dart'; import 'package:desktopapp/utils/logger.dart';
import 'package:desktopapp/utils/requests.dart';
import 'package:enough_mail/enough_mail.dart'; import 'package:enough_mail/enough_mail.dart';
class Mailer { 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; static MailClient? _mailClient;
/// High level mail API example /// High level mail API example
@ -32,7 +39,39 @@ class Mailer {
await _mailClient!.selectInbox(); await _mailClient!.selectInbox();
streamController.add( streamController.add(
StreamElement(StreamElementType.log, 'INBOX selected for $email')); 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 { _mailClient!.eventBus.on<ImapEvent>().listen((ImapEvent event) async {
await _onMessage(event, streamController, email, whitelist);
});
await _mailClient!.startPolling();
return true;
} on MailException catch (e) {
streamController.add(StreamElement(StreamElementType.log, e.toString()));
Logger.log(LoggerType.error, Mailer, 'High level API failed with $e');
return false;
}
}
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) { if (event.eventType == ImapEventType.exists) {
final evt = event as ImapMessagesExistEvent; final evt = event as ImapMessagesExistEvent;
if (evt.newMessagesExists <= evt.oldMessagesExists) return; if (evt.newMessagesExists <= evt.oldMessagesExists) return;
@ -45,32 +84,23 @@ class Mailer {
final messages = await _mailClient!.fetchMessageSequence(sequence, final messages = await _mailClient!.fetchMessageSequence(sequence,
fetchPreference: FetchPreference.envelope); fetchPreference: FetchPreference.envelope);
for (final message in messages) { 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!); var mailEvt = MailLoadEvent(message, _mailClient!);
streamController.add(StreamElement(StreamElementType.alert, _onMailEvent(mailEvt, streamController);
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 _mailClient!.startPolling();
return true;
} on MailException catch (e) {
streamController.add(StreamElement(StreamElementType.log, e.toString()));
Logger.log(LoggerType.error, Mailer, 'High level API failed with $e');
return false;
}
} }
static Future<void> disconnect() async { static Future<void> _onMailEvent(
if (_mailClient == null) return; MailLoadEvent mailLoadEvent, StreamController streamController) async {
Logger.log(LoggerType.info, Mailer, 'Disconnecting from mail'); final message =
await _mailClient!.stopPolling(); customMessages[Random.secure().nextInt(customMessages.length)];
await _mailClient!.disconnect(); await Requests.sendAlert(message);
Logger.log(LoggerType.info, Mailer, 'Disconnected from mail'); streamController.add(StreamElement(StreamElementType.alert, message));
_mailClient = null;
} }
} }

View file

@ -1,8 +1,11 @@
import 'package:desktopapp/classes/stream_element.dart'; import 'package:desktopapp/classes/stream_element.dart';
import 'package:desktopapp/utils/requests.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class StreamElementCard extends StatelessWidget { 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; final StreamElement streamElement;
@ -25,14 +28,28 @@ class StreamElementCard extends StatelessWidget {
children: [ children: [
Row( Row(
children: [ children: [
Text(
DateFormat.jms().format(streamElement.date),
style: const TextStyle(fontSize: 16),
),
const SizedBox(width: 8),
Icon( Icon(
icon, icon,
size: 30, size: 30,
), ),
SizedBox(width: 8), const SizedBox(width: 8),
Text( Expanded(
child: Text(
streamElement.message, streamElement.message,
style: TextStyle(fontSize: 16), 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: [ children: [
TextButton( TextButton(
onPressed: () async { onPressed: () async {
await Mailer.disconnect(); await Mailer.disconnect(streamController: controller);
setState(() { setState(() {
connected = false; connected = false;
}); });

View file

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

View file

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