hist.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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(struct hook_info *hi) {
  51. struct history *n;
  52. if (unlikely(!(n = malloc(sizeof(struct history))))) {
  53. fprintf(stderr, "*** malloc() failed\n");
  54. return strdup(hi->msg);
  55. }
  56. n->usrname = strdup(hi->usr);
  57. n->msg = strdup(hi->msg);
  58. klist_init(&n->node);
  59. klist_add(&history, &n->node);
  60. dprintf(hist_fd, "[%ld] %s: %s", times(NULL), hi->usr, hi->msg);
  61. return strdup(hi->msg);
  62. }
  63. int open_hist_file(void) {
  64. struct readconf_strconf *conf;
  65. char *p;
  66. char buf[PATH_MAX];
  67. if (!(conf = readconf_find_strname("histfile"))) {
  68. p = getenv("HOME");
  69. sprintf(buf, "%s/" DEFAULT_HISTFILE, p);
  70. p = buf;
  71. }
  72. else
  73. p = conf->conf_value;
  74. printf("using history file %s\n", p);
  75. if (unlikely((hist_fd = open(p, O_WRONLY | O_APPEND | O_CREAT)) < 0)) {
  76. perror("open()");
  77. return -1;
  78. }
  79. return hist_fd;
  80. }
  81. int hist_init(void) {
  82. if (unlikely((hist_fd = open_hist_file()) < 0)) {
  83. fprintf(stderr, "*** unable to open history file. \n");
  84. return 1;
  85. }
  86. register_hook(HOOK_RECV, hist_hook);
  87. register_hook(HOOK_SEND, hist_hook);
  88. return 0;
  89. }
  90. int do_cmd_hist(int argc, char **argv) {
  91. struct klist_node *n;
  92. struct history *hist;
  93. (void) argv;
  94. plg_check_enabled();
  95. if (argc != 1) {
  96. fprintf(stderr, _("usage: -hist\n"));
  97. return 1;
  98. }
  99. for (n = history.next; n != &history; n = n->next) {
  100. hist = hist_of(n);
  101. pretty_new_msg(hist->usrname, hist->msg, 0, 0);
  102. }
  103. return 0;
  104. }
  105. struct history *hist_getmsg(int last) {
  106. struct klist_node *n;
  107. for (n = history.next; n != &history && last--; n = n->next)
  108. ;
  109. if (n == &history)
  110. return NULL;
  111. return hist_of(n);
  112. }