"Fossies" - the Fresh Open Source Software Archive

Member "roundup-2.0.0/detectors/irker.py" (26 Aug 2019, 2874 Bytes) of package /linux/www/roundup-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "irker.py": 1.6.1_vs_2.0.0.

    1 # This detector sends notification on IRC through an irker daemon
    2 # (http://www.catb.org/esr/irker/) when issues are created or messages
    3 #  are added.
    4 #
    5 # Written by Ezio Melotti
    6 #
    7 # Requires a running irkerd daemon to work.  See the irker documentation
    8 # for more information about installing, configuring, and running irker.
    9 #
   10 # Add the IRC channel(s) that should receive notifications in
   11 # detectors/config.ini as a comma-separated list, using this format:
   12 #
   13 #   [irker]
   14 #   channels = irc://chat.freenode.net/channelname
   15 #
   16 
   17 from __future__ import print_function
   18 import re
   19 import json
   20 import socket
   21 
   22 IRKER_HOST = 'localhost'
   23 IRKER_PORT = 6659
   24 
   25 max_content = 120
   26 
   27 TEMPLATE = ('%(green)s%(author)s%(reset)s '
   28             '%(bluish)s#%(nodeid)s%(reset)s/%(title)s%(bold)s:%(bold)s '
   29             '%(log)s %(url)s')
   30 
   31 
   32 def sendmsg(msg):
   33     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   34     try:
   35         sock.connect((IRKER_HOST, IRKER_PORT))
   36         sock.sendall(msg + "\n")
   37     finally:
   38         sock.close()
   39 
   40 
   41 def notify_irker(db, cl, nodeid, oldvalues):
   42     messages = set(cl.get(nodeid, 'messages'))
   43     if oldvalues:
   44         messages -= set(oldvalues.get('messages', ()))
   45     if not messages:
   46         return
   47     messages = list(messages)
   48 
   49     if oldvalues:
   50         oldstatus = oldvalues['status']
   51     else:
   52         oldstatus = None
   53     newstatus = db.issue.get(nodeid, 'status')
   54     if oldstatus != newstatus:
   55         if oldvalues:
   56             status = db.status.get(newstatus, 'name')
   57         else:
   58             status = 'new'
   59         log = '[' + status + '] '
   60     else:
   61         log = ''
   62     for msg in messages:
   63         log += db.msg.get(msg, 'content')
   64     if len(log) > max_content:
   65         log = log[:max_content-3] + '...'
   66     log = re.sub('\s+', ' ', log)
   67 
   68     # include irc colors
   69     params = {
   70         'bold': '\x02',
   71         'green': '\x0303',
   72         'blue': '\x0302',
   73         'bluish': '\x0310',
   74         'yellow': '\x0307',
   75         'brown': '\x0305',
   76         'reset': '\x0F'
   77     }
   78     # extend with values used in the template
   79     params['author'] = db.user.get(db.getuid(), 'username')
   80     params['nodeid'] = nodeid
   81     params['title'] = db.issue.get(nodeid, 'title')
   82     params['log'] = log
   83     params['url'] = '%sissue%s' % (db.config.TRACKER_WEB, nodeid)
   84 
   85     # create the message and use the list of channels defined in
   86     # detectors/config.ini
   87     msg = json.dumps({
   88         'to': db.config.detectors.IRKER_CHANNELS.split(','),
   89         'privmsg': TEMPLATE % params,
   90     })
   91 
   92     try:
   93         sendmsg(msg)
   94     except Exception as e:
   95         # Ignore any errors in sending the irker;
   96         # if the server is down, that's just bad luck
   97         # XXX might want to do some logging here
   98         print('* Sending message to irker failed', str(e))
   99 
  100 def init(db):
  101     db.issue.react('create', notify_irker)
  102     db.issue.react('set', notify_irker)