4 Copyright (C) 1999-2001 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "module-formats.h"
29 #include "printtext.h"
31 #include "gui-windows.h"
32 #include "gui-printtext.h"
34 #define DEFAULT_LASTLOG_BEFORE 3
35 #define DEFAULT_LASTLOG_AFTER 3
36 #define MAX_LINES_WITHOUT_FORCE 1000
38 static void window_lastlog_clear(WINDOW_REC *window)
40 TEXT_BUFFER_VIEW_REC *view;
41 LINE_REC *line, *next;
43 term_refresh_freeze();
44 view = WINDOW_GUI(window)->view;
45 line = textbuffer_view_get_lines(view);
47 while (line != NULL) {
50 if (line->info.level & MSGLEVEL_LASTLOG)
51 textbuffer_view_remove_line(view, line);
54 textbuffer_view_redraw(view);
58 /* Only unknown keys in `optlist' should be levels.
59 Returns -1 if unknown option was given. */
60 int cmd_options_get_level(const char *cmd, GHashTable *optlist)
62 GSList *list, *tmp, *next;
65 /* get all the options, then remove the known ones. there should
66 be only one left - the server tag. */
67 list = hashtable_get_keys(optlist);
69 for (tmp = list; tmp != NULL; tmp = next) {
70 char *option = tmp->data;
73 if (command_have_option(cmd, option))
74 list = g_slist_remove(list, option);
79 while (list != NULL) {
80 level = level_get(list->data);
83 signal_emit("error command", 2,
84 GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN),
91 list = g_slist_remove(list, list->data);
97 static void show_lastlog(const char *searchtext, GHashTable *optlist,
98 int start, int count, int fhandle)
105 int level, before, after, len;
107 level = cmd_options_get_level("lastlog", optlist);
108 if (level == -1) return; /* error in options */
109 if (level == 0) level = MSGLEVEL_ALL;
111 if (g_hash_table_lookup(optlist, "clear") != NULL) {
112 window_lastlog_clear(active_win);
113 if (*searchtext == '\0')
117 /* which window's lastlog to look at? */
119 str = g_hash_table_lookup(optlist, "window");
121 window = is_numeric(str, '\0') ?
122 window_find_refnum(atoi(str)) :
123 window_find_item(NULL, str);
124 if (window == NULL) {
125 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
126 TXT_REFNUM_NOT_FOUND, str);
131 if (g_hash_table_lookup(optlist, "new") != NULL)
132 startline = textbuffer_view_get_bookmark(WINDOW_GUI(window)->view, "lastlog_last_check");
133 else if (g_hash_table_lookup(optlist, "away") != NULL)
134 startline = textbuffer_view_get_bookmark(WINDOW_GUI(window)->view, "lastlog_last_away");
138 if (startline == NULL)
139 startline = textbuffer_view_get_lines(WINDOW_GUI(window)->view);
141 str = g_hash_table_lookup(optlist, "#");
143 before = after = atoi(str);
145 str = g_hash_table_lookup(optlist, "before");
146 before = str == NULL ? 0 : *str != '\0' ?
147 atoi(str) : DEFAULT_LASTLOG_BEFORE;
149 str = g_hash_table_lookup(optlist, "after");
150 if (str == NULL) str = g_hash_table_lookup(optlist, "a");
151 after = str == NULL ? 0 : *str != '\0' ?
152 atoi(str) : DEFAULT_LASTLOG_AFTER;
155 list = textbuffer_find_text(WINDOW_GUI(window)->view->buffer, startline,
156 level, MSGLEVEL_LASTLOG,
157 searchtext, before, after,
158 g_hash_table_lookup(optlist, "regexp") != NULL,
159 g_hash_table_lookup(optlist, "word") != NULL,
160 g_hash_table_lookup(optlist, "case") != NULL);
162 len = g_list_length(list);
166 int pos = len-count-start;
167 if (pos < 0) pos = 0;
169 tmp = pos > len ? NULL : g_list_nth(list, pos);
170 len = g_list_length(tmp);
173 if (g_hash_table_lookup(optlist, "count") != NULL) {
174 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
175 TXT_LASTLOG_COUNT, len);
180 if (len > MAX_LINES_WITHOUT_FORCE && fhandle == -1 &&
181 g_hash_table_lookup(optlist, "force") == NULL) {
182 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
183 TXT_LASTLOG_TOO_LONG, len);
188 if (fhandle == -1 && g_hash_table_lookup(optlist, "-") == NULL)
189 printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_START);
191 line = g_string_new(NULL);
192 while (tmp != NULL && (count < 0 || count > 0)) {
193 LINE_REC *rec = tmp->data;
196 if (tmp->next == NULL)
199 write(fhandle, "--\n", 3);
201 printformat_window(active_win,
203 TXT_LASTLOG_SEPARATOR);
209 /* get the line text */
210 textbuffer_line2text(rec, fhandle == -1, line);
211 if (!settings_get_bool("timestamps")) {
212 struct tm *tm = localtime(&rec->info.time);
215 g_snprintf(timestamp, sizeof(timestamp),
217 tm->tm_hour, tm->tm_min);
218 g_string_prepend(line, timestamp);
221 /* write to file/window */
223 write(fhandle, line->str, line->len);
224 write(fhandle, "\n", 1);
226 printtext_window(active_win, MSGLEVEL_LASTLOG,
233 g_string_free(line, TRUE);
235 if (fhandle == -1 && g_hash_table_lookup(optlist, "-") == NULL)
236 printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_END);
238 textbuffer_view_set_bookmark_bottom(WINDOW_GUI(window)->view,
239 "lastlog_last_check");
241 textbuffer_line_unref_list(WINDOW_GUI(window)->view->buffer, list);
245 /* SYNTAX: LASTLOG [-] [-file <filename>] [-window <ref#|name>] [-new | -away]
246 [-<level> -<level...>] [-clear] [-count] [-case]
247 [-regexp | -word] [-before [<#>]] [-after [<#>]]
248 [-<# before+after>] [<pattern>] [<count> [<start>]] */
249 static void cmd_lastlog(const char *data)
252 char *text, *countstr, *start, *fname;
256 g_return_if_fail(data != NULL);
258 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS |
259 PARAM_FLAG_UNKNOWN_OPTIONS, "lastlog", &optlist,
260 &text, &countstr, &start))
263 if (*start == '\0' && is_numeric(text, 0) && *text != '0' &&
264 (*countstr == '\0' || is_numeric(countstr, 0))) {
269 count = atoi(countstr);
270 if (count == 0) count = -1;
272 /* target where to print it */
274 fname = g_hash_table_lookup(optlist, "file");
276 fname = convert_home(fname);
277 fhandle = open(fname, O_WRONLY | O_APPEND | O_CREAT,
278 octal2dec(settings_get_int("log_create_mode")));
282 if (fname != NULL && fhandle == -1) {
283 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
284 "%s", g_strerror(errno));
286 show_lastlog(text, optlist, atoi(start), count, fhandle);
291 cmd_params_free(free_arg);
294 void lastlog_init(void)
296 command_bind("lastlog", NULL, (SIGNAL_FUNC) cmd_lastlog);
298 command_set_options("lastlog", "!- # force clear -file -window new away word regexp case count @a @after @before");
301 void lastlog_deinit(void)
303 command_unbind("lastlog", (SIGNAL_FUNC) cmd_lastlog);