1 /*
2 ** Copyright 2003-2008, Double Precision Inc.
3 **
4 ** See COPYING for distribution information.
5 */
6 
7 #ifndef myserver_H
8 #define myserver_H
9 
10 #include "config.h"
11 #include "hierarchy.H"
12 
13 #include "libmail/mail.H"
14 #include "curses/timer.H"
15 #include "myreadfolders.H"
16 #include "certificates.H"
17 #include "curseshierarchy.H"
18 
19 #include <string>
20 #include <vector>
21 #include <queue>
22 #include <map>
23 
24 LIBMAIL_START
25 
26 class address;
27 
28 LIBMAIL_END
29 
30 class myServerLoginCallback;
31 
32 /////////////////////////////////////////////////////////////////////////
33 //
34 // A server/account.
35 //
36 // It's subclassed from mail::callback:disconnect, so that it receives
37 // server errors/disconnect notices.
38 //
39 
40 class myServer : public mail::callback::disconnect, public Timer {
41 
42 public:
43 	static std::vector<myServer *> server_list; // All servers
44 
45 	static myServer *getServerByUrl(std::string url);
46 	// Search for a server with the given URL.
47 
48 	static Hierarchy hierarchy; // The folder hierarchy, displayed
49 
50 	class Task;
51 	class Callback;
52 	class CreateFolderCallback;
53 	class promptInfo;
54 
55 	std::queue <Task *> tasks; // Background tasks for this server.
56 
57 	myReadFolders topLevelFolders; // Default folders.
58 
59 	std::string serverName;
60 	std::string serverDescr;
61 	std::string url;
62 	std::string password; // Saved by autologin
63 	std::string certificate;
64 
65 	std::string newsrc;	// For nntp: accounts
66 
67 	time_t mailCheckInterval; // How often to check for mail.
68 
69 	mail::account *server; // NULL if not logged in.
70 
71 	class myFolder *currentFolder; // NULL if no folder is open.
72 
73 	Hierarchy::Server *hierarchyEntry; // Entry in the Hierarchy for me
74 
75 private:
76 	// The per-server configuration data is kept in a map hashed by
77 	// setting name, with the contents being the setting value
78 
79 	std::map<std::string, std::string> server_configuration;
80 
81 	//
82 	// The per-folder configuration data is kept in a map hashed by
83 	// folder's path.  The map's contents is another map hashed by the
84 	// configuration setting name, with the contents being the
85 	// configuration setting value.
86 	//
87 	std::map<std::string, std::map<std::string, std::string>
88 	> folder_configuration;
89 
90 	// Allow refreshing message counts through messages on a named pipe.
91 	// Necessary file descriptor for reading, the other direction to keep
92 	// the pipe open and GUI pointer.
93 	static int pollFDForRefreshMessageCount;
94 	static int pollFDForRefreshMessageCountWriteDirection;
95 	static CursesHierarchy* cursesHierarchyForRefreshing;
96 
97 public:
98 	bool updateServerConfiguration(std::string name, std::string value);
99 	std::string getServerConfiguration(std::string name);
100 
101 	bool updateFolderConfiguration(const mail::folder *folder,
102 				       std::string name,
103 				       std::string value);
104 
105 	bool updateFolderConfiguration(std::string path,
106 				       std::string name,
107 				       std::string value);
108 
109 	std::string getFolderConfiguration(const mail::folder *folder,
110 				      std::string name);
111 	std::string getFolderConfiguration(std::string p, std::string name);
112 
113 	void saveFolderConfiguration();
114 	// Allow multiple updates, without saving each time
115 
116 	static std::vector<std::string> myAddresses;
117 	// Config - list of my addresses
118 
119 	static bool isMyAddress(const class mail::address &);
120 
121 	static std::vector<std::string> myListAddresses;
122 	// Config - mailing list addys
123 
124 	static std::string customHeaders;
125 	// Comma-separated list of custom headers.  Hidden headers prefixed
126 	// by '*'
127 
128 	static std::string smtpServerURL; // Config - SMTP server URL
129 	static std::string smtpServerCertificate;
130 	static std::string smtpServerPassword;
131 
132 	static bool useIMAPforSending;
133 	// Config - use main MAIL acct to send mail
134 
135 
136 	static std::string remoteConfigURL;
137 	static std::string remoteConfigPassword;
138 	static std::string remoteConfigFolder;
139 	class remoteConfig;
140 	static remoteConfig *remoteConfigAccount;
141 	// Remote configuration file
142 
143 	static Certificates *certs; // SSL client certs
144 
145 	void find_cert_by_id(std::vector<std::string> &);
146 	static void find_cert_by_id(std::vector<std::string> &certArg,
147 				    std::string certificate);
148 	static void find_cert_by_url(std::string url,
149 				     std::vector<std::string> &);
150 
151 	// Demoronization settings
152 
153 	static enum DemoronizationType {
154 		DEMORON_OFF,
155 		DEMORON_MIN,
156 		DEMORON_MAX
157 	} demoronizationType;
158 
159 	static enum postAndMailType {
160 		POSTANDMAIL_ASK,
161 		POSTANDMAIL_YES,
162 		POSTANDMAIL_NO
163 	} postAndMail;
164 
165 	static void setDemoronizationType(DemoronizationType);
166 
167 	// Detect removed folders automatically.  When a new folder
168 	// subhierarchy is opened, check for any saved folder configurations
169 	// which are inferior to the newly-opened folder hierarchy, but
170 	// are not found in the newly opened hierarchy.
171 
172 	void openedHierarchy(Hierarchy::Entry *);
173 	void openedHierarchy(Hierarchy::Entry *,
174 			     std::vector<mail::folder *> &);
175 
176 	void disconnect();
177 
178 	void disconnected(const char *errmsg);
179 	void servererror(const char *errmsg);
180 
181 	myServer(std::string name, std::string url);
182 	~myServer();
183 
184 	static unsigned cmdcount; // Nesting level of events.
185 
186 	// Main application event loop.
187 	static void eventloop(void);
188 	static bool eventloop(Callback &); // loop until event completes
189 
190 	// Next screen to open.
191 	static void (*nextScreen)(void *);
192 	static void *nextScreenArg;
193 
194 	static promptInfo prompt(promptInfo info);
195 	// Create a status line prompt, wait for a response.
196 
197 	static char *configDir;
198 	// -c parameter.
199 
200 	static std::string getConfigDir();
201 	static std::string getHomeDir();
202 	static std::string getConfigFilename();
203 
204 	static void saveconfig(bool saveRemote=true,
205 			       bool installRemote=false);
206 	static void getBackupConfigFiles(std::vector<std::string> &);
207 
208 	static void savepasswords(std::string);
209 	static bool loadpasswords(std::string);
210 
211 	// saveconfig() code has some dependencies (libxml) that just add
212 	// too much baggage.  Compartmentalize them in a dummy class, to
213 	// avoid loading the xml include files.
214 
215 	class config;
216 	friend class config;
217 
218 	static bool loadconfig();
219 
220 	void saveFolderIndex(myFolder *);
221 
222 	std::string getCachedFilename(myFolder *, const char *);
223 
224 	static bool loadFolderIndex(std::string,
225 				    std::map<std::string, size_t> &,
226 				    class myFolder *);
227 	static void initializeHierarchy();
228 	void addHierarchy(bool assignRows);
229 	void showHierarchy();
230 	void updateHierarchy();
231 
232 	static void setPollForRefreshMessageCount(std::string fn);
233 	static void closePollForRefreshMessageCount();
234 	static void setCursesHierarchyPointerForRefreshing(CursesHierarchy* h);
235 private:
236 	void addTopLevelFolders();
237 	void addTopLevelFolder(std::string);
238 	void disconnectTasks();
239 
240 public:
241 	void alarm(); // Inherited from Timer
242 
243 	void addTopLevelFolderRedraw(std::string path);
244 
245 	bool login(std::string passwordStr);
246 	bool login(myServerLoginCallback &);
247 	bool login(std::string password, myServerLoginCallback *);
248 	void openFolder(const mail::folder *, bool autoOpenDraft=false);
249 
250 	static void logout();
251 	void serverLogout();
252 
253 	static void reportProgress(size_t bytesCompleted,
254 				   size_t bytesEstimatedTotal,
255 
256 				   size_t messagesCompleted,
257 				   size_t messagesEstimatedTotal);
258 
259 	void checkNewMail();
260 	void finishCheckingNewMail();
261 
262 private:
263 	static bool eventloop(Callback *);
264 
265 };
266 
267 #endif
268