ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/IMAPHandler/IMAPHandler.cpp
(Generate patch)

Comparing trunk/IMAPHandler/IMAPHandler.cpp (file contents):
Revision 108 by douglas, 2003-03-31T20:49:38-08:00 vs.
Revision 124 by douglas, 2003-04-02T21:13:46-08:00

# Line 8 | Line 8
8  
9   IMAPHandler::IMAPHandler(const string& server, bool tls)
10   {
11 +        letter = 'a';
12 +        number = 0;
13 +
14          this->tls = tls;
15          buffer = new char[BUFSIZ + 1];
16  
# Line 83 | Line 86 | IMAPHandler::IMAPHandler(const string& s
86                  }
87          }
88  
89 <        string input = getline();
87 <        if (debug) cerr << input << "\n";
89 >        getline();
90   }
91  
92   IMAPHandler::~IMAPHandler()
# Line 111 | Line 113 | string IMAPHandler::starttls()
113  
114          if (success)
115          {
116 <                //
116 >                tls = true;
117 >
118 >                SSL_load_error_strings();
119 >                SSL_library_init();
120 >
121 >                char* seed = new char[BUFSIZ + 1];
122 >                time_t moment = time(NULL);
123 >                sprintf(seed, "froofy%sfoofoo", ctime(&moment));
124 >
125 >                RAND_add(seed, strlen(seed), 0.007456);
126 >
127 >                delete [] seed;
128 >
129 >                ctx = SSL_CTX_new(TLSv1_client_method());
130 >                if (ctx == NULL)
131 >                {
132 >                        cerr << program << ": SSL CTX New: " <<
133 >                                ERR_reason_error_string(ERR_get_error()) << "\n";
134 >                        exit(1);
135 >                }
136 >
137 >                ssl = SSL_new(ctx);
138 >
139 >                if (SSL_set_fd(ssl, sock) == 0)
140 >                {
141 >                        cerr << program << ": SSL Set FD: " <<
142 >                                ERR_reason_error_string(ERR_get_error()) << "\n";
143 >                        exit(1);
144 >                }
145 >
146 >                if (int code = SSL_connect(ssl) <= 0)
147 >                {
148 >                        error(program + ": SSL Connect", code);
149 >                        exit(1);
150 >                }
151          }
152  
153          return answer;
154   }
155  
156 + string IMAPHandler::command()
157 + {
158 +        char buffer[4];
159 +        sprintf(buffer, "%03u", number++);
160 +
161 +        string sequence = letter + string(buffer);
162 +
163 +        if (number > 999)
164 +        {
165 +                letter++;
166 +                number = 0;
167 +        }
168 +
169 +        if (letter > 'z')
170 +        {
171 +                letter = 'a';
172 +        }
173 +
174 +        return sequence;
175 + }
176 +
177   string IMAPHandler::imap(const string& imap)
178   {
179          string result;
180  
181 <        //
181 >        string sequence = command();
182 >        putline(sequence + " " + imap);
183 >
184 >        while (true)
185 >        {
186 >                string input = getline();
187 >
188 >                if (input.find(sequence + " OK") == 0)
189 >                {
190 >                        success = true;
191 >                        break;
192 >                }
193 >                else if (input.find(sequence + " NO") == 0 || input.find(sequence +
194 >                        " BAD") == 0)
195 >                {
196 >                        success = false;
197 >                        break;
198 >                }
199 >                else
200 >                {
201 >                        result += input + "\n";
202 >                }
203 >        }
204 >
205 >        return result;
206 > }
207 >
208 > string IMAPHandler::imap(const string& imap, const string& args)
209 > {
210 >        string result;
211 >
212 >        string sequence = command();
213 >        putline(sequence + " " + imap + " " + args);
214 >
215 >        while (true)
216 >        {
217 >                string input = getline();
218 >
219 >                if (input.find(sequence + " OK") == 0)
220 >                {
221 >                        success = true;
222 >                        break;
223 >                }
224 >                else if (input.find(sequence + " NO") == 0 || input.find(sequence +
225 >                        " BAD") == 0)
226 >                {
227 >                        success = false;
228 >                        break;
229 >                }
230 >                else
231 >                {
232 >                        result += input + "\n";
233 >                }
234 >        }
235  
236          return result;
237   }
238  
239 < void IMAPHandler::putline(const string line)
239 > string IMAPHandler::imap(const string& imap, const string& args, const string&
240 >        message)
241   {
242 <        sprintf(buffer, "%s\r\n", line.c_str());
242 >        string result;
243  
244 <        if (tls)
244 >        string sequence = command();
245 >        putline(sequence + " " + imap + " " + args);
246 >        putline(message);
247 >
248 >        while (true)
249          {
250 <                if (int code = SSL_write(ssl, buffer, strlen(buffer)) <= 0)
250 >                string input = getline();
251 >
252 >                if (input.find(sequence + " OK") == 0)
253                  {
254 <                        error(program + ": SSL Write", code);
255 <                        exit(1);
254 >                        success = true;
255 >                        break;
256 >                }
257 >                else if (input.find(sequence + " NO") == 0 || input.find(sequence +
258 >                        " BAD") == 0)
259 >                {
260 >                        success = false;
261 >                        break;
262 >                }
263 >                else
264 >                {
265 >                        result += input + "\n";
266                  }
267          }
268 <        else
268 >
269 >        return result;
270 > }
271 >
272 > void IMAPHandler::putline(const string line)
273 > {
274 >        istringstream lines(line);
275 >
276 >        while (lines.good())
277          {
278 <                if (send(sock, buffer, strlen(buffer), 0) == SOCKET_ERROR)
278 >                string line;
279 >
280 >                std::getline(lines, line);
281 >                if (debug) cerr << line << "\n";
282 >
283 >                sprintf(buffer, "%s\r\n", line.c_str());
284 >
285 > //              for (unsigned index = 0; index < strlen(buffer); index++)
286 > //              {
287 > //                      cout << "\"" << buffer[index] << "\" = " << int(buffer[index])
288 > //                              << "\n";
289 > //              }
290 >
291 >                if (tls)
292                  {
293 <                        error(program + ": Send");
294 <                        exit(1);
293 >                        if (int code = SSL_write(ssl, buffer, strlen(buffer)) <= 0)
294 >                        {
295 >                                error(program + ": SSL Write", code);
296 >                                exit(1);
297 >                        }
298                  }
299 +                else
300 +                {
301 +                        if (send(sock, buffer, strlen(buffer), 0) == SOCKET_ERROR)
302 +                        {
303 +                                error(program + ": Send");
304 +                                exit(1);
305 +                        }
306 +                }
307 +
308 +                lines.peek();
309          }
310   }
311  
# Line 179 | Line 340 | string IMAPHandler::getline()
340          }
341          while (byte != '\n');
342  
343 +        if (debug) cerr << line << "\n";
344 +
345          return line;
346   }
347  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines