Merged 0.7.99 irssi.
[crypto.git] / apps / irssi / src / fe-text / silc.c
1 /*
2  irssi.c : irssi
3
4     Copyright (C) 1999-2000 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
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
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "modules-load.h"
24 #include "args.h"
25 #include "signals.h"
26 #include "levels.h"
27 #include "core.h"
28 #include "settings.h"
29 #include "session.h"
30
31 #include "printtext.h"
32 #include "fe-common-core.h"
33 #include "fe-common-silc.h"
34 #include "themes.h"
35
36 #include "term.h"
37 #include "gui-entry.h"
38 #include "mainwindows.h"
39 #include "gui-printtext.h"
40 #include "gui-readline.h"
41 #include "statusbar.h"
42 #include "gui-windows.h"
43 #include "textbuffer-reformat.h"
44
45 #include <signal.h>
46
47 #ifdef HAVE_STATIC_PERL
48 void perl_core_init(void);
49 void perl_core_deinit(void);
50
51 void fe_perl_init(void);
52 void fe_perl_deinit(void);
53 #endif
54
55 void silc_init(void);
56 void silc_deinit(void);
57
58 void gui_expandos_init(void);
59 void gui_expandos_deinit(void);
60
61 void textbuffer_commands_init(void);
62 void textbuffer_commands_deinit(void);
63
64 void lastlog_init(void);
65 void lastlog_deinit(void);
66
67 void mainwindow_activity_init(void);
68 void mainwindow_activity_deinit(void);
69
70 void mainwindows_layout_init(void);
71 void mainwindows_layout_deinit(void);
72
73 void term_dummy_init(void);
74 void term_dummy_deinit(void);
75
76 static int dirty, full_redraw, dummy;
77
78 static GMainLoop *main_loop;
79 int quitting;
80
81 static const char *firsttimer_text =
82         "Looks like this is the first time you run irssi.\n"
83         "This is just a reminder that you really should go read\n"
84         "startup-HOWTO if you haven't already. Irssi's default\n"
85         "settings aren't probably what you've used to, and you\n"
86         "shouldn't judge the whole client as crap based on them.\n\n"
87         "You can find startup-HOWTO and more irssi beginner info at\n"
88         "http://irssi.org/beginner/";
89 static int display_firsttimer = FALSE;
90
91
92 static void sig_exit(void)
93 {
94         quitting = TRUE;
95 }
96
97 /* redraw irssi's screen.. */
98 void irssi_redraw(void)
99 {
100         dirty = TRUE;
101         full_redraw = TRUE;
102 }
103
104 void irssi_set_dirty(void)
105 {
106         dirty = TRUE;
107 }
108
109 static void dirty_check(void)
110 {
111         if (!dirty || dummy)
112                 return;
113
114         term_resize_dirty();
115
116         if (full_redraw) {
117                 full_redraw = FALSE;
118
119                 /* first clear the screen so curses will be
120                    forced to redraw the screen */
121                 term_clear();
122                 term_refresh(NULL);
123
124                 mainwindows_redraw();
125                 statusbar_redraw(NULL, TRUE);
126         }
127
128         mainwindows_redraw_dirty();
129         statusbar_redraw_dirty();
130         term_refresh(NULL);
131
132         dirty = FALSE;
133 }
134
135 static void textui_init(void)
136 {
137 #ifdef SIGTRAP
138         struct sigaction act;
139
140         sigemptyset(&act.sa_mask);
141         act.sa_flags = 0;
142         act.sa_handler = SIG_IGN;
143         sigaction(SIGTRAP, &act, NULL);
144 #endif
145
146         irssi_gui = IRSSI_GUI_TEXT;
147         core_init();
148         silc_init();
149         fe_common_core_init();
150         fe_common_silc_init();
151
152         theme_register(gui_text_formats);
153         signal_add_last("gui exit", (SIGNAL_FUNC) sig_exit);
154 }
155
156 static void textui_finish_init(void)
157 {
158         quitting = FALSE;
159
160         if (dummy)
161                 term_dummy_init();
162         else {
163                 term_refresh_freeze();
164                 textbuffer_init();
165                 textbuffer_view_init();
166                 textbuffer_commands_init();
167                 textbuffer_reformat_init();
168                 gui_expandos_init();
169                 gui_printtext_init();
170                 gui_readline_init();
171                 lastlog_init();
172                 mainwindows_init();
173                 mainwindow_activity_init();
174                 mainwindows_layout_init();
175                 gui_windows_init();
176                 statusbar_init();
177                 term_refresh_thaw();
178         }
179
180         settings_check();
181         module_register("core", "fe-text");
182
183 #ifdef HAVE_STATIC_PERL
184         perl_core_init();
185         fe_perl_init();
186 #endif
187
188         dirty_check();
189
190         fe_common_core_finish_init();
191         signal_emit("irssi init finished", 0);
192
193 #if 0
194         if (display_firsttimer) {
195                 printtext_window(active_win, MSGLEVEL_CLIENTNOTICE,
196                                  "%s", firsttimer_text);
197         }
198 #endif
199 }
200
201 static void textui_deinit(void)
202 {
203         signal(SIGINT, SIG_DFL);
204
205         term_refresh_freeze();
206         while (modules != NULL)
207                 module_unload(modules->data);
208
209 #ifdef HAVE_STATIC_PERL
210         perl_core_deinit();
211         fe_perl_deinit();
212 #endif
213
214         dirty_check(); /* one last time to print any quit messages */
215         signal_remove("gui exit", (SIGNAL_FUNC) sig_exit);
216
217         if (dummy)
218                 term_dummy_deinit();
219         else {
220                 lastlog_deinit();
221                 statusbar_deinit();
222                 gui_printtext_deinit();
223                 gui_readline_deinit();
224                 gui_windows_deinit();
225                 mainwindows_layout_deinit();
226                 mainwindow_activity_deinit();
227                 mainwindows_deinit();
228                 gui_expandos_deinit();
229                 textbuffer_reformat_deinit();
230                 textbuffer_commands_deinit();
231                 textbuffer_view_deinit();
232                 textbuffer_deinit();
233
234                 term_refresh_thaw();
235                 term_deinit();
236         }
237
238         theme_unregister();
239
240         fe_common_silc_deinit();
241         fe_common_core_deinit();
242         silc_deinit();
243         core_deinit();
244 }
245
246 static void check_oldcrap(void)
247 {
248         FILE *f;
249         char *path, str[256];
250         int found;
251
252         /* check that default.theme is up-to-date */
253         path = g_strdup_printf("%s/default.theme", get_irssi_dir());
254         f = fopen(path, "r+");
255         if (f == NULL) {
256                 g_free(path);
257                 return;
258         }
259         found = FALSE;
260         while (!found && fgets(str, sizeof(str), f) != NULL)
261                 found = strstr(str, "abstracts = ") != NULL;
262         fclose(f);
263
264         if (found) {
265                 g_free(path);
266                 return;
267         }
268
269         printf("\nYou seem to have old default.theme in "IRSSI_DIR_SHORT"/ directory.\n");
270         printf("Themeing system has changed a bit since last irssi release,\n");
271         printf("you should either delete your old default.theme or manually\n");
272         printf("merge it with the new default.theme.\n\n");
273         printf("Do you want to delete the old theme now? (Y/n)\n");
274
275         str[0] = '\0';
276         fgets(str, sizeof(str), stdin);
277         if (i_toupper(str[0]) == 'Y' || str[0] == '\n' || str[0] == '\0')
278                 remove(path);
279         g_free(path);
280 }
281
282 static void check_files(void)
283 {
284         struct stat statbuf;
285
286         if (stat(get_irssi_dir(), &statbuf) != 0) {
287                 /* ~/.irssi doesn't exist, first time running irssi */
288                 display_firsttimer = TRUE;
289         } else {
290                 check_oldcrap();
291         }
292 }
293
294 #ifdef WIN32
295 static void winsock_init(void)
296 {
297         WORD wVersionRequested;
298         WSADATA wsaData;
299
300         wVersionRequested = MAKEWORD(2, 2);
301
302         if (WSAStartup(wVersionRequested, &wsaData) != 0) {
303                 printf("Error initializing winsock\n");
304                 exit(1);
305         }
306 }
307 #endif
308
309 int main(int argc, char **argv)
310 {
311         static struct poptOption options[] = {
312                 { "dummy", 'd', POPT_ARG_NONE, &dummy, 0, "Use the dummy terminal mode", NULL },
313                 { NULL, '\0', 0, NULL }
314         };
315
316         dummy = FALSE;
317         quitting = FALSE;
318         core_init_paths(argc, argv);
319
320         check_files();
321
322 #ifdef WIN32
323         winsock_init();
324 #endif
325 #ifdef HAVE_SOCKS
326         SOCKSinit(argv[0]);
327 #endif
328 #ifdef ENABLE_NLS
329         /* initialize the i18n stuff */
330         bindtextdomain(PACKAGE, LOCALEDIR);
331         textdomain(PACKAGE);
332 #endif
333
334         textui_init();
335         args_register(options);
336         args_execute(argc, argv);
337
338 #if 0
339         silc_init_finish();
340 #endif
341
342         if (!dummy && !term_init()) {
343                 fprintf(stderr, "Can't initialize screen handling, quitting.\n");
344                 fprintf(stderr, "You can still use the dummy mode with -d parameter\n");
345                 return 1;
346         }
347
348         textui_finish_init();
349         main_loop = g_main_new(TRUE);
350
351         /* Does the same as g_main_run(main_loop), except we
352            can call our dirty-checker after each iteration */
353         while (!quitting) {
354                 g_main_iteration(TRUE);
355                 dirty_check();
356         }
357
358         g_main_destroy(main_loop);
359         textui_deinit();
360
361         session_upgrade(); /* if we /UPGRADEd, start the new process */
362         return 0;
363 }