<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _filter_messages(messages, products=None, levels=None):
 """filter messages for desired products and levels."""

    if products is None:
        products = []
    if levels is None:
        levels = []
    segments = []
    bounds = len(messages)
    for i, message in enumerate(messages):
        if (message[3] in products or len(products) == 0) and \
                (message[4] in levels or len(levels) == 0):
            start = int(message[1])
            if i == (bounds - 1):
                end = None
            else:
                end = int(messages[i+1][1])
            segments.append((start, end))
    return _join(segments)