xref: /haiku/src/tests/add-ons/mail/imap/imap_tester.cpp (revision 19ae20e67e91fc09cc9fc5c0e60e21e24e7a53eb)
1 #include "IMAPFolders.h"
2 #include "IMAPMailbox.h"
3 #include "IMAPStorage.h"
4 
5 #include "argv.h"
6 
7 
8 struct cmd_entry {
9 	char*	name;
10 	void	(*func)(int argc, char **argv);
11 	char*	help;
12 };
13 
14 
15 static void do_help(int argc, char** argv);
16 
17 
18 extern const char* __progname;
19 static const char* kProgramName = __progname;
20 
21 static IMAPStorage sStorage;
22 static IMAPMailbox sMailbox(sStorage);
23 
24 
25 static void
26 error(const char* context, status_t status)
27 {
28 	fprintf(stderr, "Error during %s: %s\n", context, strerror(status));
29 }
30 
31 
32 static void
33 usage()
34 {
35 	printf("Usage: %s <server> <username> <password>\n", kProgramName);
36 	exit(1);
37 }
38 
39 
40 // #pragma mark -
41 
42 
43 static void
44 do_select(int argc, char** argv)
45 {
46 	const char* folder = "INBOX";
47 	if (argc > 1)
48 		folder = argv[1];
49 
50 	status_t status = sMailbox.SelectMailbox(folder);
51 	if (status != B_OK)
52 		error("select", status);
53 }
54 
55 
56 static void
57 do_folders(int argc, char** argv)
58 {
59 	IMAPFolders folder(sMailbox);
60 	FolderList folders;
61 	status_t status = folder.GetFolders(folders);
62 	if (status != B_OK)
63 		error("folders", status);
64 
65 	for (size_t i = 0; i < folders.size(); i++) {
66 		printf(" %s %s\n", folders[i].subscribed ? "*" : " ",
67 			folders[i].folder.String());
68 	}
69 }
70 
71 
72 static void
73 do_raw(int argc, char** argv)
74 {
75 	// build command back again
76 	char command[4096];
77 	command[0] = '\0';
78 
79 	for (int i = 1; i < argc; i++) {
80 		if (i > 1)
81 			strlcat(command, " ", sizeof(command));
82 		strlcat(command, argv[i], sizeof(command));
83 	}
84 
85 	class RawCommand : public IMAPCommand {
86 	public:
87 		RawCommand(const char* command)
88 			:
89 			fCommand(command)
90 		{
91 		}
92 
93 		BString Command()
94 		{
95 			return fCommand;
96 		}
97 
98 		bool Handle(const BString& response)
99 		{
100 			return false;
101 		}
102 
103 	private:
104 		const char* fCommand;
105 	};
106 	RawCommand rawCommand(command);
107 	status_t status = sMailbox.ProcessCommand(&rawCommand, 60 * 1000);
108 	if (status != B_OK)
109 		error("raw", status);
110 }
111 
112 
113 static cmd_entry sBuiltinCommands[] = {
114 	{"select", do_select, "Selects a mailbox, defaults to INBOX"},
115 	{"folders", do_folders, "List of existing folders"},
116 	{"raw", do_raw, "Issue a raw command to the server"},
117 	{"help", do_help, "prints this help text"},
118 	{"quit", NULL, "exits the application"},
119 	{NULL, NULL, NULL},
120 };
121 
122 
123 static void
124 do_help(int argc, char** argv)
125 {
126 	printf("Available commands:\n");
127 
128 	for (cmd_entry* command = sBuiltinCommands; command->name != NULL;
129 			command++) {
130 		printf("%8s - %s\n", command->name, command->help);
131 	}
132 }
133 
134 
135 // #pragma mark -
136 
137 
138 int
139 main(int argc, char** argv)
140 {
141 	if (argc < 4)
142 		usage();
143 
144 	const char* server = argv[1];
145 	const char* user = argv[2];
146 	const char* password = argv[3];
147 	bool useSSL = argc > 4;
148 	uint16 port = useSSL ? 995 : 143;
149 
150 	printf("Connecting to \"%s\" as %s\n", server, user);
151 
152 	status_t status = sMailbox.Connect(server, user, password, useSSL, port);
153 	if (status != B_OK) {
154 		error("connect", status);
155 		return 1;
156 	}
157 
158 	while (true) {
159 		printf("> ");
160 		fflush(stdout);
161 
162 		char line[1024];
163 		if (fgets(line, sizeof(line), stdin) == NULL)
164 			break;
165 
166         argc = 0;
167         argv = build_argv(line, &argc);
168         if (argv == NULL || argc == 0)
169             continue;
170 
171 		if (!strcmp(argv[0], "quit")
172 			|| !strcmp(argv[0], "exit")
173 			|| !strcmp(argv[0], "q"))
174 			break;
175 
176         int length = strlen(argv[0]);
177 		bool found = false;
178 
179 		for (cmd_entry* command = sBuiltinCommands; command->name != NULL;
180 				command++) {
181 			if (!strncmp(command->name, argv[0], length)) {
182 				command->func(argc, argv);
183 				found = true;
184 				break;
185 			}
186 		}
187 
188 		if (!found) {
189 			fprintf(stderr, "Unknown command \"%s\". Type \"help\" for a "
190 				"list of commands.\n", argv[0]);
191 		}
192 
193 		free(argv);
194 	}
195 
196 
197 	return 0;
198 }
199