mappings as standalone module
[apps/madmutt.git] / curs_main.c
index c9ec57f..a62f166 100644 (file)
 # include "config.h"
 #endif
 
+#include <lib-lib/mem.h>
+#include <lib-lib/str.h>
+#include <lib-lib/macros.h>
+#include <lib-lib/mapping.h>
+
 #include "mutt.h"
 #include "mutt_curses.h"
 #include "mx.h"
 #include "mutt_menu.h"
-#include "mapping.h"
 #include "sort.h"
+#include "recvattach.h"
 #include "buffy.h"
 #include "mx.h"
 #include "sidebar.h"
+#include "thread.h"
+#include "xterm.h"
 
 #ifdef USE_POP
 #include "pop.h"
 #include "nntp.h"
 #endif
 
-#include "lib/mem.h"
-#include "lib/intl.h"
-#include "lib/str.h"
+#ifdef USE_SASL
+#include "mutt_sasl.h"
+#endif
+
 #include "lib/debug.h"
 
 #include <ctype.h>
@@ -58,9 +66,16 @@ static const char *Function_not_permitted_in_attach_message_mode =
 N_("Function not permitted in attach-message mode.");
 static const char *No_visible = N_("No visible messages.");
 
+#define CHECK_IN_MAILBOX if (!Context) \
+        { \
+                mutt_flushinp (); \
+                mutt_error (_(No_mailbox_is_open)); \
+                break; \
+        }
+
 #define CHECK_MSGCOUNT if (!Context) \
         { \
-                  mutt_flushinp (); \
+                mutt_flushinp (); \
                 mutt_error(_(No_mailbox_is_open)); \
                 break; \
         } \
@@ -96,26 +111,10 @@ static const char *No_visible = N_("No visible messages.");
 #define CURHDR Context->hdrs[Context->v2r[menu->current]]
 #define OLDHDR Context->hdrs[Context->v2r[menu->oldcurrent]]
 #define UNREAD(h) mutt_thread_contains_unread (Context, h)
+#define SW              (option(OPTMBOXPANE)?SidebarWidth:0)
 
-extern const char *ReleaseDate;
 extern size_t UngetCount;
 
