From 0b22bd0689104b8c5ec82a73ee63d0b7599b6893 Mon Sep 17 00:00:00 2001 From: Carlos Daniel Ruvalcaba Valenzuela Date: Fri, 22 Jun 2007 07:40:01 -0700 Subject: [PATCH] Moved pop3 to new SearchTree --- backends/protocol/pop3.cpp | 115 ++++++++++++++++++++++++++++++++++----------- compile.sh | 42 ++++++++--------- 2 files changed, 109 insertions(+), 48 deletions(-) rewrite compile.sh (69%) diff --git a/backends/protocol/pop3.cpp b/backends/protocol/pop3.cpp index 3148d29..2700363 100644 --- a/backends/protocol/pop3.cpp +++ b/backends/protocol/pop3.cpp @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include +#include char *demomsg = "From: Test John \n" "To: test@localhost\n" @@ -34,16 +35,17 @@ char *demomsg = "From: Test John \n" const int POP3_PORT = 12001; //25; const char CRLF[2] = {0x0D, 0x0A}; -const char *KEY_USER = "USER"; -const char *KEY_PASS = "PASS"; -const char *KEY_STAT = "STAT"; -const char *KEY_LIST = "LIST"; -const char *KEY_RETR = "RETR"; -const char *KEY_DELE = "DELE"; -const char *KEY_NOOP = "NOOP"; -const char *KEY_QUIT = "QUIT"; -const char *KEY_RSET = "RSET"; -const char *KEY_TOP = "TOP"; +const int CMD_USER = 1; +const int CMD_PASS = 2; +const int CMD_STAT = 3; +const int CMD_LIST = 4; +const int CMD_RETR = 5; +const int CMD_DELE = 6; +const int CMD_NOOP = 7; +const int CMD_QUIT = 8; +const int CMD_RSET = 9; +const int CMD_TOP = 10; + const char KEY_END_DATA[6] = {0x0D, 0x0A, '.', 0x0D, 0x0A, 0x00}; #define STATE_AUTH 1 @@ -51,15 +53,50 @@ const char KEY_END_DATA[6] = {0x0D, 0x0A, '.', 0x0D, 0x0A, 0x00}; #define STATE_TRANSACTION 3 #define STATE_UPDATE 4 +int knhash(char v){ + if ((v >= 65) && (v <= 90)) + return v - 65; + + if (v == 95) + return 26; + + if ((v >= 97) && (v <= 122)) + return v - 97; + + if (v == ' ') + return -1; + + if (v == 0x0D) + return -1; + + return 255; +} + class POP3Handler : public ProtocolHandler{ + SearchTree cmdtree; public: + POP3Handler(){ + cmdtree.Insert("USER", 1); + cmdtree.Insert("PASS", 2); + cmdtree.Insert("STAT", 3); + cmdtree.Insert("LIST", 4); + cmdtree.Insert("RETR", 5); + cmdtree.Insert("DELE", 6); + cmdtree.Insert("NOOP", 7); + cmdtree.Insert("QUIT", 8); + cmdtree.Insert("RSET", 9); + cmdtree.Insert("TOP", 10); + } int Handle(Socket *s){ char buffer[255]; + char userbuff[255]; char outbuff[255]; int r, i, j; - int onrun; - int state; - + int onrun, cmd, state; + odkRegex *reg; + odkRegMatch *m; + + reg = odk_regex_new("[\\w\\a]*\\s([\\w\\a]*)", 0, 0); s->Write("+OK FancyMail v0.1", 18); s->Write(CRLF, 2); @@ -71,7 +108,13 @@ public: memset(buffer, 0, 255); r = s->Read(buffer, 255); - if (!strncasecmp(KEY_QUIT, buffer, strlen(KEY_QUIT))){ + try{ + cmd = cmdtree.Lookup(buffer); + }catch (SearchNotFound){ + cmd = -1; + } + + if (cmd == CMD_QUIT){ printf("Got QUIT\n"); s->Write( "+OK", 3); s->Write( CRLF, 2); @@ -80,20 +123,31 @@ public: switch (state){ case STATE_AUTH: - if (!strncasecmp(KEY_USER, buffer, strlen(KEY_USER))){ - printf("Got USERNAME: %s\n", buffer); + if (cmd == CMD_USER){ + + m = odk_regex_match(reg, buffer, 0); + + memset(userbuff, 0, 255); + memcpy(userbuff, (char*)((int)buffer + m->submatch[0].start), m->submatch[0].end - m->submatch[0].start); + printf("Got USERNAME: %s\n", userbuff); + odk_match_free(m); s->Write( "+OK", 3); s->Write( CRLF, 2); state = STATE_AUTH2; }else{ - printf("INVALID {AUTH]: %s\n", buffer); + printf("INVALID [AUTH]: %s\n", buffer); s->Write( "-ERR", 4); s->Write( CRLF, 2); } break; case STATE_AUTH2: - if (!strncasecmp(KEY_PASS, buffer, strlen(KEY_PASS))){ - printf("Got Password: %s\n", buffer); + if (cmd == CMD_PASS){ + m = odk_regex_match(reg, buffer, 0); + + memset(outbuff, 0, 255); + memcpy(outbuff, (char*)((int)buffer + m->submatch[0].start), m->submatch[0].end - m->submatch[0].start); + printf("Got Password: %s\n", outbuff); + s->Write( "+OK", 3); s->Write( CRLF, 2); state = STATE_TRANSACTION; @@ -105,11 +159,13 @@ public: } break; case STATE_TRANSACTION: - if (!strncasecmp(KEY_STAT, buffer, strlen(KEY_STAT))){ + switch (cmd){ + case CMD_STAT: printf("Got STAT Command\n"); s->Write( "+OK 3 400", 9); s->Write( CRLF, 2); - }else if (!strncasecmp(KEY_LIST, buffer, strlen(KEY_LIST))){ + break; + case CMD_LIST: printf("Got LIST Command: %s\n", buffer); s->Write( "+OK", 3); s->Write( CRLF, 2); @@ -124,7 +180,8 @@ public: s->Write( CRLF, 2); s->Write( ".", 1); s->Write( CRLF, 2); - }else if (!strncasecmp(KEY_RETR, buffer, strlen(KEY_RETR))){ + break; + case CMD_RETR: printf("Got RETR Command: %s\n", buffer); s->Write( "+OK 150 octets", 14); s->Write( CRLF, 2); @@ -134,19 +191,22 @@ public: s->Write( CRLF, 2); s->Write( ".", 1); s->Write( CRLF, 2); - }else if (!strncasecmp(KEY_DELE, buffer, strlen(KEY_DELE))){ + break; + case CMD_DELE: printf("Got DELE Command: %s\n", buffer); s->Write( "+OK", 3); s->Write( CRLF, 2); - }else if (!strncasecmp(KEY_NOOP, buffer, strlen(KEY_NOOP))){ + break; + case CMD_NOOP: printf("Got NOOP Command\n"); s->Write( "+OK", 3); s->Write( CRLF, 2); - }else if (!strncasecmp(KEY_RSET, buffer, strlen(KEY_RSET))){ + break; + case CMD_RSET: printf("Got RSET Command\n"); s->Write( "+OK", 3); s->Write( CRLF, 2); - }else if (!strncasecmp(KEY_TOP, buffer, strlen(KEY_TOP))){ + case CMD_TOP: printf("Got TOP Command: %s\n", buffer); for (i = strlen(buffer); i > 0; i--){ if (buffer[i] == ' ') @@ -169,7 +229,8 @@ public: } s->Write( ".", 1); s->Write( CRLF, 2); - }else{ + break; + default: printf("INVALID [TRANS]: %s\n", buffer); s->Write( "-ERR", 4); s->Write( CRLF, 2); diff --git a/compile.sh b/compile.sh dissimilarity index 69% index f80312f..670b18e 100755 --- a/compile.sh +++ b/compile.sh @@ -1,21 +1,21 @@ -#!/bin/bash - -rm -fr bin - -mkdir -p bin - -CFLAGS="-g -lstdc++" - -echo "Compiling libfmail" -g++ src/baseserver.cpp -I./include ${CFLAGS} -c -o bin/baseserver.o -g++ src/socket.cpp -I./include ${CFLAGS} -c -o bin/socket.o - -g++ bin/baseserver.o bin/socket.o -shared -o bin/libfmail.so - -echo "Compiling Protocols" -g++ backends/protocol/pop3.cpp `odkutils-config --libs --cflags` -I./include -L./ -lfmail -shared ${CFLAGS} -o bin/libfmail-pop3.so -g++ backends/protocol/smtp.cpp -I./include -L./ -lfmail -shared ${CFLAGS} -o bin/libfmail-smtp.so -g++ backends/auth/dbauth.cpp -I./include -L./ -lfmail -shared `odkutils-config --libs --cflags` `odkda-config --libs --cflags` ${CFLAGS} -o bin/libfmail-dbauth.so - -echo "Compiling FancyMail Server" -g++ src/fmail.cpp -I./include -L./bin -lfmail `odkutils-config --libs --cflags` ${CFLAGS} -o bin/fmail +#!/bin/bash + +rm -fr bin + +mkdir -p bin + +CFLAGS="-g" + +echo "Compiling libfmail" +g++ src/baseserver.cpp -I./include ${CFLAGS} -c -o bin/baseserver.o +g++ src/socket.cpp -I./include ${CFLAGS} -c -o bin/socket.o + +g++ bin/baseserver.o bin/socket.o -lstdc++ -shared -o bin/libfmail.so + +echo "Compiling Protocols" +g++ backends/protocol/pop3.cpp -lstdc++ `odkutils-config --libs --cflags` -I./include -L./ -lfmail -shared ${CFLAGS} -o bin/libfmail-pop3.so +g++ backends/protocol/smtp.cpp -lstdc++ -I./include -L./ -lfmail -shared ${CFLAGS} -o bin/libfmail-smtp.so +g++ backends/auth/dbauth.cpp -lstdc++ -I./include -L./ -lfmail -shared `odkutils-config --libs --cflags` `odkda-config --libs --cflags` ${CFLAGS} -o bin/libfmail-dbauth.so + +echo "Compiling FancyMail Server" +g++ src/fmail.cpp -I./include -L./bin -lfmail -lstdc++ `odkutils-config --libs --cflags` ${CFLAGS} -o bin/fmail -- 2.11.4.GIT