OSSP CVS Repository

ossp - Difference in ossp-pkg/lmtp2nntp/lmtp2nntp_msg.c versions 1.14 and 1.15
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/lmtp2nntp/lmtp2nntp_msg.c 1.14 -> 1.15

--- lmtp2nntp_msg.c      2002/05/23 09:37:15     1.14
+++ lmtp2nntp_msg.c      2002/05/27 15:09:50     1.15
@@ -31,9 +31,6 @@
 #include "lmtp2nntp_argz.h"
 #include "fixme.h" //FIMXE logbook only
 #include "tai.h"
-#include <sys/utsname.h> //FIXME createmessageid() hack
-
-
 #include "str.h"
 
 /* third party */
@@ -46,10 +43,14 @@
 #include "dmalloc.h"
 #endif
 
-msg_t *msg_create(void)
+msg_t *msg_create(val_t *prival)
 {
+    ex_t ex;
     msg_t *msg;
 
+    if (prival == NULL)
+        return NULL;
+
     if ((msg = (msg_t *)malloc(sizeof(msg_t))) == NULL)
         return NULL;
 
@@ -68,6 +69,15 @@
     msg->azNewsgroups = NULL;
     msg->asNewsgroups = 0;
     msg->l2 = NULL; /* this is a copy only */
+    msg->prival = prival;
+
+    /* create printable variables context and mount it into the private application context */
+    try {
+        val_reg(msg->prival, "msg", VAL_TYPE_PTR, "msg context structure", NULL);
+        val_set(msg->prival, "msg", msg);
+        }
+    catch (ex)
+        rethrow;
 
     return msg;
 }
@@ -96,6 +106,8 @@
     if (msg->azNewsgroups != NULL)
         free(msg->azNewsgroups);
     msg->l2 = NULL; /* this is a copy only, the "parent" needs to clean this up */
+    if (msg->prival != NULL)
+        val_unreg(msg->prival, "msg");
 
     free(msg);
     return;
@@ -559,6 +571,7 @@
 }
 
 static var_rc_t canonifydate(
+    val_t *prival,
     const char  *cpArg,  size_t nArg,
     const char  *cpVal,  size_t nVal,
           char **cppOut, size_t *pnOut, size_t *pnOutsize)
