forked from IF-LK-2020/mymail
Added a bunch of stuff
This commit is contained in:
50
MyMail.java
50
MyMail.java
@@ -75,8 +75,7 @@ public class MyMail {
|
||||
*/
|
||||
private boolean connectToServer() {
|
||||
// TODO: Connection-Objekt initialisieren
|
||||
con = null;
|
||||
|
||||
con = new Connection(ip, port);
|
||||
// Begrüssungsnachricht des Servers empfangen und prüfen
|
||||
String mes = con.receive();
|
||||
if( mes == null || mes.startsWith("-ERR") ) {
|
||||
@@ -106,13 +105,24 @@ public class MyMail {
|
||||
* @link https://datatracker.ietf.org/doc/html/rfc1939#page-13
|
||||
*/
|
||||
private boolean login() {
|
||||
String mes = ""; // Speicher für die Antworten des Servers
|
||||
|
||||
|
||||
// TODO: Implementieren
|
||||
// USER Kommando senden und Antwort prüfen
|
||||
con.send("USER " + user);
|
||||
String mes = con.receive(); // Speicher für die Antworten des Servers
|
||||
if(mes == null || mes.startsWith("-ERR") ){
|
||||
lastError = "Fehler beim abfragen des Users!";
|
||||
return false;
|
||||
}
|
||||
|
||||
// PASS Kommando senden und Antwort prüfen
|
||||
|
||||
con.send("Pass " + password);
|
||||
mes = con.receive(); // Speicher für die Antworten des Servers
|
||||
if(mes == null || mes.startsWith("-ERR") ){
|
||||
lastError = "Fehler beim Einloggen!";
|
||||
return false;
|
||||
}
|
||||
// Anmeldung war erfolgreich
|
||||
return true;
|
||||
}
|
||||
@@ -128,8 +138,7 @@ public class MyMail {
|
||||
* @link https://datatracker.ietf.org/doc/html/rfc1939#page-6
|
||||
*/
|
||||
public int getMessageCount() {
|
||||
String mes = "";
|
||||
|
||||
|
||||
// TODO: Implementieren
|
||||
// Sende den STAT Befehl
|
||||
// Prüfe, ob die Antwort ein Fehler ist
|
||||
@@ -137,7 +146,15 @@ public class MyMail {
|
||||
// Finde die Leerzeichen mit indexOf(" ")
|
||||
// Ermittele den Text mit substring(von, bis)
|
||||
// Wandele mit Integer.parseInt() in eine Zahl um
|
||||
return 0;
|
||||
//Bei einem Fehler soll -1 zurückgegeben werden und eine
|
||||
//sinnvolle antwort zurückgegeben werden
|
||||
con.send("STAT");
|
||||
String mes = con.receive(); // Speicher für die Antworten des Servers
|
||||
if(mes == null || mes.startsWith("-ERR") ){
|
||||
lastError = "Fehler beim Abfragen der Mails!";
|
||||
return -1;
|
||||
}
|
||||
return Integer.parseInt(mes.substring(4,6));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,12 +176,22 @@ public class MyMail {
|
||||
|
||||
// Anmeldung durchführen und prüfen
|
||||
// Hinweis: Nutze die login() Methode
|
||||
if(!login()){
|
||||
gui.setError(lastError);
|
||||
return;
|
||||
}
|
||||
|
||||
// Anzahl Mails auf dem Server abfragen
|
||||
int mailCount = 0;
|
||||
int mailCount = getMessageCount();
|
||||
if(mailCount <= 0){
|
||||
gui.setError(lastError);
|
||||
return;
|
||||
}
|
||||
|
||||
// Abruf der Mails nach fortlaufender Nummer
|
||||
for( int i = 1; i <= mailCount; i++ ) {
|
||||
|
||||
|
||||
// Mail abrufen (nutze getMail(int))
|
||||
// Mail der GUI hinzufügen (nutze gui.addMailToLost(Mail))
|
||||
}
|
||||
@@ -196,7 +223,12 @@ public class MyMail {
|
||||
* @return Ein Mail-Objekt oder {@code null}.
|
||||
*/
|
||||
public Mail getMail( int pNumber ) {
|
||||
String mes = ""; // Speicher für Antworten des Servers
|
||||
con.send("RETR " + pNumber);
|
||||
String mes = con.receive(); // Speicher für Antworten des Servers
|
||||
if(mes == null || mes.startsWith("-ERR")){
|
||||
lastError = "Fehler beim abfragen der Mail!";
|
||||
return null;
|
||||
}
|
||||
|
||||
// Prüfen, ob es eine Mail mit der Nummer pNumber gibt
|
||||
|
||||
|
||||
Reference in New Issue
Block a user