-static void set_xterm_title_bar (char *title)
-{
-  fputs ("\033]2;", stdout);
-  fputs (title, stdout);
-  fputs ("\007", stdout);
-  fflush (stdout);
-}
-
-static void set_xterm_icon_name (char *name)
-{
-  fputs ("\033]1;", stdout);
-  fputs (name, stdout);
-  fputs ("\007", stdout);
-  fflush (stdout);
-}
-
 void index_make_entry (char *s, size_t l, MUTTMENU * menu, int num)
 {
   format_flag flag =
@@ -282,19 +281,16 @@ static void update_index (MUTTMENU * menu, CONTEXT * ctx, int check,
    * they will be visible in the limited view */
   if (Context->pattern) {
 #define THIS_BODY Context->hdrs[j]->content
-    if (oldcount || check == M_REOPENED) {
-      for (j = (check == M_REOPENED) ? 0 : oldcount; j < Context->msgcount;
-           j++) {
-        if (mutt_pattern_exec
-            (Context->limit_pattern, M_MATCH_FULL_ADDRESS, Context,
-             Context->hdrs[j])) {
-          Context->hdrs[j]->virtual = Context->vcount;
-          Context->v2r[Context->vcount] = j;
-          Context->hdrs[j]->limited = 1;
-          Context->vcount++;
-          Context->vsize +=
-            THIS_BODY->length + THIS_BODY->offset - THIS_BODY->hdr_offset;
-        }
+    for (j = (check == M_REOPENED) ? 0 : oldcount; j < Context->msgcount; j++) {
+      if (mutt_pattern_exec
+          (Context->limit_pattern, M_MATCH_FULL_ADDRESS, Context,
+            Context->hdrs[j])) {
+        Context->hdrs[j]->virtual = Context->vcount;
+        Context->v2r[Context->vcount] = j;
+        Context->hdrs[j]->limited = 1;
+        Context->vcount++;
+        Context->vsize +=
+          THIS_BODY->length + THIS_BODY->offset - THIS_BODY->hdr_offset;
       }
     }
 #undef THIS_BODY
@@ -302,9 +298,7 @@ static void update_index (MUTTMENU * menu, CONTEXT * ctx, int check,
 
   /* save the list of new messages */
   if (oldcount && check != M_REOPENED && ((Sort & SORT_MASK) == SORT_THREADS)) {
-    save_new =
-      (HEADER **) safe_malloc (sizeof (HEADER *) *
-                               (Context->msgcount - oldcount));
+    save_new = p_new(HEADER*, Context->msgcount - oldcount);
     for (j = oldcount; j < Context->msgcount; j++)
       save_new[j - oldcount] = Context->hdrs[j];
   }
@@ -336,7 +330,7 @@ static void update_index (MUTTMENU * menu, CONTEXT * ctx, int check,
             mutt_uncollapse_thread (Context, h);
         }
       }
-      FREE (&save_new);
+      p_delete(&save_new);
       mutt_set_virtual (Context);
     }
   }
@@ -354,7 +348,6 @@ static void update_index (MUTTMENU * menu, CONTEXT * ctx, int check,
 
   if (menu->current < 0)
     menu->current = ci_first_message ();
-
 }
 
 static void resort_index (MUTTMENU * menu)
@@ -391,7 +384,7 @@ struct mapping_t IndexHelp[] = {
   {N_("Reply"), OP_REPLY},
   {N_("Group"), OP_GROUP_REPLY},
   {N_("Help"), OP_HELP},
-  {NULL}
+  {NULL, OP_NULL}
 };
 
 #ifdef USE_NNTP
@@ -404,7 +397,7 @@ struct mapping_t IndexNewsHelp[] = {
   {N_("Followup"), OP_FOLLOWUP},
   {N_("Catchup"), OP_CATCHUP},
   {N_("Help"), OP_HELP},
-  {NULL}
+  {NULL, OP_NULL}
 };
 #endif
 
@@ -451,7 +444,7 @@ int mutt_index_menu (void)
       sidebar_set_current (Context->path);
   }
 
-  FOREVER {
+  for (;;) {
     tag = 0;                    /* clear the tag-prefix */
 
     menu->max = Context ? Context->vcount : 0;
@@ -489,7 +482,7 @@ int mutt_index_menu (void)
       if ((check = mx_check_mailbox (Context, &index_hint, 0)) < 0) {
         if (!Context->path) {
           /* fatal error occurred */
-          FREE (&Context);
+          p_delete(&Context);
           menu->redraw = REDRAW_FULL;
         }
         set_option (OPTSEARCHINVALID);
@@ -567,20 +560,22 @@ int mutt_index_menu (void)
       }
 
       if (menu->redraw & REDRAW_STATUS) {
-        DrawFullLine = 1;
         menu_status_line (buf, sizeof (buf), menu, NONULL (Status));
-        DrawFullLine = 0;
         CLEARLINE (option (OPTSTATUSONTOP) ? 0 : LINES - 2);
+        sidebar_draw_frames();
         SETCOLOR (MT_COLOR_STATUS);
-        mutt_paddstr (COLS, buf);
+        BKGDSET (MT_COLOR_STATUS);
+        move(option (OPTSTATUSONTOP) ? 0 : LINES - 2,SW);
+        mutt_paddstr (COLS-SW, buf);
         SETCOLOR (MT_COLOR_NORMAL);
+        BKGDSET (MT_COLOR_NORMAL);
         sidebar_set_buffystats (Context);
         menu->redraw &= ~REDRAW_STATUS;
         if (option (OPTXTERMSETTITLES)) {
           menu_status_line (buf, sizeof (buf), menu, NONULL (XtermTitle));
-          set_xterm_title_bar (buf);
+          mutt_xterm_set_title (buf);
           menu_status_line (buf, sizeof (buf), menu, NONULL (XtermIcon));
-          set_xterm_icon_name (buf);
+          mutt_xterm_set_icon (buf);
         }
       }
 
@@ -598,10 +593,6 @@ int mutt_index_menu (void)
         move (menu->current - menu->top + menu->offset, COLS - 1);
       mutt_refresh ();
 
-      op = km_dokey (MENU_MAIN);
-
-      debug_print (4, ("Got op %d\n", op));
-
 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
       if (SigWinch) {
         mutt_flushinp ();
@@ -619,6 +610,10 @@ int mutt_index_menu (void)
       }
 #endif
 
+      op = km_dokey (MENU_MAIN);
+
+      debug_print (4, ("Got op %d\n", op));
+
       if (op == -1)
         continue;               /* either user abort or timeout */
 
@@ -759,7 +754,7 @@ int mutt_index_menu (void)
 
         if (op == OP_GET_MESSAGE) {
           buf[0] = 0;
-          if (mutt_get_field (_("Enter Message-Id: "), buf, sizeof (buf), 0)
+          if (mutt_get_field (_("Enter Message-ID: "), buf, sizeof (buf), 0)
               != 0 || !buf[0])
             break;
         }
@@ -771,7 +766,7 @@ int mutt_index_menu (void)
 
             break;
           }
-          strfcpy (buf, ref->data, sizeof (buf));
+          m_strcpy(buf, sizeof(buf), ref->data);
         }
         if (!Context->id_hash)
           Context->id_hash = mutt_make_id_hash (Context);
@@ -810,14 +805,14 @@ int mutt_index_menu (void)
         int old = CURHDR->index, i;
 
         if (!CURHDR->env->message_id) {
-          mutt_error (_("No Message-Id. Unable to perform operation"));
+          mutt_error (_("No Message-ID. Unable to perform operation"));
 
           break;
         }
 
         if (!Context->id_hash)
           Context->id_hash = mutt_make_id_hash (Context);
-        strfcpy (buf, CURHDR->env->message_id, sizeof (buf));
+        m_strcpy(buf, sizeof(buf), CURHDR->env->message_id);
 
         if (op == OP_RECONSTRUCT_THREAD) {
           LIST *ref = CURHDR->env->references;
@@ -826,7 +821,7 @@ int mutt_index_menu (void)
             nntp_check_msgid (Context, ref->data);
             /* the last msgid in References is the root message */
             if (!ref->next)
-              strfcpy (buf, ref->data, sizeof (buf));
+              m_strcpy(buf, sizeof(buf), ref->data);
             ref = ref->next;
           }
         }
@@ -934,7 +929,7 @@ int mutt_index_menu (void)
       break;
 
     case OP_MAIN_SHOW_LIMIT:
-      CHECK_MSGCOUNT;
+      CHECK_IN_MAILBOX;
       if (!Context->pattern)
         mutt_message (_("No limit pattern is in effect."));
 
@@ -950,7 +945,7 @@ int mutt_index_menu (void)
     case OP_MAIN_LIMIT:
     case OP_TOGGLE_READ:
 
-      CHECK_MSGCOUNT;
+      CHECK_IN_MAILBOX;
       menu->oldcurrent = (Context->vcount && menu->current >= 0
                           && menu->current <
                           Context->vcount) ? CURHDR->index : -1;
@@ -964,13 +959,13 @@ int mutt_index_menu (void)
           set_option (OPTHIDEREAD);
         }
         else {
-          strfcpy (buf, Context->pattern + 8, sizeof (buf));
+          m_strcpy(buf, sizeof(buf), Context->pattern + 8);
           if (!*buf || strncmp (buf, ".*", 2) == 0)
             snprintf (buf, sizeof (buf), "~A");
           unset_option (OPTHIDEREAD);
         }
-        FREE (&Context->pattern);
-        Context->pattern = safe_strdup (buf);
+        p_delete(&Context->pattern);
+        Context->pattern = m_strdup(buf);
       }
       if ((op == OP_TOGGLE_READ && mutt_pattern_func (M_LIMIT, NULL) == 0) ||
           mutt_pattern_func (M_LIMIT, _("Limit to messages matching: ")) == 0)
