Libevent integration
[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         }
108         /* Unblock the other side */
109         read(fd, buf, 1);
110 }
111
112 /*
113  * this thread operates the select() etc. via libevent.
114  * 
115  * 
116  */
117 void *client_event_thread(void *arg) {
118         struct CitContext libevent_client_CC;
119         event_base = event_init();
120 /*
121         base = event_base_new();
122         if (!base)
123                 return NULL; /*XXXerr*/
124
125         citthread_mutex_init(&EventQueueMutex, NULL);
126         CtdlFillSystemContext(&libevent_client_CC, "LibEvent Thread");
127 //      citthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
128         CtdlLogPrintf(CTDL_DEBUG, "client_event_thread() initializing\n");
129         
130         if (pipe(event_add_pipe) != 0) {
131                 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for libevent queueing: %s\n", strerror(errno));
132                 abort();
133         }
134
135         event_set(&queue_add_event, 
136                   event_add_pipe[0], 
137                   EV_READ|EV_PERSIST,
138                   QueueEventAddCallback, 
139                   NULL);
140         
141         event_add(&queue_add_event, NULL);
142
143
144         event_dispatch();
145         CtdlClearSystemContext();
146         event_base_free(event_base);
147         citthread_mutex_destroy(&EventQueueMutex);
148         return(NULL);
149 }
150
151 #endif
152
153 CTDL_MODULE_INIT(event_client)
154 {
155 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
156         if (!threading)
157         {
158                 CtdlThreadCreate("Client event", CTDLTHREAD_BIGSTACK, client_event_thread, NULL);
159                 QueueEvents = NewHashList(1, Flathash);
160 /// todo register shutdown callback.
161         }
162 #endif
163         return "event";
164 }