1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 1999-2021 Free Software Foundation, Inc.
3 
4    GNU Mailutils is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Mailutils is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include "imap4d.h"
18 
19 static mu_list_t capa_list;
20 
21 static int
comp(const void * item,const void * data)22 comp (const void *item, const void *data)
23 {
24   return strcmp ((char*)item, (char*)data);
25 }
26 
27 void
imap4d_capability_add(const char * str)28 imap4d_capability_add (const char *str)
29 {
30   if (!capa_list)
31     {
32       mu_list_create (&capa_list);
33       mu_list_set_comparator (capa_list, comp);
34     }
35   mu_list_append (capa_list, (void*)str);
36 }
37 
38 void
imap4d_capability_remove(const char * str)39 imap4d_capability_remove (const char *str)
40 {
41   mu_list_remove (capa_list, (void*)str);
42 }
43 
44 void
imap4d_capability_init()45 imap4d_capability_init ()
46 {
47   static char *capa[] = {
48     "IMAP4rev1",
49     "NAMESPACE",
50     "ID",
51     "IDLE",
52     "LITERAL+",
53     "UNSELECT",
54     NULL
55   };
56   int i;
57 
58   for (i = 0; capa[i]; i++)
59     imap4d_capability_add (capa[i]);
60 }
61 
62 static int
print_capa(void * item,void * data)63 print_capa (void *item, void *data)
64 {
65   io_sendf (" %s", (char *)item);
66   return 0;
67 }
68 
69 int
imap4d_capability(struct imap4d_session * session,struct imap4d_command * command,imap4d_tokbuf_t tok)70 imap4d_capability (struct imap4d_session *session,
71 	           struct imap4d_command *command, imap4d_tokbuf_t tok)
72 {
73   if (imap4d_tokbuf_argc (tok) != 2)
74     return io_completion_response (command, RESP_BAD, "Invalid arguments");
75 
76   io_sendf ("* CAPABILITY");
77 
78   mu_list_foreach (capa_list, print_capa, NULL);
79 
80   imap4d_auth_capability (session);
81   io_sendf ("\n");
82 
83   return io_completion_response (command, RESP_OK, "Completed");
84 }
85