Skip to content

Instantly share code, notes, and snippets.

@jackbergus
Created November 5, 2017 09:44
Show Gist options
  • Save jackbergus/3f34669b50420231380adb0d976a0b8b to your computer and use it in GitHub Desktop.
Save jackbergus/3f34669b50420231380adb0d976a0b8b to your computer and use it in GitHub Desktop.
Script that deletes the mails from senders specified by a json file
#!/usr/bin/python
#! -*- coding: utf-8 -*-
#
# cleaner.py
# This file is part of Cleaner
#
# Copyright (C) 2012 - Giacomo Bergami
#
# Cleaner is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Cleaner is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Cleaner; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA
#
# Description: Script that deletes the mails from senders specified by a json file
#
import poplib
import gtk.gdk
import pynotify
import json
pynotify.init("cleaner") #Bugfix for new pynotify versions
## Configuration file example
#{
# "username": "giacomo90@libero.it",
# "image": "/usr/share/pixmaps/gnome-term-linux2.png",
# "config":
# [ ["no-reply@diasp.org", "Diaspora", true],
# ["amazon", "", false],
# ["sourceforge", "", false],
# ["bufferapp.com", "", false],
# ["facebookmail.com", "Facebook", true],
# ["slideshare.net", "", false],
# ["linkedin.com", "LinkedIn", true],
# ],
# "pw": "password",
# "mailserver": "mail.server.pop"
#}
l = open('config.conf','r')
example = json.loads(l.read())
l.close()
IMAGE = example['image']
MAILSERVER = example['mailserver']
USERNAME = example['username']
PW = example['pw']
undesired = []
title = []
notify = []
size = 0
for x in example['config']:
undesired.extend([x[0]])
title.extend([x[1]])
notify.extend([x[2]])
size += 1
counts = [0 for x in range(0,size)]
#Do a notification
def notif(title,descr):
n = pynotify.Notification(title,descr,IMAGE)
n.set_hint('x', gtk.gdk.screen_width()/2.)
n.set_hint('y', gtk.gdk.screen_height()/2.)
n.show()
return n
start = notif("Mail checker & Cleaner", "Service has started...")
start.show()
#connect to server
mailserver = poplib.POP3_SSL(MAILSERVER)
mailserver.user(USERNAME) #use 'recent mode'
mailserver.pass_(PW) #consider not storing in plaintext!
#get all the mails
ids = mailserver.list()[1]
maximum = len(mailserver.list()[1])
def oppure(a,b): return a or b
#get all the mails from a part of a given email address
def mail_from(mail,mitt):
for x in mail:
if "Return-Path:" in x:
if mitt in x:
print "Removing " + mitt + " (" + str(counts[undesired.index(mitt)]) + ")..."
return mitt
return None
def todelete(mail):
return reduce(oppure, map(lambda x: mail_from(mail,x),undesired))
#delete only the messages
for x in reversed(range(1,maximum+1)):
mail = mailserver.retr(x)[1]
check = todelete(mail)
if not (check is None):
mailserver.dele(x)
idx = undesired.index(check)
if notify[idx]:
counts[idx] = counts[idx] +1
start.close()
mailserver.quit()
#Final notifications
for x in range(0,len(notify)):
if notify[x]:
notif(title[x],"You have " + str(counts[x]) + " notifications")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment