Beispiel #1
0
def get_time_from_line(ptr_buffer):
    lines = weechat.hdata_pointer(weechat.hdata_get('buffer'), ptr_buffer, 'own_lines')

    if lines:
        line = weechat.hdata_pointer(weechat.hdata_get('lines'), lines, 'last_line')
        last_read_line = weechat.hdata_pointer(weechat.hdata_get('lines'), lines, 'last_read_line')
        # last line already read?
        while line != last_read_line:
            hdata_line = weechat.hdata_get('line')
            hdata_line_data = weechat.hdata_get('line_data')

            data = weechat.hdata_pointer(hdata_line, line, 'data')

            date_last_line = weechat.hdata_time(hdata_line_data, data, 'date')
            displayed = weechat.hdata_char(hdata_line_data, data, 'displayed')
            # message hidden?
            if not displayed and OPTIONS['ignore_hidden'].lower() == 'on':
                prev_line = weechat.hdata_pointer(hdata_line, line, 'prev_line')
                line = prev_line
                continue

            # buffer empty?
            if not date_last_line:
                return 0

            get_current_ticks = time.time()

            time_gone = get_current_ticks - date_last_line
            if int(OPTIONS['time']) < time_gone:
                return 1
            else:
                return 0
    return 0
Beispiel #2
0
def get_last_message_time(buffer):
    lines = weechat.hdata_pointer(weechat.hdata_get("buffer"), buffer,
                                  "own_lines")
    line = weechat.hdata_pointer(weechat.hdata_get("lines"), lines,
                                 "last_line")
    while line:
        line_data = weechat.hdata_pointer(weechat.hdata_get("line"), line,
                                          "data")
        tags_count = weechat.hdata_integer(weechat.hdata_get("line_data"),
                                           line_data, "tags_count")
        tags = [
            weechat.hdata_string(weechat.hdata_get("line_data"), line_data,
                                 "{}|tags_array".format(i))
            for i in range(tags_count)
        ]
        irc_tags = [t for t in tags if t.startswith("irc_")]
        if len(irc_tags) > 0:
            break
        line = weechat.hdata_pointer(weechat.hdata_get("line"), line,
                                     "prev_line")
    if not line:
        return None
    # TODO: get timestamp with millisecond granularity
    ts = weechat.hdata_time(weechat.hdata_get("line_data"), line_data, "date")
    t = datetime.datetime.fromtimestamp(ts, tz=datetime.timezone.utc)
    return t
Beispiel #3
0
def bufsave_cmd(data, buffer, args):
    filename = weechat.buffer_get_string(
        buffer, 'localvar_server') + '.' + weechat.buffer_get_string(
            buffer,
            'localvar_channel')[1:] + '-' + time.strftime('%y_%m_%d') + '.log'
    filename = weechat.string_eval_path_home('%h/logs/' + filename, {}, {}, {})
    try:
        fp = open(filename, 'w')
    except:
        weechat.prnt('', 'Error writing to target file!')
        return weechat.WEECHAT_RC_OK
    own_lines = weechat.hdata_pointer(weechat.hdata_get('buffer'), buffer,
                                      'own_lines')
    if own_lines:
        line = weechat.hdata_pointer(weechat.hdata_get('lines'), own_lines,
                                     'first_line')
        hdata_line = weechat.hdata_get('line')
        hdata_line_data = weechat.hdata_get('line_data')
        while line:
            data = weechat.hdata_pointer(hdata_line, line, 'data')
            if data:
                date = weechat.hdata_time(hdata_line_data, data, 'date')
                if not isinstance(date, str):
                    date = time.strftime('%F %T', time.localtime(int(date)))
                fp.write('{0} {1} {2}\n'.format(
                    date,
                    cstrip(
                        weechat.hdata_string(hdata_line_data, data, 'prefix')),
                    cstrip(
                        weechat.hdata_string(hdata_line_data, data,
                                             'message'))))
            line = weechat.hdata_move(hdata_line, line, 1)
    fp.close()
    return weechat.WEECHAT_RC_OK
Beispiel #4
0
def bufsave_cmd(data, buffer, args):
    ''' Callback for /bufsave command '''

    filename_raw = args

    if not filename_raw:
        w.command('', '/help %s' % SCRIPT_COMMAND)
        return w.WEECHAT_RC_OK

    filename = w.string_eval_path_home(filename_raw, {}, {}, {})

    if exists(filename):
        w.prnt('', 'Error: target file already exists!')
        return w.WEECHAT_RC_OK

    try:
        fp = open(filename, 'w')
    except:
        w.prnt('', 'Error writing to target file!')
        return w.WEECHAT_RC_OK

    version = w.info_get('version_number', '') or 0
    if int(version) >= 0x00030600:
        # use hdata with WeeChat >= 0.3.6 (direct access to data, very fast)
        own_lines = w.hdata_pointer(w.hdata_get('buffer'), buffer, 'own_lines')
        if own_lines:
            line = w.hdata_pointer(w.hdata_get('lines'), own_lines,
                                   'first_line')
            hdata_line = w.hdata_get('line')
            hdata_line_data = w.hdata_get('line_data')
            while line:
                data = w.hdata_pointer(hdata_line, line, 'data')
                if data:
                    date = w.hdata_time(hdata_line_data, data, 'date')
                    # since WeeChat 0.3.9, hdata_time returns long instead of string
                    if not isinstance(date, str):
                        date = time.strftime('%F %T',
                                             time.localtime(int(date)))
                    fp.write('%s %s %s\n' %(\
                            date,
                            cstrip(w.hdata_string(hdata_line_data, data, 'prefix')),
                            cstrip(w.hdata_string(hdata_line_data, data, 'message')),
                            ))
                line = w.hdata_move(hdata_line, line, 1)
    else:
        # use infolist with WeeChat <= 0.3.5 (full duplication of lines, slow and uses memory)
        infolist = w.infolist_get('buffer_lines', buffer, '')
        while w.infolist_next(infolist):
            fp.write('%s %s %s\n' %(\
                    w.infolist_time(infolist, 'date'),
                    cstrip(w.infolist_string(infolist, 'prefix')),
                    cstrip(w.infolist_string(infolist, 'message')),
                    ))
        w.infolist_free(infolist)

    fp.close()

    return w.WEECHAT_RC_OK
Beispiel #5
0
def bufsave_cmd(data, buffer, args):
    ''' Callback for /bufsave command '''

    filename_raw = args

    if not filename_raw:
        w.command('', '/help %s' %SCRIPT_COMMAND)
        return w.WEECHAT_RC_OK
    
    filename = w.string_eval_path_home(filename_raw, {}, {}, {})

    if exists(filename):
        w.prnt('', 'Error: target file already exists!')
        return w.WEECHAT_RC_OK

    try:
        fp = open(filename, 'w')
    except:
        w.prnt('', 'Error writing to target file!')
        return w.WEECHAT_RC_OK

    version = w.info_get('version_number', '') or 0
    if int(version) >= 0x00030600:
        # use hdata with WeeChat >= 0.3.6 (direct access to data, very fast)
        own_lines = w.hdata_pointer(w.hdata_get('buffer'), buffer, 'own_lines')
        if own_lines:
            line = w.hdata_pointer(w.hdata_get('lines'), own_lines, 'first_line')
            hdata_line = w.hdata_get('line')
            hdata_line_data = w.hdata_get('line_data')
            while line:
                data = w.hdata_pointer(hdata_line, line, 'data')
                if data:
                    date = w.hdata_time(hdata_line_data, data, 'date')
                    # since WeeChat 0.3.9, hdata_time returns long instead of string
                    if not isinstance(date, str):
                        date = time.strftime('%F %T', time.localtime(int(date)))
                    fp.write('%s %s %s\n' %(\
                            date,
                            cstrip(w.hdata_string(hdata_line_data, data, 'prefix')),
                            cstrip(w.hdata_string(hdata_line_data, data, 'message')),
                            ))
                line = w.hdata_move(hdata_line, line, 1)
    else:
        # use infolist with WeeChat <= 0.3.5 (full duplication of lines, slow and uses memory)
        infolist = w.infolist_get('buffer_lines', buffer, '')
        while w.infolist_next(infolist):
            fp.write('%s %s %s\n' %(\
                    w.infolist_time(infolist, 'date'),
                    cstrip(w.infolist_string(infolist, 'prefix')),
                    cstrip(w.infolist_string(infolist, 'message')),
                    ))
        w.infolist_free(infolist)

    fp.close()

    return w.WEECHAT_RC_OK
Beispiel #6
0
def logexport_export_cmd(buff, exportFile, mustExportLine):
    """ Called when an export function is called, as `/logexport time`.

    `exportFile` is the name of the file to which we will export.

    `mustExportLine`, when fed a message line (ie. `timestamp, prefix, msg`),
    should return `True` iff this line should be included in the export. The
    lines will be fed one by one starting from the most recent.
    When all the lines that should be exported have been checked, this function
    should raise `StopExport` to avoid looping uselessly through all the
    backlog.
    It is eventually called with `None, None, None` when the top of the backlog
    is reached, and could raise an exception at this time if something went
    wrong.
    """

    cBuffer = weechat.hdata_get('buffer')
    lines = weechat.hdata_pointer(cBuffer, buff, 'lines')
    # 'lines' and not 'own_lines': match what the user sees.

    cLine = weechat.hdata_pointer(weechat.hdata_get('lines'), lines,
                                  'last_line')
    hdata_line = weechat.hdata_get('line')
    hdata_line_data = weechat.hdata_get('line_data')

    gathered = []
    reachedTop = True  # Only False if we `break` at some point
    while cLine:
        data = weechat.hdata_pointer(hdata_line, cLine, 'data')
        if data:
            timestamp = weechat.hdata_time(hdata_line_data, data, 'date')
            prefix = weechat.hdata_string(hdata_line_data, data, 'prefix')
            msg = weechat.hdata_string(hdata_line_data, data, 'message')

            try:
                if mustExportLine(timestamp, prefix, msg):
                    gathered.append(LogLine(timestamp, prefix, msg))
            except StopExport:
                reachedTop = False
                break

        cLine = weechat.hdata_pointer(hdata_line, cLine, 'prev_line')

    if reachedTop:
        # Give `mustExportLine` the chance to signal something went wrong.
        mustExportLine(None, None, None)

    html = renderHtml(gathered[::-1], buff)
    writeFile(html, formatFilePath(exportFile))