Skip to content

Instantly share code, notes, and snippets.

@odderlynat
Created April 1, 2012 21:29
Show Gist options
  • Save odderlynat/2278914 to your computer and use it in GitHub Desktop.
Save odderlynat/2278914 to your computer and use it in GitHub Desktop.
MailGuy - popup notifier for mail
import wx
import poplib
import imaplib
import re
import time
import cPickle
import os,sys
import random
import zlib
#try: # Windows only
# import msvcrt
# msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
#except ImportError: pass
class MainWindow(wx.MiniFrame):
def __init__(self,parent=None,id=-1,title="mailGuy"):
#TODO - Clean up algorithm to get pos to place window in lower right corner, use tuples?
mode = wx.Display.GetCurrentMode(wx.Display(0))
x = mode.GetWidth()
y = mode.GetHeight()
sizex = 300
sizey = 150
newx = x - sizex
# windows taskbar is 30 pixels
newy = (y - sizey) - 30
wx.MiniFrame.__init__(
self,parent,id,title,size=(sizex,sizey),
style=wx.DEFAULT_FRAME_STYLE | wx.STAY_ON_TOP,pos=(newx,newy))
#Values for transparency effect
self.amount = 255
self.delta = -3
p = wx.Panel(self)
#Timer to check mail account
self.checktimer = wx.CallLater(300000,self.onTimer)
#Show a default message on startup
self.st = wx.StaticText(p,-1,str("mailGuy v%s" % myVersion),(25,25))
self.st.SetFont(wx.Font(14,wx.SWISS,wx.NORMAL,wx.NORMAL))
self.timer = wx.Timer(self)
self.timer.Start(50)
#Event bindings for the fade effect
self.Bind(wx.EVT_TIMER,self.AlphaCycle)
p.Bind(wx.EVT_LEFT_DOWN,self.HoldAlpha)
p.Bind(wx.EVT_ENTER_WINDOW,self.ResetAlpha)
self.st.Bind(wx.EVT_LEFT_DOWN,self.HoldAlpha)
self.onTimer
#AlphaCycle,ResetAlpha,HoldAlpha all deal with making the window fade away
def AlphaCycle(self,evt):
if self.amount > 0:
self.amount += self.delta
self.SetTransparent(self.amount)
def ResetAlpha(self,*args):
self.amount = 255
def HoldAlpha(self,*args):
self.SetTransparent(255)
self.amount = 0
wx.CallLater(15000,self.ResetAlpha)
def onTimer(self,*args):
params = []
f = open('config.dat','rb')
cryptparams = cPickle.load(f)
f.close()
for y in cryptparams:
params.append(ConfigWindow.crypt(myKey,y,True))
user = params[0]
pass_ = params[1]
type_ = params[2]
host = params[3]
mail = MailAccount(type_,host,user,pass_)
msgcount = mail.checkMail()
#If this is the first time we've done this, setup old message count to equal the current amount of messages
if not hasattr(self,"oldmsgcount"):
self.oldmsgcount = msgcount
if msgcount > self.oldmsgcount:
newmsg = msgcount - self.oldmsgcount
self.SetFocus
self.HoldAlpha()
elif msgcount <= self.oldmsgcount:
newmsg = 0
self.oldmsgcount = msgcount
#Just a debug message
#print "Mail Check! Msgcount: %s" % msgcount
if newmsg == 1:
plural = ""
else:
plural = "s"
if newmsg > 0:
self.st.SetLabel(str("%s:\n%s New Message%s" % (host,newmsg,plural)))
self.checktimer.Restart()
#MailAccount represents one particular email account we can check
class MailAccount:
def __init__(self,accttype,host,usr,pwd):
self.accttype = accttype
self.host = host
self.usr = usr
self.pwd = pwd
def checkMail(self):
if self.accttype == "POP3":
pop = poplib.POP3(self.host)
pop.user(self.usr)
pop.pass_(self.pwd)
stat = pop.stat()
count = stat[0]
pop.quit()
return count
if self.accttype == "Gmail":
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(self.usr,self.pwd)
resp,msgs = m.select("Inbox",readonly=1)
m.logout()
return int(msgs[0])
if self.accttype == "IMAP":
m = imaplib.IMAP4(self.host)
m.login(self.usr,self.pwd)
resp,msgs = m.select("Inbox",readonly=1)
m.logout()
return int(msgs[0])
if self.accttype == "IMAP-SSL":
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(self.usr,self.pwd)
resp,msgs = m.select("Inbox",readonly=1)
m.logout()
class ConfigWindow(wx.Frame):
def __init__(self,parent=None,id=-1,title="mailGuy Config"):
wx.Frame.__init__(
self,parent,id,title,size=(400,340),
style=wx.DEFAULT_FRAME_STYLE & ~wx.RESIZE_BORDER)
p = wx.Panel(self)
self.st = wx.StaticText(p,-1,str("mailGuy Initial Configuration"),(25,25))
self.st.SetFont(wx.Font(14,wx.SWISS,wx.NORMAL,wx.NORMAL))
lblUser = wx.StaticText(p,-1,str("Username:"),(25,25))
lblUser.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.NORMAL))
self.txtUser = wx.TextCtrl(p,-1,size=(125,-1))
lblPass = wx.StaticText(p,-1,str("Password:"),(25,25))
lblPass.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.NORMAL))
self.txtPass = wx.TextCtrl(p,-1,size=(125,-1),style=wx.TE_PASSWORD)
lblSvrType= wx.StaticText(p,-1,str("Service Type:"),(25,25))
lblSvrType.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.NORMAL))
svrTypeLst = ['POP3','IMAP','IMAP-SSL','Gmail']
self.cmbSvrType = wx.ComboBox(p,500,"",(90,50),(130,-1),svrTypeLst,wx.CB_DROPDOWN)
lblMailServer = wx.StaticText(p,-1,str("Mail Server:"),(25,25))
lblMailServer.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.NORMAL))
self.txtMailServer = wx.TextCtrl(p,-1,size=(145,-1))
b = wx.Button(p,10,"Ok")
self.Bind(wx.EVT_BUTTON,self.createConfig,b)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.st,0,wx.ALL,3)
sizer.Add((50,0),0,wx.ALL,3)
sizer.Add(lblUser,0,wx.ALL,3)
sizer.Add(self.txtUser,0,wx.ALL,3)
sizer.Add(lblPass,0,wx.ALL,3)
sizer.Add(self.txtPass,0,wx.ALL,3)
sizer.Add(lblSvrType,0,wx.ALL,3)
sizer.Add(self.cmbSvrType,0,wx.ALL,3)
sizer.Add(lblMailServer,0,wx.ALL,3)
sizer.Add(self.txtMailServer,0,wx.ALL,3)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add((300,0),0,wx.ALL)
sizer2.Add(b,0,wx.ALL)
box = wx.BoxSizer(wx.VERTICAL)
box.Add(sizer,0,wx.ALL,20)
box.Add(sizer2,0,wx.ALL,-1)
p.SetSizer(box)
self.SetIcon(icon1)
self.Show(True)
def createConfig(self,evt):
parameters = [self.txtUser.GetValue(),self.txtPass.GetValue(),
self.cmbSvrType.GetValue(),self.txtMailServer.GetValue()
]
paracrypt = []
f = open("config.dat","wb")
for x in parameters:
paracrypt.append(ConfigWindow.crypt(myKey,x))
cPickle.dump(paracrypt,f,1)
f.close()
frame = MainWindow()
frame.SetIcon(icon1)
frame.Show()
self.Close()
def crypt(key,text,reverse=False):
rand = random.Random(key).randrange
if not reverse:
text = zlib.compress(text)
text = ''.join([chr(ord(elem)^rand(256)) for elem in text])
if reverse:
text = zlib.decompress(text)
return text
crypt = staticmethod(crypt)
myKey = "all good things..."
myVersion = "0.4"
app = wx.PySimpleApp()
config = "config.dat"
icon1 = wx.Icon("icon.ico",wx.BITMAP_TYPE_ICO)
if os.path.exists(os.path.join(os.getcwd(),config)):
frame = MainWindow()
frame.SetIcon(icon1)
frame.Show()
else:
frame = ConfigWindow()
frame.Show()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment