Beispiel #1
0
Datei: editor.c Projekt: Kami/ckl
int ckl_editor_read_file(ckl_msg_t *m, const char *path)
{
  FILE *fp = fopen(path, "r");
  
  char *out = strdup("");
  
  if (!fp) {
    ckl_error_out("Unable to read editted file?");
    return -1;
  }
  
  char buf[8096];
  char *p = NULL;
  
  while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
    /* comment lines */
    if (p[0] == '#') {
      continue;
    }
    
    ckl_nuke_newlines(p);
    
    char *t = calloc(1, strlen(out) + strlen(p) + 1);
    strncpy(t, out, strlen(out));
    strncpy(t+strlen(out), p, strlen(p));
    free(out);
    out = t;
  }
  
  m->msg = out;
  
  return 0;
}
Beispiel #2
0
int main(int argc, char *const *argv)
{
  int c;
  int rv;
  char *node_id_file = getenv("CK_NODE_ID_FILE");
  ckl_conf_t *conf = calloc(1, sizeof(ckl_conf_t));

  curl_global_init(CURL_GLOBAL_ALL);

  if(node_id_file == NULL) {
    node_id_file = "/var/lib/cloudkick-agent/node_id";
  }

  while ((c = getopt(argc, argv, "f:")) != -1) {
    switch (c) {
      case 'f':
        node_id_file = optarg;
        break;
    }
  }

  rv = ckl_conf_init(conf);

  if (rv < 0) {
    ckl_error_out("conf_init failed");
  }

  FILE *fp = fopen(node_id_file, "r");
  if (fp == NULL) {
    ckl_error_out("couldn't open node_id file");
  }
  char node_id[128];
  char *p = NULL;
  p = fgets(node_id, sizeof(node_id), fp);

  fclose(fp);

  do_motd(conf, node_id);

  ckl_conf_free(conf);

  curl_global_cleanup();

  return rv;
}
Beispiel #3
0
Datei: ckl.c Projekt: Kami/ckl
static int do_detail(ckl_conf_t *conf, const char *slug)
{
  int rv;
  ckl_transport_t *transport = calloc(1, sizeof(ckl_transport_t));

  rv = ckl_transport_init(transport, conf);
  if (rv < 0) {
    ckl_error_out("transport_init failed.");
    return rv;
  }

  rv = ckl_transport_detail(transport, conf, slug);
  if (rv < 0) {
    ckl_error_out("ckl_transport_detail failed.");
    return rv;
  }

  ckl_transport_free(transport);

  return 0;
}
Beispiel #4
0
Datei: ckl.c Projekt: Kami/ckl
static int do_list(ckl_conf_t *conf)
{
  int rv;
  ckl_transport_t *transport = calloc(1, sizeof(ckl_transport_t));

  rv = ckl_transport_init(transport, conf);
  if (rv < 0) {
    ckl_error_out("transport_init failed.");
    return rv;
  }

  rv = ckl_transport_list(transport, conf, 5);
  if (rv < 0) {
    ckl_error_out("ckl_transport_list failed.");
    return rv;
  }

  ckl_transport_free(transport);

  return 0;
}
Beispiel #5
0
int ckl_script_init(ckl_script_t *s, ckl_conf_t *conf)
{
  int rv;

  rv = ckl_tmp_file(&s->path, &s->fd);
  if (rv < 0) {
    ckl_error_out("failed to create script temp file");
    return rv;
  }

  const char *sh = getenv("SHELL");

  if (!sh) {
    sh = "/bin/sh";
  }

  s->shell = strdup(sh);

  return 0;
}
Beispiel #6
0
Datei: ckl.c Projekt: Kami/ckl
static int do_send_msg(ckl_conf_t *conf, const char *usermsg)
{
  int rv;
  const char *editor;
  ckl_msg_t *msg = calloc(1, sizeof(ckl_msg_t));
  ckl_transport_t *transport = calloc(1, sizeof(ckl_transport_t));
  ckl_script_t *script = calloc(1, sizeof(ckl_script_t));

  rv = ckl_msg_init(msg);
  if (rv < 0) {
    ckl_error_out("msg_init failed.");
    return rv;
  }

  if (usermsg == NULL) {
    rv = ckl_editor_find(&editor);
    if (rv < 0) {
      fprintf(stderr, "Warning: no EDITOR found, using `vi`\n");
      editor = "vi";
    }

    char *path;
    FILE *fd;

    rv = ckl_tmp_file(&path, &fd);
    if (rv < 0) {
      ckl_error_out("Failed to setup tempfile for editting.");
      return rv;
    }

    rv = ckl_editor_fill_file(conf, msg, fd);
    if (rv < 0) {
      ckl_error_out("Failed to prefile file");
      return rv;
    }

    rv = ckl_editor_edit(editor, path);
    if (rv < 0) {
      ckl_error_out("editor broke?");
      return rv;
    }

    rv = ckl_editor_read_file(msg, path);
    if (rv < 0) {
      ckl_error_out("Failed to read editted file");
      return rv;
    }

    fclose(fd);
    unlink(path);
  }
  else {
    msg->msg = strdup(usermsg);
  }

  if (msg->msg == NULL) {
    ckl_error_out("no message specified");
  }

  if (conf->script_mode) {
    rv = ckl_script_init(script, conf);
    if (rv < 0) {
      ckl_error_out("script_init failed.");
      return rv;
    }

    rv = ckl_script_record(script, msg);
    if (rv < 0) {
      ckl_error_out("script_record failed.");
      return rv;
    }
  }

  rv = ckl_transport_init(transport, conf);
  if (rv < 0) {
    ckl_error_out("transport_init failed.");
    return rv;
  }

  rv = ckl_transport_msg_send(transport, conf, msg);
  if (rv < 0) {
    ckl_error_out("msg_send failed.");
    return rv;
  }

  ckl_transport_free(transport);
  ckl_msg_free(msg);
  ckl_script_free(script);

  return 0;
}
Beispiel #7
0
Datei: ckl.c Projekt: Kami/ckl
int main(int argc, char *const *argv)
{
  int mode = MODE_SEND_MSG;
  int c;
  int rv;
  const char *detail = NULL;
  const char *usermsg = NULL;
  ckl_conf_t *conf = calloc(1, sizeof(ckl_conf_t));

  curl_global_init(CURL_GLOBAL_ALL);

  while ((c = getopt(argc, argv, "hVslm:d:")) != -1) {
    switch (c) {
      case 'V':
        show_version();
        break;
      case 'h':
        show_help();
        break;
      case 'l':
        mode = MODE_LIST;
        break;
      case 'd':
        mode = MODE_DETAIL;
        detail = optarg;
        break;
      case 'm':
        usermsg = optarg;
        break;
      case 's':
        conf->script_mode = 1;
        break;
      case '?':
        ckl_error_out("See -h for correct options");
        break;
    }
  }

  rv = ckl_conf_init(conf);

  if (rv < 0) {
    ckl_error_out("conf_init failed");
  }

  switch (mode) {
    case MODE_SEND_MSG:
      rv = do_send_msg(conf, usermsg);
      break;
    case MODE_LIST:
      rv = do_list(conf);
      break;
    case MODE_DETAIL:
      rv = do_detail(conf, detail);
      break;
  }

  ckl_conf_free(conf);

  curl_global_cleanup();

  return rv;
}