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