MailFunctions.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.noahvogt.miniprojekt.data;
  2. import android.util.Patterns;
  3. import android.widget.EditText;
  4. import com.chaquo.python.PyObject;
  5. import com.chaquo.python.Python;
  6. import java.util.List;
  7. public class MailFunctions {
  8. public static boolean canConnect(String host, String email, String password) {
  9. Python python = Python.getInstance();
  10. PyObject pythonMailFunctions = python.getModule("mailFunctions");
  11. return pythonMailFunctions.callAttr("checkConnection", host, email, password, 993).toBoolean();
  12. }
  13. public static void sendStarttlsMail(String host, String sendingMail, String receivingMail, String password, String message, String subject, String cc, String bcc) {
  14. Python python = Python.getInstance();
  15. PyObject pythonMailFunctions = python.getModule("mailFunctions");
  16. pythonMailFunctions.callAttr("sendStarttls", host, sendingMail, receivingMail, password, message, subject, 587, cc, bcc);
  17. }
  18. public static PyObject getIMAPConnection(String host, String email, String password, int port) {
  19. Python python = Python.getInstance();
  20. PyObject pythonMailFunctions = python.getModule("mailFunctions");
  21. return pythonMailFunctions.callAttr("connect", host, email, password, port);
  22. }
  23. public static List listMailboxes(PyObject IMAPConnection) {
  24. Python python = Python.getInstance();
  25. PyObject pythonMailFunctions = python.getModule("mailFunctions");
  26. return pythonMailFunctions.callAttr("listMailboxes", IMAPConnection).asList();
  27. }
  28. public static String fetchMailsFromBox(PyObject IMAPConnection, String Folder) {
  29. Python python = Python.getInstance();
  30. PyObject pythonMailFunctions = python.getModule("mailFunctions");
  31. return pythonMailFunctions.callAttr("fetchMails", IMAPConnection, Folder).toString();
  32. }
  33. public static boolean validateName(EditText emailName) {
  34. String name = emailName.getText().toString().trim();
  35. if (name.isEmpty()) {
  36. emailName.setError("Field can't be empty");
  37. return false;
  38. } else if (name.length() > 50) {
  39. emailName.setError("Name too long");
  40. return false;
  41. } else {
  42. emailName.setError(null);
  43. return true;
  44. }
  45. }
  46. public static String getImapHostFromEmail(String email) {
  47. String topLevelHost = email.substring(email.lastIndexOf("@") + 1);
  48. if (topLevelHost.endsWith("edubs.ch")) {
  49. return "teamwork.edubs.ch";
  50. }else if (topLevelHost.endsWith("yahoo.com")){
  51. return "imap.mail.yahoo.com";
  52. } else if (topLevelHost.endsWith("gmx.ch")){
  53. return "imap.gmx.net";
  54. } else if (topLevelHost.endsWith("gmx.de")){
  55. return "imap.gmx.net";
  56. }
  57. else {
  58. return "imap." + topLevelHost;
  59. }
  60. }
  61. public static String getSmtpHostFromEmail(String email) {
  62. String topLevelHost = email.substring(email.lastIndexOf("@") + 1);
  63. if (topLevelHost.equals("noahvogt.com")) {
  64. return "mail.noahvogt.com";
  65. } else if (topLevelHost.endsWith("yahoo.com")){
  66. return "smtp.mail.yahoo.com";
  67. }else if (topLevelHost.endsWith("gmx.ch")){
  68. return "mail.gmx.net";
  69. }else if (topLevelHost.endsWith("gmx.de")) {
  70. return "mail.gmx.net";
  71. }
  72. else {
  73. return "smtp." + topLevelHost;
  74. }
  75. }
  76. public static boolean validateEmail(EditText emailAddress) {
  77. String email = emailAddress.getText().toString().trim();
  78. if (email.isEmpty()) {
  79. emailAddress.setError("Field can't be empty");
  80. return false;
  81. } else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
  82. emailAddress.setError("Please enter a valid email address");
  83. return false;
  84. } else {
  85. emailAddress.setError(null);
  86. return true;
  87. }
  88. }
  89. public static boolean validatePassword(EditText emailPassword) {
  90. String password = emailPassword.getText().toString().trim();
  91. if (password.isEmpty()) {
  92. emailPassword.setError("Field can't be empty");
  93. return false;
  94. } else {
  95. emailPassword.setError(null);
  96. return true;
  97. }
  98. }
  99. public static boolean validateSubject(EditText emailSubject) {
  100. String subject = emailSubject.getText().toString();
  101. /* TODO: check email protocol specification for what is allowed for subjects */
  102. return true;
  103. }
  104. public static boolean validateMessageBody(EditText emailMessageBody) {
  105. String messageBody = emailMessageBody.getText().toString();
  106. /* TODO: check email protocol specification for what is allowed for message bodies */
  107. return true;
  108. }
  109. public static boolean checkForSameEmail(EditText firstAddress, EditText secondAddress) {
  110. String firstEmail = firstAddress.getText().toString();
  111. String secondEmail = secondAddress.getText().toString();
  112. if (firstEmail.equals(secondEmail)) {
  113. firstAddress.setError("Fields cannot be the same");
  114. secondAddress.setError("Fields cannot be the same");
  115. return true;
  116. } else {
  117. firstAddress.setError(null);
  118. secondAddress.setError(null);
  119. return false;
  120. }
  121. }
  122. }