ayttm-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Ayttm-commits] CVS: ayttm/modules/smtp smtp.c,1.1,1.2


From: Philip S Tellis <address@hidden>
Subject: [Ayttm-commits] CVS: ayttm/modules/smtp smtp.c,1.1,1.2
Date: Thu, 30 Jan 2003 01:51:42 -0500

Update of /cvsroot/ayttm/ayttm/modules/smtp
In directory subversions:/tmp/cvs-serv17370

Modified Files:
        smtp.c 
Log Message:
asynchronous smtp

Index: smtp.c
===================================================================
RCS file: /cvsroot/ayttm/ayttm/modules/smtp/smtp.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- smtp.c      30 Jan 2003 05:30:09 -0000      1.1
+++ smtp.c      30 Jan 2003 06:51:38 -0000      1.2
@@ -39,7 +39,6 @@
 #include "service.h"
 #include "plugin_api.h"
 
-#include "dialog.h"            /* MAX_PREF_LEN */
 #include "input_list.h"
 #include "value_pair.h"
 #include "util.h"
@@ -285,91 +284,6 @@
        return count;
 }
 
-static int validate_or_die_gracefully(char *buff, char *valid, int fd)
-{
-       if(strstr(buff, valid) == buff) {
-               return 1;
-       } 
-
-       fprintf(stderr, "%s\n", buff);
-       smtp_tcp_writeline("QUIT", fd);
-       close(fd);
-       return 0;
-}
-
-static int send_message(char *from, char *to, char *msg)
-{
-       int fd;
-       char buff[1024];
-       char localhost[255];
-       int n, len;
-
-       if(gethostname(localhost, sizeof(localhost)-1) == -1) {
-               strcpy(localhost, "localhost");
-               fprintf(stderr, "could not get localhost name: %s\n", 
-                               strerror(errno));
-       }
-
-       fd = smtp_connect(smtp_host, atoi(smtp_port));
-       if(smtp_tcp_readline(buff, sizeof(buff)-1, fd) <= 0) {
-               fprintf(stderr, "smtp server closed connection\n");
-               close(fd);
-               return 0;
-       };
-
-       if(!validate_or_die_gracefully(buff, "220", fd))
-               return 0;
-
-       snprintf(buff, sizeof(buff)-1, "HELO %s", localhost);
-       smtp_tcp_writeline(buff, fd);
-       smtp_tcp_readline(buff, sizeof(buff)-1, fd);
-
-       if(!validate_or_die_gracefully(buff, "250", fd))
-               return 0;
-
-       snprintf(buff, sizeof(buff)-1, "MAIL FROM: %s", from);
-       smtp_tcp_writeline(buff, fd);
-       smtp_tcp_readline(buff, sizeof(buff)-1, fd);
-
-       if(!validate_or_die_gracefully(buff, "250", fd))
-               return 0;
-
-       snprintf(buff, sizeof(buff)-1, "RCPT TO: %s", to);
-       smtp_tcp_writeline(buff, fd);
-       smtp_tcp_readline(buff, sizeof(buff)-1, fd);
-
-       if(!validate_or_die_gracefully(buff, "250", fd))
-               return 0;
-
-       smtp_tcp_writeline("DATA", fd);
-       smtp_tcp_readline(buff, sizeof(buff)-1, fd);
-
-       if(!validate_or_die_gracefully(buff, "354", fd))
-               return 0;
-
-       len = strlen(msg);
-       for(n=1; msg[len-n] == '\r' || msg[len-n] == '\n'; n++)
-               msg[len-n] = '\0';
-
-       if(strstr(msg, "Subject:") != msg)
-               smtp_tcp_writeline("", fd);
-       smtp_tcp_writeline(msg, fd);
-       smtp_tcp_writeline(".", fd);
-
-       smtp_tcp_readline(buff, sizeof(buff)-1, fd);
-
-       if(!validate_or_die_gracefully(buff, "250", fd))
-               return 0;
-
-       smtp_tcp_writeline("QUIT", fd);
-       smtp_tcp_readline(buff, sizeof(buff)-1, fd);
-       if(!validate_or_die_gracefully(buff, "221", fd))
-               return 1;
-
-       close(fd);
-       return 1;
-}
-
 static eb_local_account *eb_smtp_read_local_account_config(LList * pairs)
 {
        eb_local_account *ela;
@@ -602,18 +516,135 @@
                        _("SMTP send file"), 0);
 }
 
