commit - d2e4f1e938dd709aa0969b8f334c91dd2f1d24ef
commit + f644422d21ee0332b215816504feb72255f748e3
blob - f4b5063722029cc5a154459dd9e915a680617558
blob + dd3b8d43fddb4bb78fefe6bf760c31fe4a44e3f5
--- gotd/libexec/got-notify-email/got-notify-email.c
+++ gotd/libexec/got-notify-email/got-notify-email.c
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
-#include <sha1.h>
-#include <sha2.h>
+#include <err.h>
+#include <limits.h>
+#include <pwd.h>
#include <unistd.h>
-#include "got_error.h"
-#include "got_repository.h"
-#include "got_object.h"
-#include "got_reference.h"
-
-
__dead static void
usage(void)
{
static char *
set_default_fromaddr(void)
{
- return NULL;
+ struct passwd *pw = NULL;
+ char *s;
+ char hostname[255];
+
+ pw = getpwuid(getuid());
+ if (pw == NULL) {
+ errx(1, "my UID %d was not found in password database",
+ getuid());
+ }
+
+ if (gethostname(hostname, sizeof(hostname)) == -1)
+ err(1, "hostname");
+
+ if (asprintf(&s, "%s@%s", pw->pw_name, hostname) == -1)
+ err(1, "asprintf");
+
+ return s;
}
-static const struct got_error *
-send_notifications(struct got_repository *repo, const char *fromaddr,
- const char *recipient, char *replytoaddr, int shortlog,
- int with_diff)
+static void
+send_email(const char *fromaddr, const char *recipient,
+ const char *replytoaddr)
{
- const struct got_error *err;
- char *line = NULL, *space = NULL, *refname = NULL;
+ char *line = NULL;
size_t linesize = 0;
ssize_t linelen;
- struct got_object_id *id = NULL;
- struct got_object_id_queue ids;
- struct got_object_qid *qid = NULL;
- STAILQ_INIT(&ids);
-
while ((linelen = getline(&line, &linesize, stdin)) != -1) {
- while (linelen > 0 && line[linelen - 1] == '\n') {
- line[linelen - 1] = '\0';
- linelen--;
- }
-
- space = strchr(line, ' ');
- if (space == NULL) {
- err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
- "no separator found between commit ID and "
- "reference name: %s", line);
- goto done;
- }
-
- *space = '\0';
-
- refname = strdup(space + 1);
- if (refname == NULL) {
- err = got_error_from_errno("strdup");
- goto done;
- }
- if (!got_ref_name_is_valid(refname)) {
- err = got_error_fmt(GOT_ERR_BAD_REF_NAME, "%s",
- refname);
- goto done;
- }
-
- err = got_object_resolve_id_str(&id, repo, line);
- if (err)
- goto done;
-
- err = got_object_qid_alloc(&qid, id);
- if (err)
- goto done;
-
- qid->data = refname;
- refname = NULL;
-
- STAILQ_INSERT_TAIL(&ids, qid, entry);
-
- free(id);
- id = NULL;
}
- if (STAILQ_EMPTY(&ids)) {
- err = got_error_fmt(GOT_ERR_EOF,
- "reading 'commit reference-name' pairs from stdin");
- goto done;
- }
-
- STAILQ_FOREACH(qid, &ids, entry) {
- char *idstr;
- got_object_id_str(&idstr, &qid->id);
- refname = qid->data;
- printf("%s: commit=%s refname=%s\n", __func__, idstr, refname);
- }
-done:
free(line);
- free(id);
- free(refname);
- if (qid)
- got_object_qid_free(qid);
- return err;
}
int
main(int argc, char *argv[])
{
- const struct got_error *error = NULL;
- struct got_repository *repo = NULL;
- int ch, *pack_fds = NULL;
- char *cwd = NULL, *repo_path = NULL, *fromaddr = NULL;
- char *recipient = NULL, *replytoaddr = NULL;
- int shortlog = 0, with_diff = 0;
+ char *fromaddr = NULL;
+ const char *recipient = NULL, *replytoaddr = NULL;
+ int ch;
- while ((ch = getopt(argc, argv, "df:r:R:s")) != -1) {
+ while ((ch = getopt(argc, argv, "f:r:")) != -1) {
switch (ch) {
- case 'd':
- with_diff = 1;
- break;
case 'f':
- fromaddr = optarg;
+ fromaddr = strdup(optarg);
+ if (fromaddr == NULL)
+ err(1, "strdup");
break;
case 'r':
- repo_path = optarg;
- break;
- case 'R':
replytoaddr = optarg;
break;
- case 's':
- shortlog = 1;
- break;
}
}
if (argc != 1)
usage();
-
+#ifndef PROFILE
+ if (pledge("stdio dns inet pwd", NULL) == -1)
+ err(1, "pledge");
+#endif
recipient = argv[0];
if (!validate_email_addr(recipient))
goto done;
if (fromaddr == NULL)
fromaddr = set_default_fromaddr();
- if (repo_path == NULL) {
- cwd = getcwd(NULL, 0);
- if (cwd == NULL)
- error = got_error_from_errno("getcwd");
- repo_path = cwd;
- }
- error = got_repo_pack_fds_open(&pack_fds);
- if (error != NULL)
- goto done;
-
- error = got_repo_open(&repo, repo_path, NULL, pack_fds);
- if (error)
- goto done;
-
- error = send_notifications(repo, fromaddr, recipient, replytoaddr,
- shortlog, with_diff);
+#ifndef PROFILE
+ if (pledge("stdio dns inet", NULL) == -1)
+ err(1, "pledge");
+#endif
+ send_email(fromaddr, recipient, replytoaddr);
done:
- if (pack_fds) {
- const struct got_error *pack_err =
- got_repo_pack_fds_close(pack_fds);
- if (error == NULL)
- error = pack_err;
- }
- if (repo) {
- const struct got_error *close_err = got_repo_close(repo);
- if (error == NULL)
- error = close_err;
- }
- free(cwd);
- if (error) {
- fflush(stdout);
- fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
- return 1;
- }
+ free(fromaddr);
return 0;
}