~ubuntu-branches/ubuntu/hardy/emesene/hardy-updates

« back to all changes in this revision

Viewing changes to plugins_base/mailChecker.py

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2008-03-29 21:48:29 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080329214829-spbg3uej5aozf2c1
Tags: 1.0-dist-1
* New upstream stable release from the emesene-1.0-dist upstream tarball
  (which contains setup.py and misc/ ).
* debian/patches/01_setup_py_update_get_orig_source.patch:
  - Removed, not needed anymore as we aren't using get-orig-source.
* debian/rules:
  - Remove get-orig-source rule, as from now on we will use upstream
    tarballs.
  - Remove python-patchsys include.
  - Run dh_icons and dh_desktop
  - Build the package with python2.5 since it FTBFS with python2.4 due to
    an issue with distutils.
* debian/copyright:
  - Updated.
* debian/watch:
  - Updated so that it reports 1.* as the newer versions.
* debian/emesene-launcher:
  - Update for the new Controller.py location.
* debian/control:
  - Build-Depend on python2.5 since the package is built with python2.5,
    as it fails with python2.4's distutils.
  - Wrap Build-Depends.
  - Build-Depend on debhelper >= 5.0.51~ for dh_icons

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
 
3
 
#   This file is part of emesene.
4
 
#
5
 
#    Emesene is free software; you can redistribute it and/or modify
6
 
#    it under the terms of the GNU General Public License as published by
7
 
#    the Free Software Foundation; either version 2 of the License, or
8
 
#    (at your option) any later version.
9
 
#
10
 
#    emesene is distributed in the hope that it will be useful,
11
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
#    GNU General Public License for more details.
14
 
#
15
 
#    You should have received a copy of the GNU General Public License
16
 
#    along with emesene; if not, write to the Free Software
17
 
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 
19
 
import re
20
 
import os
21
 
import gtk
22
 
import pango
23
 
import gobject
24
 
 
25
 
from imaplib import *
26
 
 
27
 
import Plugin
28
 
 
29
 
class MainClass( Plugin.Plugin ):
30
 
 
31
 
    def __init__( self, controller, msn ):
32
 
        Plugin.Plugin.__init__( self, controller, msn )
33
 
 
34
 
        self.description = _('An IMAP mail checker')
35
 
        self.authors = { 'Jacopo Lamanna' : 'jazzenco@fastwebnet.it' }
36
 
        self.website = 'www.jazzenco.org'
37
 
        self.displayName = _('mailChecker')
38
 
        self.name = 'mailChecker'
39
 
        self.config = controller.getConfig()
40
 
        self.config.readPluginConfig(self.name)
41
 
        self.enabled = False
42
 
        self.controller = controller
43
 
        self.isbutt = 0
44
 
        self.checking = False
45
 
        
46
 
        self.deffun = Plugin.Function( self.function, self.callback )
47
 
        
48
 
    def start( self ):
49
 
            
50
 
        self.enabled = True
51
 
        if self.isbutt == 0 :
52
 
            '''create the gui look'''
53
 
            self.Mbox = gtk.HBox(homogeneous=False, spacing=5)
54
 
            self.controller.mainWindow.vbox.pack_start(self.Mbox, False, False)
55
 
            self.Mbox.show_all()
56
 
            self.button = gtk.Button()
57
 
            self.button.set_relief(gtk.RELIEF_NONE)
58
 
            self.img = gtk.Image()
59
 
            self.mailIcon = self.controller.theme.getSmiley( '(e)' )
60
 
            self.img.set_from_animation(self.mailIcon)
61
 
            self.img.set_pixel_size(10)
62
 
            self.button.set_image(self.img)
63
 
            self.button.connect('clicked', self.Mcheck , None)
64
 
            self.Mbox.pack_start(self.button, False, False)
65
 
            self.button.show_all()
66
 
            
67
 
            self.Tbutton = gtk.Button()
68
 
            self.Tbutton.connect('clicked', self.Client , None)
69
 
            self.Mtext = gtk.Label(_('Not checked yet.'))
70
 
            self.Mtext.set_ellipsize( pango.ELLIPSIZE_END )
71
 
            self.Ttooltip = gtk.Tooltips()
72
 
            self.Mtext.set_use_underline(True)
73
 
            self.Tbutton.add(self.Mtext)
74
 
            self.Tbutton.set_relief(gtk.RELIEF_NONE)
75
 
            self.Tbutton.show_all()
76
 
            self.Mbox.pack_start(self.Tbutton, True, True)
77
 
            self.Mtext.show_all()
78
 
            
79
 
            self.isbutt = 1
80
 
 
81
 
        self.Mcheck( None , None )
82
 
        
83
 
        self.interval = 60*1000*int(self.config.getPluginValue(
84
 
            self.name, 'time', '5' ))
85
 
        self.source_id = gobject.timeout_add(self.interval, 
86
 
                self.Mcheck, self , None)
87
 
 
88
 
    def Client( self , widget , data=None):
89
 
        os.popen(self.config.getPluginValue( self.name, 
90
 
            'client', 'thunderbird' )+' &')
91
 
    
92
 
    def Mcheck( self , widget , data=None):
93
 
        if not self.checking:
94
 
            self.checking = True
95
 
            self.Mtext.set_label( _('Checking') )
96
 
            self.deffun( data )
97
 
            
98
 
        return True
99
 
    
100
 
    def function( self, data ):
101
 
        '''this function will run on a thread and the result will be passed
102
 
        to the callback function'''
103
 
        mail = ''
104
 
        numMes = '0'
