Module: PostfixStatusLine::Core

Defined in:
ext/postfix_status_line_core.c

Class Method Summary collapse

Class Method Details

.parse(v_str, v_mask, v_hash, v_salt) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'ext/postfix_status_line_core.c', line 286

static VALUE rb_postfix_status_line_parse(VALUE self, VALUE v_str, VALUE v_mask, VALUE v_hash, VALUE v_salt) {
  Check_Type(v_str, T_STRING);

  char *str = RSTRING_PTR(v_str);
  size_t len = RSTRING_LEN(v_str);
  bool mask = true;

  bool include_hash = false;
  char *salt = NULL;
  size_t salt_len = -1;

  switch (TYPE(v_hash)) {
  case T_FALSE:
  case T_NIL:
    break;
  default:
#ifdef HAVE_OPENSSL_SHA_H
    include_hash = true;

    if (!NIL_P(v_salt)) {
      Check_Type(v_salt, T_STRING);
      salt = RSTRING_PTR(v_salt);
      salt_len = RSTRING_LEN(v_salt);
    }
#else
    rb_raise(rb_eArgumentError, "OpenSSL is not linked");
#endif // HAVE_OPENSSL_SHA_H
  }

  switch (TYPE(v_mask)) {
  case T_FALSE:
  case T_NIL:
    mask = false;
  }

  if (len < 1) {
    return Qnil;
  }

  char buf[len + 1];
  strncpy(buf, str, len);
  buf[len] = '\0';

  char *tm, *hostname, *process, *queue_id, *attrs;

  if (!split_line1(buf, &tm, &hostname, &process, &queue_id, &attrs)) {
    return Qnil;
  }

  VALUE hash = rb_hash_new();
  rb_hash_aset(hash, rb_str_new2("time"), rb_str_new2(tm));
  rb_hash_aset(hash, rb_str_new2("hostname"), rb_str_new2(hostname));
  rb_hash_aset(hash, rb_str_new2("process"), rb_str_new2(process));
  rb_hash_aset(hash, rb_str_new2("queue_id"), rb_str_new2(queue_id));

  split_line2(attrs, mask, hash, include_hash, salt, salt_len);

  return hash;
}