Add aide message functionality that uses the UT table to protect the user from an...
authorWilfried Goesgens <dothebart@citadel.org>
Mon, 6 Aug 2012 21:31:59 +0000 (23:31 +0200)
committerWilfried Goesgens <dothebart@citadel.org>
Mon, 6 Aug 2012 21:31:59 +0000 (23:31 +0200)
citadel/include/ctdl_module.h
citadel/msgbase.c
citadel/msgbase.h

index 9b7f8e2f96ae460f671a8cddfb8c39561987abfb..0287daa7be280d459bbc76e9d7704ee0a5ab81d8 100644 (file)
 
 #define CTDL_UPGRADE_CALL(module_name) ctdl_module_##module_name##_upgrade ()
 
 
 #define CTDL_UPGRADE_CALL(module_name) ctdl_module_##module_name##_upgrade ()
 
-#define CtdlAideMessage(TEXT, SUBJECT) quickie_message("Citadel",NULL,NULL,AIDEROOM,TEXT,FMT_CITADEL,SUBJECT) 
+#define CtdlAideMessage(TEXT, SUBJECT)         \
+       quickie_message(                        \
+               "Citadel",                      \
+               NULL,                           \
+               NULL,                           \
+               AIDEROOM,                       \
+               TEXT,                           \
+               FMT_CITADEL,                    \
+               SUBJECT) 
+
+
+#define CtdlAideFPMessage(TEXT, SUBJECT, N, STR, STRLEN) \
+       flood_protect_quickie_message(                   \
+               "Citadel",                               \
+               NULL,                                    \
+               NULL,                                    \
+               AIDEROOM,                                \
+               TEXT,                                    \
+               FMT_CITADEL,                             \
+               SUBJECT,                                 \
+               N,                                       \
+               STR,                                     \
+               STRLEN)
 /*
  * Hook functions available to modules.
  */
 /*
  * Hook functions available to modules.
  */
index ce52cc11365267a1cdd84efe402972e6f288bb65..568cefb2e06f6c57175510351cbf1b05acf5b049 100644 (file)
@@ -38,6 +38,9 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <regex.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <regex.h>
+
+#include "md5.h"
+
 #include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
 #include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
@@ -3539,6 +3542,73 @@ void quickie_message(const char *from,
        if (recp != NULL) free_recipients(recp);
 }
 
        if (recp != NULL) free_recipients(recp);
 }
 
+void flood_protect_quickie_message(const char *from,
+                                  const char *fromaddr,
+                                  char *to,
+                                  char *room,
+                                  const char *text, 
+                                  int format_type,
+                                  const char *subject,
+                                  int nCriterions,
+                                  const char **CritStr,
+                                  long *CritStrLen)
+{
+       int i;
+       struct UseTable ut;
+       u_char rawdigest[MD5_DIGEST_LEN];
+       struct MD5Context md5context;
+       StrBuf *guid;
+       struct cdbdata *cdbut;
+       char timestamp[64];
+       long tslen;
+       time_t ts = time(NULL);
+       time_t tsday = ts / (8*60*60); /* just care for a day... */
+
+       tslen = snprintf(timestamp, sizeof(timestamp), "%ld", tsday);
+       MD5Init(&md5context);
+
+       for (i = 0; i < nCriterions; i++)
+               MD5Update(&md5context,
+                         (const unsigned char*)CritStr[i], CritStrLen[i]);
+       MD5Update(&md5context,
+                 (const unsigned char*)timestamp, tslen);
+       MD5Final(rawdigest, &md5context);
+
+       guid = NewStrBufPlain(NULL,
+                             MD5_DIGEST_LEN * 2 + 12);
+       StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
+       StrBufAppendBufPlain(guid, HKEY("_fldpt"), 0);
+       if (StrLength(guid) > 40)
+               StrBufCutAt(guid, 40, NULL);
+       /* Find out if we've already sent a similar message */
+       memcpy(ut.ut_msgid, SKEY(guid));
+       ut.ut_timestamp = ts;
+
+       cdbut = cdb_fetch(CDB_USETABLE, SKEY(guid));
+
+       if (cdbut != NULL) {
+               /* yes, we did. flood protection kicks in. */
+               syslog(LOG_DEBUG,
+                      "not sending message again\n");
+               cdb_free(cdbut);
+       }
+
+       /* rewrite the record anyway, to update the timestamp */
+       cdb_store(CDB_USETABLE,
+                 SKEY(guid),
+                 &ut, sizeof(struct UseTable) );
+
+       if (cdbut != NULL) return;
+       /* no, this message isn't sent recently; go ahead. */
+       quickie_message(from,
+                       fromaddr,
+                       to,
+                       room,
+                       text, 
+                       format_type,
+                       subject);
+}
+
 
 /*
  * Back end function used by CtdlMakeMessage() and similar functions
 
 /*
  * Back end function used by CtdlMakeMessage() and similar functions
index 68894b52caf73b805fc210475ea52cdf6d462142..3bf009e58bd851c5147aa5622649fdba48070ff4 100644 (file)
@@ -111,7 +111,26 @@ void cmd_dlat (char *cmdbuf);
 long send_message (struct CtdlMessage *);
 void loadtroom (void);
 long CtdlSubmitMsg(struct CtdlMessage *, struct recptypes *, const char *, int);
 long send_message (struct CtdlMessage *);
 void loadtroom (void);
 long CtdlSubmitMsg(struct CtdlMessage *, struct recptypes *, const char *, int);
-void quickie_message (const char *, const char *, char *, char *, const char *, int, const char *);
+
+void quickie_message(const char *from,
+                    const char *fromaddr,
+                    char *to,
+                    char *room,
+                    const char *text, 
+                    int format_type,
+                    const char *subject);
+
+void flood_protect_quickie_message(const char *from,
+                                  const char *fromaddr,
+                                  char *to,
+                                  char *room,
+                                  const char *text, 
+                                  int format_type,
+                                  const char *subject,
+                                  int nCriterions,
+                                  const char **CritStr,
+                                  long *CritStrLen);
+
 void cmd_ent0 (char *entargs);
 void cmd_dele (char *delstr);
 void cmd_move (char *args);
 void cmd_ent0 (char *entargs);
 void cmd_dele (char *delstr);
 void cmd_move (char *args);