File: pop3.cc

package info (click to toggle)
mailfilter 0.8.6-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 3,280 kB
  • ctags: 2,501
  • sloc: cpp: 2,452; sh: 1,393; ansic: 1,259; lex: 570; yacc: 468; makefile: 167; perl: 72
file content (233 lines) | stat: -rw-r--r-- 6,929 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// pop3.cc - source file for the mailfilter program
// Copyright (c) 2003 - 2009  Andreas Bauer <baueran@gmail.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <string>
#include "socket.hh"
#include "pop3.hh"
#include "feedback.hh"
#include "preferences.hh"
#include "mailfilter.hh"
#include "header.hh"
#include "weeder.hh"
#include "defines.hh"
#include "protocol.hh"
#include "rfc822parser.hh"

// This is necessary to use multiple lexer classes.  See the flex man
// page for further information.
#undef yyFlexLexer
#define yyFlexLexer rfcFlexLexer
#include <FlexLexer.h>

extern "C"
{ int rfcparse (void*); }

using namespace std;

// Declare lexer globally, so the parser can find it.
FlexLexer*          rfclexer;
extern Weeder       weeder;

bool POP3 :: login (const char* usr, 
		    const char* pass, 
		    const unsigned int enc) const
{
  string usr_name = (string)"USER " + usr + (string)"\r\n";
  string pass_wd = (string)"PASS " + pass + (string)"\r\n";

  if (conn->c_read () == -1)
    return false;

  Feedback* logger = Feedback :: Instance ();
  
  // Send user name and read server reply.
  if (conn->c_write (usr_name.c_str ()) == -1 || !REPLY_OK)
    {
      logger->print_err("Error occurred while sending username to server.");
      return false;
    }

  // Send password and read server reply.
  if (conn->c_write (pass_wd.c_str ()) == -1 || !REPLY_OK)
    {
      logger->print_err("Error occurred while sending password to server.");
      return false;
    }

  return true;
}

int POP3 :: status (void) const
{
  // Send user name and read server reply.
  if (conn->c_write ("STAT\r\n") == -1)
    return GEN_FAILURE_FLAG;
  if (!REPLY_OK)
    return GEN_FAILURE_FLAG;

  istringstream response (conn->c_reply ()->c_str ());
  string no_msgs;

  // The second word in the server's output string contains the number
  // of unread messages in a POP3 mailbox.  Hence we shift the reply
  // string twice.
  for (int i = 1; i <= 2; i++)
    response >> no_msgs;
  return atoi (no_msgs.c_str ());
}

// The function scans the headers inside a POP3 account for spam.  It
// will delete all spam messages in the account and return 0 when all
// the hard work is done.  A negative integer is returned if an error
// occurred.

int POP3 :: scan (void) const
{
  Feedback* logger = Feedback :: Instance ();

  Header* msg_header;
  int num_messages;
  stringstream msg_no;
  string cmd;

  // Determine number of messages waiting to be examined.
  if ((num_messages = status ()) < 0)
    {
      logger->print_err ("Error occurred while sending STAT to server.");
      return GEN_FAILURE_FLAG;
    }
  
  try
    {
      for (int i = 1; i <= num_messages; i++)
	{
	  // Reserve heap for the message to be stored, parsed, and
	  // processed.
	  msg_header = new Header;
	  
	  // Convert current message number to string.
	  msg_no << i;
	  
	  // Determine message size.
	  cmd = "LIST " + msg_no.str () + "\r\n";
	  if (conn->c_write (cmd.c_str ()) == -1 || !REPLY_OK)
	    {
	      logger->print_err ("Error occurred while sending LIST to server.");
	      return GEN_FAILURE_FLAG;
	    }
	  
	  msg_header->set_size (atoi
				((conn->c_reply ()->
				  substr (conn->c_reply ()->
					  find_last_of (" ") + 1)).c_str ()));
	  
	  // Read the header of the current message.
	  cmd = "TOP " + msg_no.str () + " 0\r\n";
	  if (conn->c_write (cmd.c_str ()) == -1 || !HEADER_OK)
	    {
	      logger->print_err ("Error occurred while sending TOP to server.");
	      return GEN_FAILURE_FLAG;
	    }

	  // Store the header in a separate file, if the user has given
	  // a path definition via SHOW_HEADERS.
	  if (Preferences :: Instance ().headers_file ().length ())
	    if (!logger->print_header (conn->c_reply ()->c_str ()))
	      logger->print_err ("Could not write headers to separate file.");
	  
	  // Strip topmost status line of server reply, e.g.  "+OK Message
	  // follows."  The +1 in the end is necessary to skip the actual
	  // first newline itself.
	  string message = conn->c_reply ()->substr
	    (conn->c_reply ()->find_first_of ("\n") + 1);
	  
	  // Now parse the header of the current message and store it in
	  // the msg_header object.
	  if (invoke_msg_parser (&message, msg_header) < 0)
	    {
	      logger->print_err ("Parsing the header of message "
				+ msg_no.str ()
				+ " failed.");
	      return GEN_FAILURE_FLAG;
	    }
	  
	  // Now pass msg_header on to the weeder in order to determine
	  // whether it stores a spam mail.
	  if (weeder.is_weed (msg_header) == 1)
	    remove_msg (i);

	  // Delete memory occupied by the current message header.
	  delete msg_header;
	  
	  // Reset stringstream for next int to string conversion.
	  msg_no.clear ();
	  msg_no.str (string ());
	}
    }
  catch (...) { throw; }

  return 0;
}

// This function accepts a string pointer as argument which contains
// the entire header of an email message.  It is used to parse that
// header and store it in a Header-class, msg_header, in order to
// determine whether that particular message qualifies as Spam.  The
// function returns a positive integer upon success, and a negative
// one otherwise.  Failure is usually related to out-of-memory errors.

int POP3 :: invoke_msg_parser (const string* header, 
			       const Header* msg_header) const
{
  if (header && msg_header)
    {
      try
	{
	  stringstream cur_header;
	  cur_header << *header;
	  rfclexer = new rfcFlexLexer;
	  rfclexer->switch_streams (&cur_header, NULL);
	  int error = rfcparse ((void*) msg_header);
	  delete rfclexer;
	  return error;
	}
      catch (...) {  return MEM_FAILURE_FLAG; }
    }
  else
    return GEN_FAILURE_FLAG;
}

bool POP3 :: logout (void) const
{ return (conn->c_write ("QUIT\r\n") == -1) ? false : true; }

int POP3 :: remove_msg (const unsigned int num) const
{  
  if (Preferences :: Instance ().test_mode ())
    {
      Feedback* logger = Feedback :: Instance ();
      logger->print_msg ("Debugging: Simulating DELE command.", 6);
      return 0;
    }

  ostringstream ostr; ostr << num;
  string cmd = (string)"DELE " + ostr.str () + (string)"\r\n";
  return (conn->c_write (cmd.c_str ()) == -1) ? GEN_FAILURE_FLAG : 0;
}