@@ -989,10 +984,12 @@ int mutt_index_menu (void)
         else
           menu->current = 0;
         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
-        if ((Sort & SORT_MASK) == SORT_THREADS)
+        if (Context->msgcount && (Sort & SORT_MASK) == SORT_THREADS)
           mutt_draw_tree (Context);
         menu->redraw = REDRAW_FULL;
       }
+      if (Context->pattern)
+        mutt_message _("To view all messages, limit to \"all\".");
       break;
 
     case OP_QUIT:
@@ -1155,7 +1152,7 @@ int mutt_index_menu (void)
 
       /* check for a fatal error, or all messages deleted */
       if (!Context->path)
-        FREE (&Context);
+        p_delete(&Context);
 
       /* if we were in the pager, redisplay the message */
       if (menu->menu == MENU_PAGER) {
@@ -1198,23 +1195,27 @@ int mutt_index_menu (void)
           cp = _("Open newsgroup in read-only mode");
         else
           cp = _("Open newsgroup");
-        nntp_buffy (buf);
+        nntp_buffy (buf, sizeof (buf));
       }
       else
 #endif
       {
         if (Context && Context->path)
-          strncpy (buf, Context->path, sizeof (buf));
-       if (op != OP_SIDEBAR_OPEN)
-         buffy_next (buf, sizeof (buf));
+          m_strcpy(buf, sizeof(buf), Context->path);
+        if (op != OP_SIDEBAR_OPEN)
+          buffy_next (buf, sizeof (buf));
       }
 
       if (op == OP_SIDEBAR_OPEN) {
-        strncpy (buf, NONULL(sidebar_get_current ()), sizeof (buf));
+        m_strcpy(buf, sizeof(buf), sidebar_get_current());
+      }
+      else if (mutt_enter_fname (cp, buf, sizeof (buf), &menu->redraw, 1) == -1) {
+        if (menu->menu == MENU_PAGER) {
+          op = OP_DISPLAY_MESSAGE;
+          continue;
+        } else
+          break;
       }
