4de6fff6add53efc2d4c938bc0fb1d8a639c4449
[citadel.git] / citadel / modules / eventclient / serv_evventclient.c
1 /*
2  * This module is an SMTP and ESMTP implementation for the Citadel system.
3  * It is compliant with all of the following:
4  *
5  * RFC  821 - Simple Mail Transfer Protocol
6  * RFC  876 - Survey of SMTP Implementations
7  * RFC 1047 - Duplicate messages and SMTP
8  * RFC 1652 - 8 bit MIME
9  * RFC 1869 - Extended Simple Mail Transfer Protocol
10  * RFC 1870 - SMTP Service Extension for Message Size Declaration
11  * RFC 2033 - Local Mail Transfer Protocol
12  * RFC 2197 - SMTP Service Extension for Command Pipelining
13  * RFC 2476 - Message Submission
14  * RFC 2487 - SMTP Service Extension for Secure SMTP over TLS
15  * RFC 2554 - SMTP Service Extension for Authentication
16  * RFC 2821 - Simple Mail Transfer Protocol
17  * RFC 2822 - Internet Message Format
18  * RFC 2920 - SMTP Service Extension for Command Pipelining
19  *  
20  * The VRFY and EXPN commands have been removed from this implementation
21  * because nobody uses these commands anymore, except for spammers.
22  *
23  * Copyright (c) 1998-2009 by the citadel.org team
24  *
25  *  This program is free software; you can redistribute it and/or modify
26  *  it under the terms of the GNU General Public License as published by
27  *  the Free Software Foundation; either version 3 of the License, or
28  *  (at your option) any later version.
29  *
30  *  This program is distributed in the hope that it will be useful,
31  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
32  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  *  GNU General Public License for more details.
34  *
35  *  You should have received a copy of the GNU General Public License
36  *  along with this program; if not, write to the Free Software
37  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38  */
39
40 #include "sysdep.h"
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <termios.h>
45 #include <fcntl.h>
46 #include <signal.h>
47 #include <pwd.h>
48 #include <errno.h>
49 #include <sys/types.h>
50 #include <syslog.h>
51
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
54 # include <time.h>
55 #else
56 # if HAVE_SYS_TIME_H
57 #  include <sys/time.h>
58 # else
59 #  include <time.h>
60 # endif
61 #endif
62 #include <sys/wait.h>
63 #include <ctype.h>
64 #include <string.h>
65 #include <limits.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <libcitadel.h>
70 #include "citadel.h"
71 #include "server.h"
72 #include "citserver.h"
73 #include "support.h"
74
75 #include "ctdl_module.h"
76
77 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
78
79 #include "event_client.h"
80
81 int event_add_pipe[2];
82
83 citthread_mutex_t EventQueueMutex; /* locks the access to the following vars: */
84 void *QueueEventAddPtr = NULL;
85 EventContextAttach EventContextAttachPtr;
86 AsyncIO *QueueThisIO;
87 HashList *QueueEvents = NULL;
88
89 static struct event_base *event_base;
90 struct event queue_add_event;
91
92 static void QueueEventAddCallback(int fd, short event, void *ctx)
93 {
94         char buf[10];
95
96         /* get the control command... */
97         read(fd, buf, 1);
98         switch (buf[0]) {
99         case '+':
100                 EventContextAttachPtr(QueueEventAddPtr);
101 /// TODO: add it to QueueEvents
102                 break;
103         case 'x':
104                 event_del(&queue_add_event);
105                 close(event_add_pipe[0]);
106 /// TODO; flush QueueEvents fd's and delete it.
107                 event_base_loopexit(event_base, NULL);
108         }
109         /* Unblock the other side */
110         read(fd, buf, 1);
111 }
112
113 /*
114  * this thread operates the select() etc. via libevent.
115  * 
116  * 
117  */
118 void *client_event_thread(void *arg) {
119         struct CitContext libevent_client_CC;
120         event_base = event_init();
121 /*
122         base = event_base_new();
123         if (!base)
124                 return NULL; /*XXXerr*/
125
126         citthread_mutex_init(&EventQueueMutex, NULL);
127         CtdlFillSystemContext(&libevent_client_CC, "LibEvent Thread");
128 //      citthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
129         CtdlLogPrintf(CTDL_DEBUG, "client_event_thread() initializing\n");
130         
131         if (pipe(event_add_pipe) != 0) {
132                 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for libevent queueing: %s\n", strerror(errno));
133                 abort();
134         }
135
136         event_set(&queue_add_event, 
137                   event_add_pipe[0], 
138                   EV_READ|EV_PERSIST,
139                   QueueEventAddCallback, 
140                   NULL);
141         
142         event_add(&queue_add_event, NULL);
143
144
145         event_dispatch();
146         CtdlClearSystemContext();
147         event_base_free(event_base);
148         citthread_mutex_destroy(&EventQueueMutex);
149         return(NULL);
150 }
151
152 #endif
153
154 CTDL_MODULE_INIT(event_client)
155 {
156 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
157         if (!threading)
158         {
159                 CtdlThreadCreate("Client event", CTDLTHREAD_BIGSTACK, client_event_thread, NULL);
160                 QueueEvents = NewHash(1, Flathash);
161 /// todo register shutdown callback.
162         }
163 #endif
164         return "event";
165 }