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
|
#include "ed/inc/sys.h"
#include <stdlib.h>
#include <SDL/SDL_net.h>
#include <time.h>
/* donnees de connexion */
#if 0
#define SERVER "smtp.free.fr"
#define PORT 25
#define RET_ADDR "<xx@free.fr>"
#else
#define SERVER "mail.noos.fr"
#define PORT 25
#define RET_ADDR "<xx@noos.fr>"
#endif
/* log */
#if 0
#define MLOG "message.txt"
#define CLOG "connection.log"
#else
#define MLOG "con"
#define CLOG "con"
#endif
/* fin de ligne (CRLF) */
#define SMTP_EOL "\r\n"
static void get_stime (char *stime, size_t size)
{
time_t rawtime;
struct tm *timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (stime, size, "%H%M%S", timeinfo);
}
static void msg_log (char const *fmt, ...)
{
FILE *fichier = fopen (MLOG, "w");
if (fichier != NULL)
{
{
char stime[8];
get_stime (stime, sizeof stime);
fprintf (fichier, "msg : %s : ", stime);
}
{
va_list ap;
va_start (ap, fmt);
vfprintf (fichier, fmt, ap);
va_end (ap);
}
fclose (fichier);
}
}
static void cnx_log (char const *fmt, ...)
{
FILE *fichier = fopen (CLOG, "w");
if (fichier != NULL)
{
{
char stime[8];
get_stime (stime, sizeof stime);
fprintf (fichier, "cnx : %s : ", stime);
}
{
va_list ap;
va_start (ap, fmt);
vfprintf (fichier, fmt, ap);
va_end (ap);
}
fclose (fichier);
}
}
static int ecoute (TCPsocket Socket)
{
int err = 0;
char recu[1024];
int result = SDLNet_TCP_Recv (Socket, recu, sizeof recu - 1);
if (result <= 0)
{
// TCP Connection is broken. (because of error or closure)
msg_log ("TCP Connection is broken\n");
err = 1;
}
else
{
recu[result] = 0;
msg_log ("Received: '%s'\n", recu);
}
return err;
}
static void post (char const *envoi, TCPsocket Socket)
{
int len;
int result;
len = strlen (envoi);
msg_log ("Xmitted: '%s'\n", envoi);
result = SDLNet_TCP_Send (Socket, envoi, len);
if (result < len)
{
cnx_log ("SDLNet_TCP_Send: %s\n", SDLNet_GetError ());
// It may be good to disconnect sock because it is likely invalid now.
}
}
static void smtp_connect (void)
{
SDLNet_Init ();
IPaddress Hote;
SDLNet_ResolveHost (&Hote, SERVER, PORT); // Pour se connecter à
// irc.freenode.net:6667
{
TCPsocket Socket = SDLNet_TCP_Open (&Hote);
// Maintenant Socket contient une socket valide que l'on peut utiliser
// pour communiquer, ou est égale à NULL
if (Socket == NULL)
{
cnx_log ("Connection impossible");
}
else
{
int err;
err = ecoute (Socket);
if (!err)
{
char const *s = "HELO salut" SMTP_EOL;
post (s, Socket);
}
err = ecoute (Socket);
if (!err)
{
char const *s = "MAIL FROM:" RET_ADDR SMTP_EOL;
post (s, Socket);
}
err = ecoute (Socket);
if (!err)
{
char const *s = "RCPT TO:" RET_ADDR SMTP_EOL;
post (s, Socket);
}
err = ecoute (Socket);
if (!err)
{
char const *s = "HELP" SMTP_EOL;
post (s, Socket);
}
err = ecoute (Socket);
if (!err)
{
/* DATA <CRLF> */
char const *s = "DATA" SMTP_EOL;
post (s, Socket);
}
err = ecoute (Socket);
if (!err)
{
/* DATA <CRLF> */
char const *s = "Hello world on SMTP !"SMTP_EOL "." SMTP_EOL;
post (s, Socket);
}
err = ecoute (Socket);
if (!err)
{
/* QUIT <CRLF> */
char const *s = "QUIT" SMTP_EOL;
post (s, Socket);
}
err = ecoute (Socket);
}
SDLNet_TCP_Close (Socket);
}
SDLNet_Quit ();
}
int main (int argc, char **args)
{
smtp_connect ();
(void) argc;
(void) args;
return 0;
} |
Partager