nakedmudv3.6/
nakedmudv3.6/lib/
nakedmudv3.6/lib/help/A/
nakedmudv3.6/lib/help/B/
nakedmudv3.6/lib/help/C/
nakedmudv3.6/lib/help/D/
nakedmudv3.6/lib/help/G/
nakedmudv3.6/lib/help/H/
nakedmudv3.6/lib/help/J/
nakedmudv3.6/lib/help/L/
nakedmudv3.6/lib/help/M/
nakedmudv3.6/lib/help/O/
nakedmudv3.6/lib/help/P/
nakedmudv3.6/lib/help/R/
nakedmudv3.6/lib/help/S/
nakedmudv3.6/lib/help/W/
nakedmudv3.6/lib/logs/
nakedmudv3.6/lib/misc/
nakedmudv3.6/lib/players/
nakedmudv3.6/lib/txt/
nakedmudv3.6/lib/world/
nakedmudv3.6/lib/world/examples/
nakedmudv3.6/lib/world/examples/mproto/
nakedmudv3.6/lib/world/examples/oproto/
nakedmudv3.6/lib/world/examples/reset/
nakedmudv3.6/lib/world/examples/rproto/
nakedmudv3.6/lib/world/examples/trigger/
nakedmudv3.6/lib/world/limbo/
nakedmudv3.6/lib/world/limbo/room/
nakedmudv3.6/lib/world/limbo/rproto/
nakedmudv3.6/src/alias/
nakedmudv3.6/src/dyn_vars/
nakedmudv3.6/src/editor/
nakedmudv3.6/src/example_module/
nakedmudv3.6/src/help2/
nakedmudv3.6/src/set_val/
nakedmudv3.6/src/socials/
nakedmudv3.6/src/time/
"""
universal_newline.py

This module makes sure that all newlines are in a universal form (\r\n)
regardless of how people send them out. \r, \n, \n\r, and \r\n all get
transformed to \r\n via this module
"""
import mud, mudsock, hooks



################################################################################
# universal newline hook
################################################################################
def universal_newline_hook(info):
    sock,  = hooks.parse_info(info)
    buf    = sock.outbound_text
    newbuf = []
    cr     = False

    i = 0
    while i < len(buf):
        if not (buf[i] == '\r' or buf[i] == '\n'):
            newbuf.append(buf[i])
        elif buf[i] == '\r':
            if len(newbuf) == 0 or not newbuf[-1] == '\n':
                cr = True
                newbuf.append(buf[i])
        else: # buf[i] == '\n':
            if cr == False:
                newbuf.append('\r')
            cr = False
            newbuf.append(buf[i])
        i = i + 1
    if cr == True:
        newbuf.append('\n')

    sock.outbound_text = ''.join(newbuf)



################################################################################
# initializing and unloading our hooks
################################################################################
hooks.add("process_outbound_text",   universal_newline_hook)
hooks.add("process_outbound_prompt", universal_newline_hook)

def __unload__():
    '''detaches our newline module from the game'''
    hooks.remove("process_outbound_text",   universal_newline_hook)
    hooks.remove("process_outbound_prompt", universal_newline_hook)