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,
183 MSGLEVEL_CLIENTNOTICE|MSGLEVEL_LASTLOG,
184 TXT_LASTLOG_TOO_LONG, len);
189 if (fhandle == -1 && g_hash_table_lookup(optlist, "-") == NULL)
190 printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_START);
192 line = g_string_new(NULL);
193 while (tmp != NULL && (count < 0 || count > 0)) {
194 LINE_REC *rec = tmp->data;
197 if (tmp->next == NULL)
200 write(fhandle, "--\n", 3);
202 printformat_window(active_win,
204 TXT_LASTLOG_SEPARATOR);
210 /* get the line text */
211 textbuffer_line2text(rec, fhandle == -1, line);
212 if (!settings_get_bool("timestamps")) {
213 struct tm *tm = localtime(&rec->info.time);
216 g_snprintf(timestamp, sizeof(timestamp),
218 tm->tm_hour, tm->tm_min);
219 g_string_prepend(line, timestamp);
222 /* write to file/window */
224 write(fhandle, line->str, line->len);
225 write(fhandle, "\n", 1);
227 printtext_window(active_win, MSGLEVEL_LASTLOG,
234 g_string_free(line, TRUE);
236 if (fhandle == -1 && g_hash_table_lookup(optlist, "-") == NULL)
237 printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_END);
239 textbuffer_view_set_bookmark_bottom(WINDOW_GUI(window)->view,
240 "lastlog_last_check");
242 textbuffer_line_unref_list(WINDOW_GUI(window)->view->buffer, list);
246 /* SYNTAX: LASTLOG [-] [-file <filename>] [-window <ref#|name>] [-new | -away]
247 [-<level> -<level...>] [-clear] [-count] [-case]
248 [-regexp | -word] [-before [<#>]] [-after [<#>]]
249 [-<# before+after>] [<pattern>] [<count> [<start>]] */
250 static void cmd_lastlog(const char *data)
253 char *text, *countstr, *start, *fname;
257 g_return_if_fail(data != NULL);
259 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS |
260 PARAM_FLAG_UNKNOWN_OPTIONS, "lastlog", &optlist,
261 &text, &countstr, &start))
264 if (*start == '\0' && is_numeric(text, 0) && *text != '0' &&
265 (*countstr == '\0' || is_numeric(countstr, 0))) {
270 count = atoi(countstr);
271 if (count == 0) count = -1;
273 /* target where to print it */
275 fname = g_hash_table_lookup(optlist, "file");
277 fname = convert_home(fname);
278 fhandle = open(fname, O_WRONLY | O_APPEND | O_CREAT,
279 octal2dec(settings_get_int("log_create_mode")));
283 if (fname != NULL && fhandle == -1) {
284 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
285 "%s", g_strerror(errno));
287 show_lastlog(text, optlist, atoi(start), count, fhandle);
292 cmd_params_free(free_arg);
295 void lastlog_init(void)
297 command_bind("lastlog", NULL, (SIGNAL_FUNC) cmd_lastlog);
299 command_set_options("lastlog", "!- # force clear -file -window new away word regexp case count @a @after @before");
302 void lastlog_deinit(void)
304 command_unbind("lastlog", (SIGNAL_FUNC) cmd_lastlog);