1 |
douglas |
104 |
/* ============================================================================ |
2 |
|
|
* Douglas Thrift's Web Contact License |
3 |
|
|
* |
4 |
|
|
* Copyright (C) 2002, Douglas Thrift. All Rights Reserved. |
5 |
|
|
* |
6 |
|
|
* Redistribution and use in source and binary forms, with or without |
7 |
|
|
* modification, are permitted provided that the following conditions are met: |
8 |
|
|
* |
9 |
|
|
* 1. Redistributions of source code must retain the above copyright notice, |
10 |
|
|
* this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, |
13 |
|
|
* this list of conditions and the following disclaimer in the documentation |
14 |
|
|
* and/or other materials provided with the distribution. |
15 |
|
|
* |
16 |
|
|
* 3. The end-user documentation included with the redistribution, if any, must |
17 |
|
|
* include the following acknowledgment: |
18 |
|
|
* |
19 |
|
|
* "This product includes software developed by Douglas Thrift |
20 |
|
|
* (http://computers.douglasthrift.net/webcontact.html)." |
21 |
|
|
* |
22 |
|
|
* Alternately, this acknowledgment may appear in the software itself, if |
23 |
|
|
* and wherever such third-party acknowledgments normally appear. |
24 |
|
|
* |
25 |
|
|
* 4. The names "Douglas Thrift" and "Douglas Thrift's Web Contact" must not be |
26 |
|
|
* used to endorse or promote products derived from this software without |
27 |
|
|
* specific prior written permission. For written permission, please visit |
28 |
|
|
* http://www.douglasthrift.net/contact.html for contact information. |
29 |
|
|
* |
30 |
|
|
* 5. Products derived from this software may not be called "Douglas Thrift's |
31 |
|
|
* Web Contact", nor may "Douglas Thrift's Web Contact" appear in their |
32 |
|
|
* name, without prior written permission. |
33 |
|
|
* |
34 |
|
|
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
35 |
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
36 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
37 |
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
38 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
39 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
40 |
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
41 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
42 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
43 |
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
44 |
|
|
* ============================================================================ |
45 |
|
|
*/ |
46 |
|
|
// Windows XP FAQ Poll |
47 |
|
|
// |
48 |
|
|
// Douglas Thrift |
49 |
|
|
// |
50 |
|
|
// IMAPHandler.h |
51 |
|
|
|
52 |
|
|
#ifndef _IMAPHandler_h_ |
53 |
|
|
#define _IMAPHandler_h_ |
54 |
|
|
|
55 |
douglas |
107 |
#include <iostream> |
56 |
|
|
#include <string> |
57 |
|
|
#include <cstdlib> |
58 |
|
|
#include <cstdio> |
59 |
douglas |
104 |
|
60 |
douglas |
107 |
using namespace std; |
61 |
|
|
|
62 |
|
|
// Lovely C Sockets! |
63 |
|
|
#ifdef _WIN32 |
64 |
|
|
|
65 |
|
|
// Windows Sockets |
66 |
|
|
#include <winsock2.h> |
67 |
|
|
#include <windows.h> |
68 |
|
|
|
69 |
|
|
#else |
70 |
|
|
|
71 |
|
|
// BSD Sockets |
72 |
|
|
#include <unistd.h> |
73 |
|
|
#include <sys/types.h> |
74 |
|
|
#include <sys/socket.h> |
75 |
|
|
#include <netinet/in.h> |
76 |
|
|
#include <netdb.h> |
77 |
|
|
|
78 |
|
|
#define INVALID_SOCKET -1 |
79 |
|
|
#define SOCKET_ERROR -1 |
80 |
|
|
|
81 |
|
|
typedef int SOCKET; |
82 |
|
|
|
83 |
|
|
inline int closesocket(SOCKET s) { return close(s); } |
84 |
|
|
|
85 |
|
|
#endif // _WIN32 |
86 |
|
|
|
87 |
|
|
#include <openssl/ssl.h> |
88 |
|
|
#include <openssl/err.h> |
89 |
|
|
#include <openssl/rand.h> |
90 |
|
|
|
91 |
|
|
extern string program; |
92 |
|
|
extern bool debug; |
93 |
|
|
|
94 |
douglas |
104 |
class IMAPHandler |
95 |
|
|
{ |
96 |
|
|
private: |
97 |
douglas |
107 |
#ifdef _WIN32 |
98 |
|
|
WSADATA data; |
99 |
|
|
#endif // _WIN32 |
100 |
|
|
SOCKET sock; |
101 |
|
|
SSL_CTX* ctx; |
102 |
|
|
SSL* ssl; |
103 |
|
|
bool tls; |
104 |
|
|
char* buffer; |
105 |
|
|
char letter; |
106 |
|
|
unsigned number; |
107 |
|
|
bool success; |
108 |
|
|
string command(); |
109 |
|
|
string imap(const string& imap); |
110 |
|
|
string imap(const string& imap, const string& args); |
111 |
|
|
string imap(const string& imap, const string& args, const string& message); |
112 |
|
|
void putline(const string line = ""); |
113 |
|
|
string getline(); |
114 |
|
|
void error(const string& prefix, bool host = false); |
115 |
|
|
void error(const string& prefix, int code); |
116 |
douglas |
104 |
public: |
117 |
douglas |
107 |
IMAPHandler(const string& server, bool tls = false); |
118 |
douglas |
104 |
~IMAPHandler(); |
119 |
douglas |
107 |
string capability() { return imap("CAPABILITY"); } |
120 |
|
|
string starttls(); |
121 |
|
|
string noop() { return imap("NOOP"); } |
122 |
|
|
string logout() { return imap("LOGOUT"); } |
123 |
|
|
string login(const string& args) { return imap("LOGIN", args); } |
124 |
douglas |
116 |
string select(const string& args) { return imap("SELECT", args); } |
125 |
|
|
string examine(const string& args) { return imap("EXAMINE", args); } |
126 |
|
|
string create(const string& args) { return imap("CREATE", args); } |
127 |
|
|
string delete_(const string& args) { return imap("DELETE", args); } |
128 |
|
|
string rename(const string& args) { return imap("RENAME", args); } |
129 |
|
|
string subscribe(const string& args) { return imap("SUBSCRIBE", args); } |
130 |
|
|
string unsubscribe(const string& args) { return imap("UNSUBSCRIBE", args); } |
131 |
|
|
string list(const string& args) { return imap("LIST", args); } |
132 |
|
|
string lsub(const string& args) { return imap("LSUB", args); } |
133 |
|
|
string status_(const string& args) { return imap("STATUS", args); } |
134 |
douglas |
104 |
string append(const string& args, const string& message) |
135 |
|
|
{ |
136 |
douglas |
116 |
return imap("APPEND", args, message); |
137 |
douglas |
104 |
} |
138 |
douglas |
116 |
string check() { return imap("CHECK"); } |
139 |
|
|
string close() { return imap("CLOSE"); } |
140 |
|
|
string expunge() { return imap("EXPUNGE"); } |
141 |
|
|
string search(const string& args) { return imap("SEARCH", args); } |
142 |
|
|
string fetch(const string& args) { return imap("FETCH", args); } |
143 |
|
|
string store(const string& args) { return imap("STORE", args); } |
144 |
|
|
string copy(const string& args) { return imap("COPY", args); } |
145 |
|
|
string uid(const string& args) { return imap("UID", args); } |
146 |
douglas |
107 |
bool successful() { return success; } |
147 |
douglas |
104 |
}; |
148 |
|
|
|
149 |
|
|
#endif // _IMAPHandler_h_ |