-      else if (mutt_enter_fname (cp, buf, sizeof (buf), &menu->redraw, 1) ==
-               -1)
-        break;
       if (!buf[0]) {
         CLEARLINE (LINES - 1);
         break;
@@ -1254,7 +1255,7 @@ int mutt_index_menu (void)
           menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
           break;
         }
-        FREE (&Context);
+        p_delete(&Context);
       }
 
       mutt_sleep (0);
@@ -1310,6 +1311,10 @@ int mutt_index_menu (void)
           menu->current = mutt_thread_next_unread (Context, CURHDR);
       }
 
+      if (option (OPTPGPAUTODEC) && 
+          (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED)))
+        mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
+
       if ((op = mutt_display_message (CURHDR)) == -1) {
         unset_option (OPTNEEDRESORT);
         break;
@@ -1333,7 +1338,7 @@ int mutt_index_menu (void)
       {
         if (Context) {
           mx_fastclose_mailbox (Context);
-          FREE (&Context);
+          p_delete(&Context);
         }
         done = 1;
       }
@@ -1707,7 +1712,7 @@ int mutt_index_menu (void)
 
     case OP_TOGGLE_WRITE:
 
-      CHECK_MSGCOUNT;
+      CHECK_IN_MAILBOX;
       if (mx_toggle_write (Context) == 0)
         menu->redraw |= REDRAW_STATUS;
       break;
@@ -2015,6 +2020,10 @@ int mutt_index_menu (void)
       CHECK_MSGCOUNT;
       CHECK_VISIBLE;
       CHECK_ATTACH;
+
+      if (option (OPTPGPAUTODEC) &&
+          (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))) 
+        mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
       ci_send_message (SENDFORWARD, NULL, NULL, Context, tag ? NULL : CURHDR);
       menu->redraw = REDRAW_FULL;
       break;
@@ -2029,6 +2038,11 @@ int mutt_index_menu (void)
       CHECK_MSGCOUNT;
       CHECK_VISIBLE;
       CHECK_ATTACH;
+
+      if (option (OPTPGPAUTODEC) &&
+          (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))) 
+        mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
+
       ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, Context,
                        tag ? NULL : CURHDR);
       menu->redraw = REDRAW_FULL;
