You've already forked Mailu
							
							
				mirror of
				https://github.com/Mailu/Mailu.git
				synced 2025-10-30 23:37:43 +02:00 
			
		
		
		
	Display fetchmail errors to the user, fixes #23
This commit is contained in:
		| @@ -208,3 +208,5 @@ class Fetch(Base): | ||||
|     tls = db.Column(db.Boolean(), nullable=False) | ||||
|     username = db.Column(db.String(255), nullable=False) | ||||
|     password = db.Column(db.String(255), nullable=False) | ||||
|     last_check = db.Column(db.DateTime, nullable=True) | ||||
|     error = db.Column(db.String(1023), nullable=True) | ||||
|   | ||||
| @@ -17,11 +17,10 @@ Fetched accounts | ||||
|   <tbody> | ||||
|     <tr> | ||||
|       <th>Actions</th> | ||||
|       <th>Protocol</th> | ||||
|       <th>Hostname</th> | ||||
|       <th>Port</th> | ||||
|       <th>TLS</th> | ||||
|       <th>Endpoint</th> | ||||
|       <th>Username</th> | ||||
|       <th>Last check</th> | ||||
|       <th>Status</th> | ||||
|       <th>Created</th> | ||||
|       <th>Last edit</th> | ||||
|     </tr> | ||||
| @@ -31,11 +30,10 @@ Fetched accounts | ||||
|         <a href="{{ url_for('.fetch_edit', fetch_id=fetch.id) }}" title="Edit"><i class="fa fa-pencil"></i></a>  | ||||
|         <a href="{{ url_for('.fetch_delete', fetch_id=fetch.id) }}" title="Delete"><i class="fa fa-trash"></i></a> | ||||
|       </td> | ||||
|       <td>{{ fetch.protocol }}</td> | ||||
|       <td>{{ fetch.host }}</td> | ||||
|       <td>{{ fetch.port }}</td> | ||||
|       <td>{{ fetch.tls }}</td> | ||||
|       <td>{{ fetch.protocol }}{{ 's' if fetch.tls else '' }}://{{ fetch.host }}:{{ fetch.port }}</td> | ||||
|       <td>{{ fetch.username }}</td> | ||||
|       <td>{{ fetch.last_check or '-' }}</td> | ||||
|       <td>{{ fetch.error or '-' }}</td> | ||||
|       <td>{{ fetch.created_at }}</td> | ||||
|       <td>{{ fetch.updated_at or '' }}</td> | ||||
|     </tr> | ||||
|   | ||||
							
								
								
									
										25
									
								
								admin/migrations/versions/dc8c25cf5b98_.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								admin/migrations/versions/dc8c25cf5b98_.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| """ Add metadata related to fetches | ||||
|  | ||||
| Revision ID: dc8c25cf5b98 | ||||
| Revises: a4accda8a8c7 | ||||
| Create Date: 2016-09-10 12:41:01.161357 | ||||
|  | ||||
| """ | ||||
|  | ||||
| # revision identifiers, used by Alembic. | ||||
| revision = 'dc8c25cf5b98' | ||||
| down_revision = 'a4accda8a8c7' | ||||
|  | ||||
| from alembic import op | ||||
| import sqlalchemy as sa | ||||
|  | ||||
|  | ||||
| def upgrade(): | ||||
|     op.add_column('fetch', sa.Column('error', sa.String(length=1023), nullable=True)) | ||||
|     op.add_column('fetch', sa.Column('last_check', sa.DateTime(), nullable=True)) | ||||
|  | ||||
|  | ||||
| def downgrade(): | ||||
|     with op.batch_alter_table('fetch') as batch: | ||||
|         batch.drop_column('last_check') | ||||
|         batch.drop_column('error') | ||||
| @@ -7,8 +7,4 @@ RUN apk add --update \ | ||||
|  | ||||
| COPY fetchmail.py /fetchmail.py | ||||
|  | ||||
| RUN mkdir /var/spool/mail \ | ||||
|  && chown mail: /var/spool/mail | ||||
| USER mail | ||||
|  | ||||
| CMD ["/fetchmail.py"] | ||||
|   | ||||
| @@ -5,6 +5,7 @@ import time | ||||
| import os | ||||
| import tempfile | ||||
| import shlex | ||||
| import subprocess | ||||
|  | ||||
|  | ||||
| FETCHMAIL = """ | ||||
| @@ -28,20 +29,21 @@ def escape_rc_string(arg): | ||||
|  | ||||
|  | ||||
| def fetchmail(fetchmailrc): | ||||
|     print(fetchmailrc) | ||||
|     with tempfile.NamedTemporaryFile() as handler: | ||||
|         handler.write(fetchmailrc.encode("utf8")) | ||||
|         handler.flush() | ||||
|         os.system(FETCHMAIL.format(shlex.quote(handler.name))) | ||||
|         command = FETCHMAIL.format(shlex.quote(handler.name)) | ||||
|         output = subprocess.check_output(command, shell=True) | ||||
|         return output | ||||
|  | ||||
|  | ||||
| def run(cursor): | ||||
| def run(connection, cursor): | ||||
|     cursor.execute(""" | ||||
|         SELECT user_email, protocol, host, port, tls, username, password | ||||
|         FROM fetch | ||||
|     """) | ||||
|     fetchmailrc = "" | ||||
|     for line in cursor.fetchall(): | ||||
|         fetchmailrc = "" | ||||
|         user_email, protocol, host, port, tls, username, password = line | ||||
|         options = "options ssl" if tls else "" | ||||
|         fetchmailrc += RC_LINE.format( | ||||
| @@ -53,7 +55,19 @@ def run(cursor): | ||||
|             password=escape_rc_string(password), | ||||
|             options=options | ||||
|         ) | ||||
|     fetchmail(fetchmailrc) | ||||
|         try: | ||||
|             print(fetchmail(fetchmailrc)) | ||||
|             error_message = "" | ||||
|         except subprocess.CalledProcessError as error: | ||||
|             error_message = error.output.decode("utf8") | ||||
|             print(error.output) | ||||
|         finally: | ||||
|             cursor.execute(""" | ||||
|                 UPDATE fetch SET error=?, last_check=datetime('now') | ||||
|                 WHERE user_email=? | ||||
|             """, (error_message.split("\n")[0], user_email)) | ||||
|             connection.commit() | ||||
|  | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
| @@ -62,5 +76,5 @@ if __name__ == "__main__": | ||||
|     while True: | ||||
|         time.sleep(int(os.environ.get("FETCHMAIL_DELAY", 10))) | ||||
|         cursor = connection.cursor() | ||||
|         run(cursor) | ||||
|         run(connection, cursor) | ||||
|         cursor.close() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user