-
Notifications
You must be signed in to change notification settings - Fork 0
/
correo.c
198 lines (156 loc) · 4.51 KB
/
correo.c
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
/* Parches de Terra Networks S.A.
*
* Services is copyright (c) 1996-1999 Andy Church.
* E-mail: <achurch@dragonfire.net>
* This program is free but copyrighted software; see the file COPYING for
* details.
*
* Modulo de envio de correo hecho por Toni García -zoltan <zolty@terra.es>
*/
#include "services.h"
#ifdef REG_NICK_MAIL
#ifdef SMTP
int check_smtp(char buf[BUFSIZE]);
#endif
/*
* Esta funcion envia un correo con el email configurado
* del source k sea con el subject y body pertinentes al
* destino indicado.
*/
int send_mail(const char * destino, const char *subject, const char *body)
{
#ifdef SMTP
int smtpsock = -1;
char bufer[BUFSIZE];
char tmp[BUFSIZE];
smtpsock = conn(SMTP_HOST, SMTP_PORT, "localhost", 0);
if (smtpsock < 0) {
log("SMTP Error :no se pudo abrir socket");
return 0;
}
sgets(bufer, sizeof(bufer), smtpsock);
if (!check_smtp(bufer)) {
disconn(smtpsock);
return 0;
}
sputs("HELO services.terra.es\r\n",smtpsock);
sgets(bufer, sizeof(bufer), smtpsock);
if (!check_smtp(bufer)) {
disconn(smtpsock);
return 0;
}
sprintf(tmp,"MAIL FROM: %s\n",SendFrom);
sputs(tmp,smtpsock);
sgets(bufer, sizeof(bufer), smtpsock);
if (!check_smtp(bufer)) {
disconn(smtpsock);
return 0;
}
sprintf(tmp,"RCPT TO: %s\n",destino);
sputs(tmp,smtpsock);
sgets(bufer, sizeof(bufer), smtpsock);
if (!check_smtp(bufer)) {
disconn(smtpsock);
return 0;
}
sputs("DATA\n",smtpsock);
sgets(bufer, sizeof(bufer), smtpsock);
if (!check_smtp(bufer)) {
disconn(smtpsock);
return 0;
}
sprintf(tmp,"From: %s\n",SendFrom);
sputs(tmp,smtpsock);
sprintf(tmp,"To: %s\n",destino);
sputs(tmp,smtpsock);
sprintf(tmp,"Return-Path: %s\n",SendFrom);
sputs(tmp,smtpsock);
sprintf(tmp,"Subject: %s\n",subject);
sputs(tmp,smtpsock);
sprintf(tmp,"%s\n\n",body);
sputs(tmp,smtpsock);
sputs(".\n",smtpsock);
sgets(bufer, sizeof(bufer), smtpsock);
if (!check_smtp(bufer)) {
disconn(smtpsock);
return 0;
}
sputs("QUIT\n",smtpsock);
disconn(smtpsock);
smtpsock = -1;
return 1;
#elif defined (SENDMAIL1)
FILE *p;
char sendmail[PATH_MAX];
snprintf(sendmail, sizeof(sendmail), SendMailPatch, destino);
if (!(p = popen(sendmail, "w"))) {
// privmsg(s_NickServ, u->nick, "mail jodido");
log("mail jodio");
return 0;
}
fprintf(p, "From: %s\n", SendFrom);
fprintf(p, "To: %s\n", destino);
fprintf(p, "Return-Patch: %s\n", SendFrom);
fprintf(p, "Subject: %s\n", subject);
fprintf(p, "%s\n", body);
fprintf(p, "\n.\n");
pclose(p);
return 1;
#elif defined (SENDMAIL2)
char file[1024];
char cmd[1024];
FILE *p;
snprintf(file, 1023, "%s/%u", MAILSPOOL, getpid());
if ((p = fopen(file, "w+")) != NULL) {
fprintf(p, "To: %s\n", destino);
fprintf(p, "From: %s\n", SendFrom);
fprintf(p, "Return-Path: %s\n", SendFrom);
fprintf(p, "Subject: %s\n\n", subject);
fprintf(p, "%s\n", body);
fclose(p);
sprintf(cmd, "%s -f%s -t < %s ", RUTA_SENDMAIL, Sendfrom, file);
system(cmd);
unlink(file):
return 1;
}
else
return 0;
#endif
}
#ifdef SMTP
int check_smtp(char buf[BUFSIZE])
{
/* Esta funcion recibe la contestacion que recibimos
* del smtp y nos devuelve si se ha producido un error
* o no. Caso de ser asi, loguea el error.
*/
char cod[5];
int codigo;
strncpy(cod,buf,4);
codigo = strlen(cod);
cod[codigo] = '\0';
if (cod)
codigo = atoi(cod);
else
codigo = 500;
switch(codigo) {
case 220:
return 1;
break;
case 221:
return 1;
break;
case 250:
return 1;
break;
case 354:
return 1;
break;
default:
log("SMTP ERROR(%d) : %s",codigo,buf);
return 0;
}
return 0;
}
#endif /* SMTP */
#endif /* REG_NICK_MAIL */