From 41b191bdd382338231ee0a368a1ecfdca9221509 Mon Sep 17 00:00:00 2001 From: "Florian RICHER (MrDev023)" Date: Thu, 14 Apr 2022 22:48:15 +0200 Subject: [PATCH] Update --- lib/classes/stream_element.dart | 2 + lib/utils/mailer.dart | 76 +++++++++++++------ .../components/stream_element_card.dart | 27 +++++-- lib/widgets/pages/home.dart | 2 +- pubspec.lock | 2 +- pubspec.yaml | 1 + 6 files changed, 80 insertions(+), 30 deletions(-) diff --git a/lib/classes/stream_element.dart b/lib/classes/stream_element.dart index 1e0c9f0..716c53a 100644 --- a/lib/classes/stream_element.dart +++ b/lib/classes/stream_element.dart @@ -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; } diff --git a/lib/utils/mailer.dart b/lib/utils/mailer.dart index e8d5fcb..73c1f08 100644 --- a/lib/utils/mailer.dart +++ b/lib/utils/mailer.dart @@ -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 customMessages = [ + 'Un gueux vient de commander de la HOLY', + 'Un nouveau gueux vient de commander de la HOLY' + ]; + static final List 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 whitelist = [email, ...whiteListEmail]; + streamController.add(StreamElement( + StreamElementType.log, 'Listen for emails: ${whitelist.join(', ')}')); _mailClient!.eventBus.on().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 disconnect() async { + static Future disconnect({ + required StreamController 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 _onMessage( + ImapEvent event, + StreamController streamController, + String email, + List 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().firstWhere( + (mEmail) => whitelist.contains(mEmail!.email), + orElse: () => null); + + if (mail == null) continue; + + var mailEvt = MailLoadEvent(message, _mailClient!); + _onMailEvent(mailEvt, streamController); + } + } + } + + static Future _onMailEvent( + MailLoadEvent mailLoadEvent, StreamController streamController) async { + final message = + customMessages[Random.secure().nextInt(customMessages.length)]; + await Requests.sendAlert(message); + streamController.add(StreamElement(StreamElementType.alert, message)); } } diff --git a/lib/widgets/components/stream_element_card.dart b/lib/widgets/components/stream_element_card.dart index de4055b..5270dd2 100644 --- a/lib/widgets/components/stream_element_card.dart +++ b/lib/widgets/components/stream_element_card.dart @@ -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); + }, + ), ], ) ], diff --git a/lib/widgets/pages/home.dart b/lib/widgets/pages/home.dart index c233463..4cfc12b 100644 --- a/lib/widgets/pages/home.dart +++ b/lib/widgets/pages/home.dart @@ -43,7 +43,7 @@ class _HomePageState extends State { children: [ TextButton( onPressed: () async { - await Mailer.disconnect(); + await Mailer.disconnect(streamController: controller); setState(() { connected = false; }); diff --git a/pubspec.lock b/pubspec.lock index c6b8b1f..f1ff375 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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" diff --git a/pubspec.yaml b/pubspec.yaml index f6b501c..6024654 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: