예제 #1
0
int
read_command_prep()
{
    int		i;
    char       *fname;
    MAILSTREAM *m;

    /*
     * Before we sniff at the input queue, make sure no external event's
     * changed our picture of the message sequence mapping.  If so,
     * recalculate the dang thing and run thru whatever processing loop
     * we're in again...
     */
    for(i = 0; i < ps_global->s_pool.nstream; i++){
	m = ps_global->s_pool.streams[i];
	if(m && sp_flagged(m, SP_LOCKED) && sp_flagged(m, SP_USERFLDR)
	   && sp_expunge_count(m)){
	    fname = STREAMNAME(m);
	    q_status_message3(SM_ORDER, 3, 3,
			      "%s message%s expunged from folder \"%s\"",
			      long2string(sp_expunge_count(m)),
			      plural(sp_expunge_count(m)),
			      pretty_fn(fname));
	    sp_set_expunge_count(m, 0L);
	    display_message('x');
	}
    }

    if(sp_mail_box_changed(ps_global->mail_stream)
       && sp_new_mail_count(ps_global->mail_stream)){
        dprint((2, "Noticed %ld new msgs! \n",
		   sp_new_mail_count(ps_global->mail_stream)));
	return(FALSE);		/* cycle thru so caller can update */
    }

    return(TRUE);
}
예제 #2
0
int
optionally_enter(char *utf8string, int y_base, int x_base, int utf8string_size,
		 char *utf8prompt, ESCKEY_S *escape_list, HelpType help, int *flags)
{
    UCS           *string = NULL, ucs;
    size_t         string_size;
    UCS           *s2;
    UCS           *saved_original = NULL;
    char          *candidate;
    UCS           *kill_buffer = NULL;
    UCS           *k, *kb;
    int            field_pos;		/* offset into array dline.vl */
    int            i, j, return_v, cols, prompt_width, too_thin,
                   real_y_base, km_popped, passwd;
    char         **help_text;
    long	   fkey_table[12];
    struct	   key_menu *km;
    bitmap_t	   bitmap;
    COLOR_PAIR    *lastc = NULL, *promptc = NULL;
    struct variable *vars = ps_global->vars;
    struct display_line dline;
#ifdef	_WINDOWS
    int		   cursor_shown;
#endif

    dprint((5, "=== optionally_enter called ===\n"));
    dprint((9, "utf8string:\"%s\"  y:%d  x:%d  length: %d append: %d\n",
               utf8string ? utf8string : "",
	       x_base, y_base, utf8string_size,
	       (flags && *flags & OE_APPEND_CURRENT)));
    dprint((9, "passwd:%d   utf8prompt:\"%s\"   label:\"%s\"\n",
	       (flags && *flags & OE_PASSWD_NOAST) ? 10 :
		   (flags && *flags & OE_PASSWD) ? 1 : 0,
	       utf8prompt ? utf8prompt : "",
	       (escape_list && escape_list[0].ch != -1 && escape_list[0].label)
		 ? escape_list[0].label: ""));

    if(!ps_global->ttyo)
      return(pre_screen_config_opt_enter(utf8string, utf8string_size, utf8prompt,
					 escape_list, help, flags));

#ifdef _WINDOWS
    if (mswin_usedialog ())
      return(win_dialog_opt_enter(utf8string, utf8string_size, utf8prompt,
				  escape_list, help, flags));
#endif


    /*
     * Utf8string comes in as UTF-8. We'll convert it to a UCS-4 array and operate on
     * that array, then convert it back before returning. Utf8string_size is the size
     * of the utf8string array but that doesn't help us much for the array we need to
     * operate on here. We'll just allocate a big array and then cut it off when
     * sending it back.
     *
     * This should come before the specialized calls above but those aren't
     * converted to use UCS-4 yet.
     */
    string = utf8_to_ucs4_cpystr(utf8string);
    dline.vused = ucs4_strlen(string);

    string_size = (2 * MAX(utf8string_size,dline.vused) + 100);
    fs_resize((void **) &string, string_size * sizeof(UCS));

    suspend_busy_cue();
    cols         = ps_global->ttyo->screen_cols;
    prompt_width = utf8_width(utf8prompt);
    too_thin   = 0;
    km_popped  = 0;
    if(y_base > 0)
      real_y_base = y_base;
    else{
        real_y_base = y_base + ps_global->ttyo->screen_rows;
	real_y_base = MAX(real_y_base, 0);
    }

    flush_ordered_messages();
    mark_status_dirty();

    if(flags && *flags & OE_APPEND_CURRENT) /* save a copy in case of cancel */
      saved_original = ucs4_cpystr(string);

    /*
     * build the function key mapping table, skipping predefined keys...
     */
    memset(fkey_table, NO_OP_COMMAND, 12 * sizeof(long));
    for(i = 0, j = 0; escape_list && escape_list[i].ch != -1 && i+j < 12; i++){
	if(i+j == OE_HELP_KEY)
	  j++;

	if(i+j == OE_CANCEL_KEY)
	  j++;

	if(i+j == OE_ENTER_KEY)
	  j++;

	fkey_table[i+j] = escape_list[i].ch;
    }

    /* assumption that HelpType is char **  */
    help_text = help;
    if(help_text){			/*---- Show help text -----*/
	int width = ps_global->ttyo->screen_cols - x_base;

	if(FOOTER_ROWS(ps_global) == 1){
	    km_popped++;
	    FOOTER_ROWS(ps_global) = 3;
	    clearfooter(ps_global);

	    y_base = -3;
	    real_y_base = y_base + ps_global->ttyo->screen_rows;
	}

	for(j = 0; j < 2 && help_text[j]; j++){
	    MoveCursor(real_y_base + 1 + j, x_base);
	    CleartoEOLN();

	    if(width < utf8_width(help_text[j])){
		char *tmp = cpystr(help_text[j]);
		(void) utf8_truncate(tmp, width);
		PutLine0(real_y_base + 1 + j, x_base, tmp);
		fs_give((void **) &tmp);
	    }
	    else
	      PutLine0(real_y_base + 1 + j, x_base, help_text[j]);
	}
    }
    else{
	clrbitmap(bitmap);
	clrbitmap((km = &oe_keymenu)->bitmap);		/* force formatting */
	if(!(flags && (*flags) & OE_DISALLOW_HELP))
	  setbitn(OE_HELP_KEY, bitmap);

	setbitn(OE_ENTER_KEY, bitmap);
	if(!(flags && (*flags) & OE_DISALLOW_CANCEL))
	  setbitn(OE_CANCEL_KEY, bitmap);

	setbitn(OE_CTRL_T_KEY, bitmap);

        /*---- Show the usual possible keys ----*/
	for(i=0,j=0; escape_list && escape_list[i].ch != -1 && i+j < 12; i++){
	    if(i+j == OE_HELP_KEY)
	      j++;

	    if(i+j == OE_CANCEL_KEY)
	      j++;

	    if(i+j == OE_ENTER_KEY)
	      j++;

	    oe_keymenu.keys[i+j].label = escape_list[i].label;
	    oe_keymenu.keys[i+j].name = escape_list[i].name;
	    setbitn(i+j, bitmap);
	}

	for(i = i+j; i < 12; i++)
	  if(!(i == OE_HELP_KEY || i == OE_ENTER_KEY || i == OE_CANCEL_KEY))
	    oe_keymenu.keys[i].name = NULL;

	draw_keymenu(km, bitmap, cols, 1-FOOTER_ROWS(ps_global), 0, FirstMenu);
    }
    
    if(pico_usingcolor() && VAR_PROMPT_FORE_COLOR &&
       VAR_PROMPT_BACK_COLOR &&
       pico_is_good_color(VAR_PROMPT_FORE_COLOR) &&
       pico_is_good_color(VAR_PROMPT_BACK_COLOR)){
	lastc = pico_get_cur_color();
	if(lastc){
	    promptc = new_color_pair(VAR_PROMPT_FORE_COLOR,
				     VAR_PROMPT_BACK_COLOR);
	    (void)pico_set_colorp(promptc, PSC_NONE);
	}
    }
    else
      StartInverse();

    /*
     * if display length isn't wide enough to support input,
     * shorten up the prompt...
     */
    if((dline.dwid = cols - (x_base + prompt_width)) < MIN_OPT_ENT_WIDTH){
	char *p;
	unsigned got_width;

	/*
	 * Scoot prompt pointer forward at least (MIN_OPT_ENT_WIDTH - dline.dwid) screencells.
	 */
	p = utf8_count_forw_width(utf8prompt, MIN_OPT_ENT_WIDTH-dline.dwid, &got_width);
	if(got_width < MIN_OPT_ENT_WIDTH-dline.dwid)
	  p = utf8_count_forw_width(utf8prompt, MIN_OPT_ENT_WIDTH+1-dline.dwid, &got_width);

	if(p){
	    prompt_width = utf8_width(p);
	    dline.dwid =  cols - (x_base + prompt_width);
	    utf8prompt = p;
	}
    }

    /*
     * How many UCS-4 characters will we need to make up the width dwid? It could be
     * unlimited because of zero-width characters, I suppose, but realistically it
     * isn't going to be much more than dwid.
     */
    dline.dlen = 2 * dline.dwid + 100;

    dline.dl    = (UCS *) fs_get(dline.dlen * sizeof(UCS));
    dline.olddl = (UCS *) fs_get(dline.dlen * sizeof(UCS));
    memset(dline.dl,    0, dline.dlen * sizeof(UCS));
    memset(dline.olddl, 0, dline.dlen * sizeof(UCS));

    dline.movecursor = MoveCursor;
    dline.writechar  = Writewchar;

    dline.row   = real_y_base;
    dline.col   = x_base + prompt_width;

    dline.vl    = string;
    dline.vlen  = --string_size;		/* -1 for terminating zero */
    dline.vbase = field_pos = 0;

#ifdef	_WINDOWS
    cursor_shown = mswin_showcaret(1);
#endif
    
    PutLine0(real_y_base, x_base, utf8prompt);

    /*
     * If appending, position field_pos at end of input.
     */
    if(flags && *flags & OE_APPEND_CURRENT)
      while(string[field_pos])
	field_pos++;

    passwd = (flags && *flags & OE_PASSWD_NOAST) ? 10 :
              (flags && *flags & OE_PASSWD)       ?  1 : 0;
    line_paint(field_pos, &dline, &passwd);

    /*----------------------------------------------------------------------
      The main loop
	loops until someone sets the return_v.
      ----------------------------------------------------------------------*/
    return_v = -10;

    while(return_v == -10) {

#ifdef	MOUSE
	mouse_in_content(KEY_MOUSE, -1, -1, 0x5, 0);
	register_mfunc(mouse_in_content, 
		       real_y_base, x_base + prompt_width,
		       real_y_base, ps_global->ttyo->screen_cols);
#endif
#ifdef	_WINDOWS
	mswin_allowpaste(MSWIN_PASTE_LINE);
	g_mc_row = real_y_base;
	g_mc_col = x_base + prompt_width;
	mswin_mousetrackcallback(pcpine_oe_cursor);
#endif

	/* Timeout 10 min to keep imap mail stream alive */
	ps_global->conceal_sensitive_debugging = passwd ? 1 : 0;
        ucs = read_char(600);
	ps_global->conceal_sensitive_debugging = 0;

#ifdef	MOUSE
	clear_mfunc(mouse_in_content);
#endif
#ifdef	_WINDOWS
	mswin_allowpaste(MSWIN_PASTE_DISABLE);
	mswin_mousetrackcallback(NULL);
#endif

	/*
	 * Don't want to intercept all characters if typing in passwd.
	 * We select an ad hoc set that we will catch and let the rest
	 * through.  We would have caught the set below in the big switch
	 * but we skip the switch instead.  Still catch things like ^K,
	 * DELETE, ^C, RETURN.
	 */
	if(passwd)
	  switch(ucs){
            case ctrl('F'):  
	    case KEY_RIGHT:
            case ctrl('B'):
	    case KEY_LEFT:
            case ctrl('U'):
            case ctrl('A'):
	    case KEY_HOME:
            case ctrl('E'):
	    case KEY_END:
	    case TAB:
	      goto ok_for_passwd;
	  }

        if(too_thin && ucs != KEY_RESIZE && ucs != ctrl('Z') && ucs != ctrl('C'))
          goto bleep;

	switch(ucs){

	    /*--------------- KEY RIGHT ---------------*/
          case ctrl('F'):  
	  case KEY_RIGHT:
	    if(field_pos >= string_size || string[field_pos] == '\0')
              goto bleep;

	    line_paint(++field_pos, &dline, &passwd);
	    break;

	    /*--------------- KEY LEFT ---------------*/
          case ctrl('B'):
	  case KEY_LEFT:
	    if(field_pos <= 0)
	      goto bleep;

	    line_paint(--field_pos, &dline, &passwd);
	    break;

          /*-------------------- WORD SKIP --------------------*/
	  case ctrl('@'):
	    /*
	     * Note: read_char *can* return NO_OP_COMMAND which is
	     * the def'd with the same value as ^@ (NULL), BUT since
	     * read_char has a big timeout (>25 secs) it won't.
	     */

	    /* skip thru current word */
	    while(string[field_pos]
		  && isalnum((unsigned char) string[field_pos]))
	      field_pos++;

	    /* skip thru current white space to next word */
	    while(string[field_pos]
		  && !isalnum((unsigned char) string[field_pos]))
	      field_pos++;

	    line_paint(field_pos, &dline, &passwd);
	    break;

          /*--------------------  RETURN --------------------*/
	  case PF4:
	    if(F_OFF(F_USE_FK,ps_global)) goto bleep;
	  case ctrl('J'): 
	  case ctrl('M'): 
	    return_v = 0;
	    break;

          /*-------------------- Destructive backspace --------------------*/
	  case '\177': /* DEL */
	  case ctrl('H'):
            /*   Try and do this with by telling the terminal to delete a
                 a character. If that fails, then repaint the rest of the
                 line, acheiving the same much less efficiently
             */
	    if(field_pos <= 0)
	      goto bleep;

	    field_pos--;
	    /* drop thru to pull line back ... */

          /*-------------------- Delete char --------------------*/
	  case ctrl('D'): 
	  case KEY_DEL: 
            if(field_pos >= string_size || !string[field_pos])
	      goto bleep;

	    dline.vused--;
	    for(s2 = &string[field_pos]; *s2 != 0; s2++)
	      *s2 = s2[1];

	    *s2 = 0;			/* Copy last NULL */
	    line_paint(field_pos, &dline, &passwd);
	    if(flags)		/* record change if requested  */
	      *flags |= OE_USER_MODIFIED;

	    break;

            /*--------------- Kill line -----------------*/
          case ctrl('K'):
            if(kill_buffer != NULL)
              fs_give((void **) &kill_buffer);

	    if(field_pos != 0 || string[0]){
		if(!passwd && F_ON(F_DEL_FROM_DOT, ps_global))
		  dline.vused -= ucs4_strlen(&string[i = field_pos]);
		else
		  dline.vused = i = 0;

		kill_buffer = ucs4_cpystr(&string[field_pos = i]);
		string[field_pos] = '\0';
		line_paint(field_pos, &dline, &passwd);
		if(flags)		/* record change if requested  */
		  *flags |= OE_USER_MODIFIED;
	    }

            break;

            /*------------------- Undelete line --------------------*/
          case ctrl('U'):
            if(kill_buffer == NULL)
              goto bleep;

            /* Make string so it will fit */
            kb = ucs4_cpystr(kill_buffer);
            if(ucs4_strlen(kb) + ucs4_strlen(string) > string_size) 
                kb[string_size - ucs4_strlen(string)] = '\0';
                       
            if(string[field_pos] == '\0') {
                /*--- adding to the end of the string ----*/
                for(k = kb; *k; k++)
		  string[field_pos++] = *k;

                string[field_pos] = '\0';
            }
	    else{
		int shift;

		shift = ucs4_strlen(kb);

		/* shift field_pos ... end to right */
		for(k = &string[field_pos] + ucs4_strlen(&string[field_pos]);
		    k >= &string[field_pos]; k--)
		  *(k+shift) = *k;

                for(k = kb; *k; k++)
		  string[field_pos++] = *k;
            }

	    if(*kb && flags)		/* record change if requested  */
	      *flags |= OE_USER_MODIFIED;

	    dline.vused = ucs4_strlen(string);
            fs_give((void **) &kb);
	    line_paint(field_pos, &dline, &passwd);
            break;
            
	    /*-------------------- Interrupt --------------------*/
	  case ctrl('C'): /* ^C */ 
	    if(F_ON(F_USE_FK,ps_global) || (flags && ((*flags) & OE_DISALLOW_CANCEL)))
	      goto bleep;

	    goto cancel;

	  case PF2:
	    if(F_OFF(F_USE_FK,ps_global) || (flags && ((*flags) & OE_DISALLOW_CANCEL)))
	      goto bleep;

	  cancel:
	    return_v = 1;
	    if(saved_original){
		for(i = 0; saved_original[i]; i++)
		  string[i] = saved_original[i];

		string[i] = 0;
	    }

	    break;

          case ctrl('A'):
	  case KEY_HOME:
            /*-------------------- Start of line -------------*/
	    line_paint(field_pos = 0, &dline, &passwd);
            break;

          case ctrl('E'):
	  case KEY_END:
            /*-------------------- End of line ---------------*/
	    line_paint(field_pos = dline.vused, &dline, &passwd);
            break;

	    /*-------------------- Help --------------------*/
	  case ctrl('G') : 
	  case PF1:
	    if(flags && ((*flags) & OE_DISALLOW_HELP))
	      goto bleep;
	    else if(FOOTER_ROWS(ps_global) == 1 && km_popped == 0){
		km_popped++;
		FOOTER_ROWS(ps_global) = 3;
		clearfooter(ps_global);
		if(lastc)
		  (void)pico_set_colorp(lastc, PSC_NONE);
		else
		  EndInverse();

		draw_keymenu(km, bitmap, cols, 1-FOOTER_ROWS(ps_global),
			     0, FirstMenu);

		if(promptc)
		  (void)pico_set_colorp(promptc, PSC_NONE);
		else
		  StartInverse();

		mark_keymenu_dirty();
		y_base = -3;
		dline.row = real_y_base = y_base + ps_global->ttyo->screen_rows;
		PutLine0(real_y_base, x_base, utf8prompt);
		memset(dline.dl,    0, dline.dlen * sizeof(UCS));
		memset(dline.olddl, 0, dline.dlen * sizeof(UCS));
		line_paint(field_pos, &dline, &passwd);
		break;
	    }

	    if(FOOTER_ROWS(ps_global) > 1){
		mark_keymenu_dirty();
		return_v = 3;
	    }
	    else
	      goto bleep;

	    break;


#ifdef	MOUSE
			    /* Mouse support untested in pine 5.00 */
	  case KEY_MOUSE :
	    {
	      MOUSEPRESS mp;
	      int w;

	      mouse_get_last (NULL, &mp);

	      switch(mp.button){
		case M_BUTTON_LEFT :			/* position cursor */
		  mp.col -= dline.col;

		  /*
		   * We have to figure out which character is under the cursor.
		   * This is complicated by the fact that characters may
		   * be other than one cell wide.
		   */

		  /* the -1 is for the '<' when text is offscreen left */
		  w = (dline.vbase > 0) ? mp.col-1 : mp.col;

		  if(mp.col <= 0)
		    field_pos = dline.vbase - 1;
		  else{
		    if(dline.vused <= dline.vbase
		       || ucs4_str_width_a_to_b(dline.vl,dline.vbase,dline.vused-1) <= w)
		      field_pos = dline.vused;
		    else{
		      /*
		       * Find index of 1st character that causes the
		       * width to be > w.
		       */
		      for(i = 0;
			  ucs4_str_width_a_to_b(dline.vl,dline.vbase,dline.vbase+i) <= w;
			  i++)
			;

		      field_pos = dline.vbase + i;
		    }
		  }
		  
		  field_pos = MIN(MAX(field_pos, 0), dline.vused);

		  /* just allow line_paint to choose vbase */
		  line_paint(field_pos, &dline, &passwd);
		  break;

		case M_BUTTON_RIGHT :
#ifdef	_WINDOWS

		  /*
		   * Same as M_BUTTON_LEFT except we paste in text after
		   * moving the cursor.
		   */

		  mp.col -= dline.col;

		  /* the -1 is for the '<' when text is offscreen left */
		  w = (dline.vbase > 0) ? mp.col-1 : mp.col;

		  if(mp.col <= 0)
		    field_pos = dline.vbase - 1;
		  else{
		    if(dline.vused <= dline.vbase
		       || ucs4_str_width_a_to_b(dline.vl,dline.vbase,dline.vused-1) <= w)
		      field_pos = dline.vused;
		    else{
		      /*
		       * Find index of 1st character that causes the
		       * width to be > w.
		       */
		      for(i = 0;
			  ucs4_str_width_a_to_b(dline.vl,dline.vbase,dline.vbase+i) <= w;
			  i++)
			;

		      field_pos = dline.vbase + i;
		    }
		  }
		  
		  field_pos = MIN(MAX(field_pos, 0), dline.vused);

		  line_paint(field_pos, &dline, &passwd);

		  mswin_allowpaste(MSWIN_PASTE_LINE);
		  mswin_paste_popup();
		  mswin_allowpaste(MSWIN_PASTE_DISABLE);
		  break;
#endif

		case M_BUTTON_MIDDLE :			/* NO-OP for now */
		default:				/* just ignore */
		  break;
	      }
	    }

	    break;
#endif


          case NO_OP_IDLE:
	    /*
	     * Keep mail stream alive by checking for new mail.
	     * If we're asking for a password in a login prompt
	     * we don't want to check for new_mail because the
	     * new mail check might be what got us here in the first
	     * place (because of a filter trying to save a message).
	     * If we need to wait for the user to come back then
	     * the caller will just have to deal with the failure
	     * to login.
	     */
	    i = -1;
	    if(!ps_global->no_newmail_check_from_optionally_enter)
	      i = new_mail(0, 2, NM_DEFER_SORT);

	    if(sp_expunge_count(ps_global->mail_stream) &&
	       flags && ((*flags) & OE_SEQ_SENSITIVE))
	      goto cancel;

	    if(i < 0){
	      line_paint(field_pos, &dline, &passwd);
	      break;			/* no changes, get on with life */
	    }
	    /* Else fall into redraw */

	    /*-------------------- Redraw --------------------*/
	  case ctrl('L'):
            /*---------------- re size ----------------*/
          case KEY_RESIZE:
            
	    dline.row = real_y_base = y_base > 0 ? y_base :
					 y_base + ps_global->ttyo->screen_rows;
	    if(lastc)
	      (void)pico_set_colorp(lastc, PSC_NONE);
	    else
	      EndInverse();

            ClearScreen();
            redraw_titlebar();
            if(ps_global->redrawer != (void (*)(void))NULL)
              (*ps_global->redrawer)();

            redraw_keymenu();
	    if(promptc)
	      (void)pico_set_colorp(promptc, PSC_NONE);
	    else
	      StartInverse();
            
            PutLine0(real_y_base, x_base, utf8prompt);
            cols     =  ps_global->ttyo->screen_cols;
            too_thin = 0;
            if(cols < x_base + prompt_width + 4){
		Writechar(BELL, 0);
                PutLine0(real_y_base, 0, "Screen's too thin. Ouch!");
                too_thin = 1;
            }
	    else{
		dline.col  = x_base + prompt_width;
		dline.dwid = cols - (x_base + prompt_width);
		dline.dlen = 2 * dline.dwid + 100;
		fs_resize((void **) &dline.dl, (size_t) dline.dlen * sizeof(UCS));
		fs_resize((void **) &dline.olddl, (size_t) dline.dlen * sizeof(UCS));
		memset(dline.dl,    0, dline.dlen * sizeof(UCS));
		memset(dline.olddl, 0, dline.dlen * sizeof(UCS));
		line_paint(field_pos, &dline, &passwd);
            }

            fflush(stdout);

            dprint((9,
                    "optionally_enter  RESIZE new_cols:%d  too_thin: %d\n",
                       cols, too_thin));
            break;

	  case PF3 :		/* input to potentially remap */
	  case PF5 :
	  case PF6 :
	  case PF7 :
	  case PF8 :
	  case PF9 :
	  case PF10 :
	  case PF11 :
	  case PF12 :
	      if(F_ON(F_USE_FK,ps_global)
		 && fkey_table[ucs - PF1] != NO_OP_COMMAND)
		ucs = fkey_table[ucs - PF1]; /* remap function key input */
  
          default:
	    if(escape_list){		/* in the escape key list? */
		for(j=0; escape_list[j].ch != -1; j++){
		    if(escape_list[j].ch == ucs){
			return_v = escape_list[j].rval;
			break;
		    }
		}

		if(return_v != -10)
		  break;
	    }

	    if(ucs < 0x80 && FILTER_THIS((unsigned char) ucs)){
       bleep:
		putc(BELL, stdout);
		continue;
	    }

       ok_for_passwd:
	    /*--- Insert a character -----*/
	    if(dline.vused >= string_size)
	      goto bleep;

	    /*---- extending the length of the string ---*/
	    for(s2 = &string[++dline.vused]; s2 - string > field_pos; s2--)
	      *s2 = *(s2-1);

	    string[field_pos++] = ucs;
	    line_paint(field_pos, &dline, &passwd);
	    if(flags)		/* record change if requested  */
	      *flags |= OE_USER_MODIFIED;
		    
	}   /*---- End of switch on char ----*/
    }

#ifdef	_WINDOWS
    if(!cursor_shown)
      mswin_showcaret(0);
#endif

    if(dline.dl)
      fs_give((void **) &dline.dl);

    if(dline.olddl)
      fs_give((void **) &dline.olddl);

    if(saved_original) 
      fs_give((void **) &saved_original);

    if(kill_buffer)
      fs_give((void **) &kill_buffer);

    /*
     * Change string back into UTF-8.
     */
    candidate = ucs4_to_utf8_cpystr(string);

    if(string) 
      fs_give((void **) &string);

    if(candidate){
	strncpy(utf8string, candidate, utf8string_size);
	utf8string[utf8string_size-1] = '\0';
	fs_give((void **) &candidate);
    }

    if (!(flags && (*flags) & OE_KEEP_TRAILING_SPACE))
      removing_trailing_white_space(utf8string);

    if(lastc){
	(void)pico_set_colorp(lastc, PSC_NONE);
	free_color_pair(&lastc);
	if(promptc)
	  free_color_pair(&promptc);
    }
    else
      EndInverse();

    MoveCursor(real_y_base, x_base); /* Move the cursor to show we're done */
    fflush(stdout);
    resume_busy_cue(0);
    if(km_popped){
	FOOTER_ROWS(ps_global) = 1;
	clearfooter(ps_global);
	ps_global->mangled_body = 1;
    }

    return(return_v);
}
예제 #3
0
파일: radio.c 프로젝트: nysan/alpine
/*----------------------------------------------------------------------
    Prompt user for a choice among alternatives

Args --  utf8prompt:    The prompt for the question/selection
         line:      The line to prompt on, if negative then relative to bottom
         esc_list:  ESC_KEY_S list of keys
         dflt:	    The selection when the <CR> is pressed (should probably
		      be one of the chars in esc_list)
         on_ctrl_C: The selection when ^C is pressed
         help_text: Text to be displayed on bottom two lines
	 flags:     Logically OR'd flags modifying our behavior to:
		RB_FLUSH_IN    - Discard any pending input chars.
		RB_ONE_TRY     - Only give one chance to answer.  Returns
				 on_ctrl_C value if not answered acceptably
				 on first try.
		RB_NO_NEWMAIL  - Quell the usual newmail check.
		RB_SEQ_SENSITIVE - The caller is sensitive to sequence number
				   changes so return on_ctrl_C if an
				   unsolicited expunge happens while we're
				   viewing a message.
		RB_RET_HELP    - Instead of the regular internal handling
				 way of handling help_text, this just causes
				 radio_buttons to return 3 when help is
				 asked for, so that the caller handles it
				 instead.
	
	 Note: If there are enough keys in the esc_list to need a second
	       screen, and there is no help, then the 13th key will be
	       put in the help position.

Result -- Returns the letter pressed. Will be one of the characters in the
          esc_list argument, or dflt, or on_ctrl_C, or SEQ_EXCEPTION.

This will pause for any new status message to be seen and then prompt the user.
The prompt will be truncated to fit on the screen. Redraw and resize are
handled along with ^Z suspension. Typing ^G will toggle the help text on and
off. Character types that are not buttons will result in a beep (unless one_try
is set).
  ----*/
int
radio_buttons(char *utf8prompt, int line, ESCKEY_S *esc_list, int dflt,
	      int on_ctrl_C, HelpType help_text, int flags)
{
    UCS              ucs;
    register int     ch, real_line;
    char            *q, *ds = NULL;
    unsigned         maxcol;
    int              max_label, i, start, fkey_table[12];
    int		     km_popped = 0;
    struct key	     rb_keys[12];
    struct key_menu  rb_keymenu;
    bitmap_t	     bitmap;
    struct variable *vars = ps_global->vars;
    COLOR_PAIR      *lastc = NULL, *promptc = NULL;

#ifdef	_WINDOWS
    int		     cursor_shown;

    if (mswin_usedialog()){
	MDlgButton button_list[25];
	LPTSTR     free_names[25];
	LPTSTR     free_labels[25];
	int        b, i, ret;
	char     **help;

	memset(&free_names, 0, sizeof(LPTSTR) * 25);
	memset(&free_labels, 0, sizeof(LPTSTR) * 25);
	memset(&button_list, 0, sizeof (MDlgButton) * 25);
	b = 0;

	if(flags & RB_RET_HELP){
	    if(help_text != NO_HELP)
	      alpine_panic("RET_HELP and help in radio_buttons!");

	    button_list[b].ch = '?';
	    button_list[b].rval = 3;
	    button_list[b].name = TEXT("?");
	    free_labels[b] = utf8_to_lptstr(N_("Help"));
	    button_list[b].label = free_labels[b];
	    ++b;
	}

	for(i = 0; esc_list && esc_list[i].ch != -1 && i < 23; ++i){
	  if(esc_list[i].ch != -2){
	    button_list[b].ch = esc_list[i].ch;
	    button_list[b].rval = esc_list[i].rval;
	    free_names[b] = utf8_to_lptstr(esc_list[i].name);
	    button_list[b].name = free_names[b];
	    free_labels[b] = utf8_to_lptstr(esc_list[i].label);
	    button_list[b].label = free_labels[b];
	    ++b;
	  }
	}

	button_list[b].ch = -1;
	
	/* assumption here is that HelpType is char **  */
	help = help_text;

	ret = mswin_select(utf8prompt, button_list, dflt, on_ctrl_C, help, flags);
	for(i = 0; i < 25; i++){
	    if(free_names[i])
	      fs_give((void **) &free_names[i]);
	    if(free_labels[i])
	      fs_give((void **) &free_labels[i]);
	}

	return (ret);
    }

#endif /* _WINDOWS */

    suspend_busy_cue();
    flush_ordered_messages();		/* show user previous status msgs */
    mark_status_dirty();		/* clear message next display call */
    real_line = line > 0 ? line : ps_global->ttyo->screen_rows + line;
    MoveCursor(real_line, RAD_BUT_COL);
    CleartoEOLN();

    /*---- Find widest label ----*/
    max_label = 0;
    for(i = 0; esc_list && esc_list[i].ch != -1 && i < 11; i++){
      if(esc_list[i].ch == -2) /* -2 means to skip this key and leave blank */
	continue;
      if(esc_list[i].name)
        max_label = MAX(max_label, utf8_width(esc_list[i].name));
    }

    if(ps_global->ttyo->screen_cols - max_label - 1 > 0)
      maxcol = ps_global->ttyo->screen_cols - max_label - 1;
    else
      maxcol = 0;

    /*
     * We need to be able to truncate q, so copy it in case it is
     * a readonly string.
     */
    q = cpystr(utf8prompt);

    /*---- Init structs for keymenu ----*/
    for(i = 0; i < 12; i++)
      memset((void *)&rb_keys[i], 0, sizeof(struct key));

    memset((void *)&rb_keymenu, 0, sizeof(struct key_menu));
    rb_keymenu.how_many = 1;
    rb_keymenu.keys     = rb_keys;

    /*---- Setup key menu ----*/
    start = 0;
    clrbitmap(bitmap);
    memset(fkey_table, NO_OP_COMMAND, 12 * sizeof(int));
    if(flags & RB_RET_HELP && help_text != NO_HELP)
      alpine_panic("RET_HELP and help in radio_buttons!");

    /* if shown, always at position 0 */
    if(help_text != NO_HELP || flags & RB_RET_HELP){
	rb_keymenu.keys[0].name  = "?";
	rb_keymenu.keys[0].label = N_("Help");
	setbitn(0, bitmap);
	fkey_table[0] = ctrl('G');
	start++;
    }

    if(on_ctrl_C){
	rb_keymenu.keys[1].name  = "^C";
	rb_keymenu.keys[1].label = N_("Cancel");
	setbitn(1, bitmap);
	fkey_table[1] = ctrl('C');
	start++;
    }

    start = start ? 2 : 0;
    /*---- Show the usual possible keys ----*/
    for(i=start; esc_list && esc_list[i-start].ch != -1; i++){
	/*
	 * If we have an esc_list item we'd like to put in the non-existent
	 * 13th slot, and there is no help, we put it in the help slot
	 * instead.  We're hacking now...!
	 *
	 * We may also have invisible esc_list items that don't show up
	 * on the screen.  We use this when we have two different keys
	 * which are synonyms, like ^P and KEY_UP.  If all the slots are
	 * already full we can still fit invisible keys off the screen to
	 * the right.  A key is invisible if it's label is "".
	 */
	if(i >= 12){
	    if(esc_list[i-start].label
	       && esc_list[i-start].label[0] != '\0'){  /* visible */
		if(i == 12){  /* special case where we put it in help slot */
		    if(help_text != NO_HELP)
		  alpine_panic("Programming botch in radio_buttons(): too many keys");

		    if(esc_list[i-start].ch != -2)
		      setbitn(0, bitmap); /* the help slot */

		    fkey_table[0] = esc_list[i-start].ch;
		    rb_keymenu.keys[0].name  = esc_list[i-start].name;
		    if(esc_list[i-start].ch != -2
		       && esc_list[i-start].rval == dflt
		       && esc_list[i-start].label){
		        size_t l;

			l = strlen(esc_list[i-start].label) + 2;
			ds = (char *)fs_get((l+1) * sizeof(char));
			snprintf(ds, l+1, "[%s]", esc_list[i-start].label);
			ds[l] = '\0';
			rb_keymenu.keys[0].label = ds;
		    }
		    else
		      rb_keymenu.keys[0].label = esc_list[i-start].label;
		}
		else
		  alpine_panic("Botch in radio_buttons(): too many keys");
	    }
	}
	else{
	    if(esc_list[i-start].ch != -2)
	      setbitn(i, bitmap);

	    fkey_table[i] = esc_list[i-start].ch;
	    rb_keymenu.keys[i].name  = esc_list[i-start].name;
	    if(esc_list[i-start].ch != -2
	       && esc_list[i-start].rval == dflt
	       && esc_list[i-start].label){
	        size_t l;

		l = strlen(esc_list[i-start].label) + 2;
		ds = (char *)fs_get((l+1) * sizeof(char));
		snprintf(ds, l+1, "[%s]", esc_list[i-start].label);
		ds[l] = '\0';
		rb_keymenu.keys[i].label = ds;
	    }
	    else
	      rb_keymenu.keys[i].label = esc_list[i-start].label;
	}
    }

    for(; i < 12; i++)
      rb_keymenu.keys[i].name = NULL;

    ps_global->mangled_footer = 1;

#ifdef	_WINDOWS
    cursor_shown = mswin_showcaret(1);
#endif

    if(pico_usingcolor() && VAR_PROMPT_FORE_COLOR &&
       VAR_PROMPT_BACK_COLOR &&
       pico_is_good_color(VAR_PROMPT_FORE_COLOR) &&
       pico_is_good_color(VAR_PROMPT_BACK_COLOR)){
	lastc = pico_get_cur_color();
	if(lastc){
	    promptc = new_color_pair(VAR_PROMPT_FORE_COLOR,
				     VAR_PROMPT_BACK_COLOR);
	    (void)pico_set_colorp(promptc, PSC_NONE);
	}
    }
    else
      StartInverse();

    draw_radio_prompt(real_line, RAD_BUT_COL, maxcol, q);

    while(1){
        fflush(stdout);

	/*---- Paint the keymenu ----*/
	if(lastc)
	  (void)pico_set_colorp(lastc, PSC_NONE);
	else
	  EndInverse();

	draw_keymenu(&rb_keymenu, bitmap, ps_global->ttyo->screen_cols,
		     1 - FOOTER_ROWS(ps_global), 0, FirstMenu);
	if(promptc)
	  (void)pico_set_colorp(promptc, PSC_NONE);
	else
	  StartInverse();

	MoveCursor(real_line, MIN(RAD_BUT_COL+utf8_width(q), maxcol+1));

	if(flags & RB_FLUSH_IN)
	  flush_input();

  newcmd:
	/* Timeout 5 min to keep imap mail stream alive */
        ucs = read_char(600);
        dprint((2,
                   "Want_to read: %s (0x%x)\n", pretty_command(ucs), ucs));
	if((ucs < 0x80) && isupper((unsigned char) ucs))
	  ucs = tolower((unsigned char) ucs);

	if(F_ON(F_USE_FK,ps_global)
	   && (((ucs < 0x80) && isalpha((unsigned char) ucs) && !strchr("YyNn",(int) ucs))
	       || ((ucs >= PF1 && ucs <= PF12)
		   && (ucs = fkey_table[ucs - PF1]) == NO_OP_COMMAND))){
	    /*
	     * The funky test above does two things.  It maps
	     * esc_list character commands to function keys, *and* prevents
	     * character commands from input while in function key mode.
	     * NOTE: this breaks if we ever need more than the first
	     * twelve function keys...
	     */
	    if(flags & RB_ONE_TRY){
		ch = ucs = on_ctrl_C;
	        goto out_of_loop;
	    }

	    Writechar(BELL, 0);
	    continue;
	}

        switch(ucs){

          default:
	    for(i = 0; esc_list && esc_list[i].ch != -1; i++)
	      if(ucs == esc_list[i].ch){
		  int len, n;

		  MoveCursor(real_line,len=MIN(RAD_BUT_COL+utf8_width(q),maxcol+1));
		  for(n = 0, len = ps_global->ttyo->screen_cols - len;
		      esc_list[i].label && esc_list[i].label[n] && len > 0;
		      n++, len--)
		    Writechar(esc_list[i].label[n], 0);

		  ch = esc_list[i].rval;
		  goto out_of_loop;
	      }

	    if(flags & RB_ONE_TRY){
		ch = on_ctrl_C;
	        goto out_of_loop;
	    }

	    Writechar(BELL, 0);
	    break;

          case ctrl('M'):
          case ctrl('J'):
            ch = dflt;
	    for(i = 0; esc_list && esc_list[i].ch != -1; i++)
	      if(ch == esc_list[i].rval){
		  int len, n;

		  MoveCursor(real_line,len=MIN(RAD_BUT_COL+utf8_width(q),maxcol+1));
		  for(n = 0, len = ps_global->ttyo->screen_cols - len;
		      esc_list[i].label && esc_list[i].label[n] && len > 0;
		      n++, len--)
		    Writechar(esc_list[i].label[n], 0);
		  break;
	      }

            goto out_of_loop;

          case ctrl('C'):
	    if(on_ctrl_C || (flags & RB_ONE_TRY)){
		ch = on_ctrl_C;
		goto out_of_loop;
	    }

	    Writechar(BELL, 0);
	    break;


          case '?':
          case ctrl('G'):
	    if(FOOTER_ROWS(ps_global) == 1 && km_popped == 0){
		km_popped++;
		FOOTER_ROWS(ps_global) = 3;
		line = -3;
		real_line = ps_global->ttyo->screen_rows + line;
		if(lastc)
		  (void)pico_set_colorp(lastc, PSC_NONE);
		else
		  EndInverse();

		clearfooter(ps_global);
		if(promptc)
		  (void)pico_set_colorp(promptc, PSC_NONE);
		else
		  StartInverse();

		draw_radio_prompt(real_line, RAD_BUT_COL, maxcol, q);
		break;
	    }

	    if(flags & RB_RET_HELP){
		ch = 3;
		goto out_of_loop;
	    }
	    else if(help_text != NO_HELP && FOOTER_ROWS(ps_global) > 1){
		mark_keymenu_dirty();
		if(lastc)
		  (void)pico_set_colorp(lastc, PSC_NONE);
		else
		  EndInverse();

		MoveCursor(real_line + 1, RAD_BUT_COL);
		CleartoEOLN();
		MoveCursor(real_line + 2, RAD_BUT_COL);
		CleartoEOLN();
		radio_help(real_line, RAD_BUT_COL, help_text);
		sleep(5);
		MoveCursor(real_line, MIN(RAD_BUT_COL+utf8_width(q), maxcol+1));
		if(promptc)
		  (void)pico_set_colorp(promptc, PSC_NONE);
		else
		  StartInverse();
	    }
	    else
	      Writechar(BELL, 0);

            break;
            

          case NO_OP_COMMAND:
	    goto newcmd;		/* misunderstood escape? */

          case NO_OP_IDLE:		/* UNODIR, keep the stream alive */
	    if(flags & RB_NO_NEWMAIL)
	      goto newcmd;

	    i = new_mail(0, VeryBadTime, NM_DEFER_SORT);
	    if(sp_expunge_count(ps_global->mail_stream)
	       && flags & RB_SEQ_SENSITIVE){
		if(on_ctrl_C)
		  ch = on_ctrl_C;
		else
		  ch = SEQ_EXCEPTION;

		goto out_of_loop;
	    }

	    if(i < 0)
	      break;		/* no changes, get on with life */
            /* Else fall into redraw to adjust displayed numbers and such */


          case KEY_RESIZE:
          case ctrl('L'):
            real_line = line > 0 ? line : ps_global->ttyo->screen_rows + line;
	    if(lastc)
	      (void)pico_set_colorp(lastc, PSC_NONE);
	    else
	      EndInverse();

            ClearScreen();
            redraw_titlebar();
            if(ps_global->redrawer != NULL)
              (*ps_global->redrawer)();
	    if(FOOTER_ROWS(ps_global) == 3 || km_popped)
              redraw_keymenu();

	    if(ps_global->ttyo->screen_cols - max_label - 1 > 0)
	      maxcol = ps_global->ttyo->screen_cols - max_label - 1;
	    else
	      maxcol = 0;

	    if(promptc)
	      (void)pico_set_colorp(promptc, PSC_NONE);
	    else
	      StartInverse();

	    draw_radio_prompt(real_line, RAD_BUT_COL, maxcol, q);
            break;

        } /* switch */
    }

  out_of_loop:

#ifdef	_WINDOWS
    if(!cursor_shown)
      mswin_showcaret(0);
#endif

    fs_give((void **) &q);
    if(ds)
      fs_give((void **) &ds);

    if(lastc){
	(void) pico_set_colorp(lastc, PSC_NONE);
	free_color_pair(&lastc);
	if(promptc)
	  free_color_pair(&promptc);
    }
    else
      EndInverse();

    fflush(stdout);
    resume_busy_cue(0);
    if(km_popped){
	FOOTER_ROWS(ps_global) = 1;
	clearfooter(ps_global);
	ps_global->mangled_body = 1;
    }

    return(ch);
}
예제 #4
0
/*----------------------------------------------------------------------
     new_mail() - check for new mail in the inbox
 
  Input:  force       -- flag indicating we should check for new mail no matter
          time_for_check_point -- 0: GoodTime, 1: BadTime, 2: VeryBadTime
	  flags -- whether to q a new mail status message or defer the sort

  Result: returns -1 if there was no new mail. Otherwise returns the
          sorted message number of the smallest message number that
          was changed. That is the screen needs to be repainted from
          that message down.

  Limit frequency of checks because checks use some resources. That is
  they generate an IMAP packet or require locking the local mailbox.
  (Acutally the lock isn't required, a stat will do, but the current
   c-client mail code locks before it stats.)

  Returns >= 0 only if there is a change in the given mail stream. Otherwise
  this returns -1.  On return the message counts in the pine
  structure are updated to reflect the current number of messages including
  any new mail and any expunging.
 
 --- */
long
new_mail(int force_arg, CheckPointTime time_for_check_point, int flags)
{
    static time_t last_check_point_call = 0;
    long          since_last_input;
    time_t        expunged_reaper_to, adj_idle_timeout, interval, adj;
    static int    nexttime = 0;
    time_t        now;
    long          n, rv = 0, t_nm_count = 0, exp_count;
    MAILSTREAM   *m;
    int           force, i, started_on;
    int           new_mail_was_announced = 0;
    int           have_pinged_non_special = 0;
    int		  timeo;

    dprint((9, "new mail called (force=%d %s flags=0x%x)\n",
	       force_arg,
	       time_for_check_point == GoodTime    ? "GoodTime" :
	        time_for_check_point == BadTime     ? "BadTime"  :
		 time_for_check_point == VeryBadTime ? "VeryBad"  :
		  time_for_check_point == DoItNow     ? "DoItNow"  : "?",
	       flags));

    force = force_arg;

    now = time(0);

    timeo = get_input_timeout();

    if(time_for_check_point == GoodTime)
      adrbk_maintenance();

    if(time_for_check_point == GoodTime || force_arg)
      folder_unseen_count_updater(UFU_ANNOUNCE | (force_arg ? UFU_FORCE : 0));

    if(sp_need_to_rethread(ps_global->mail_stream))
      force = 1;

    if(!force && sp_unsorted_newmail(ps_global->mail_stream))
      force = !(flags & NM_DEFER_SORT);

    if(!ps_global->mail_stream
       || !(timeo || force || sp_a_locked_stream_changed()))
      return(-1);

    last_check_point_call = now;
    since_last_input = (long) now - (long) time_of_last_input();

    /*
     * We have this for loop followed by the do-while so that we will prefer
     * to ping the active streams before we ping the inactive ones, in cases
     * where the pings or checks are taking a long time.
     */
    for(i = 0; i < ps_global->s_pool.nstream; i++){
	m = ps_global->s_pool.streams[i];
	if(!m || m->halfopen ||
	   (m != ps_global->mail_stream &&
	    !(force_arg && sp_flagged(m, SP_LOCKED))))
	  continue;
	
	/*
	 * This for() loop is only the current folder, unless the user
	 * has forced the check, in which case it is all locked folders.
	 */
	
	/*
	 * After some amount of inactivity on a stream, the server may
	 * close the stream. Each protocol has its own idea of how much
	 * inactivity should be allowed before the stream is closed. For
	 * example, IMAP specifies that the server should not close the
	 * stream unilaterally until at least 30 minutes of inactivity.
	 * The GET_IDLETIMEOUT call gives us that time in minutes. We
	 * want to be sure to keep the stream alive if it is about to die
	 * due to inactivity.
	 */
	adj_idle_timeout = 60 * (long) mail_parameters(m,GET_IDLETIMEOUT,NULL);
	if(adj_idle_timeout <= 0)
	  adj_idle_timeout = 600;

	adj = (adj_idle_timeout >= 50 * FUDGE) ? 5 * FUDGE :
	        (adj_idle_timeout >= 30 * FUDGE) ? 3 * FUDGE :
	          (adj_idle_timeout >= 20 * FUDGE) ? 2 * FUDGE : FUDGE;
	adj_idle_timeout = MAX(adj_idle_timeout - adj, 120);

	/*
	 * Set interval to mail-check-interval unless
	 * mail-check-interval-noncurrent is nonzero and this is not inbox
	 * or current stream.
	 */
	if(ps_global->check_interval_for_noncurr > 0
	   && m != ps_global->mail_stream
	   && !sp_flagged(m, SP_INBOX))
	  interval = ps_global->check_interval_for_noncurr;
	else
	  interval = timeo;

	/*
	 * We want to make sure that we notice expunges, but we don't have
	 * to be fanatical about it. Every once in a while we'll do a ping
	 * because we haven't had a command that notices expunges for a
	 * while. It's also a larger number than interval so it gives us a
	 * convenient interval to do slower pinging than interval if we
	 * are busy.
	 */
	if(interval <= adj_idle_timeout)
	  expunged_reaper_to = MIN(MAX(2*interval,180), adj_idle_timeout);
	else
	  expunged_reaper_to = interval;

	/*
	 * User may want to avoid new mail checking while composing.
	 * In this case we will only do the keepalives.
	 */
	if(timeo == 0
	   || (flags & NM_FROM_COMPOSER
	       && ((F_ON(F_QUELL_PINGS_COMPOSING, ps_global)
	            && !sp_flagged(m, SP_INBOX))
	           ||
	           (F_ON(F_QUELL_PINGS_COMPOSING_INBOX, ps_global)
	            && sp_flagged(m, SP_INBOX))))){
	    interval = expunged_reaper_to = 0;
	}

	dprint((9,
	       "%s: force=%d interval=%ld exp_reap_to=%ld adj_idle_to=%ld\n",
	       STREAMNAME(m), force_arg, (long) interval,
	       (long) expunged_reaper_to, (long) adj_idle_timeout));
	dprint((9,
	       "   since_last_ping=%ld since_last_reap=%ld\n",
	       (long) (now - sp_last_ping(m)),
	       (long) (now - sp_last_expunged_reaper(m))));

	/* if check_point does a check it resets last_ping time */
	if(force_arg || (timeo && expunged_reaper_to > 0))
	  (void) check_point(m, time_for_check_point, flags);

	/*
	 * Remember that unless force_arg is set, this check is really
	 * only for the current folder. This is usually going to fire
	 * on the first test, which is the interval the user set.
	 */
	if(force_arg
	   ||
	   (timeo
	    &&
	    ((interval && (now - sp_last_ping(m) >= interval-1))
	     ||
	     (expunged_reaper_to
	      && (now - sp_last_expunged_reaper(m)) >= expunged_reaper_to)
	     ||
	     (now - sp_last_ping(m) >= adj_idle_timeout)))){

	    if((flags & NM_STATUS_MSG) && pith_opt_newmail_check_cue)
	      (*pith_opt_newmail_check_cue)(TRUE);

	    dprint((7, "Mail_Ping(%s): lastping=%ld er=%ld%s%s %s\n",
		   STREAMNAME(m),
		   (long) (now - sp_last_ping(m)),
		   (long) (now - sp_last_expunged_reaper(m)),
		   force_arg                      ? " [forced]" :
		    interval && (now-sp_last_ping(m) >= interval-1)
						   ? " [it's time]" :
		    expunged_reaper_to
		     && (now - sp_last_expunged_reaper(m) >= expunged_reaper_to)
						    ? " [expunged reaper]"
						    : " [keepalive]",
		   m == ps_global->mail_stream ? " [current]" : "",
		   debug_time(0,1)));

	    /*
	     * We're about to ping the stream.
	     * If the stream is a #move Mail Drop there is a minimum time
	     * between re-opens of the mail drop to check for new mail.
	     * If the check is forced by the user, they want it to
	     * happen now. We use knowledge of c-client internals to
	     * make this happen.
	     */
	    if(force_arg && m && m->snarf.name)
	      m->snarf.time = 0;

	    /*-- Ping the stream to check for new mail --*/
	    if(sp_dead_stream(m)){
		dprint((6, "no check: stream is dead\n"));
	    }
	    else if(!pine_mail_ping(m)){
		dprint((6, "ping failed: stream is dead\n"));
		sp_set_dead_stream(m, 1);
	    }

	    dprint((7, "Ping complete: %s\n", debug_time(0,1)));

	    if((flags & NM_STATUS_MSG) && pith_opt_newmail_check_cue)
	      (*pith_opt_newmail_check_cue)(FALSE);
	}
    }

    if(nexttime < 0 || nexttime >= ps_global->s_pool.nstream)
      nexttime = 0;

    i = started_on = nexttime;
    do {
	m = ps_global->s_pool.streams[i];

	nexttime = i;
	if(ps_global->s_pool.nstream > 0)
	  i = (i + 1) % ps_global->s_pool.nstream;

	/*
	 * This do() loop handles all the other streams that weren't covered
	 * in the for() loop above.
	 */
	if((!m || m->halfopen) ||
	   (m == ps_global->mail_stream ||
	    (force_arg && sp_flagged(m, SP_LOCKED)))){
	    nexttime = i;
	    continue;
	}

	/*
	 * If it is taking an extra long time to do the pings and checks,
	 * think about skipping some of them. Always do at least one of
	 * these non-special streams (if they are due to be pinged).
	 * The nexttime variable keeps track of where we were so that we
	 * don't always ping the first one in the list and then skip out.
	 */
	if((time(0) - now >= 5) && have_pinged_non_special){
	    dprint((7, "skipping pings due to delay: %s\n",
			debug_time(0,1)));
	    break;
	}

	nexttime = i;
	have_pinged_non_special++;

	adj_idle_timeout = 60 * (long) mail_parameters(m,GET_IDLETIMEOUT,NULL);
	if(adj_idle_timeout <= 0)
	  adj_idle_timeout = 600;

	adj = (adj_idle_timeout >= 50 * FUDGE) ? 5 * FUDGE :
	        (adj_idle_timeout >= 30 * FUDGE) ? 3 * FUDGE :
	          (adj_idle_timeout >= 20 * FUDGE) ? 2 * FUDGE : FUDGE;
	adj_idle_timeout = MAX(adj_idle_timeout - adj, 120);

	if(ps_global->check_interval_for_noncurr > 0
	   && m != ps_global->mail_stream
	   && !sp_flagged(m, SP_INBOX))
	  interval = ps_global->check_interval_for_noncurr;
	else
	  interval = timeo;

	if(interval <= adj_idle_timeout)
	  expunged_reaper_to = MIN(MAX(2*interval,180), adj_idle_timeout);
	else
	  expunged_reaper_to = interval;

	if(timeo == 0
	   || (flags & NM_FROM_COMPOSER
	       && ((F_ON(F_QUELL_PINGS_COMPOSING, ps_global)
	            && !sp_flagged(m, SP_INBOX))
	           ||
	           (F_ON(F_QUELL_PINGS_COMPOSING_INBOX, ps_global)
	            && sp_flagged(m, SP_INBOX))))){
	    interval = expunged_reaper_to = 0;
	}

	dprint((9,
	       "%s: force=%d interval=%ld exp_reap_to=%ld adj_idle_to=%ld\n",
	       STREAMNAME(m), force_arg, (long) interval,
	       (long) expunged_reaper_to, (long) adj_idle_timeout));
	dprint((9,
	    "   since_last_ping=%ld since_last_reap=%ld since_last_input=%ld\n",
	    (long) (now - sp_last_ping(m)),
	    (long) (now - sp_last_expunged_reaper(m)),
	    (long) since_last_input));

	/* if check_point does a check it resets last_ping time */
	if(force_arg || (timeo && expunged_reaper_to > 0))
	  (void) check_point(m, time_for_check_point, flags);


	/*
	 * The check here is a little bit different from the current folder
	 * check in the for() loop above. In particular, we defer our
	 * pinging for awhile if the last input was recent (except for the
	 * inbox!). We ping streams which are cached but not actively being
	 * used (that is, the non-locked streams) at a slower rate.
	 * If we don't use them for a long time we will eventually close them
	 * (in maybe_kill_old_stream()) but we do it when we want to instead
	 * of when the server wants us to by attempting to keep it alive here.
	 * The other reason to ping the cached streams is that we only get
	 * told the new mail in those streams is recent one time, the messages
	 * that weren't handled here will no longer be recent next time
	 * we open the folder.
	 */
	if((force_arg && sp_flagged(m, SP_LOCKED))
	   ||
	   (timeo
	    &&
	    ((interval
	      && sp_flagged(m, SP_LOCKED)
	      && ((since_last_input >= 3
		   && (now-sp_last_ping(m) >= interval-1))
		  || (sp_flagged(m, SP_INBOX)
		      && (now-sp_last_ping(m) >= interval-1))))
	     ||
	     (expunged_reaper_to
	      && sp_flagged(m, SP_LOCKED)
	      && (now-sp_last_expunged_reaper(m) >= expunged_reaper_to))
	     ||
	     (expunged_reaper_to
	      && !sp_flagged(m, SP_LOCKED)
	      && since_last_input >= 3
	      && (now-sp_last_ping(m) >= expunged_reaper_to))
	     ||
	     (now - sp_last_ping(m) >= adj_idle_timeout)))){

	    if((flags & NM_STATUS_MSG) && pith_opt_newmail_check_cue)
	      (*pith_opt_newmail_check_cue)(TRUE);

	    dprint((7,
		   "Mail_Ping(%s): lastping=%ld er=%ld%s idle: %ld %s\n",
		   STREAMNAME(m),
		   (long) (now - sp_last_ping(m)),
		   (long) (now - sp_last_expunged_reaper(m)),
		   (force_arg && sp_flagged(m, SP_LOCKED)) ? " [forced]" :
		   (interval
		    && sp_flagged(m, SP_LOCKED)
		    && ((since_last_input >= 3
		         && (now-sp_last_ping(m) >= interval-1))
		        || (sp_flagged(m, SP_INBOX)
			    && (now-sp_last_ping(m) >= interval-1))))
				    ? " [it's time]" :
		   (expunged_reaper_to
		    && sp_flagged(m, SP_LOCKED)
		    && (now-sp_last_expunged_reaper(m) >= expunged_reaper_to))
				    ? " [expunged reaper]" :
		   (expunged_reaper_to
		    && !sp_flagged(m, SP_LOCKED)
		    && since_last_input >= 3
		    && (now-sp_last_ping(m) >= expunged_reaper_to))
				     ? " [slow ping]" : " [keepalive]",
		   since_last_input,
		   debug_time(0,1)));

	    /*
	     * We're about to ping the stream.
	     * If the stream is a #move Mail Drop there is a minimum time
	     * between re-opens of the mail drop to check for new mail.
	     * If the check is forced by the user, they want it to
	     * happen now. We use knowledge of c-client internals to
	     * make this happen.
	     */
	    if(force_arg && m && m->snarf.name)
	      m->snarf.time = 0;

	    /*-- Ping the stream to check for new mail --*/
	    if(sp_dead_stream(m)){
		dprint((6, "no check: stream is dead\n"));
	    }
	    else if(!pine_mail_ping(m)){
		dprint((6, "ping failed: stream is dead\n"));
		sp_set_dead_stream(m, 1);
	    }

	    dprint((7, "Ping complete: %s\n", debug_time(0,1)));

	    if((flags & NM_STATUS_MSG) && pith_opt_newmail_check_cue)
	      (*pith_opt_newmail_check_cue)(FALSE);
	}
    } while(i != started_on);

    /*
     * Current mail box state changed, could be additions or deletions.
     * Also check if we need to do sorting that has been deferred.
     * We handle the current stream separately from the rest in the
     * similar loop that follows this paragraph.
     */
    m = ps_global->mail_stream;
    if(sp_mail_box_changed(m) || sp_unsorted_newmail(m)
       || sp_need_to_rethread(m)){
        dprint((7,
	 "Cur new mail, %s, new_mail_count: %ld expunge: %ld, max_msgno: %ld\n",
		   (m && m->mailbox) ? m->mailbox : "?",
		   sp_new_mail_count(m),
		   sp_expunge_count(m),
		   mn_get_total(sp_msgmap(m))));

	new_mail_was_announced = 0;
	if(sp_mail_box_changed(m))
	  fixup_flags(m, sp_msgmap(m));

        if(sp_new_mail_count(m))
	  process_filter_patterns(m, sp_msgmap(m), sp_new_mail_count(m));

	/* worry about sorting */
        if((sp_new_mail_count(m) > 0L
	    || sp_unsorted_newmail(m)
	    || sp_need_to_rethread(m))
	   && !((flags & NM_DEFER_SORT)
		|| any_lflagged(sp_msgmap(m), MN_HIDE)))
	  refresh_sort(m, sp_msgmap(m),
		       (flags & NM_STATUS_MSG) ? SRT_VRB : SRT_NON);
        else if(sp_new_mail_count(m) > 0L)
	  sp_set_unsorted_newmail(m, 1);

        if(sp_new_mail_count(m) > 0L){
            sp_set_mail_since_cmd(m, sp_mail_since_cmd(m)+sp_new_mail_count(m));
	    rv += (t_nm_count = sp_new_mail_count(m));
	    sp_set_new_mail_count(m, 0L);

	    if((flags & NM_STATUS_MSG) && pith_opt_newmail_announce){
		for(n = m->nmsgs; n > 1L; n--)
		  if(!get_lflag(m, NULL, n, MN_EXLD))
		    break;

		(*pith_opt_newmail_announce)(m, n, t_nm_count);

		if(n)
		  new_mail_was_announced++;
	    }
        }

	update_folder_unseen_by_stream(m, new_mail_was_announced ? UFU_NONE : UFU_ANNOUNCE);

	if(flags & NM_STATUS_MSG)
	  sp_set_mail_box_changed(m, 0);
    }

    /* the rest of the streams */
    for(i = 0; i < ps_global->s_pool.nstream; i++){
	m = ps_global->s_pool.streams[i];
	if(!m || m == ps_global->mail_stream)
	  continue;

	if(sp_mail_box_changed(m)){
	    /*-- New mail for this stream, queue up the notification --*/
	    dprint((7,
	     "New mail, %s, new_mail_count: %ld expunge: %ld, max_msgno: %ld\n",
		       (m && m->mailbox) ? m->mailbox : "?",
		       sp_new_mail_count(m),
		       sp_expunge_count(m),
		       mn_get_total(sp_msgmap(m))));

	    new_mail_was_announced = 0;
	    fixup_flags(m, sp_msgmap(m));

	    if(sp_new_mail_count(m))
	      process_filter_patterns(m, sp_msgmap(m), sp_new_mail_count(m));

	    if(sp_new_mail_count(m) > 0){
		sp_set_unsorted_newmail(m, 1);
		sp_set_mail_since_cmd(m, sp_mail_since_cmd(m) +
					 sp_new_mail_count(m));
		if(sp_flagged(m, SP_LOCKED) && sp_flagged(m, SP_USERFLDR))
		  rv += (t_nm_count = sp_new_mail_count(m));

		sp_set_new_mail_count(m, 0L);

		/* messages only for user's streams */
		if(flags & NM_STATUS_MSG
		   && pith_opt_newmail_announce
		   && sp_flagged(m, SP_LOCKED)
		   && sp_flagged(m, SP_USERFLDR)){
		    for(n = m->nmsgs; n > 1L; n--)
		      if(!get_lflag(m, NULL, n, MN_EXLD))
			break;

		    (*pith_opt_newmail_announce)(m, n, t_nm_count);

		    if(n)
		      new_mail_was_announced++;
		}
	    }

	    update_folder_unseen_by_stream(m, new_mail_was_announced ? UFU_NONE : UFU_ANNOUNCE);

	    if(flags & NM_STATUS_MSG)
	      sp_set_mail_box_changed(m, 0);
	}
    }

    /* so quit_screen can tell new mail from expunged mail */
    exp_count = sp_expunge_count(ps_global->mail_stream);

    /* see if we want to kill any cached streams */
    for(i = 0; i < ps_global->s_pool.nstream; i++){
	m = ps_global->s_pool.streams[i];
	if(sp_last_ping(m) >= now)
          maybe_kill_old_stream(m);
    }

    /*
     * This is here to prevent banging on the down arrow key (or holding
     * it down and repeating) at the end of the index from causing
     * a whole bunch of new mail checks. Last_nextitem_forcechk does get
     * set at the place it is tested in mailcmd.c, but this is here to
     * reset it to a little bit later in case it takes a second or more
     * to check for the mail. If we didn't do this, we'd just check every
     * keystroke as long as the checks took more than a second.
     */
    if(force_arg)
      ps_global->last_nextitem_forcechk = time(0);

    dprint((6, "******** new mail returning %ld  ********\n", 
	       rv ? rv : (exp_count ? 0 : -1)));
    return(rv ? rv : (exp_count ? 0 : -1));
}