def say_digits(number):
    """
    10/04/09: called by read_back_caller_number().
    says the phone numbers
    """

    dsh_common_db.init_log(quiet=True)
    funcName = 'dsh_common_agi.say_digits:'
    number = number.strip()

    soundDir = dsh_common_config.lookup('DSH_PROMPT_DIR')
    sorrySound = dsh_config.lookup('DSH_PROMPT_SORRY')
    sorrySound = os.path.join(soundDir, sorrySound)
    
    if not number:
        dsh_agi.say_it(sorrySound)
        return

    try:
        inumber = int(number)
    except:
        message = '%s not a number: %s.' % (funcName, number)
        dsh_agi.report_event(message, reportLevel='ERR')
        dsh_utils.give_bad_news(message, logging.error)
        dsh_agi.say_it(sorrySound)
        return

    datesDir = os.path.join(soundDir, 'dates')
    dateDir = os.path.join(datesDir, 'dates')

    for digit in number:
        digit = '0' + digit
        sound = os.path.join(dateDir, digit)
        dsh_agi.say_it(sound)
def chop_prefix_digits_for_local(callNum, callee):
    """
    10/02/02: chopping of the leading zero is moved from
    dsh_django_utils.generate_dot_call_file().
    also adding code to remove 0522.
    """

    dsh_common_db.init_log(quiet=True)

    #if callee.phone_std:
        #
        # 10/03/13:
        # dial as is.
        #
        #return callNum

    dsh_utils.db_print(
        'chop_prefix_digits_for_local: enter: callNum: ' + callNum, 152)
    if len(callNum) > 1 and (not callee.phone_std) and \
       (not callee.phone_landline) and callNum[0] == '0' and \
       (callNum[1] == '9' or callNum[1] == '8'):
        #
        # it looks like Asterisk doesn't like the leading 0
        # for cell phone numbers, chop it off.
        # 10/03/14:
        # now dealing with phone numbers that start with 8.
        #
        callNum = callNum[1:]
        dsh_utils.db_print(
            'chop_prefix_digits_for_local: return1: callNum: ' + callNum, 152)
        return callNum

    localLandLinePrefix = dsh_db_config.get('local_land_line_prefix')
    if not localLandLinePrefix:
        message = 'chop_prefix_digits_for_local: no local land line prefix.'
        dsh_utils.give_bad_news(message, logging.warning)
        dsh_agi.report_event(message, reportLevel='WRN')
        #
        # try_remove_prefix() will deal with null prefix
        # so no need to worry about anything else than logging message.

    #
    # 10/03/14:
    # should look at the callee.phone_landline flag.
    # but I'm not: it's still legit to look for 0522 and kill it
    # even if it was not explicitly marked as a landline.
    #
    callNum = try_remove_prefix(callNum, localLandLinePrefix)

    dsh_utils.db_print(
        'chop_prefix_digits_for_local: returned ' + callNum, 152)
    return callNum
def person_has_any_message_at_all(caller, 
                                  itemTable, keyWordTable, eventTable,
                                  noLogging=False,
                                  sessionID=''):
    """
    10/03/30:
    called by somewhere from the signal handler in dsh_django2.py.
    called by remove_from_current_dial_if_no_message().
    modeled after dsh_django_utils.check_auto_timed_calls_for_person().
    same idea, except we won't actually put out .call files.
    the idea is this.
    we'll put people in the CDS.
    they get called once.
    if they have no message of any kind waiting for them,
    and they have signed up for auto-dial,
    and they picked up the phone,
    after this one call, we will remove them from the CDS.
    returns (hasMessage, strExplainReason)
    note100331
    """

    dsh_common_db.init_log(quiet=True)
    
    activeBroadcast = dsh_django_utils.should_auto_dial_broadcast(caller)
    if activeBroadcast and activeBroadcast.dummy:
        activeBroadcast = None

    if activeBroadcast:
        return (True, 'broadcast')

    #
    # 10/03/31:
    # the peer-shared issue need to be thought through for doctors.
    # right now, we don't have peer-shared messages,
    # so we don't much care.
    #
    peerShared = dsh_common_db.should_auto_dial_for_peer_share(
        caller, itemTable, keyWordTable, eventTable,
        noLogging=True, sessionID=sessionID)

    if peerShared:
        return (True, 'peer-shared')

    hasPersonal = dsh_common_db.person_has_personalized_message(
        caller, eventTable)

    if hasPersonal:
        return (True, 'personalized')

    return (False, 'nothing')
def remove_from_current_dial_if_no_message(
    caller, itemTable, keyWordTable, eventTable,
    noLogging=False, sessionID=''):
    """
    called by somewhere from the signal handler in dsh_django2.py.
    see note100331.
    """
    
    dsh_common_db.init_log(quiet=True)

    if not caller.current_dial:
        dsh_utils.db_print('remove_from_current_dial_if_no_message: ' +\
                           'not in CDS.', 165)
        return
    
    hasMessage,whatStr = person_has_any_message_at_all(
        caller, itemTable, keyWordTable, eventTable,
        noLogging=noLogging, sessionID=sessionID)

    if hasMessage:
        dsh_utils.db_print('remove_from_current_dial_if_no_message: ' +\
                           'has messages: ' + whatStr, 165)
        return

    caller.current_dial = False

    try:
        caller.save(noLogging=noLogging)
    except:
        message = 'dsh_common_agi.remove_from_current_dial_if_no_message: ' +\
                  'failed to save person: ' + caller.dsh_uid
        dsh_django_utils.error_event(message, item=person, errorLevel='CRT')
        dsh_utils.give_bad_news(message, reportLevel='CRT')
        return

    message = 'dsh_common_agi.remove_from_current_dial_if_no_message: ' +\
              'removed from current dial: ' + caller.__unicode__()
    dsh_agi.report_event(
        message,
        action='RCDS',
        owner=caller,
        sessionID=sessionID)
    dsh_utils.give_news(message, logging.info)
def demo_reply(itemTable, keyWordTable, eventTable,
               fromPhone=False, sessionID=''):
    """
    10/04/09.
    send a demo reply to the most recent caller.
    called by dsh_common_views.send_demo_reply_now() and
    dsh_common_test.test_demo_reply().
    fromPhone=True when invoked from the phone interface.
    False when invoked from the web interface.
    """
    dsh_common_db.init_log(quiet=True)
    response = ''
    recent,msg = dsh_common_db.get_most_recent_message(
        itemTable, sessionID=sessionID)
    response += msg
    success,msg = read_back_caller_number(
        recent, fromPhone=fromPhone, sessionID=sessionID)
    response += msg

    if not success:
        return (False, response)

    #
    # when called from dsh_common_test.test_demo_reply(),
    # it's initiated from the phone.  it will go through this and
    # "go all the way."
    # when it's called by dsh_common_views.send_demo_reply_now(),
    # it's initiated from the web interface.  it doesn't continue here.
    # it'll require a confirmation link, and it will continue in
    # demo_reply_prompt_confirm() where it meets the continuation of the
    # code below.
    #
    if fromPhone:
        demo_reply_prompt_confirm(
            recent, itemTable, keyWordTable, eventTable, sessionID=sessionID)
        
    return (True, response)