2
0

main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * lws-api-test-smtp_client
  3. *
  4. * Written in 2010-2019 by Andy Green <[email protected]>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. */
  9. #include <libwebsockets.h>
  10. #include <signal.h>
  11. static int interrupted, result = 1;
  12. static const char *recip;
  13. static void
  14. sigint_handler(int sig)
  15. {
  16. interrupted = 1;
  17. }
  18. static int
  19. done_cb(struct lws_smtp_email *email, void *buf, size_t len)
  20. {
  21. /* you could examine email->data here */
  22. if (buf) {
  23. char dotstar[96];
  24. lws_strnncpy(dotstar, (const char *)buf, len, sizeof(dotstar));
  25. lwsl_notice("%s: %s\n", __func__, dotstar);
  26. } else
  27. lwsl_notice("%s:\n", __func__);
  28. /* destroy any allocations in email */
  29. free((char *)email->payload);
  30. result = 0;
  31. interrupted = 1;
  32. return 0;
  33. }
  34. int main(int argc, const char **argv)
  35. {
  36. int n = 1, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
  37. struct lws_context_creation_info info;
  38. lws_smtp_sequencer_args_t ss_args;
  39. struct lws_context *context;
  40. lws_smtp_sequencer_t *sseq;
  41. lws_smtp_email_t *email;
  42. struct lws_vhost *vh;
  43. char payload[2048];
  44. const char *p;
  45. /* the normal lws init */
  46. signal(SIGINT, sigint_handler);
  47. if ((p = lws_cmdline_option(argc, argv, "-d")))
  48. logs = atoi(p);
  49. p = lws_cmdline_option(argc, argv, "-r");
  50. if (!p) {
  51. lwsl_err("-r <recipient email> is required\n");
  52. return 1;
  53. }
  54. recip = p;
  55. lws_set_log_level(logs, NULL);
  56. lwsl_user("LWS API selftest: SMTP client\n");
  57. memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
  58. info.port = CONTEXT_PORT_NO_LISTEN;
  59. info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
  60. context = lws_create_context(&info);
  61. if (!context) {
  62. lwsl_err("lws init failed\n");
  63. return 1;
  64. }
  65. vh = lws_create_vhost(context, &info);
  66. if (!vh) {
  67. lwsl_err("Failed to create first vhost\n");
  68. goto bail1;
  69. }
  70. memset(&ss_args, 0, sizeof(ss_args));
  71. ss_args.helo = "lws-abs-smtp-test";
  72. ss_args.vhost = vh;
  73. sseq = lws_smtp_sequencer_create(&ss_args);
  74. if (!sseq) {
  75. lwsl_err("%s: smtp sequencer create failed\n", __func__);
  76. goto bail1;
  77. }
  78. /* attach an email to it */
  79. n = lws_snprintf(payload, sizeof(payload),
  80. "From: [email protected]\n"
  81. "To: %s\n"
  82. "Subject: Test email for lws smtp-client\n"
  83. "\n"
  84. "Hello this was an api test for lws smtp-client\n"
  85. "\r\n.\r\n", recip);
  86. if (lws_smtpc_add_email(sseq, payload, n, "testserver",
  87. "[email protected]", recip, NULL, done_cb)) {
  88. lwsl_err("%s: failed to add email\n", __func__);
  89. goto bail1;
  90. }
  91. /* the usual lws event loop */
  92. while (n >= 0 && !interrupted)
  93. n = lws_service(context, 0);
  94. bail1:
  95. lwsl_user("Completed: %s\n", result ? "FAIL" : "PASS");
  96. lws_context_destroy(context);
  97. return result;
  98. }