Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / src / fe-text / lastlog.c
1 /*
2  lastlog.c : irssi
3
4     Copyright (C) 1999-2001 Timo Sirainen
5
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.
10
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.
15
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.
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "commands.h"
24 #include "misc.h"
25 #include "levels.h"
26 #include "settings.h"
27
28 #include "module-formats.h"
29 #include "printtext.h"
30
31 #include "gui-windows.h"
32 #include "gui-printtext.h"
33
34 #define DEFAULT_LASTLOG_BEFORE 3
35 #define DEFAULT_LASTLOG_AFTER 3
36 #define MAX_LINES_WITHOUT_FORCE 1000
37
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)
41 {
42         GSList *list, *tmp, *next;
43         int level, retlevel;
44
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);
48         if (cmd != NULL) {
49                 for (tmp = list; tmp != NULL; tmp = next) {
50                         char *option = tmp->data;
51                         next = tmp->next;
52
53                         if (command_have_option(cmd, option))
54                                 list = g_slist_remove(list, option);
55                 }
56         }
57
58         retlevel = 0;
59         while (list != NULL) {
60                 level = level_get(list->data);
61                 if (level == 0) {
62                         /* unknown option */
63                         signal_emit("error command", 2,
64                                     GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN),
65                                     list->data);
66                         retlevel = -1;
67                         break;
68                 }
69
70                 retlevel |= level;
71                 list = g_slist_remove(list, list->data);
72         }
73
74         return retlevel;
75 }
76
77 static void show_lastlog(const char *searchtext, GHashTable *optlist,
78                          int start, int count, FILE *fhandle)
79 {
80         WINDOW_REC *window;
81         LINE_REC *startline;
82         GList *list, *tmp;
83         GString *line;
84         char *str;
85         int level, before, after, len;
86
87         level = cmd_options_get_level("lastlog", optlist);
88         if (level == -1) return; /* error in options */
89         if (level == 0) level = MSGLEVEL_ALL;
90
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')
94                         return;
95         }
96
97         /* which window's lastlog to look at? */
98         window = active_win;
99         str = g_hash_table_lookup(optlist, "window");
100         if (str != NULL) {
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);
107                         return;
108                 }
109         }
110
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");
115         else
116                 startline = NULL;
117
118         if (startline == NULL)
119                 startline = textbuffer_view_get_lines(WINDOW_GUI(window)->view);
120
121         str = g_hash_table_lookup(optlist, "#");
122         if (str != NULL) {
123                 before = after = atoi(str);
124         } else {
125                 str = g_hash_table_lookup(optlist, "before");
126                 before = str == NULL ? 0 : *str != '\0' ?
127                         atoi(str) : DEFAULT_LASTLOG_BEFORE;
128
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;
133         }
134
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);
141
142         len = g_list_length(list);
143         if (count <= 0)
144                 tmp = list;
145         else {
146                 int pos = len-count-start;
147                 if (pos < 0) pos = 0;
148
149                 tmp = pos > len ? NULL : g_list_nth(list, pos);
150                 len = g_list_length(tmp);
151         }
152
153         if (g_hash_table_lookup(optlist, "count") != NULL) {
154                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
155                                    TXT_LASTLOG_COUNT, len);
156                 g_list_free(list);
157                 return;
158         }
159
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);
165                 g_list_free(list);
166                 return;
167         }
168
169         if (fhandle == NULL && g_hash_table_lookup(optlist, "-") == NULL)
170                 printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_START);
171
172         line = g_string_new(NULL);
173         while (tmp != NULL && (count < 0 || count > 0)) {
174                 LINE_REC *rec = tmp->data;
175
176                 if (rec == NULL) {
177                         if (tmp->next == NULL)
178                                 break;
179                         if (fhandle != NULL) {
180                                 fwrite("--\n", 3, 1, fhandle);
181                         } else {
182                                 printformat_window(active_win,
183                                                    MSGLEVEL_LASTLOG,
184                                                    TXT_LASTLOG_SEPARATOR);
185                         }
186                         tmp = tmp->next;
187                         continue;
188                 }
189
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);
194                         char timestamp[10];
195
196                         g_snprintf(timestamp, sizeof(timestamp),
197                                    "%02d:%02d ",
198                                    tm->tm_hour, tm->tm_min);
199                         g_string_prepend(line, timestamp);
200                 }
201
202                 /* write to file/window */
203                 if (fhandle != NULL) {
204                         fwrite(line->str, line->len, 1, fhandle);
205                         fputc('\n', fhandle);
206                 } else {
207                         printtext_window(active_win, MSGLEVEL_LASTLOG,
208                                          "%s", line->str);
209                 }
210
211                 count--;
212                 tmp = tmp->next;
213         }
214         g_string_free(line, TRUE);
215
216         if (fhandle == NULL && g_hash_table_lookup(optlist, "-") == NULL)
217                 printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_END);
218
219         textbuffer_view_set_bookmark_bottom(WINDOW_GUI(window)->view,
220                                             "lastlog_last_check");
221
222         g_list_free(list);
223 }
224
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)
230 {
231         GHashTable *optlist;
232         char *text, *countstr, *start, *fname;
233         void *free_arg;
234         int count, fd;
235         FILE *fhandle;
236
237         g_return_if_fail(data != NULL);
238
239         if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_OPTIONS |
240                             PARAM_FLAG_UNKNOWN_OPTIONS, "lastlog", &optlist,
241                             &text, &countstr, &start))
242                 return;
243
244         if (*start == '\0' && is_numeric(text, 0) && *text != '0' &&
245             (*countstr == '\0' || is_numeric(countstr, 0))) {
246                 start = countstr;
247                 countstr = text;
248                 text = "";
249         }
250         count = atoi(countstr);
251         if (count == 0) count = -1;
252
253         /* target where to print it */
254         fhandle = NULL;
255         fname = g_hash_table_lookup(optlist, "file");
256         if (fname != NULL) {
257                 fname = convert_home(fname);
258                 fd = open(fname, O_WRONLY | O_APPEND | O_CREAT,
259                           octal2dec(settings_get_int("log_create_mode")));
260                 if (fd != -1) {
261                         fhandle = fdopen(fd, "a");
262                         if (fhandle == NULL)
263                                 close(fd);
264                 }
265                 g_free(fname);
266         }
267
268         if (fname != NULL && fhandle == NULL) {
269                 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
270                           "Could not open lastlog: %s", g_strerror(errno));
271         } else {
272                 show_lastlog(text, optlist, atoi(start), count, fhandle);
273                 if (fhandle != NULL) {
274                         if (ferror(fhandle))
275                                 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
276                                           "Could not write lastlog: %s", g_strerror(errno));
277                         fclose(fhandle);
278                 }
279         }
280
281         cmd_params_free(free_arg);
282 }
283
284 void lastlog_init(void)
285 {
286         command_bind("lastlog", NULL, (SIGNAL_FUNC) cmd_lastlog);
287
288         command_set_options("lastlog", "!- # force clear -file -window new away word regexp case count @a @after @before");
289 }
290
291 void lastlog_deinit(void)
292 {
293         command_unbind("lastlog", (SIGNAL_FUNC) cmd_lastlog);
294 }