4b53032eb75157e727d2b9bfcdcef85c9246f23b
[citadel.git] / citadel / msgbase.h
1
2 #ifndef MSGBASE_H
3 #define MSGBASE_H
4
5 #include "event_client.h"
6 enum {
7         MSGS_ALL,
8         MSGS_OLD,
9         MSGS_NEW,
10         MSGS_FIRST,
11         MSGS_LAST,
12         MSGS_GT,
13         MSGS_EQ,
14         MSGS_SEARCH,
15         MSGS_LT
16 };
17
18 enum {
19         MSG_HDRS_BRIEF = 0,
20         MSG_HDRS_ALL = 1,
21         MSG_HDRS_EUID = 4
22 };
23
24 /*
25  * Possible return codes from CtdlOutputMsg()
26  */
27 enum {
28         om_ok,
29         om_not_logged_in,
30         om_no_such_msg,
31         om_mime_error,
32         om_access_denied
33 };
34
35 /*
36  * Values of "headers_only" when calling message output routines
37  */
38 #define HEADERS_ALL     0       /* Headers and body */
39 #define HEADERS_ONLY    1       /* Headers only */
40 #define HEADERS_NONE    2       /* Body only */
41 #define HEADERS_FAST    3       /* Headers only with no MIME info */
42
43
44 struct ma_info {
45         int is_ma;              /* Set to 1 if we are using this stuff */
46         int freeze;             /* Freeze the replacement chain because we're
47                                  * digging through a subsection */
48         int did_print;          /* One alternative has been displayed */
49         char chosen_part[128];  /* Which part of a m/a did we choose? */
50         int chosen_pref;        /* Chosen part preference level (lower is better) */
51         int use_fo_hooks;       /* Use fixed output hooks */
52         int dont_decode;        /* should we call the decoder or not? */
53 };
54
55
56 struct repl {                   /* Info for replication checking */
57         char exclusive_id[SIZ];
58         time_t highest;
59 };
60
61
62
63 /*
64  * This is a list of "harvested" email addresses that we might want to
65  * stick into someone's address book.  But we defer this operaiton so
66  * it can be done asynchronously.
67  */
68 struct addresses_to_be_filed {
69         struct addresses_to_be_filed *next;
70         char *roomname;
71         char *collected_addresses;
72 };
73
74 extern struct addresses_to_be_filed *atbf;
75
76 void memfmout (char *mptr, const char *nl);
77 void output_mime_parts(char *);
78 long send_message (struct CtdlMessage *);
79 void loadtroom (void);
80 long CtdlSubmitMsg(struct CtdlMessage *, recptypes *, const char *, int);
81
82 void quickie_message(const char *from,
83                      const char *fromaddr,
84                      const char *to,
85                      char *room,
86                      const char *text, 
87                      int format_type,
88                      const char *subject);
89
90 void flood_protect_quickie_message(const char *from,
91                                    const char *fromaddr,
92                                    const char *to,
93                                    char *room,
94                                    const char *text, 
95                                    int format_type,
96                                    const char *subject,
97                                    int nCriterions,
98                                    const char **CritStr,
99                                    const long *CritStrLen,
100                                    long ccid,
101                                    long ioid,
102                                    time_t NOW);
103
104 void GetMetaData(struct MetaData *, long);
105 void PutMetaData(struct MetaData *);
106 void AdjRefCount(long, int);
107 void TDAP_AdjRefCount(long, int);
108 int TDAP_ProcessAdjRefCountQueue(void);
109 void simple_listing(long, void *);
110 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template);
111 typedef void (*ForEachMsgCallback)(long MsgNumber, void *UserData);
112 int CtdlForEachMessage(int mode,
113                         long ref,
114                         char *searchstring,
115                         char *content_type,
116                         struct CtdlMessage *compare,
117                         ForEachMsgCallback CallBack,
118                         void *userdata);
119 int CtdlDeleteMessages(const char *, long *, int, char *);
120 void CtdlWriteObject(char *req_room,                    /* Room to stuff it in */
121                         char *content_type,             /* MIME type of this object */
122                         char *raw_message,              /* Data to be written */
123                         off_t raw_length,               /* Size of raw_message */
124                         struct ctdluser *is_mailbox,    /* Mailbox room? */
125                         int is_binary,                  /* Is encoding necessary? */
126                         int is_unique,                  /* Del others of this type? */
127                         unsigned int flags              /* Internal save flags */
128 );
129 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body, int run_msg_hooks);
130 struct CtdlMessage * CM_Duplicate
131                        (struct CtdlMessage *OrgMsg);
132 int  CM_IsEmpty        (struct CtdlMessage *Msg, eMsgField which);
133 void CM_SetField       (struct CtdlMessage *Msg, eMsgField which, const char *buf, long length);
134 void CM_SetFieldLONG   (struct CtdlMessage *Msg, eMsgField which, long lvalue);
135 void CM_CopyField      (struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy);
136 void CM_CutFieldAt     (struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen);
137 void CM_FlushField     (struct CtdlMessage *Msg, eMsgField which);
138 void CM_Flush          (struct CtdlMessage *Msg);
139 void CM_SetAsField     (struct CtdlMessage *Msg, eMsgField which, char **buf, long length);
140 void CM_SetAsFieldSB   (struct CtdlMessage *Msg, eMsgField which, StrBuf **buf);
141 void CM_GetAsField     (struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen);
142 void CM_PrependToField (struct CtdlMessage *Msg, eMsgField which, const char *buf, long length);
143
144 void CM_Free           (struct CtdlMessage *msg);
145 void CM_FreeContents   (struct CtdlMessage *msg);
146 int  CM_IsValidMsg     (struct CtdlMessage *msg);
147
148 #define CM_KEY(Message, Which) Message->cm_fields[Which], Message->cm_lengths[Which]
149 #define CM_RANGE(Message, Which) Message->cm_fields[Which], \
150                 Message->cm_fields[Which] + Message->cm_lengths[Which]
151
152 void CtdlSerializeMessage(struct ser_ret *, struct CtdlMessage *);
153 struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const char *Buffer, long Length);
154 void ReplicationChecks(struct CtdlMessage *);
155 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
156                               int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj);
157 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int do_repl_check, struct CtdlMessage *msg);
158 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply);
159 char *CtdlReadMessageBody(char *terminator, long tlen, size_t maxlen, StrBuf *exist, int crlf, int *sock);
160 StrBuf *CtdlReadMessageBodyBuf(char *terminator,        /* token signalling EOT */
161                                long tlen,
162                                size_t maxlen,           /* maximum message length */
163                                StrBuf *exist,           /* if non-null, append to it;
164                                                            exist is ALWAYS freed  */
165                                int crlf,                /* CRLF newlines instead of LF */
166                                int *sock                /* socket handle or 0 for this session's client socket */
167         );
168
169 int CtdlOutputMsg(long msg_num,         /* message number (local) to fetch */
170                 int mode,               /* how would you like that message? */
171                 int headers_only,       /* eschew the message body? */
172                 int do_proto,           /* do Citadel protocol responses? */
173                 int crlf,               /* 0=LF, 1=CRLF */
174                 char *section,          /* output a message/rfc822 section */
175                 int flags,              /* should the bessage be exported clean? */
176                 char **Author,          /* if you want to know the author of the message... */
177                 char **Address,         /* if you want to know the sender address of the message... */
178                 char **MessageID        /* if you want to know the Message-ID of the message... */
179 );
180
181 /* Flags which may be passed to CtdlOutputMsg() and CtdlOutputPreLoadedMsg() */
182 #define QP_EADDR        (1<<0)          /* quoted-printable encode email addresses */
183 #define CRLF            (1<<1)
184 #define ESC_DOT         (1<<2)          /* output a line containing only "." as ".." instead */
185 #define SUPPRESS_ENV_TO (1<<3)          /* suppress Envelope-to: header (warning: destructive!) */
186
187 int CtdlOutputPreLoadedMsg(struct CtdlMessage *,
188                            int mode,            /* how would you like that message? */
189                            int headers_only,    /* eschew the message body? */
190                            int do_proto,        /* do Citadel protocol responses? */
191                            int crlf,            /* 0=LF, 1=CRLF */
192                            int flags            /* should the bessage be exported clean? */
193 );
194
195
196 /* values for which_set */
197 enum {
198         ctdlsetseen_seen,
199         ctdlsetseen_answered
200 };
201 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
202                  int target_setting, int which_set,
203                 struct ctdluser *which_user, struct ctdlroom *which_room);
204 void CtdlGetSeen(char *buf, int which_set);
205
206
207 struct CtdlMessage *CtdlMakeMessage(
208         struct ctdluser *author,        /* author's user structure */
209         char *recipient,                /* NULL if it's not mail */
210         char *recp_cc,                  /* NULL if it's not mail */
211         char *room,                     /* room where it's going */
212         int type,                       /* see MES_ types in header file */
213         int format_type,                /* variformat, plain text, MIME... */
214         char *fake_name,                /* who we're masquerading as */
215         char *my_email,                 /* which of my email addresses to use (empty is ok) */
216         char *subject,                  /* Subject (optional) */
217         char *supplied_euid,            /* ...or NULL if this is irrelevant */
218         char *preformatted_text,        /* ...or NULL to read text from client */
219         char *references                /* Thread references */
220 );
221
222 struct CtdlMessage *CtdlMakeMessageLen(
223         struct ctdluser *author,        /* author's user structure */
224         char *recipient,                /* NULL if it's not mail */
225         long rcplen,
226         char *recp_cc,                  /* NULL if it's not mail */
227         long cclen,
228         char *room,                     /* room where it's going */
229         long roomlen,
230         int type,                       /* see MES_ types in header file */
231         int format_type,                /* variformat, plain text, MIME... */
232         char *fake_name,                /* who we're masquerading as */
233         long fnlen,
234         char *my_email,                 /* which of my email addresses to use (empty is ok) */
235         long myelen,
236         char *subject,                  /* Subject (optional) */
237         long subjlen,
238         char *supplied_euid,            /* ...or NULL if this is irrelevant */
239         long euidlen,
240         char *preformatted_text,        /* ...or NULL to read text from client */
241         long textlen,
242         char *references,               /* Thread references */
243         long reflen);
244
245 /* 
246  * loading messages async via an FD: 
247  * add IO->ReadMsg = NewAsyncMsg(...)
248  * and then call CtdlReadMessageBodyAsync() from your linreader handler.
249  */
250
251 ReadAsyncMsg *NewAsyncMsg(const char *terminator,       /* token signalling EOT */
252                           long tlen,
253                           size_t expectlen,             /* if we expect a message, how long should it be? */
254                           size_t maxlen,                /* maximum message length */
255                           StrBuf *exist,                /* if non-null, append to it;
256                                                            exist is ALWAYS freed  */
257                           long eLen,                    /* length of exist */
258                           int crlf                      /* CRLF newlines instead of LF */
259         );
260
261 eReadState CtdlReadMessageBodyAsync(AsyncIO *IO);
262 void DeleteAsyncMsg(ReadAsyncMsg **Msg);
263
264 extern int MessageDebugEnabled;
265
266 #define MSGDBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (MessageDebugEnabled != 0))
267 #define CCCID CCC->cs_pid
268 #define MSG_syslog(LEVEL, FORMAT, ...)                  \
269         MSGDBGLOG(LEVEL) syslog(LEVEL,                  \
270                                 "CC[%d]MSG " FORMAT,    \
271                                 CCCID, __VA_ARGS__)
272
273 #define MSGM_syslog(LEVEL, FORMAT)                      \
274         MSGDBGLOG(LEVEL) syslog(LEVEL,                  \
275                                 "CC[%d]MSG " FORMAT,    \
276                                 CCCID)
277
278
279
280 #endif /* MSGBASE_H */