Remove useless imap_cmd_running()
[apps/madmutt.git] / lib-ui / complete.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #include <lib-lib/lib-lib.h>
11
12 #include "mutt.h"
13 #include "mx.h"
14 #include <imap/imap.h>
15 #ifdef USE_NNTP
16 #include <nntp/nntp.h>
17 #endif
18
19 /* given a partial pathname, this routine fills in as much of the rest of the
20  * path as is unique.
21  *
22  * return 0 if ok, -1 if no matches
23  */
24 int mutt_complete (char *s, ssize_t slen)
25 {
26   char *p;
27   DIR *dirp = NULL;
28   struct dirent *de;
29   int i, init = 0;
30   ssize_t len;
31   char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
32   char filepart[_POSIX_PATH_MAX];
33
34   char imap_path[LONG_STRING];
35
36 #ifdef USE_NNTP
37   if (option (OPTNEWS)) {
38     string_list_t *l = CurrentNewsSrv->list;
39
40     m_strcpy(filepart, sizeof(filepart), s);
41
42     /*
43      * special case to handle when there is no filepart yet.
44      * find the first subscribed newsgroup
45      */
46     if ((len = m_strlen(filepart)) == 0) {
47       for (; l; l = l->next) {
48         NNTP_DATA *data = (NNTP_DATA *) l->data;
49
50         if (data && data->subscribed) {
51           m_strcpy(filepart, sizeof(filepart), data->group);
52           init++;
53           l = l->next;
54           break;
55         }
56       }
57     }
58
59     for (; l; l = l->next) {
60       NNTP_DATA *data = (NNTP_DATA *) l->data;
61
62       if (data && data->subscribed &&
63           m_strncmp(data->group, filepart, len) == 0) {
64         if (init) {
65           for (i = 0; filepart[i] && data->group[i]; i++) {
66             if (filepart[i] != data->group[i]) {
67               filepart[i] = 0;
68               break;
69             }
70           }
71           filepart[i] = 0;
72         }
73         else {
74           m_strcpy(filepart, sizeof(filepart), data->group);
75           init = 1;
76         }
77       }
78     }
79
80     strcpy (s, filepart);
81
82     return (init ? 0 : -1);
83   }
84 #endif
85
86   /* we can use '/' as a delimiter, imap_complete rewrites it */
87   if (*s == '=' || *s == '+' || *s == '!') {
88       const char *q = NONULL(*s == '!' ? Spoolfile : Maildir);
89       mutt_concat_path(imap_path, sizeof(imap_path), q, s + 1);
90   }
91   else
92     m_strcpy(imap_path, sizeof(imap_path), s);
93
94   if (mx_get_magic (imap_path) == M_IMAP)
95     return imap_complete (s, slen, imap_path);
96
97   if (*s == '=' || *s == '+' || *s == '!') {
98     dirpart[0] = *s;
99     dirpart[1] = 0;
100     if (*s == '!')
101       m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Spoolfile));
102     else
103       m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Maildir));
104     if ((p = strrchr (s, '/'))) {
105       char buf[_POSIX_PATH_MAX];
106
107       *p++ = 0;
108       mutt_concat_path(buf, sizeof(buf), exp_dirpart, s + 1);
109       m_strcpy(exp_dirpart, sizeof(exp_dirpart), buf);
110       snprintf (buf, sizeof (buf), "%s%s/", dirpart, s + 1);
111       m_strcpy(dirpart, sizeof(dirpart), buf);
112       m_strcpy(filepart, sizeof(filepart), p);
113     }
114     else
115       m_strcpy(filepart, sizeof(filepart), s + 1);
116     dirp = opendir (exp_dirpart);
117   }
118   else {
119     if ((p = strrchr (s, '/'))) {
120       if (p == s) {             /* absolute path */
121         p = s + 1;
122         m_strcpy(dirpart, sizeof(dirpart), "/");
123         exp_dirpart[0] = 0;
124         m_strcpy(filepart, sizeof(filepart), p);
125         dirp = opendir (dirpart);
126       }
127       else {
128         *p = 0;
129         len = p - s;
130         memcpy(dirpart, s, len);
131         dirpart[len] = 0;
132         p++;
133         m_strcpy(filepart, sizeof(filepart), p);
134         m_strcpy(exp_dirpart, sizeof(exp_dirpart), dirpart);
135         mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
136         dirp = opendir (exp_dirpart);
137       }
138     }
139     else {
140       /* no directory name, so assume current directory. */
141       dirpart[0] = 0;
142       m_strcpy(filepart, sizeof(filepart), s);
143       dirp = opendir (".");
144     }
145   }
146
147   if (dirp == NULL) {
148     return (-1);
149   }
150
151   /*
152    * special case to handle when there is no filepart yet.  find the first
153    * file/directory which is not ``.'' or ``..''
154    */
155   if ((len = m_strlen(filepart)) == 0) {
156     while ((de = readdir (dirp)) != NULL) {
157       if (m_strcmp(".", de->d_name) != 0
158           && m_strcmp("..", de->d_name) != 0) {
159         m_strcpy(filepart, sizeof(filepart), de->d_name);
160         init++;
161         break;
162       }
163     }
164   }
165
166   while ((de = readdir (dirp)) != NULL) {
167     if (m_strncmp(de->d_name, filepart, len) == 0) {
168       if (init) {
169         for (i = 0; filepart[i] && de->d_name[i]; i++) {
170           if (filepart[i] != de->d_name[i]) {
171             filepart[i] = 0;
172             break;
173           }
174         }
175         filepart[i] = 0;
176       }
177       else {
178         char buf[_POSIX_PATH_MAX];
179         struct stat st;
180
181         m_strcpy(filepart, sizeof(filepart), de->d_name);
182
183         /* check to see if it is a directory */
184         if (dirpart[0]) {
185           m_strcpy(buf, sizeof(buf), exp_dirpart);
186           m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), "/");
187         }
188         else
189           buf[0] = 0;
190         m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), filepart);
191         if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
192           m_strcpy(filepart + m_strlen(filepart),
193                    sizeof(filepart) - m_strlen(filepart), "/");
194         init = 1;
195       }
196     }
197   }
198   closedir (dirp);
199
200   if (dirpart[0]) {
201     m_strcpy(s, slen, dirpart);
202     if (m_strcmp("/", dirpart) != 0 && dirpart[0] != '='
203         && dirpart[0] != '+')
204       m_strcpy(s + m_strlen(s), slen - m_strlen(s), "/");
205     m_strcpy(s + m_strlen(s), slen - m_strlen(s), filepart);
206   }
207   else
208     m_strcpy(s, slen, filepart);
209
210   return (init ? 0 : -1);
211 }