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
  
#pike __REAL_VERSION__ 
 
class protocol 
{ 
  // Maybe this should be the other way around? 
  inherit .NNTP.protocol; 
} 
 
//! 
class client 
{ 
  inherit protocol; 
 
  //! A mapping(int:string) that maps SMTP return 
  //! codes to english textual messages. 
  constant reply_codes = 
  ([ 211:"System status, or system help reply", 
     214:"Help message", 
     220:"<host> Service ready", 
     221:"<host> Service closing transmission channel", 
     250:"Requested mail action okay, completed", 
     251:"User not local; will forward to <forward-path>", 
     354:"Start mail input; end with <CRLF>.<CRLF>", 
     421:"<host> Service not available, closing transmission channel " 
         "[This may be a reply to any command if the service knows it " 
         "must shut down]", 
     450:"Requested mail action not taken: mailbox unavailable " 
         "[E.g., mailbox busy]", 
     451:"Requested action aborted: local error in processing", 
     452:"Requested action not taken: insufficient system storage", 
     500:"Syntax error, command unrecognized " 
         "[This may include errors such as command line too long]", 
     501:"Syntax error in parameters or arguments", 
     502:"Command not implemented", 
     503:"Bad sequence of commands", 
     504:"Command parameter not implemented", 
     550:"Requested action not taken: mailbox unavailable " 
         "[E.g., mailbox not found, no access]", 
     551:"User not local; please try <forward-path>", 
     552:"Requested mail action aborted: exceeded storage allocation", 
     553:"Requested action not taken: mailbox name not allowed " 
         "[E.g., mailbox syntax incorrect or relaying denied]", 
     554:"Transaction failed" ]); 
 
  static private int cmd(string c, string|void comment) 
  { 
    int r = command(c); 
    switch(r) { 
    case 200..399: 
      break; 
    default: 
      throw(({"SMTP: "+c+"\n"+(comment?"SMTP: "+comment+"\n":"")+ 
              "SMTP: "+reply_codes[r]+"\n", backtrace()})); 
    } 
    return r; 
  } 
 
  //! @decl void create() 
  //! @decl void create(Stdio.File server) 
  //! @decl void create(string server, void|int port) 
  //! Creates an SMTP mail client and connects it to the 
  //! the @[server] provided. The server parameter may 
  //! either be a string witht the hostnam of the mail server, 
  //! or it may be a file object acting as a mail server. 
  //! If @[server] is a string, than an optional port parameter 
  //! may be provided. If no port parameter is provided, port 
  //! 25 is assumed. If no parameters at all is provided 
  //! the client will look up the mail host by searching 
  //! for the DNS MX record. 
  //! 
  //! @throws 
  //!   Throws an exception if the client fails to connect to 
  //!   the mail server. 
  void create(void|string|Stdio.File server, int|void port) 
  { 
    if(!server) 
    { 
      // Lookup MX record here (Using DNS.pmod) 
      object dns=master()->resolv("Protocols")["DNS"]->client(); 
      server=dns->get_primary_mx(gethostname()); 
    } 
 
    if (objectp(server)) 
       assign(server); 
    else 
    { 
       if(!port) 
          port = 25; 
 
       if(!server || !connect(server, port)) 
       { 
          throw(({"Failed to connect to mail server.\n",backtrace()})); 
       } 
    } 
 
    if(readreturncode()/100 != 2) 
      throw(({"Connection refused by SMTP server.\n",backtrace()})); 
 
    if(catch(cmd("EHLO "+gethostname()))) 
      cmd("HELO "+gethostname(), "greeting failed."); 
  } 
   
  //! Sends a mail message from @[from] to the mail addresses 
  //! listed in @[to] with the mail body @[body]. The body 
  //! should be a correctly formatted mail DATA block, e.g. 
  //! produced by @[MIME.Message]. 
  //! 
  //! @seealso 
  //!   @[simple_mail] 
  //! 
  //! @throws 
  //!   If the mail server returns any other return code than 
  //!   200-399 an exception will be thrown. 
  void send_message(string from, array(string) to, string body) 
  { 
    cmd("MAIL FROM: <" + from + ">"); 
    foreach(to, string t) { 
      cmd("RCPT TO: <" + t + ">"); 
    } 
    cmd("DATA"); 
 
    // Perform quoting according to RFC 2821 4.5.2. 
    if (sizeof(body) && body[0] == '.') { 
      body = "." + body; 
    } 
    body = replace(body, "\r\n.", "\r\n.."); 
 
    // RFC 2821 4.1.1.4: 
    //   An extra <CRLF> MUST NOT be added, as that would cause an empty 
    //   line to be added to the message. 
    if (has_suffix(body, "\r\n")) { 
      body += "."; 
    } else { 
      body += "\r\n."; 
    } 
    cmd(body); 
    cmd("QUIT"); 
  } 
 
  static string parse_addr(string addr) 
  { 
    array(string|int) tokens = replace(MIME.tokenize(addr), '@', "@"); 
 
    int i; 
    tokens = tokens[search(tokens, '<') + 1..]; 
 
    if ((i = search(tokens, '>')) != -1) { 
      tokens = tokens[..i-1]; 
    } 
    return tokens*""; 
  } 
 
  //! Sends an e-mail. Wrapper function that uses @[send_message]. 
  //! 
  //! @throws 
  //!   If the mail server returns any other return code than 
  //!   200-399 an exception will be thrown. 
  void simple_mail(string to, string subject, string from, string msg) 
  { 
    if (search(msg,"\r\n")==-1) 
      msg=replace(msg,"\n","\r\n"); // *simple* mail /Mirar 
    send_message(parse_addr(from), ({ parse_addr(to) }), 
                 (string)MIME.Message(msg, (["mime-version":"1.0", 
                                             "subject":subject, 
                                             "from":from, 
                                             "to":to, 
                                             "content-type": 
                                               "text/plain;charset=iso-8859-1", 
                                             "content-transfer-encoding": 
                                               "8bit"]))); 
  } 
 
  //! Verifies the mail address @[addr] against the mail server. 
  //! 
  //! @returns 
  //!   @array 
  //!     @elem int code 
  //!       The numerical return code from the VRFY call. 
  //!     @elem string message 
  //!       The textual answer to the VRFY call. 
  //!  @endarray 
  //! 
  //! @note 
  //!   Some mail servers does not answer truthfully to 
  //!   verfification queries in order to prevent spammers 
  //!   and others to gain information about the mail 
  //!   addresses present on the mail server. 
  //! 
  //! @throws 
  //!   If the mail server returns any other return code than 
  //!   200-399 an exception will be thrown. 
  array(int|string) verify(string addr) 
  { 
    return ({command("VRFY "+addr),rest}); 
  } 
}