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