hgext/notify.py
changeset 7498 64840fcb79e1
parent 7369 87158be081b8
child 7658 44b3f7bbe2f3
equal deleted inserted replaced
7476:6644c111f9e9 7498:64840fcb79e1
   103             self.ui.readsections(cfg, 'usersubs', 'reposubs')
   103             self.ui.readsections(cfg, 'usersubs', 'reposubs')
   104         self.repo = repo
   104         self.repo = repo
   105         self.stripcount = int(self.ui.config('notify', 'strip', 0))
   105         self.stripcount = int(self.ui.config('notify', 'strip', 0))
   106         self.root = self.strip(self.repo.root)
   106         self.root = self.strip(self.repo.root)
   107         self.domain = self.ui.config('notify', 'domain')
   107         self.domain = self.ui.config('notify', 'domain')
       
   108         self.test = self.ui.configbool('notify', 'test', True)
   108         self.charsets = mail._charsets(self.ui)
   109         self.charsets = mail._charsets(self.ui)
   109         self.subs = self.subscribers()
   110         self.subs = self.subscribers()
   110 
   111 
   111         mapfile = self.ui.config('notify', 'style')
   112         mapfile = self.ui.config('notify', 'style')
   112         template = (self.ui.config('notify', hooktype) or
   113         template = (self.ui.config('notify', hooktype) or
   155         for pat, users in self.ui.configitems('reposubs'):
   156         for pat, users in self.ui.configitems('reposubs'):
   156             if fnmatch.fnmatch(self.repo.root, pat):
   157             if fnmatch.fnmatch(self.repo.root, pat):
   157                 for user in users.split(','):
   158                 for user in users.split(','):
   158                     subs[self.fixmail(user)] = 1
   159                     subs[self.fixmail(user)] = 1
   159         subs = util.sort(subs)
   160         subs = util.sort(subs)
   160         return [mail.addressencode(self.ui, s, self.charsets) for s in subs]
   161         return [mail.addressencode(self.ui, s, self.charsets, self.test)
       
   162                 for s in subs]
   161 
   163 
   162     def url(self, path=None):
   164     def url(self, path=None):
   163         return self.ui.config('web', 'baseurl') + (path or self.root)
   165         return self.ui.config('web', 'baseurl') + (path or self.root)
   164 
   166 
   165     def node(self, node):
   167     def node(self, node):
   184         # store sender and subject
   186         # store sender and subject
   185         sender, subject = msg['From'], msg['Subject']
   187         sender, subject = msg['From'], msg['Subject']
   186         # create fresh mime message from msg body
   188         # create fresh mime message from msg body
   187         text = msg.get_payload()
   189         text = msg.get_payload()
   188         # for notification prefer readability over data precision
   190         # for notification prefer readability over data precision
   189         msg = mail.mimeencode(self.ui, text, self.charsets)
   191         msg = mail.mimeencode(self.ui, text, self.charsets, self.test)
   190 
   192 
   191         def fix_subject(subject):
   193         def fix_subject(subject):
   192             '''try to make subject line exist and be useful.'''
   194             '''try to make subject line exist and be useful.'''
   193 
   195 
   194             if not subject:
   196             if not subject:
   199                     s = changes[4].lstrip().split('\n', 1)[0].rstrip()
   201                     s = changes[4].lstrip().split('\n', 1)[0].rstrip()
   200                     subject = '%s: %s' % (self.root, s)
   202                     subject = '%s: %s' % (self.root, s)
   201             maxsubject = int(self.ui.config('notify', 'maxsubject', 67))
   203             maxsubject = int(self.ui.config('notify', 'maxsubject', 67))
   202             if maxsubject and len(subject) > maxsubject:
   204             if maxsubject and len(subject) > maxsubject:
   203                 subject = subject[:maxsubject-3] + '...'
   205                 subject = subject[:maxsubject-3] + '...'
   204             msg['Subject'] = mail.headencode(self.ui, subject, self.charsets)
   206             msg['Subject'] = mail.headencode(self.ui, subject,
       
   207                                              self.charsets, self.test)
   205 
   208 
   206         def fix_sender(sender):
   209         def fix_sender(sender):
   207             '''try to make message have proper sender.'''
   210             '''try to make message have proper sender.'''
   208 
   211 
   209             if not sender:
   212             if not sender:
   210                 sender = self.ui.config('email', 'from') or self.ui.username()
   213                 sender = self.ui.config('email', 'from') or self.ui.username()
   211             if '@' not in sender or '@localhost' in sender:
   214             if '@' not in sender or '@localhost' in sender:
   212                 sender = self.fixmail(sender)
   215                 sender = self.fixmail(sender)
   213             msg['From'] = mail.addressencode(self.ui, sender, self.charsets)
   216             msg['From'] = mail.addressencode(self.ui, sender,
       
   217                                              self.charsets, self.test)
   214 
   218 
   215         msg['Date'] = util.datestr(format="%a, %d %b %Y %H:%M:%S %1%2")
   219         msg['Date'] = util.datestr(format="%a, %d %b %Y %H:%M:%S %1%2")
   216         fix_subject(subject)
   220         fix_subject(subject)
   217         fix_sender(sender)
   221         fix_sender(sender)
   218 
   222 
   222                                  (short(node), int(time.time()),
   226                                  (short(node), int(time.time()),
   223                                   hash(self.repo.root), socket.getfqdn()))
   227                                   hash(self.repo.root), socket.getfqdn()))
   224         msg['To'] = ', '.join(self.subs)
   228         msg['To'] = ', '.join(self.subs)
   225 
   229 
   226         msgtext = msg.as_string(0)
   230         msgtext = msg.as_string(0)
   227         if self.ui.configbool('notify', 'test', True):
   231         if self.test:
   228             self.ui.write(msgtext)
   232             self.ui.write(msgtext)
   229             if not msgtext.endswith('\n'):
   233             if not msgtext.endswith('\n'):
   230                 self.ui.write('\n')
   234                 self.ui.write('\n')
   231         else:
   235         else:
   232             self.ui.status(_('notify: sending %d subscribers %d changes\n') %
   236             self.ui.status(_('notify: sending %d subscribers %d changes\n') %