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, v_parse_time) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'ext/postfix_status_line_core.c', line 338

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

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

  if (len < 1) {
    return Qnil;
  }

  bool mask = rb_value_to_bool(v_mask);
  bool parse_time = rb_value_to_bool(v_parse_time);

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

if (rb_value_to_bool(v_hash)) {
#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
  }

  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));

  if (parse_time) {
    put_epoch(tm, hash);
  }

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

  return hash;
}