@@ -680,28 +693,61 @@
 }
 
 static var_rc_t createmessageid(
+    val_t *prival,
     const char  *cpArg,  size_t nArg,
     const char  *cpVal,  size_t nVal,
           char **cppOut, size_t *pnOut, size_t *pnOutsize)
 {
     char *cp;
-    static int mcounter = 0; //FIXME no statics in this program!
+    int msgcount;
+    msg_t *msg;
+    char *foreignid;
+    char *nodename;
+    time_t cachetime;
+    pid_t cachepid;
+    int size;
+    int maxsize;
+    char *format;
+
+    if (val_get(prival, "msgcount", &msgcount) != VAL_OK)
+        msgcount = 0;
 
-    struct utsname name; //FIXME this must be taken from global - requires context
-    if (uname(&name) == -1) {
-        memcpy(name.nodename, "localhost.invalid", strlen("localhost.invalid") + 1);
+    if (val_get(prival, "msg", &msg) != VAL_OK)
+        msg = NULL;
+
+    if (msg != NULL && msg->cpFid != NULL)
+        foreignid = msg->cpFid;
+    else
+        foreignid = "noFid";
+
+    if (val_get(prival, "nodename", &nodename) != VAL_OK)
+        nodename = "localhost.invalid";
+
+    cachetime = time(NULL);
+    cachepid = getpid();
+
+    /* idea and first implementation as a contribution to lmtp2nntp v1.0.0 by Christos Ricudis <ricudis@paiko.gr> */
+
+    maxsize = WRAPAT - strlen("Message-ID: ");
+    format = "<%0.11d$%0.6d$%0.4d$%.20s@%s>";
+    size = str_format(NULL, -1, format, cachetime, cachepid, msgcount, foreignid, nodename);
+    if (size >= 38) {
+        if (size > maxsize)
+            size = maxsize;
+        cp = (char *)mallocex(size + 1);
+        size = str_format(cp, size + 1, format, cachetime, cachepid, msgcount, foreignid, nodename);
+        if (size > maxsize)
+            cp[size-2] = '>';
     }
+    else
+        cp = strdupex("<>"); /* if format fails, do not end the wholly program abnormally */
 
-    /* idea and first implementation as a contribution to lmtp2nntp v1.0.0
-       by Christos Ricudis <ricudis@paiko.gr> */
+    if (msg != NULL) {
+        if (msg->cpMsgid != NULL)
+            free(msg->cpMsgid);
+        msg->cpMsgid = strdupex(cp);
+    }
 
-    cp = (char *)malloc( 100); //FIXME how much exacly?
-    str_format(cp, 100, "<%0.11d$%0.6d$%0.2d$%.20s$@%.40s>",
-            time(NULL), getpid(), mcounter++, "noFid" /*FIXME ((msg->cpFid == NULL)?"noFid":(msg->cpFid))*/, name.nodename);
-    /*if (msg->cpMsgid != NULL)
-        free(msg->cpMsgid);
-    msg->cpMsgid = cp; //FIXME what about aligning lib_val here?
-    */
     *cppOut    = strdupex(cp);
     *pnOutsize = strlen(cp) + 1;
     *pnOut     = strlen(cp);
@@ -709,32 +755,26 @@
 }
 
 static var_rc_t operate_cb(
-    var_t *var, void *ctx,
+    var_t *var, void *_prival,
     const char  *op_ptr, size_t op_len,
     const char  *arg_ptr, size_t arg_len,
     const char  *val_ptr, size_t val_len,
     char **out_ptr, size_t *out_len, size_t *out_size)
 {
+    val_t *prival = _prival;
     int i;
 
     if (val_ptr == NULL) {
-        *out_ptr = "";
+        *out_ptr = ""; //FIXME will lib_val try to free() this?
         *out_len = 0;
         *out_size = 0;
         return VAR_OK;
     }
-    if (op_len == 6 && strncmp(op_ptr, "return", 6) == 0) { //FIXME needless block
-        *out_ptr = malloc(arg_len);
-        *out_len = arg_len;
-        *out_size = arg_len;
-        memcpy(*out_ptr, arg_ptr, arg_len);
-        return VAR_OK;
-    }
     else if (op_len == 15 && strncmp(op_ptr, "createmessageid", 15) == 0) {
-        return createmessageid(arg_ptr, arg_len, val_ptr, val_len, out_ptr, out_len, out_size);
+        return createmessageid(prival, arg_ptr, arg_len, val_ptr, val_len, out_ptr, out_len, out_size);
     }
     else if (op_len == 12 && strncmp(op_ptr, "canonifydate", 12) == 0) {
-        return canonifydate(arg_ptr, arg_len, val_ptr, val_len, out_ptr, out_len, out_size);
+        return canonifydate   (prival, arg_ptr, arg_len, val_ptr, val_len, out_ptr, out_len, out_size);
     }
     else 
         return VAR_ERR_UNDEFINED_OPERATION;
@@ -882,7 +922,7 @@
         logbook(ctx->l2, L2_LEVEL_ERROR, "configure regex callback failed with %s (%d)", var_strerror(ctx->config_varregex, rc, &cp) == VAR_OK ? cp : "Unknown Error", rc);
         throw(0,0,0);
     }
-    if ((rc = var_config(ctx->config_varctx, VAR_CONFIG_CB_OPERATION, operate_cb, NULL)) != VAR_OK) { //FIXME isn't main a better place to do this?
+    if ((rc = var_config(ctx->config_varctx, VAR_CONFIG_CB_OPERATION, operate_cb, ctx->prival)) != VAR_OK) { //FIXME isn't main a better place to do this?
         logbook(ctx->l2, L2_LEVEL_ERROR, "configure operate callback failed with %s (%d)", var_strerror(ctx->config_varctx, rc, &cp) == VAR_OK ? cp : "Unknown Error", rc);
         throw(0,0,0);
     }

CVSTrac 2.0.1