-/* TODO make this async */
-static void eb_smtp_send_im(eb_local_account * account_from, 
-               eb_account * account_to, char * message)
+static int validate_or_die_gracefully(const char *buff, const char *valid, int 
fd)
+{
+       if(strstr(buff, valid) == buff) {
+               return 1;
+       } 
+
+       fprintf(stderr, "%s\n", buff);
+       smtp_tcp_writeline("QUIT", fd);
+       close(fd);
+       return 0;
+}
+
+enum smtp_states { 
+       SMTP_CONNECT, SMTP_HELO, 
+       SMTP_FROM, SMTP_TO, 
+       SMTP_DATA, SMTP_DATA_END,
+       SMTP_QUIT
+};
+
+struct smtp_callback_data {
+       int tag;
+       char localhost[255];
+       eb_local_account * from;
+       eb_account * to;
+       char * msg;
+       enum smtp_states state;
+};
+
+static const char *expected[7]={
+       "220", "250", "250", "250", "354", "250", "221"
+};
+static void destroy_callback_data(struct smtp_callback_data * d)
+{
+       eb_input_remove(d->tag);
+       free(d->msg);
+       free(d);
+}
+
+static void smtp_message_sent(struct smtp_callback_data *d, int success)
 {
        char reply[1024] = "<FONT COLOR=\"#a0a0a0\"><I>";
-       if(smtp_send_message(account_from->handle, account_to->handle, message))
+       if(success)
                strcat(reply, _("Message Sent"));
        else
                strcat(reply, _("Error Sending Message"));
        strcat(reply, "</I></FONT>");
 
-       eb_parse_incoming_message(account_from, account_to, &SERVICE_INFO, 
reply);
+       eb_parse_incoming_message(d->from, d->to, &SERVICE_INFO, reply);
+}
+
+static void send_message_async(void * data, int fd, eb_input_condition cond)
+{
+       struct smtp_callback_data * d = data;
+       char buff[1024];
+
+       if(smtp_tcp_readline(buff, sizeof(buff)-1, fd) <= 0) {
+               fprintf(stderr, "smtp server closed connection\n");
+               close(fd);
+               destroy_callback_data(d);
+       };
+
+       if(!validate_or_die_gracefully(buff, expected[d->state], fd)) {
+               smtp_message_sent(d, 0);
+               destroy_callback_data(d);
+       }
+
+       switch(d->state) {
+       case SMTP_CONNECT:
+               snprintf(buff, sizeof(buff)-1, "HELO %s", d->localhost);
+               d->state = SMTP_HELO;
+               break;
+       case SMTP_HELO:
+               snprintf(buff, sizeof(buff)-1, "MAIL FROM: %s", 
d->from->handle);
+               d->state = SMTP_FROM;
+               break;
+       case SMTP_FROM:
+               snprintf(buff, sizeof(buff)-1, "RCPT TO: %s", d->to->handle);
+               d->state = SMTP_TO;
+               break;
+       case SMTP_TO:
+               strcpy(buff, "DATA");
+               d->state = SMTP_DATA;
+               break;
+       case SMTP_DATA:
+               {
+               int n, len = strlen(d->msg);
+               for(n=1; d->msg[len-n] == '\r' || d->msg[len-n] == '\n'; n++)
+                       d->msg[len-n] = '\0';
+               if(!strncasecmp(d->msg, "Subject:", strlen("Subject:")))
+                       smtp_tcp_writeline("", fd);
+               smtp_tcp_writeline(d->msg, fd);
+               strcpy(buff, ".");
+               }
+               d->state = SMTP_DATA_END;
+               break;
+       case SMTP_DATA_END:
+               strcpy(buff, "QUIT");
+               d->state = SMTP_QUIT;
+               break;
+       case SMTP_QUIT:
+               smtp_message_sent(d, 1);
+               destroy_callback_data(d);
+               return;
+       }
+       smtp_tcp_writeline(buff, fd);
+}
+
+static void eb_smtp_send_im(eb_local_account * account_from, 
+               eb_account * account_to, char * message)
+
+{
+       int fd;
+       char localhost[255];
+       struct smtp_callback_data * d;
+
+       if(gethostname(localhost, sizeof(localhost)-1) == -1) {
+               strcpy(localhost, "localhost");
+               fprintf(stderr, "could not get localhost name: %s\n", 
+                               strerror(errno));
+               return;
+       }
+
+       fd = smtp_connect(smtp_host, atoi(smtp_port));
+       d = calloc(1, sizeof(struct smtp_callback_data));
+       strcpy(d->localhost, localhost);
+       d->from = account_from;
+       d->to = account_to;
+       d->msg = strdup(message);
+       d->tag = eb_input_add(fd, EB_INPUT_READ, send_message_async, d);
 }
 
 static char * eb_smtp_check_login(char * user, char * pass)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]