105
 
        
106
 
        server_name = self.config.getPluginValue(self.name, 
107
 
                'server', '')
108
 
        user_name = self.config.getPluginValue(self.name, 'user', '' )
109
 
        password = self.config.getPluginValue(self.name, 'pass', '' )
110
 
 
111
 
        try:
112
 
            try:
113
 
                server = IMAP4(server_name)
114
 
                server.login(user_name, password) 
115
 
            except:
116
 
                if '@' in user_name:
117
 
                    old_server_name = server_name
118
 
                    user_name, server_name = user_name.split('@')
119
 
                    try:
120
 
                        server = IMAP4(server_name)
121
 
                        server.login(user_name, password) 
122
 
                    except:
123
 
                        server = IMAP4(old_server_name)
124
 
                        server.login(user_name, password) 
125
 
                else:
126
 
                    raise
127
 
        except:
128
 
            numMes = 'error'
129
 
        else:
130
 
            r = server.select()
131
 
            if r[0] == 'OK' :
132
 
                numMes = r[1][0]
133
 
                
134
 
                if int(numMes) <= 5:
135
 
                    numList = range(int(numMes)) 
136
 
                else:
137
 
                    numList = range(int(numMes - 5), int(numMes)) 
138
 
                
139
 
                for i in numList:
140
 
                    m = re.search('.*(?=\r\n\r\n)', 
141
 
                            server.fetch(i+1, 
142
 
                                '(BODY[HEADER.FIELDS (FROM)])')[1][0][1])
143
 
                    mail = mail+m.group(0)+'\n'
144
 
                    m = re.search('.*(?=\r\n\r\n)', 
145
 
                            server.fetch(i+1, 
146
 
                                '(BODY[HEADER.FIELDS (SUBJECT)])')[1][0][1])
147
 
                    mail = mail+m.group(0)+'\n\n'
148
 
            else:
149
 
                numMes = 'error'
150
 
        
151
 
            if numMes != 'error' and int(numMes) > 5:
152
 
                mail += _("and more..")
153
 
                
154
 
            self.config.setPluginValue( self.name, 'numMes', numMes )
155
 
            server.logout()
156
 
        return (numMes,mail)
157
 
        
158
 
    def callback( self, args ):
159
 
        numMes,mail = args
160
 
        self.checking = False
161
 
        if numMes == 'error' :
162
 
            self.Mtext.set_label(_('Server error'))
163
 
        else:
164
 
            if numMes >= '1' :
165
 
                params = {'num': numMes, 'user': self.config.getPluginValue(\
166
 
                    self.name, 'user','user')}
167
 
                self.Mtext.set_label(_('%(num) messages for %(user)') % params)
168
 
                self.Ttooltip.set_tip( self.Tbutton, mail )
169
 
            else:
170
 
                self.Mtext.set_label( _('No messages for %s') %  \
171
 
                    self.config.getPluginValue( self.name, 'user', 'user'))
172
 
                self.Ttooltip.set_tip( self.Tbutton, _('No new messages') )
173
 
   
174
 
    def stop( self ):
175
 
        self.enabled = False
176
 
        gobject.source_remove(self.source_id)
177
 
        self.controller.mainWindow.vbox.remove( self.Mbox )
178
 
        self.isbutt = 0
179
 
 
180
 
    def check( self ):
181
 
        return ( True, 'Ok' )
182
 
 
183
 
    def configure( self ):
184
 
        dataM = []
185
 
        dataM.append( Plugin.Option( 'time', str, _('Check every [min]:'), '',\
186
 
            self.config.getPluginValue( self.name, 'time', '5' )) )
187
 
        dataM.append( Plugin.Option( 'server', str, _('IMAP server:'), '', \
188
 
            self.config.getPluginValue( self.name, 'server', 'pop.test.com' )))
189
 
        dataM.append( Plugin.Option('client', str, _('Client to execute:'),'',\
190
 
            self.config.getPluginValue( self.name, 'client', 'thunderbird' )) )
191
 
        dataM.append( Plugin.Option( 'user', str, _('Username:'), '', \
192
 
            self.config.getPluginValue( self.name, 'user', 'user')) )
193
 
        
194
 
        #dataM.append( Plugin.Option( 'pass', str, 'password:', '', self.config.getPluginValue( self.name, 'pass', '*****')) )        
195
 
        
196
 
        self.confW = Plugin.ConfigWindow( _('Mail checker config'), dataM)
197
 
        
198
 
        #Let's keep the pass entry secret
199
 
        label = gtk.Label( _('Password:') )
200
 
        label.set_justify(gtk.JUSTIFY_LEFT)
201
 
        label.set_width_chars( 20 )
202
 
        entry = gtk.Entry()
203
 
        entry.set_text('')
204
 
        entry.set_visibility(False) #with this op.
205
 
        hbox = gtk.HBox()
206
 
        hbox.pack_start( label )
207
 
        hbox.pack_start( entry )
208
 
        self.confW.vbox.pack_start( hbox )
209
 
        self.confW.vbox.show_all()
210
 
        
211
 
        r = self.confW.run()
212
 
        passwd = str(entry.get_text())
213
 
        
214
 
        if r is not None:
215
 
            self.config.setPluginValue(self.name, 'time', r['time'].value)
216
 
            self.config.setPluginValue(self.name, 'server', r['server'].value)
217
 
            self.config.setPluginValue(self.name, 'user', r['user'].value)
218
 
            self.config.setPluginValue(self.name, 'pass', passwd )
219
 
            self.config.setPluginValue(self.name, 'client', r['client'].value)
220
 
            
221
 
        #self.start()
222
 
        return True
223