* custom sockets need to work buffered too...
[citadel.git] / citadel / context.h
1 /* $Id: sysdep_decls.h 7265 2009-03-25 23:18:46Z dothebart $ */
2
3 #ifndef CONTEXT_H
4 #define CONTEXT_H
5
6 #include <stdarg.h>
7 #include "sysdep.h"
8 #include "server.h"
9 #include "sysdep_decls.h"
10 #include "threads.h"
11
12
13 /*
14  * Here's the big one... the Citadel context structure.
15  *
16  * This structure keeps track of all information relating to a running 
17  * session on the server.  We keep one of these for each session thread.
18  *
19  */
20 struct CitContext {
21         struct CitContext *prev;        /* Link to previous session in list */
22         struct CitContext *next;        /* Link to next session in the list */
23
24         int cs_pid;             /* session ID */
25         int dont_term;          /* for special activities like artv so we don't get killed */
26         time_t lastcmd;         /* time of last command executed */
27         time_t lastidle;        /* For computing idle time */
28         int state;              /* thread state (see CON_ values below) */
29         int kill_me;            /* Set to nonzero to flag for termination */
30
31         const char *Pos;        /* Our read position inside of the ReadBuf */
32         StrBuf *ReadBuf;        /* Our block buffered read buffer */
33         StrBuf *MigrateBuf;        /* Our block buffered read buffer */
34
35         const char *sPos;        /* Our read position inside of the ReadBuf */
36         StrBuf *sReadBuf;        /* Our block buffered read buffer */
37         StrBuf *sMigrateBuf;        /* Our block buffered read buffer */
38         int client_socket;
39         int is_local_socket;    /* set to 1 if client is on unix domain sock */
40         /* Redirect this session's output to a memory buffer? */
41         char *redirect_buffer;          /* the buffer */
42         size_t redirect_len;            /* length of data in buffer */
43         size_t redirect_alloc;          /* length of allocated buffer */
44 #ifdef HAVE_OPENSSL
45         SSL *ssl;
46         int redirect_ssl;
47 #endif
48
49         char curr_user[USERNAME_SIZE];  /* name of current user */
50         int logged_in;          /* logged in */
51         int internal_pgm;       /* authenticated as internal program */
52         int nologin;            /* not allowed to log in */
53         int curr_view;          /* The view type for the current user/room */
54         int is_master;          /* Is this session logged in using the master user? */
55
56         char net_node[32]       ;/* Is the client another Citadel server? */
57         time_t previous_login;  /* Date/time of previous login */
58         char lastcmdname[5];    /* name of last command executed */
59         unsigned cs_flags;      /* miscellaneous flags */
60         int is_async;           /* Nonzero if client accepts async msgs */
61         int async_waiting;      /* Nonzero if there are async msgs waiting */
62         int input_waiting;      /* Nonzero if there is client input waiting */
63         int can_receive_im;     /* Session is capable of receiving instant messages */
64
65         /* Client information */
66         int cs_clientdev;       /* client developer ID */
67         int cs_clienttyp;       /* client type code */
68         int cs_clientver;       /* client version number */
69         uid_t cs_UDSclientUID;  /* the uid of the client when talking via UDS */
70         char cs_clientname[32]; /* name of client software */
71         char cs_host[64];       /* host logged in from */
72         char cs_addr[64];       /* address logged in from */
73
74         /* The Internet type of thing */
75         char cs_inet_email[128];                /* Return address of outbound Internet mail */
76         char cs_inet_other_emails[1024];        /* User's other valid Internet email addresses */
77         char cs_inet_fn[128];                   /* Friendly-name of outbound Internet mail */
78
79         FILE *download_fp;      /* Fields relating to file transfer */
80         size_t download_fp_total;
81         char download_desired_section[128];
82         FILE *upload_fp;
83         char upl_file[256];
84         char upl_path[PATH_MAX];
85         char upl_comment[256];
86         char upl_filedir[PATH_MAX];
87         char upl_mimetype[64];
88         char dl_is_net;
89         char upload_type;
90
91         struct ctdluser user;   /* Database record buffers */
92         struct ctdlroom room;
93
94         /* Beginning of cryptography - session nonce */
95         char cs_nonce[NONCE_SIZE];      /* The nonce for this session's next auth transaction */
96
97
98         /* A linked list of all instant messages sent to us. */
99         struct ExpressMessage *FirstExpressMessage;
100         int disable_exp;        /* Set to 1 to disable incoming pages */
101         int newmail;            /* Other sessions increment this */
102
103         /* Masqueraded values in the 'who is online' list */
104         char fake_username[USERNAME_SIZE];
105         char fake_hostname[64];
106         char fake_roomname[ROOMNAMELEN];
107
108         /* Preferred MIME formats */
109         char preferred_formats[256];
110         int msg4_dont_decode;
111
112         /* Dynamically allocated session data */
113         char *session_specific_data;            /* Used by individual protocol modules */
114         struct cit_ical *CIT_ICAL;              /* calendaring data */
115         struct ma_info *ma;                     /* multipart/alternative data */
116         const char *ServiceName;                /* readable purpose of this session */
117         void *openid_data;                      /* Data stored by the OpenID module */
118         char *ldap_dn;                          /* DN of user when using AUTHMODE_LDAP */
119
120
121         void (*h_command_function) (void) ;     /* service command function */
122         void (*h_async_function) (void) ;       /* do async msgs function */
123         void (*h_greeting_function) (void) ;    /* greeting function for session startup */
124 };
125
126 typedef struct CitContext CitContext;
127
128 /*
129  * Values for CitContext.state
130  * 
131  * A session that is doing nothing is in CON_IDLE state.  When activity
132  * is detected on the socket, it goes to CON_READY, indicating that it
133  * needs to have a worker thread bound to it.  When a thread binds to
134  * the session, it goes to CON_EXECUTING and does its thing.  When the
135  * transaction is finished, the thread sets it back to CON_IDLE and lets
136  * it go.
137  */
138 enum {
139         CON_IDLE,               /* This context is doing nothing */
140         CON_GREETING,           /* This context needs to output its greeting */
141         CON_STARTING,           /* This context is outputting its greeting */
142         CON_READY,              /* This context needs attention */
143         CON_EXECUTING           /* This context is bound to a thread */
144 };
145
146 #define CC MyContext()
147
148
149 extern citthread_key_t MyConKey;                        /* TSD key for MyContext() */
150 extern int num_sessions;
151 extern CitContext masterCC;
152 extern CitContext *ContextList;
153
154 CitContext *MyContext (void);
155 void RemoveContext (struct CitContext *);
156 CitContext *CreateNewContext (void);
157 void context_cleanup(void);
158 void kill_session (int session_to_kill);
159 INLINE void become_session(struct CitContext *which_con);
160 void InitializeMasterCC(void);
161 void dead_session_purge(int force);
162
163 /* Deprecated, user CtdlBumpNewMailCounter() instead */
164 void BumpNewMailCounter(long) __attribute__ ((deprecated));
165
166 void terminate_idle_sessions(void);
167 int CtdlTerminateOtherSession (int session_num);
168 /* bits returned by CtdlTerminateOtherSession */
169 #define TERM_FOUND      0x01
170 #define TERM_ALLOWED    0x02
171 #define TERM_KILLED     0x03
172 #define TERM_NOTALLOWED -1
173 #endif /* CONTEXT_H */