* This module glues libSieve to the Citadel server in order to implement
* the Sieve mailbox filtering language (RFC 3028).
*
- * Copyright (c) 1987-2012 by the citadel.org team
+ * Copyright (c) 1987-2018 by the citadel.org team
*
* This program is open source software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
*/
int ctdl_debug(sieve2_context_t *s, void *my)
{
- syslog(LOG_DEBUG, "Sieve: %s", sieve2_getvalue_string(s, "message"));
+ syslog(LOG_DEBUG, "%s", sieve2_getvalue_string(s, "message"));
return SIEVE2_OK;
}
int ctdl_errparse(sieve2_context_t *s, void *my)
{
syslog(LOG_WARNING, "Error in script, line %d: %s",
- sieve2_getvalue_int(s, "lineno"),
- sieve2_getvalue_string(s, "message")
+ sieve2_getvalue_int(s, "lineno"),
+ sieve2_getvalue_string(s, "message")
);
return SIEVE2_OK;
}
int ctdl_errexec(sieve2_context_t *s, void *my)
{
syslog(LOG_WARNING, "Error executing script: %s",
- sieve2_getvalue_string(s, "message")
- );
+ sieve2_getvalue_string(s, "message")
+ );
return SIEVE2_OK;
}
{
struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
struct CtdlMessage *msg = NULL;
- struct recptypes *valid = NULL;
+ recptypes *valid = NULL;
char recp[256];
safestrncpy(recp, sieve2_getvalue_string(s, "address"), sizeof recp);
return SIEVE2_ERROR_BADARGS;
}
- msg = CtdlFetchMessage(cs->msgnum, 1);
+ msg = CtdlFetchMessage(cs->msgnum, 1, 1);
if (msg == NULL) {
syslog(LOG_WARNING, "REDIRECT failed: unable to fetch msg %ld", cs->msgnum);
free_recipients(valid);
CtdlSubmitMsg(msg, valid, NULL, 0);
cs->cancel_implicit_keep = 1;
free_recipients(valid);
- CtdlFreeMessage(msg);
+ CM_Free(msg);
return SIEVE2_OK;
}
}
/* Yes, we actually have to go there */
- CtdlUserGoto(NULL, 0, 0, NULL, NULL);
+ CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL, 0);
/* Go back to the room we came from */
if (strcasecmp(original_room_name, CC->room.QRname)) {
- CtdlUserGoto(original_room_name, 0, 0, NULL, NULL);
+ CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
}
if (c == 0) {
}
-
/*
* Callback function to indicate that a message should be rejected
*/
}
-
/*
* Callback function to indicate that a vacation message should be generated
*/
}
+#if 0
/*
* Callback function to parse addresses per local system convention
* It is disabled because we don't support subaddresses.
*/
-#if 0
int ctdl_getsubaddress(sieve2_context_t *s, void *my)
{
struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
}
+#if 0
/*
* Callback function to fetch message body
* (Uncomment the code if we implement this extension)
*
+ */
int ctdl_getbody(sieve2_context_t *s, void *my)
{
return SIEVE2_ERROR_UNSUPPORTED;
}
- *
- */
+#endif
/*
/*
- * Return a pointer to the active Sieve script
+ * Return a pointer to the active Sieve script.
+ * (Caller does NOT own the memory and should not free the returned pointer.)
*/
-char *get_active_script(struct ctdl_sieve *cs) {
+char *get_active_script(struct sdm_userdata *u) {
struct sdm_script *sptr;
- for (sptr=cs->u->first_script; sptr!=NULL; sptr=sptr->next) {
+ for (sptr=u->first_script; sptr!=NULL; sptr=sptr->next) {
if (sptr->script_active > 0) {
syslog(LOG_DEBUG, "get_active_script() is using script '%s'", sptr->script_name);
return(sptr->script_content);
int ctdl_getscript(sieve2_context_t *s, void *my) {
struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
- char *active_script = get_active_script(cs);
+ char *active_script = get_active_script(cs->u);
if (active_script != NULL) {
sieve2_setvalue_string(s, "script", active_script);
return SIEVE2_OK;
return SIEVE2_ERROR_GETSCRIPT;
}
+
/*
* Callback function to retrieve message headers
*/
}
-
/*
* Add a room to the list of those rooms which potentially require sieve processing
*/
}
-
/*
* Perform sieve processing for one message (called by sieve_do_room() for each message)
*/
size_t headers_len = 0;
int len = 0;
- if (u == NULL)
- {
- syslog(LOG_EMERG, "Can't process message <%ld> without userdata!", msgnum);
+ if (u == NULL) {
+ syslog(LOG_ERR, "Can't process message <%ld> without userdata!", msgnum);
return;
}
/*
* Make sure you include message body so you can get those second-level headers ;)
*/
- msg = CtdlFetchMessage(msgnum, 1);
+ msg = CtdlFetchMessage(msgnum, 1, 1);
if (msg == NULL) return;
/*
my.u = u; /* Hand off a pointer to the rest of this info */
/* Keep track of the recipient so we can do handling based on it later */
- process_rfc822_addr(msg->cm_fields['R'], my.recp_user, my.recp_node, my.recp_name);
+ process_rfc822_addr(msg->cm_fields[eRecipient], my.recp_user, my.recp_node, my.recp_name);
/* Keep track of the sender so we can use it for REJECT and VACATION responses */
- if (msg->cm_fields['F'] != NULL) {
- safestrncpy(my.sender, msg->cm_fields['F'], sizeof my.sender);
- }
- else if ( (msg->cm_fields['A'] != NULL) && (msg->cm_fields['N'] != NULL) ) {
- snprintf(my.sender, sizeof my.sender, "%s@%s", msg->cm_fields['A'], msg->cm_fields['N']);
+ if (!CM_IsEmpty(msg, erFc822Addr)) {
+ safestrncpy(my.sender, msg->cm_fields[erFc822Addr], sizeof my.sender);
}
- else if (msg->cm_fields['A'] != NULL) {
- safestrncpy(my.sender, msg->cm_fields['A'], sizeof my.sender);
+ else if (!CM_IsEmpty(msg, eAuthor)) {
+ safestrncpy(my.sender, msg->cm_fields[eAuthor], sizeof my.sender);
}
else {
strcpy(my.sender, "");
}
/* Keep track of the subject so we can use it for VACATION responses */
- if (msg->cm_fields['U'] != NULL) {
- safestrncpy(my.subject, msg->cm_fields['U'], sizeof my.subject);
+ if (!CM_IsEmpty(msg, eMsgSubject)) {
+ safestrncpy(my.subject, msg->cm_fields[eMsgSubject], sizeof my.subject);
}
else {
strcpy(my.subject, "");
}
/* Keep track of the envelope-from address (use body-from if not found) */
- if (msg->cm_fields['P'] != NULL) {
- safestrncpy(my.envelope_from, msg->cm_fields['P'], sizeof my.envelope_from);
+ if (!CM_IsEmpty(msg, eMessagePath)) {
+ safestrncpy(my.envelope_from, msg->cm_fields[eMessagePath], sizeof my.envelope_from);
stripallbut(my.envelope_from, '<', '>');
}
- else if (msg->cm_fields['F'] != NULL) {
- safestrncpy(my.envelope_from, msg->cm_fields['F'], sizeof my.envelope_from);
+ else if (!CM_IsEmpty(msg, erFc822Addr)) {
+ safestrncpy(my.envelope_from, msg->cm_fields[erFc822Addr], sizeof my.envelope_from);
stripallbut(my.envelope_from, '<', '>');
}
else {
}
if (haschar(my.envelope_from, '@') == 0) {
strcat(my.envelope_from, "@");
- strcat(my.envelope_from, config.c_fqdn);
+ strcat(my.envelope_from, CtdlGetConfigStr("c_fqdn"));
}
/* Keep track of the envelope-to address (use body-to if not found) */
- if (msg->cm_fields['V'] != NULL) {
- safestrncpy(my.envelope_to, msg->cm_fields['V'], sizeof my.envelope_to);
+ if (!CM_IsEmpty(msg, eenVelopeTo)) {
+ safestrncpy(my.envelope_to, msg->cm_fields[eenVelopeTo], sizeof my.envelope_to);
stripallbut(my.envelope_to, '<', '>');
}
- else if (msg->cm_fields['R'] != NULL) {
- safestrncpy(my.envelope_to, msg->cm_fields['R'], sizeof my.envelope_to);
- if (msg->cm_fields['D'] != NULL) {
+ else if (!CM_IsEmpty(msg, eRecipient)) {
+ safestrncpy(my.envelope_to, msg->cm_fields[eRecipient], sizeof my.envelope_to);
+ if (!CM_IsEmpty(msg, eDestination)) {
strcat(my.envelope_to, "@");
- strcat(my.envelope_to, msg->cm_fields['D']);
+ strcat(my.envelope_to, msg->cm_fields[eDestination]);
}
stripallbut(my.envelope_to, '<', '>');
}
}
if (haschar(my.envelope_to, '@') == 0) {
strcat(my.envelope_to, "@");
- strcat(my.envelope_to, config.c_fqdn);
+ strcat(my.envelope_to, CtdlGetConfigStr("c_fqdn"));
}
- CtdlFreeMessage(msg);
+ CM_Free(msg);
syslog(LOG_DEBUG, "Calling sieve2_execute()");
res = sieve2_execute(sieve2_context, &my);
if (res != SIEVE2_OK) {
- syslog(LOG_CRIT, "sieve2_execute() returned %d: %s", res, sieve2_errstr(res));
+ syslog(LOG_ERR, "sieve2_execute() returned %d: %s", res, sieve2_errstr(res));
}
free(my.rfc822headers);
struct sdm_userdata *u = (struct sdm_userdata *) userdata;
struct CtdlMessage *msg;
char *conf;
+ long conflen;
u->config_msgnum = msgnum;
- msg = CtdlFetchMessage(msgnum, 1);
+ msg = CtdlFetchMessage(msgnum, 1, 1);
if (msg == NULL) {
u->config_msgnum = (-1) ;
return;
}
- conf = msg->cm_fields['M'];
- msg->cm_fields['M'] = NULL;
- CtdlFreeMessage(msg);
+ CM_GetAsField(msg, eMesageText, &conf, &conflen);
+
+ CM_Free(msg);
if (conf != NULL) {
parse_sieve_config(conf, u);
return;
}
- /* FIXME this is where we have to check for empty scripts */
+ /*
+ * Check to see whether the script is empty and should not be processed.
+ * A script is considered non-empty if it contains at least one semicolon.
+ */
+ if (
+ (get_active_script(&u) == NULL)
+ || (strchr(get_active_script(&u), ';') == NULL)
+ ) {
+ syslog(LOG_DEBUG, "Sieve script is empty. No processing is required.");
+ return;
+ }
syslog(LOG_DEBUG, "Rules found. Performing Sieve processing for <%s>", roomname);
if (CtdlGetRoom(&CC->room, roomname) != 0) {
- syslog(LOG_CRIT, "ERROR: cannot load <%s>", roomname);
+ syslog(LOG_ERR, "ERROR: cannot load <%s>", roomname);
return;
}
res = sieve2_alloc(&sieve2_context);
if (res != SIEVE2_OK) {
- syslog(LOG_CRIT, "sieve2_alloc() returned %d: %s", res, sieve2_errstr(res));
+ syslog(LOG_ERR, "sieve2_alloc() returned %d: %s", res, sieve2_errstr(res));
return;
}
res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
if (res != SIEVE2_OK) {
- syslog(LOG_CRIT, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
+ syslog(LOG_ERR, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
goto BAIL;
}
my.u = &u;
res = sieve2_validate(sieve2_context, &my);
if (res != SIEVE2_OK) {
- syslog(LOG_CRIT, "sieve2_validate() returned %d: %s", res, sieve2_errstr(res));
+ syslog(LOG_ERR, "sieve2_validate() returned %d: %s", res, sieve2_errstr(res));
goto BAIL;
}
BAIL:
res = sieve2_free(&sieve2_context);
if (res != SIEVE2_OK) {
- syslog(LOG_CRIT, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
+ syslog(LOG_ERR, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
}
/* Rewrite the config if we have to */
extract_token(script_name, argbuf, 1, '|', sizeof script_name);
if (!IsEmptyStr(script_name)) {
cprintf("%d Transmit script now\n", SEND_LISTING);
- script_content = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
+ script_content = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
msiv_putscript(&u, script_name, script_content);
changes_made = 1;
}
*/
res = sieve2_alloc(&sieve2_context);
if (res != SIEVE2_OK) {
- syslog(LOG_CRIT, "sieve2_alloc() returned %d: %s", res, sieve2_errstr(res));
+ syslog(LOG_ERR, "sieve2_alloc() returned %d: %s", res, sieve2_errstr(res));
return;
}
res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
if (res != SIEVE2_OK) {
- syslog(LOG_CRIT, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
+ syslog(LOG_ERR, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
goto BAIL;
}
BAIL: res = sieve2_free(&sieve2_context);
if (res != SIEVE2_OK) {
- syslog(LOG_CRIT, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
+ syslog(LOG_ERR, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
}
}
+
void cleanup_sieve(void)
{
struct RoomProcList *ptr, *ptr2;
end_critical_section(S_SIEVELIST);
}
+
int serv_sieve_room(struct ctdlroom *room)
{
if (!strcasecmp(&room->QRname[11], MAILROOM)) {
return 0;
}
+
CTDL_MODULE_INIT(sieve)
{
if (!threading)
{
-
ctdl_sieve_init();
CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
CtdlRegisterRoomHook(serv_sieve_room);
- CtdlRegisterSessionHook(perform_sieve_processing, EVT_HOUSE);
+ CtdlRegisterSessionHook(perform_sieve_processing, EVT_HOUSE, PRIO_HOUSE + 10);
CtdlRegisterCleanupHook(cleanup_sieve);
}
/* return our module name for the log */
return "sieve";
}
-