@@ -2039,6 +2053,11 @@ int mutt_index_menu (void)
       CHECK_ATTACH;
       CHECK_MSGCOUNT;
       CHECK_VISIBLE;
+
+      if (option (OPTPGPAUTODEC) &&
+          (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))) 
+        mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
+
       ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, Context,
                        tag ? NULL : CURHDR);
       menu->redraw = REDRAW_FULL;
@@ -2089,6 +2108,14 @@ int mutt_index_menu (void)
       CHECK_MSGCOUNT;
       CHECK_VISIBLE;
       mutt_pipe_message (tag ? NULL : CURHDR);
+#ifdef USE_IMAP
+      /* in an IMAP folder index with imap_peek=no, piping could change
+       * * new or old messages status to read. Redraw what's needed.
+       */
+      if (Context->magic == M_IMAP && !option (OPTIMAPPEEK)) {
+        menu->redraw = (tag ? REDRAW_INDEX : REDRAW_CURRENT) | REDRAW_STATUS;
+      }
+#endif
       MAYBE_REDRAW (menu->redraw);
       break;
 
@@ -2097,6 +2124,14 @@ int mutt_index_menu (void)
       CHECK_MSGCOUNT;
       CHECK_VISIBLE;
       mutt_print_message (tag ? NULL : CURHDR);
+#ifdef USE_IMAP
+      /* in an IMAP folder index with imap_peek=no, piping could change
+       * * new or old messages status to read. Redraw what's needed.
+       */
+      if (Context->magic == M_IMAP && !option (OPTIMAPPEEK)) {
+        menu->redraw = (tag ? REDRAW_INDEX : REDRAW_CURRENT) | REDRAW_STATUS;
+      }
+#endif
       break;
 
     case OP_MAIN_READ_THREAD:
@@ -2160,7 +2195,7 @@ int mutt_index_menu (void)
         sleep (2);
       }
       else if (op != OP_FOLLOWUP || !CURHDR->env->followup_to ||
-               safe_strcasecmp (CURHDR->env->followup_to, "poster") ||
+               m_strcasecmp(CURHDR->env->followup_to, "poster") ||
                query_quadoption (OPT_FOLLOWUPTOPOSTER,
                                  _("Reply by mail as poster prefers?")) !=
                M_YES) {
@@ -2192,6 +2227,11 @@ int mutt_index_menu (void)
       CHECK_ATTACH;
       CHECK_MSGCOUNT;
       CHECK_VISIBLE;
+
+      if (option (OPTPGPAUTODEC) &&
+          (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))) 
+        mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
+
       ci_send_message (SENDREPLY, NULL, NULL, Context, tag ? NULL : CURHDR);
       menu->redraw = REDRAW_FULL;
       break;
@@ -2280,6 +2320,8 @@ int mutt_index_menu (void)
       break;
 
     case OP_BUFFY_LIST:
+      if (option (OPTFORCEBUFFYCHECK))
+        buffy_check (1);
       buffy_list ();
       menu->redraw = REDRAW_FULL;
       break;
@@ -2300,6 +2342,10 @@ int mutt_index_menu (void)
       mutt_what_key ();
       break;
 
+    case OP_REBUILD_CACHE:
+      mx_rebuild_cache ();
+      break;
+
     case OP_SIDEBAR_SCROLL_UP:
     case OP_SIDEBAR_SCROLL_DOWN:
     case OP_SIDEBAR_NEXT:
@@ -2325,16 +2371,19 @@ int mutt_index_menu (void)
       break;
   }
 
+  if (!attach_msg) {
 #ifdef USE_IMAP
   /* Close all open IMAP connections */
-  if (!attach_msg)
     imap_logout_all ();
 #endif
 #ifdef USE_NNTP
   /* Close all open NNTP connections */
-  if (!attach_msg)
     nntp_logout_all ();
 #endif
+#ifdef USE_SASL
+    mutt_sasl_done ();
+#endif
+  }
 
   mutt_menuDestroy (&menu);
   return (close);