4b450a416720bbc9cd0c9eb415a12415b50f5516
[citadel.git] / citadel / context.h
1
2 #ifndef CONTEXT_H
3 #define CONTEXT_H
4
5 #include <stdarg.h>
6 #include "sysdep.h"
7 #include "server.h"
8 #include "sysdep_decls.h"
9 #include "threads.h"
10
11
12 /*
13  * Values for CitContext.state
14  * 
15  * A session that is doing nothing is in CON_IDLE state.  When activity
16  * is detected on the socket, it goes to CON_READY, indicating that it
17  * needs to have a worker thread bound to it.  When a thread binds to
18  * the session, it goes to CON_EXECUTING and does its thing.  When the
19  * transaction is finished, the thread sets it back to CON_IDLE and lets
20  * it go.
21  */
22 typedef enum __CCState {
23         CON_IDLE,               /* This context is doing nothing */
24         CON_GREETING,           /* This context needs to output its greeting */
25         CON_STARTING,           /* This context is outputting its greeting */
26         CON_READY,              /* This context needs attention */
27         CON_EXECUTING,          /* This context is bound to a thread */
28         CON_SYS                 /* This is a system context and mustn't be purged */
29 } CCState;
30
31 #ifndef __ASYNCIO__
32 #define __ASYNCIO__
33 typedef struct AsyncIO AsyncIO; /* forward declaration for event_client.h */
34 #endif
35 #ifndef __CIT_CONTEXT__
36 #define __CIT_CONTEXT__
37 typedef struct CitContext CitContext;
38 #endif
39
40 /*
41  * Here's the big one... the Citadel context structure.
42  *
43  * This structure keeps track of all information relating to a running 
44  * session on the server.  We keep one of these for each session thread.
45  *
46  */
47 struct CitContext {
48         CitContext *prev;       /* Link to previous session in list */
49         CitContext *next;       /* Link to next session in the list */
50
51         int cs_pid;             /* session ID */
52         int dont_term;          /* for special activities like artv so we don't get killed */
53         double created;      /* time of birth */
54         time_t lastcmd;         /* time of last command executed */
55         time_t lastidle;        /* For computing idle time */
56         CCState state;          /* thread state (see CON_ values below) */
57         int kill_me;            /* Set to nonzero to flag for termination */
58
59         IOBuffer SendBuf, /* Our write Buffer */
60                 RecvBuf, /* Our block buffered read buffer */
61                 SBuf; /* Our block buffered read buffer for clients */
62
63         StrBuf *MigrateBuf;        /* Our block buffered read buffer */
64         StrBuf *sMigrateBuf;        /* Our block buffered read buffer */
65
66         int client_socket;
67         int is_local_socket;    /* set to 1 if client is on unix domain sock */
68         /* Redirect this session's output to a memory buffer? */
69         StrBuf *redirect_buffer;                /* the buffer */
70         StrBuf *StatusMessage;
71 #ifdef HAVE_OPENSSL
72         SSL *ssl;
73         int redirect_ssl;
74 #endif
75
76         char curr_user[USERNAME_SIZE];  /* name of current user */
77         int logged_in;          /* logged in */
78         int internal_pgm;       /* authenticated as internal program */
79         int nologin;            /* not allowed to log in */
80         int curr_view;          /* The view type for the current user/room */
81         int is_master;          /* Is this session logged in using the master user? */
82
83         char net_node[32]       ;/* Is the client another Citadel server? */
84         time_t previous_login;  /* Date/time of previous login */
85         char lastcmdname[5];    /* name of last command executed */
86         unsigned cs_flags;      /* miscellaneous flags */
87         int is_async;           /* Nonzero if client accepts async msgs */
88         int async_waiting;      /* Nonzero if there are async msgs waiting */
89         int input_waiting;      /* Nonzero if there is client input waiting */
90         int can_receive_im;     /* Session is capable of receiving instant messages */
91
92         /* Client information */
93         int cs_clientdev;       /* client developer ID */
94         int cs_clienttyp;       /* client type code */
95         int cs_clientver;       /* client version number */
96         char cs_clientinfo[256];/* if its a unix domain socket, some info for logging. */
97         uid_t cs_UDSclientUID;  /* the uid of the client when talking via UDS */
98         char cs_clientname[32]; /* name of client software */
99         char cs_host[64];       /* host logged in from */
100         char cs_addr[64];       /* address logged in from */
101
102         /* The Internet type of thing */
103         char cs_inet_email[128];                /* Return address of outbound Internet mail */
104         char cs_inet_other_emails[1024];        /* User's other valid Internet email addresses */
105         char cs_inet_fn[128];                   /* Friendly-name of outbound Internet mail */
106
107         FILE *download_fp;      /* Fields relating to file transfer */
108         size_t download_fp_total;
109         char download_desired_section[128];
110         FILE *upload_fp;
111         char upl_file[256];
112         char upl_path[PATH_MAX];
113         char upl_comment[256];
114         char upl_filedir[PATH_MAX];
115         char upl_mimetype[64];
116         char dl_is_net;
117         char upload_type;
118
119         struct ctdluser user;   /* Database record buffers */
120         struct ctdlroom room;
121
122         /* A linked list of all instant messages sent to us. */
123         struct ExpressMessage *FirstExpressMessage;
124         int disable_exp;        /* Set to 1 to disable incoming pages */
125         int newmail;            /* Other sessions increment this */
126
127         /* Masqueraded values in the 'who is online' list */
128         char fake_username[USERNAME_SIZE];
129         char fake_hostname[64];
130         char fake_roomname[ROOMNAMELEN];
131
132         /* Preferred MIME formats */
133         char preferred_formats[256];
134         int msg4_dont_decode;
135
136         /* Dynamically allocated session data */
137         void *session_specific_data;            /* Used by individual protocol modules */
138         struct cit_ical *CIT_ICAL;              /* calendaring data */
139         struct ma_info *ma;                     /* multipart/alternative data */
140         const char *ServiceName;                /* readable purpose of this session */
141         long tcp_port;
142         void *openid_data;                      /* Data stored by the OpenID module */
143         char *ldap_dn;                          /* DN of user when using AUTHMODE_LDAP */
144
145         void (*h_command_function) (void) ;     /* service command function */
146         void (*h_async_function) (void) ;       /* do async msgs function */
147         void (*h_greeting_function) (void) ;    /* greeting function for session startup */
148
149         long *cached_msglist;                   /* results of the previous CtdlForEachMessage() */
150         int cached_num_msgs;
151
152         char vcard_updated_by_ldap;             /* !0 iff ldap changed the vcard, treat as aide update */
153
154         AsyncIO *IO;                            /* if this session has AsyncIO going on... */
155 };
156
157
158
159 #define CC MyContext()
160
161
162 extern pthread_key_t MyConKey;                  /* TSD key for MyContext() */
163 extern int num_sessions;
164 extern CitContext masterCC;
165 extern CitContext *ContextList;
166
167 CitContext *MyContext (void);
168 void RemoveContext (struct CitContext *);
169 CitContext *CreateNewContext (void);
170 void context_cleanup(void);
171 void kill_session (int session_to_kill);
172 void InitializeMasterCC(void);
173 void dead_session_purge(int force);
174 void set_async_waiting(struct CitContext *ccptr);
175
176 CitContext *CloneContext(CitContext *CloneMe);
177
178 /* forcibly close and flush fd's on shutdown */
179 void terminate_all_sessions(void);
180
181 /* Deprecated, user CtdlBumpNewMailCounter() instead */
182 void BumpNewMailCounter(long) __attribute__ ((deprecated));
183
184 void terminate_idle_sessions(void);
185 int CtdlTerminateOtherSession (int session_num);
186 /* bits returned by CtdlTerminateOtherSession */
187 #define TERM_FOUND      0x01
188 #define TERM_ALLOWED    0x02
189 #define TERM_KILLED     0x03
190 #define TERM_NOTALLOWED -1
191
192 /*
193  * Bind a thread to a context.  (It's inline merely to speed things up.)
194  */
195 static INLINE void become_session(CitContext *which_con) {
196 /*
197         pid_t tid = syscall(SYS_gettid);
198 */
199         pthread_setspecific(MyConKey, (void *)which_con );
200 /*
201         syslog(LOG_DEBUG, "[%d]: Now doing %s\n", 
202                       (int) tid, 
203                       ((which_con != NULL) && (which_con->ServiceName != NULL)) ? 
204                       which_con->ServiceName:"");
205 */
206 }
207
208
209
210 /* typedef void (*CtdlDbgFunction) (const int); */
211
212 extern int DebugSession;
213 #define CONDBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (DebugSession != 0))
214
215 #define CON_syslog(LEVEL, FORMAT, ...)                          \
216         CONDBGLOG(LEVEL) syslog(LEVEL,                          \
217                                 "Context: " FORMAT, __VA_ARGS__)
218
219 #define CONM_syslog(LEVEL, FORMAT)                      \
220         CONDBGLOG(LEVEL) syslog(LEVEL,                  \
221                                 "Context: " FORMAT);
222
223
224 #endif /* CONTEXT_H */