2020-12-05 18:47:13 +01:00
using Hermes.Model ;
using Newtonsoft.Json ;
using System ;
using System.Collections.Generic ;
using System.Collections.ObjectModel ;
2020-12-28 00:24:12 +01:00
using System.IO ;
using System.Net ;
using System.Security.Cryptography ;
using System.Text ;
using System.Text.RegularExpressions ;
2020-12-05 18:47:13 +01:00
using System.Windows ;
using System.Windows.Documents ;
2021-01-01 16:28:38 +01:00
using System.Windows.Input ;
2020-12-05 12:29:30 +01:00
namespace Hermes {
public partial class SmsWindow : Window {
2020-12-05 18:47:13 +01:00
private ObservableCollection < SmsSendingStatus > status = new ObservableCollection < SmsSendingStatus > ( ) ;
private bool sending = false ;
2020-12-28 18:56:26 +01:00
private static Regex telFrRgx = new Regex ( @"^0\d{9}" ) ;
private static Regex telFrPrefixRgx = new Regex ( @"^0" ) ;
2020-12-28 00:24:12 +01:00
public static string HashSHA1 ( string input ) {
SHA1 sha = SHA1 . Create ( ) ;
byte [ ] bHash = sha . ComputeHash ( Encoding . UTF8 . GetBytes ( input ) ) ;
StringBuilder sBuilder = new StringBuilder ( ) ;
for ( int i = 0 ; i < bHash . Length ; i + + ) {
sBuilder . Append ( bHash [ i ] . ToString ( "x2" ) ) ;
}
return sBuilder . ToString ( ) ;
}
2020-12-05 18:47:13 +01:00
2020-12-06 21:54:59 +01:00
public SmsWindow ( Window parent , List < Citoyen > rcps ) {
2020-12-05 12:29:30 +01:00
InitializeComponent ( ) ;
2020-12-06 21:54:59 +01:00
Owner = parent ;
foreach ( Citoyen rcp in rcps ) {
SmsSendingStatus s = new SmsSendingStatus ( ) ;
s . Nom = rcp . Nom ;
s . Prenom = rcp . Prenom ;
2020-12-28 18:56:26 +01:00
s . Mobile = rcp . TelPort . Replace ( " " , "" ) . Replace ( "." , "" ) ;
Match m = telFrRgx . Match ( s . Mobile ) ;
if ( m . Success ) {
// numéro FR (+33)
s . Mobile = telFrPrefixRgx . Replace ( s . Mobile , "+33" ) ;
}
2020-12-06 21:54:59 +01:00
s . Status = "En attente" ;
status . Add ( s ) ;
}
2020-12-05 12:29:30 +01:00
2020-12-05 18:47:13 +01:00
lvLog . ItemsSource = status ;
}
2020-12-05 12:29:30 +01:00
private void Window_Closing ( object sender , System . ComponentModel . CancelEventArgs e ) {
2020-12-05 18:47:13 +01:00
if ( sending ) {
2020-12-06 21:54:59 +01:00
e . Cancel = true ;
2020-12-05 18:47:13 +01:00
return ;
}
}
2020-12-05 12:29:30 +01:00
public void Envoyer_Click ( object sender , RoutedEventArgs e ) {
2020-12-06 21:54:59 +01:00
ModelContext dbContext = ModelContext . Getinstance ( ) ;
2020-12-05 18:47:13 +01:00
bool error = false ;
string message = new TextRange ( messageTextBox . Document . ContentStart , messageTextBox . Document . ContentEnd ) . Text ;
2020-12-28 00:24:12 +01:00
Preferences pref = dbContext . Preferences . Local [ 0 ] ;
2020-12-05 18:47:13 +01:00
2020-12-28 18:56:26 +01:00
if ( String . IsNullOrWhiteSpace ( pref . ovhSmsServiceName ) | | String . IsNullOrWhiteSpace ( pref . ovhSmsApplicationKey )
| | String . IsNullOrWhiteSpace ( pref . ovhSmsApplicationSecret ) | | String . IsNullOrWhiteSpace ( pref . ovhSmsConsumerKey ) ) {
2020-12-28 00:24:12 +01:00
MessageBox . Show ( "Erreur lors de la tentative d'envoi : Clés d'accès invalides." , "Envoi de SMS" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
2020-12-05 18:47:13 +01:00
return ;
}
if ( String . IsNullOrWhiteSpace ( message ) ) {
MessageBox . Show ( "Votre message est vide." , "Envoi de SMS" , MessageBoxButton . OK , MessageBoxImage . Warning ) ;
return ;
}
envoyerButton . IsEnabled = false ;
annulerButton . IsEnabled = false ;
messageTextBox . IsEnabled = false ;
sending = true ;
2021-01-01 16:28:38 +01:00
Cursor previousCursor = Mouse . OverrideCursor ;
Mouse . OverrideCursor = Cursors . Wait ;
2020-12-05 18:47:13 +01:00
2020-12-28 00:24:12 +01:00
string query = $"https://eu.api.ovh.com/1.0/sms/{pref.ovhSmsServiceName}/jobs" ;
2020-12-06 21:54:59 +01:00
2020-12-05 18:47:13 +01:00
foreach ( SmsSendingStatus stat in status ) {
if ( ! stat . Status . Equals ( "Envoyé" ) ) {
2020-12-28 00:24:12 +01:00
OVHQueryBody body = new OVHQueryBody ( ) ;
body . charset = "UTF-8" ;
body . receivers = new string [ ] { stat . Mobile } ;
body . message = message ;
body . priority = "high" ;
body . senderForResponse = true ;
string bodyStr = JsonConvert . SerializeObject ( body ) ;
string ts = ( ( Int32 ) DateTime . UtcNow . Subtract ( new DateTime ( 1970 , 1 , 1 ) ) . TotalSeconds ) . ToString ( ) ;
2020-12-28 18:56:26 +01:00
string signature = "$1$" + HashSHA1 ( pref . ovhSmsApplicationSecret + "+" + pref . ovhSmsConsumerKey + "+" + "POST" + "+" + query + "+" + bodyStr + "+" + ts ) ;
2020-12-28 00:24:12 +01:00
2021-01-01 16:28:38 +01:00
try {
HttpWebRequest req = ( HttpWebRequest ) HttpWebRequest . Create ( query ) ;
req . Method = "POST" ;
req . ContentType = "application/json" ;
req . Headers . Add ( "X-Ovh-Application:" + pref . ovhSmsApplicationKey ) ;
req . Headers . Add ( "X-Ovh-Consumer:" + pref . ovhSmsConsumerKey ) ;
req . Headers . Add ( "X-Ovh-Signature:" + signature ) ;
req . Headers . Add ( "X-Ovh-Timestamp:" + ts ) ;
using ( System . IO . Stream s = req . GetRequestStream ( ) ) {
using ( System . IO . StreamWriter sw = new System . IO . StreamWriter ( s ) ) {
sw . Write ( bodyStr ) ;
}
2020-12-28 00:24:12 +01:00
}
HttpWebResponse resp = ( HttpWebResponse ) req . GetResponse ( ) ;
using ( var stream = resp . GetResponseStream ( ) ) {
var reader = new StreamReader ( stream ) ;
OVHResponseBody rb = JsonConvert . DeserializeObject < OVHResponseBody > ( reader . ReadToEnd ( ) . Trim ( ) ) ;
if ( rb . validReceivers . Length > 0 & & rb . validReceivers [ 0 ] ! = null & & rb . validReceivers [ 0 ] . Equals ( stat . Mobile ) ) {
2020-12-05 18:47:13 +01:00
stat . Status = "Envoyé" ;
} else {
error = true ;
2020-12-28 00:24:12 +01:00
stat . Status = "Numéro de mobile invalide" ;
2020-12-05 18:47:13 +01:00
}
}
2020-12-28 00:24:12 +01:00
resp . Close ( ) ;
} catch ( WebException ex ) {
2020-12-05 18:47:13 +01:00
error = true ;
2020-12-28 00:24:12 +01:00
WebResponse resp = ex . Response ;
2021-01-01 17:30:11 +01:00
if ( resp = = null ) {
string errorMsg = ex . Message = = null ? "" : ex . Message ;
stat . Status = $"Erreur de transmission : {errorMsg}" ;
continue ;
}
try {
using ( var stream = resp . GetResponseStream ( ) ) {
StreamReader reader = new StreamReader ( stream ) ;
String result = reader . ReadToEnd ( ) . Trim ( ) ;
stat . Status = result ;
}
} catch ( Exception ) {
stat . Status = $"Erreur de transmission" ;
2020-12-05 18:47:13 +01:00
}
2021-01-01 16:28:38 +01:00
} catch ( Exception ex ) {
error = true ;
string errorMsg = ex . Message = = null ? "" : ex . Message ;
stat . Status = $"Erreur de transmission : {errorMsg}" ;
2020-12-05 18:47:13 +01:00
}
}
}
envoyerButton . IsEnabled = true ;
annulerButton . IsEnabled = true ;
messageTextBox . IsEnabled = true ;
sending = false ;
2021-01-01 16:28:38 +01:00
Mouse . OverrideCursor = previousCursor ;
2020-12-05 18:47:13 +01:00
if ( error ) {
2021-01-01 17:30:11 +01:00
MessageBox . Show ( "Plusieurs envois se sont mal déroulés. Certains numéros de téléphone ne sont peut-être pas valides." , "Envoi de SMS" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
2020-12-05 18:47:13 +01:00
}
2020-12-05 12:29:30 +01:00
}
public void Annuler_Click ( object sender , RoutedEventArgs e ) {
Close ( ) ;
}
}
}