hist.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* vcc/plugins/hist/hist.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vcc is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vcc. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <limits.h>
  24. #include <sys/types.h>
  25. #include <sys/times.h>
  26. #include <vcc/vcc.h>
  27. #include <vcc/pretty.h>
  28. #include <vcc/plugin.h>
  29. #include <vcc/version.h>
  30. #include <vcc/interfaces.h>
  31. #include <klist.h>
  32. #include <readconf/readconf.h>
  33. int hist_init(void);
  34. int do_cmd_hist(int argc, char **argv);
  35. int hist_fd;
  36. struct cmdtable hist_cmdtab[] = {
  37. { "-hist", do_cmd_hist, "Dump history (from login to now). \n" },
  38. { NULL, NULL, NULL }
  39. };
  40. struct plugin plg_hist = {
  41. .state = 0,
  42. .name = "hist",
  43. .descr = "Show history. ",
  44. .init = hist_init,
  45. .cmdtab = hist_cmdtab
  46. };
  47. struct klist_node history = KLIST_NODE_INIT(&history);
  48. #define this_plg (&plg_hist)
  49. #define DEFAULT_HISTFILE ".vcc_history"
  50. char *hist_hook(char *s, char *usr) {
  51. struct history *n;
  52. if (unlikely(!(n = malloc(sizeof(struct history))))) {
  53. fprintf(stderr, "*** malloc() failed\n");
  54. return strdup(s);
  55. }
  56. n->usrname = strdup(usr);
  57. n->msg = strdup(s);
  58. klist_init(&n->node);
  59. klist_add(&history, &n->node);
  60. dprintf(hist_fd, "[%ld] %s: %s", times(NULL), usr, s);
  61. return strdup(s);
  62. }
  63. char *hist_msghook(char *s) {
  64. return hist_hook(s, usrname);
  65. }
  66. int open_hist_file(void) {
  67. struct readconf_strconf *conf;
  68. char *p;
  69. char buf[PATH_MAX];
  70. if (!(conf = readconf_find_strname("histfile"))) {
  71. p = getenv("HOME");
  72. sprintf(buf, "%s/" DEFAULT_HISTFILE, p);
  73. p = buf;
  74. }
  75. else
  76. p = conf->conf_value;
  77. printf("using history file %s\n", p);
  78. if (unlikely((hist_fd = open(p, O_WRONLY | O_APPEND | O_CREAT)) < 0)) {
  79. perror("open()");
  80. return -1;
  81. }
  82. return hist_fd;
  83. }
  84. int hist_init(void) {
  85. if (unlikely((hist_fd = open_hist_file()) < 0)) {
  86. fprintf(stderr, "*** unable to open history file. \n");
  87. return 1;
  88. }
  89. register_recvhook(hist_hook, this_plg);
  90. register_msghook(hist_msghook, this_plg);
  91. return 0;
  92. }
  93. int do_cmd_hist(int argc, char **argv) {
  94. struct klist_node *n;
  95. struct history *hist;
  96. (void) argv;
  97. plg_check_enabled();
  98. if (argc != 1) {
  99. fprintf(stderr, _("usage: -hist\n"));
  100. return 1;
  101. }
  102. for (n = history.next; n != &history; n = n->next) {
  103. hist = hist_of(n);
  104. pretty_new_msg(hist->usrname, hist->msg, 0, 0);
  105. }
  106. return 0;
  107. }