Changed smtp for new worker classes
[fmail.git] / backends / mailbox / fsmailbox.cpp
blobd588d572302b3651e4612759a3639cd1e50d67a7
2 #include <libfmail.h>
3 #include <libfmail/socketipc.h>
4 #include <dirent.h>
6 enum {MSG_LIST = 1, MSG_STORE = 2, MSG_RETR = 3};
8 int knhash(char v){
9 if ((v >= 'A') && (v <= 'Z'))
10 return v - 'A';
12 if ((v >= 'a') && (v <= 'z'))
13 return v - 'a';
15 return 255;
18 class FSMailbox : public ProtocolHandler{
19 private:
20 SearchTree<int, 26, &knhash> cmdtree;
21 public:
22 FSMailbox(){
23 cmdtree.Insert("list", MSG_LIST);
24 cmdtree.Insert("store", MSG_STORE);
25 cmdtree.Insert("retr", MSG_RETR);
27 int Handle(Socket *s){
28 IPCMessage *msg;
29 IPC *ipc;
30 int onRun, msgid, n, i, len;
31 char *user, *rcpt, *msglen, buffer[255];
32 FILE *fd;
33 //DIR *dir;
34 struct dirent **dent;
36 printf("Got Client\n");
37 printf("Creating new IPC\n");
39 ipc = new SocketIPC(s);
40 onRun = 1;
42 while(onRun){
43 printf("Waiting for Message\n");
44 if (ipc->PeekMessage()){
45 printf("Got Message\n");
46 msg = ipc->PopMessage();
47 try{
48 msgid = cmdtree.Lookup((char*)msg->GetMessageName());
49 }catch (SearchNotFound){
50 msgid = -1;
52 switch(msgid){
53 case MSG_LIST:
54 user = msg->PopParam();
55 sprintf(buffer, "/tmp/fmail/%s/", user);
56 n = scandir(buffer, &dent, NULL, alphasort);
57 sprintf(buffer, "%i", n);
58 msg = new IPCMessage("ok");
59 msg->PushParam(buffer);
60 ipc->PushMessage(msg);
61 for (i = 0; i < n; i++){
62 msg = new IPCMessage("mailinfo");
63 msg->PushParam(dent[i]->d_name);
64 free(dent[i]);
66 free(dent);
67 break;
68 case MSG_STORE:
69 user = msg->PopParam();
70 rcpt = msg->PopParam();
71 msglen = msg->PopParam();
72 //len = atoi(msglen);
73 len = 1025;
74 fd = fopen("/tmp/fmail/in/1", "w");
75 n = 0;
76 while(n < len){
77 i = ipc->RawRead(buffer, 255);
78 fwrite(buffer, 1, i, fd);
79 if (i == 0)
80 break;
81 n += i;
83 fclose(fd);
84 break;
85 case MSG_RETR:
86 break;
87 case 4:
88 break;
89 default:
90 break;
94 ipc->CloseIPC();
95 delete ipc;
96 return 0;
100 class SimpleLoad : public LoadHandler{
101 public:
102 int Dispatch(Socket *sock, ProtocolHandler *ph){
103 printf("SimpleLoad: Dispatching\n");
104 if (ph)
105 return ph->Handle(sock);
106 return 0;
110 int main(){
111 //BaseServer *serv;
112 LoadHandler *lh;
113 FSMailbox *handler;
114 Socket *s, *client;
115 int r;
117 s = Socket::CreateSocket(SOCKET_INET, 0);
118 s->setAddress("127.0.0.1");
119 s->setPort(14001);
121 s->Bind();
122 s->Listen(10);
124 printf("Creating new FSMailbox\n");
125 handler = new FSMailbox();
127 printf("Creating new SimpleLoad\n");
128 lh = new SimpleLoad();
130 while (1){
131 r = s->Poll(10000, SOCKET_POLL_READ);
132 if (r == SOCKET_POLL_READ){
133 printf("Got new client!\n");
134 client = s->Accept();
135 lh->Dispatch(client, handler);
139 return 0;