a4203e479468023cca5cd8cd9bf2ab530f759661
[silc.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 "args.h"
24 #include "signals.h"
25 #include "levels.h"
26 #include "core.h"
27 #include "settings.h"
28
29 #include "printtext.h"
30 #include "fe-common-core.h"
31 #include "fe-common-silc.h"
32 #include "themes.h"
33
34 #include "screen.h"
35 #include "gui-entry.h"
36 #include "mainwindows.h"
37 #include "gui-printtext.h"
38 #include "gui-readline.h"
39 #include "statusbar.h"
40 #include "gui-windows.h"
41
42 #include <signal.h>
43
44 #ifdef HAVE_STATIC_PERL
45 void perl_core_init(void);
46 void perl_core_deinit(void);
47
48 void fe_perl_init(void);
49 void fe_perl_deinit(void);
50 #endif
51
52 void silc_init(void);
53 void silc_deinit(void);
54
55 void gui_expandos_init(void);
56 void gui_expandos_deinit(void);
57
58 void textbuffer_commands_init(void);
59 void textbuffer_commands_deinit(void);
60
61 void lastlog_init(void);
62 void lastlog_deinit(void);
63
64 void mainwindow_activity_init(void);
65 void mainwindow_activity_deinit(void);
66
67 void mainwindows_save_init(void);
68 void mainwindows_save_deinit(void);
69
70 static GMainLoop *main_loop;
71 int quitting;
72
73 static const char *firsttimer_text =
74         "Looks like this is the first time you run irssi.\n"
75         "This is just a reminder that you really should go read\n"
76         "startup-HOWTO if you haven't already. Irssi's default\n"
77         "settings aren't probably what you've used to, and you\n"
78         "shouldn't judge the whole client as crap based on them.\n\n"
79         "You can find startup-HOWTO and more irssi beginner info at\n"
80         "http://irssi.org/beginner/";
81 static int display_firsttimer = FALSE;
82
83
84 static void sig_exit(void)
85 {
86         g_main_quit(main_loop);
87 }
88
89 /* redraw irssi's screen.. */
90 void irssi_redraw(void)
91 {
92         clear();
93         refresh();
94
95         /* windows */
96         mainwindows_redraw();
97         /* statusbar */
98         statusbar_redraw(NULL);
99         /* entry line */
100         gui_entry_redraw();
101 }
102
103 static void textui_init(void)
104 {
105         static struct poptOption options[] = {
106                 POPT_AUTOHELP
107                 { NULL, '\0', 0, NULL }
108         };
109
110         args_register(options);
111
112         irssi_gui = IRSSI_GUI_TEXT;
113         core_init();
114         silc_init();
115         fe_common_core_init();
116         fe_common_silc_init();
117
118         theme_register(gui_text_formats);
119         signal_add("gui exit", (SIGNAL_FUNC) sig_exit);
120 }
121
122 static void textui_finish_init(void)
123 {
124         quitting = FALSE;
125
126         screen_refresh_freeze();
127         textbuffer_init();
128         textbuffer_view_init();
129         textbuffer_commands_init();
130         gui_entry_init();
131         gui_expandos_init();
132         gui_printtext_init();
133         gui_readline_init();
134         lastlog_init();
135         mainwindows_init();
136         mainwindow_activity_init();
137         mainwindows_save_init();
138         gui_windows_init();
139         statusbar_init();
140
141         settings_check();
142
143         fe_common_core_finish_init();
144
145 #ifdef HAVE_STATIC_PERL
146         perl_core_init();
147         fe_perl_init();
148 #endif
149         signal_emit("irssi init finished", 0);
150
151 #if 0
152         if (display_firsttimer) {
153                 printtext_window(active_win, MSGLEVEL_CLIENTNOTICE,
154                                  "%s", firsttimer_text);
155         }
156 #endif
157
158         screen_refresh_thaw();
159 }
160
161 static void textui_deinit(void)
162 {
163         quitting = TRUE;
164         signal(SIGINT, SIG_DFL);
165
166         screen_refresh_freeze();
167         while (modules != NULL)
168                 module_unload(modules->data);
169
170         signal_remove("gui exit", (SIGNAL_FUNC) sig_exit);
171
172         lastlog_deinit();
173         statusbar_deinit();
174         gui_printtext_deinit();
175         gui_readline_deinit();
176         gui_windows_deinit();
177         mainwindows_save_deinit();
178         mainwindow_activity_deinit();
179         mainwindows_deinit();
180         gui_expandos_deinit();
181         gui_entry_deinit();
182         textbuffer_commands_deinit();
183         textbuffer_view_deinit();
184         textbuffer_deinit();
185
186         screen_refresh_thaw();
187         deinit_screen();
188
189 #ifdef HAVE_STATIC_PERL
190         fe_perl_deinit();
191         perl_core_deinit();
192 #endif
193
194         theme_unregister();
195
196         fe_common_silc_deinit();
197         fe_common_core_deinit();
198         silc_deinit();
199         core_deinit();
200 }
201
202 static void check_oldcrap(void)
203 {
204         FILE *f;
205         char *path, str[256];
206         int found;
207
208         /* check that default.theme is up-to-date */
209         path = g_strdup_printf("%s/.silc/default.theme", g_get_home_dir());
210         f = fopen(path, "r+");
211         if (f == NULL) {
212                 g_free(path);
213                 return;
214         }
215         found = FALSE;
216         while (!found && fgets(str, sizeof(str), f) != NULL)
217                 found = strstr(str, "abstracts = ") != NULL;
218         fclose(f);
219
220         if (found) {
221                 g_free(path);
222                 return;
223         }
224
225         printf("\nYou seem to have old default.theme in ~/.silc/ directory.\n");
226         printf("Themeing system has changed a bit since last irssi release,\n");
227         printf("you should either delete your old default.theme or manually\n");
228         printf("merge it with the new default.theme.\n\n");
229         printf("Do you want to delete the old theme now? (Y/n)\n");
230
231         str[0] = '\0';
232         fgets(str, sizeof(str), stdin);
233         if (toupper(str[0]) == 'Y' || str[0] == '\n' || str[0] == '\0')
234                 remove(path);
235         g_free(path);
236 }
237
238 static void check_files(void)
239 {
240         struct stat statbuf;
241         char *path;
242
243         path = g_strdup_printf("%s/.silc", g_get_home_dir());
244         if (stat(path, &statbuf) != 0) {
245                 /* ~/.irssi doesn't exist, first time running irssi */
246                 display_firsttimer = TRUE;
247         } else {
248                 check_oldcrap();
249         }
250         g_free(path);
251 }
252
253 #ifdef WIN32
254 static void winsock_init(void)
255 {
256         WORD wVersionRequested;
257         WSADATA wsaData;
258
259         wVersionRequested = MAKEWORD(2, 2);
260
261         if (WSAStartup(wVersionRequested, &wsaData) != 0) {
262                 printf("Error initializing winsock\n");
263                 exit(1);
264         }
265 }
266 #endif
267
268 int main(int argc, char **argv)
269 {
270         check_files();
271 #ifdef WIN32
272         winsock_init();
273 #endif
274 #ifdef HAVE_SOCKS
275         SOCKSinit(argv[0]);
276 #endif
277 #ifdef ENABLE_NLS
278         /* initialize the i18n stuff */
279         bindtextdomain(PACKAGE, LOCALEDIR);
280         textdomain(PACKAGE);
281 #endif
282
283         textui_init();
284         args_execute(argc, argv);
285         silc_init_finish();
286
287         if (!init_screen())
288                 g_error("Can't initialize screen handling, quitting.\n");
289
290         textui_finish_init();
291         main_loop = g_main_new(TRUE);
292         g_main_run(main_loop);
293         g_main_destroy(main_loop);
294         textui_deinit();
295
296 #ifdef MEM_DEBUG
297         ig_mem_profile();
298 #endif
299
300         return 0;
301 }