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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 /* Only unknown keys in `optlist' should be levels.
39 Returns -1 if unknown option was given. */
40 int cmd_options_get_level(const char *cmd, GHashTable *optlist)
42 GSList *list, *tmp, *next;
45 /* get all the options, then remove the known ones. there should
46 be only one left - the server tag. */
47 list = hashtable_get_keys(optlist);
49 for (tmp = list; tmp != NULL; tmp = next) {
50 char *option = tmp->data;
53 if (command_have_option(cmd, option))
54 list = g_slist_remove(list, option);
59 while (list != NULL) {
60 level = level_get(list->data);
63 signal_emit("error command", 2,
64 GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN),
71 list = g_slist_remove(list, list->data);
77 static void show_lastlog(const char *searchtext, GHashTable *optlist,
78 int start, int count, FILE *fhandle)
85 int level, before, after, len;
87 level = cmd_options_get_level("lastlog", optlist);
88 if (level == -1) return; /* error in options */
89 if (level == 0) level = MSGLEVEL_ALL;
91 if (g_hash_table_lookup(optlist, "clear") != NULL) {
92 textbuffer_view_remove_lines_by_level(WINDOW_GUI(active_win)->view, MSGLEVEL_LASTLOG);
93 if (*searchtext == '\0')
97 /* which window's lastlog to look at? */
99 str = g_hash_table_lookup(optlist, "window");
101 window = is_numeric(str, '\0') ?
102 window_find_refnum(atoi(str)) :
103 window_find_item(NULL, str);
104 if (window == NULL) {
105 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
106 TXT_REFNUM_NOT_FOUND, str);
111 if (g_hash_table_lookup(optlist, "new") != NULL)
112 startline = textbuffer_view_get_bookmark(WINDOW_GUI(window)->view, "lastlog_last_check");
113 else if (g_hash_table_lookup(optlist, "away") != NULL)
114 startline = textbuffer_view_get_bookmark(WINDOW_GUI(window)->view, "lastlog_last_away");
118 if (startline == NULL)
119 startline = textbuffer_view_get_lines(WINDOW_GUI(window)->view);
121 str = g_hash_table_lookup(optlist, "#");
123 before = after = atoi(str);
125 str = g_hash_table_lookup(optlist, "before");
126 before = str == NULL ? 0 : *str != '\0' ?
127 atoi(str) : DEFAULT_LASTLOG_BEFORE;
129 str = g_hash_table_lookup(optlist, "after");
130 if (str == NULL) str = g_hash_table_lookup(optlist, "a");
131 after = str == NULL ? 0 : *str != '\0' ?
132 atoi(str) : DEFAULT_LASTLOG_AFTER;
135 list = textbuffer_find_text(WINDOW_GUI(window)->view->buffer, startline,
136 level, MSGLEVEL_LASTLOG,
137 searchtext, before, after,
138 g_hash_table_lookup(optlist, "regexp") != NULL,
139 g_hash_table_lookup(optlist, "word") != NULL,
140 g_hash_table_lookup(optlist, "case") != NULL);
142 len = g_list_length(list);
146 int pos = len-count-start;
147 if (pos < 0) pos = 0;
149 tmp = pos > len ? NULL : g_list_nth(list, pos);
150 len = g_list_length(tmp);
153 if (g_hash_table_lookup(optlist, "count") != NULL) {
154 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
155 TXT_LASTLOG_COUNT, len);
160 if (len > MAX_LINES_WITHOUT_FORCE && fhandle == NULL &&
161 g_hash_table_lookup(optlist, "force") == NULL) {
162 printformat_window(active_win,
163 MSGLEVEL_CLIENTNOTICE|MSGLEVEL_LASTLOG,
164 TXT_LASTLOG_TOO_LONG, len);
169 if (fhandle == NULL && g_hash_table_lookup(optlist, "-") == NULL)
170 printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_START);
172 line = g_string_new(NULL);
173 while (tmp != NULL && (count < 0 || count > 0)) {
174 LINE_REC *rec = tmp->data;
177 if (tmp->next == NULL)
179 if (fhandle != NULL) {
180 fwrite("--\n", 3, 1, fhandle);
182 printformat_window(active_win,
184 TXT_LASTLOG_SEPARATOR);
190 /* get the line text */
191 textbuffer_line2text(rec, fhandle == NULL, line);
192 if (!settings_get_bool("timestamps")) {
193 struct tm *tm = localtime(&rec->info.time);
196 g_snprintf(timestamp, sizeof(timestamp),
198 tm->tm_hour, tm->tm_min);
199 g_string_prepend(line, timestamp);
202 /* write to file/window */
203 if (fhandle != NULL) {
204 fwrite(line->str, line->len, 1, fhandle);
205 fputc('\n', fhandle);
207 printtext_window(active_win, MSGLEVEL_LASTLOG,
214 g_string_free(line, TRUE);
216 if (fhandle == NULL && g_hash_table_lookup(optlist, "-") == NULL)
217 printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_END);
219 textbuffer_view_set_bookmark_bottom(WINDOW_GUI(window)->view,
220 "lastlog_last_check");
225 /* SYNTAX: LASTLOG [-] [-file <filename>] [-window <ref#|name>] [-new | -away]
226 [-<level> -<level...>] [-clear] [-count] [-case]
227 [-regexp | -word] [-before [<#>]] [-after [<#>]]
228 [-<# before+after>] [<pattern>] [<count> [<start>]] */
229 static void cmd_lastlog(const char *data)
232 char *text, *countstr, *start, *fname;
237 g_return_if_fail(data != NULL);
239 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS |
240 PARAM_FLAG_UNKNOWN_OPTIONS, "lastlog", &optlist,
241 &text, &countstr, &start))
244 if (*start == '\0' && is_numeric(text, 0) && *text != '0' &&
245 (*countstr == '\0' || is_numeric(countstr, 0))) {
250 count = atoi(countstr);
251 if (count == 0) count = -1;
253 /* target where to print it */
255 fname = g_hash_table_lookup(optlist, "file");
257 fname = convert_home(fname);
258 fd = open(fname, O_WRONLY | O_APPEND | O_CREAT,
259 octal2dec(settings_get_int("log_create_mode")));
261 fhandle = fdopen(fd, "a");
268 if (fname != NULL && fhandle == NULL) {
269 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
270 "Could not open lastlog: %s", g_strerror(errno));
272 show_lastlog(text, optlist, atoi(start), count, fhandle);
273 if (fhandle != NULL) {
275 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
276 "Could not write lastlog: %s", g_strerror(errno));
281 cmd_params_free(free_arg);
284 void lastlog_init(void)
286 command_bind("lastlog", NULL, (SIGNAL_FUNC) cmd_lastlog);
288 command_set_options("lastlog", "!- # force clear -file -window new away word regexp case count @a @after @before");
291 void lastlog_deinit(void)
293 command_unbind("lastlog", (SIGNAL_FUNC) cmd_lastlog);