Mercurial > ~mikael > mcabber > hg
annotate mcabber/src/main.c @ 880:ea983a945eba
/buffer scroll_unlock: Don't always jump to the end of the buffer
If there is no pending message, do not jump to the bottom of the buffer.
For special buffers (e.g. for the status buffer) pending flag is currently
disabled so we jump unconditionally.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sat, 27 May 2006 15:59:05 +0200 |
parents | a0ddc43b421e |
children | b461d7ee6d96 |
rev | line source |
---|---|
307 | 1 /* |
2 * main.c | |
393 | 3 * |
699 | 4 * Copyright (C) 2005, 2006 Mikael Berthe <bmikael@lists.lilotux.net> |
307 | 5 * Parts of this file come from Cabber <cabber@ajmacias.com> |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or (at | |
10 * your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
20 * USA | |
21 */ | |
22 | |
24 | 23 #include <stdio.h> |
24 #include <stdlib.h> | |
25 #include <unistd.h> | |
26 #include <string.h> | |
27 #include <signal.h> | |
28 #include <termios.h> | |
28 | 29 #include <getopt.h> |
163 | 30 #include <sys/types.h> |
31 #include <sys/wait.h> | |
169 | 32 #include <glib.h> |
404 | 33 #include <config.h> |
24 | 34 |
81 | 35 #include "jabglue.h" |
24 | 36 #include "screen.h" |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
37 #include "settings.h" |
102 | 38 #include "roster.h" |
96 | 39 #include "commands.h" |
113 | 40 #include "histolog.h" |
163 | 41 #include "hooks.h" |
81 | 42 #include "utils.h" |
781
d3af6877a7df
Add --enable-hgcset to configure script
Mikael Berthe <mikael@lilotux.net>
parents:
779
diff
changeset
|
43 |
d3af6877a7df
Add --enable-hgcset to configure script
Mikael Berthe <mikael@lilotux.net>
parents:
779
diff
changeset
|
44 #ifdef ENABLE_HGCSET |
d3af6877a7df
Add --enable-hgcset to configure script
Mikael Berthe <mikael@lilotux.net>
parents:
779
diff
changeset
|
45 # include "hgcset.h" |
d3af6877a7df
Add --enable-hgcset to configure script
Mikael Berthe <mikael@lilotux.net>
parents:
779
diff
changeset
|
46 #endif |
24 | 47 |
801
d8e0a1ce3e8a
Define WAIT_ANY if needed.
Soren Andersen <somian@pobox.com>
parents:
789
diff
changeset
|
48 #ifndef WAIT_ANY |
d8e0a1ce3e8a
Define WAIT_ANY if needed.
Soren Andersen <somian@pobox.com>
parents:
789
diff
changeset
|
49 # define WAIT_ANY -1 |
d8e0a1ce3e8a
Define WAIT_ANY if needed.
Soren Andersen <somian@pobox.com>
parents:
789
diff
changeset
|
50 #endif |
24 | 51 |
789
1fa93cc6f5e3
Correctly restore term settings when interrupting the password query
Mikael Berthe <mikael@lilotux.net>
parents:
784
diff
changeset
|
52 static struct termios *backup_termios; |
1fa93cc6f5e3
Correctly restore term settings when interrupting the password query
Mikael Berthe <mikael@lilotux.net>
parents:
784
diff
changeset
|
53 |
777
e6d42f239541
"/version" shows Mercurial changeset (if there's one)
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
54 char *mcabber_version(void) |
e6d42f239541
"/version" shows Mercurial changeset (if there's one)
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
55 { |
e6d42f239541
"/version" shows Mercurial changeset (if there's one)
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
56 char *ver; |
e6d42f239541
"/version" shows Mercurial changeset (if there's one)
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
57 #ifdef HGCSET |
e6d42f239541
"/version" shows Mercurial changeset (if there's one)
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
58 ver = g_strdup_printf("%s (%s)", PACKAGE_VERSION, HGCSET); |
e6d42f239541
"/version" shows Mercurial changeset (if there's one)
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
59 #else |
e6d42f239541
"/version" shows Mercurial changeset (if there's one)
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
60 ver = g_strdup(PACKAGE_VERSION); |
e6d42f239541
"/version" shows Mercurial changeset (if there's one)
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
61 #endif |
e6d42f239541
"/version" shows Mercurial changeset (if there's one)
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
62 return ver; |
e6d42f239541
"/version" shows Mercurial changeset (if there's one)
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
63 } |
e6d42f239541
"/version" shows Mercurial changeset (if there's one)
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
64 |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
65 void mcabber_connect(void) |
35 | 66 { |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
67 const char *username, *password, *resource, *servername; |
429 | 68 const char *proxy_host; |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
69 char *jid; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
70 int ssl; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
71 unsigned int port; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
72 |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
73 servername = settings_opt_get("server"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
74 username = settings_opt_get("username"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
75 password = settings_opt_get("password"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
76 resource = settings_opt_get("resource"); |
429 | 77 proxy_host = settings_opt_get("proxy_host"); |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
78 |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
79 if (!servername) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
80 scr_LogPrint(LPRINT_NORMAL, "Server name has not been specified!"); |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
81 return; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
82 } |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
83 if (!username) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
84 scr_LogPrint(LPRINT_NORMAL, "User name has not been specified!"); |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
85 return; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
86 } |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
87 if (!password) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
88 scr_LogPrint(LPRINT_NORMAL, "Password has not been specified!"); |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
89 return; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
90 } |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
91 if (!resource) |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
92 resource = "mcabber"; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
93 |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
94 ssl = (settings_opt_get_int("ssl") > 0); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
95 port = (unsigned int) settings_opt_get_int("port"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
96 |
815
04edcb6d7ee3
Warn when ssl is request but not compiled in
Mikael Berthe <mikael@lilotux.net>
parents:
801
diff
changeset
|
97 #if !defined(HAVE_OPENSSL) && !defined(HAVE_GNUTLS) |
04edcb6d7ee3
Warn when ssl is request but not compiled in
Mikael Berthe <mikael@lilotux.net>
parents:
801
diff
changeset
|
98 if (ssl) { |
04edcb6d7ee3
Warn when ssl is request but not compiled in
Mikael Berthe <mikael@lilotux.net>
parents:
801
diff
changeset
|
99 scr_LogPrint(LPRINT_LOGNORM, |
04edcb6d7ee3
Warn when ssl is request but not compiled in
Mikael Berthe <mikael@lilotux.net>
parents:
801
diff
changeset
|
100 "** Warning: SSL is NOT available, ignoring 'ssl' value"); |
04edcb6d7ee3
Warn when ssl is request but not compiled in
Mikael Berthe <mikael@lilotux.net>
parents:
801
diff
changeset
|
101 ssl = 0; |
04edcb6d7ee3
Warn when ssl is request but not compiled in
Mikael Berthe <mikael@lilotux.net>
parents:
801
diff
changeset
|
102 } |
04edcb6d7ee3
Warn when ssl is request but not compiled in
Mikael Berthe <mikael@lilotux.net>
parents:
801
diff
changeset
|
103 #endif |
04edcb6d7ee3
Warn when ssl is request but not compiled in
Mikael Berthe <mikael@lilotux.net>
parents:
801
diff
changeset
|
104 |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
105 /* Connect to server */ |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
106 scr_LogPrint(LPRINT_NORMAL|LPRINT_DEBUG, "Connecting to server: %s", |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
107 servername); |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
108 if (port) |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
109 scr_LogPrint(LPRINT_NORMAL|LPRINT_DEBUG, " using port %d", port); |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
110 |
429 | 111 if (proxy_host) { |
112 int proxy_port = settings_opt_get_int("proxy_port"); | |
113 if (proxy_port <= 0 || proxy_port > 65535) { | |
114 scr_LogPrint(LPRINT_LOGNORM, "Invalid proxy port: %d", proxy_port); | |
115 } else { | |
116 const char *proxy_user, *proxy_pass; | |
117 proxy_user = settings_opt_get("proxy_user"); | |
118 proxy_pass = settings_opt_get("proxy_pass"); | |
119 // Proxy initialization | |
120 cw_setproxy(proxy_host, proxy_port, proxy_user, proxy_pass); | |
121 scr_LogPrint(LPRINT_NORMAL|LPRINT_DEBUG, " using proxy %s:%d", | |
122 proxy_host, proxy_port); | |
123 } | |
124 } | |
125 | |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
126 jid = compose_jid(username, servername, resource); |
403
17aa60c6dc63
Allow a different server name than the jid domain name
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
127 jc = jb_connect(jid, servername, port, ssl, password); |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
128 g_free(jid); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
129 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
130 if (!jc) |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
131 scr_LogPrint(LPRINT_LOGNORM, "Error connecting to (%s)", servername); |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
132 |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
133 jb_reset_keepalive(); |
35 | 134 } |
135 | |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
136 void mcabber_disconnect(const char *msg) |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
137 { |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
138 jb_disconnect(); |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
139 scr_TerminateCurses(); |
789
1fa93cc6f5e3
Correctly restore term settings when interrupting the password query
Mikael Berthe <mikael@lilotux.net>
parents:
784
diff
changeset
|
140 |
1fa93cc6f5e3
Correctly restore term settings when interrupting the password query
Mikael Berthe <mikael@lilotux.net>
parents:
784
diff
changeset
|
141 // Restore term settings, if needed. |
1fa93cc6f5e3
Correctly restore term settings when interrupting the password query
Mikael Berthe <mikael@lilotux.net>
parents:
784
diff
changeset
|
142 if (backup_termios) |
1fa93cc6f5e3
Correctly restore term settings when interrupting the password query
Mikael Berthe <mikael@lilotux.net>
parents:
784
diff
changeset
|
143 tcsetattr(fileno(stdin), TCSAFLUSH, backup_termios); |
1fa93cc6f5e3
Correctly restore term settings when interrupting the password query
Mikael Berthe <mikael@lilotux.net>
parents:
784
diff
changeset
|
144 |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
145 if (msg) |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
146 fprintf(stderr, "%s\n", msg); |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
147 printf("Bye!\n"); |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
148 exit(EXIT_SUCCESS); |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
149 } |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
150 |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
151 void sig_handler(int signum) |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
152 { |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
153 if (signum == SIGCHLD) { |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
154 int status; |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
155 pid_t pid; |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
156 do { |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
157 pid = waitpid (WAIT_ANY, &status, WNOHANG); |
676
80d4959422ca
New "eventcmd_checkstatus" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
158 // Check the exit status value if 'eventcmd_checkstatus' is set |
80d4959422ca
New "eventcmd_checkstatus" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
159 if (settings_opt_get_int("eventcmd_checkstatus")) { |
80d4959422ca
New "eventcmd_checkstatus" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
160 if (pid > 0) { |
80d4959422ca
New "eventcmd_checkstatus" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
161 // exit status 2 -> beep |
80d4959422ca
New "eventcmd_checkstatus" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
162 if (WIFEXITED(status) && WEXITSTATUS(status) == 2) { |
80d4959422ca
New "eventcmd_checkstatus" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
163 scr_Beep(); |
80d4959422ca
New "eventcmd_checkstatus" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
164 } |
80d4959422ca
New "eventcmd_checkstatus" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
165 } |
80d4959422ca
New "eventcmd_checkstatus" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
166 } |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
167 } while (pid > 0); |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
168 signal(SIGCHLD, sig_handler); |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
169 } else if (signum == SIGTERM) { |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
170 mcabber_disconnect("Killed by SIGTERM"); |
389
6e895f397474
Ncurses changes + Ctrl-C does not send a signal anylore
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
171 } else if (signum == SIGINT) { |
6e895f397474
Ncurses changes + Ctrl-C does not send a signal anylore
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
172 mcabber_disconnect("Killed by SIGINT"); |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
173 } else { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
174 scr_LogPrint(LPRINT_LOGNORM, "Caught signal: %d", signum); |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
175 } |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
176 } |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
177 |
336
eb994ee40029
Make some functions static
Mikael Berthe <mikael@lilotux.net>
parents:
333
diff
changeset
|
178 static void ask_password(void) |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
179 { |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
180 char *password, *p; |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
181 size_t passsize = 128; |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
182 struct termios orig, new; |
482
2ea7591584ab
Use fgets() instead of getline()
Mikael Berthe <mikael@lilotux.net>
parents:
429
diff
changeset
|
183 |
2ea7591584ab
Use fgets() instead of getline()
Mikael Berthe <mikael@lilotux.net>
parents:
429
diff
changeset
|
184 password = g_new0(char, passsize); |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
185 |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
186 /* Turn echoing off and fail if we can't. */ |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
187 if (tcgetattr(fileno(stdin), &orig) != 0) return; |
789
1fa93cc6f5e3
Correctly restore term settings when interrupting the password query
Mikael Berthe <mikael@lilotux.net>
parents:
784
diff
changeset
|
188 backup_termios = &orig; |
1fa93cc6f5e3
Correctly restore term settings when interrupting the password query
Mikael Berthe <mikael@lilotux.net>
parents:
784
diff
changeset
|
189 |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
190 new = orig; |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
191 new.c_lflag &= ~ECHO; |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
192 if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) return; |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
193 |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
194 /* Read the password. */ |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
195 printf("Please enter password: "); |
482
2ea7591584ab
Use fgets() instead of getline()
Mikael Berthe <mikael@lilotux.net>
parents:
429
diff
changeset
|
196 fgets(password, passsize, stdin); |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
197 |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
198 /* Restore terminal. */ |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
199 tcsetattr(fileno(stdin), TCSAFLUSH, &orig); |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
200 printf("\n"); |
789
1fa93cc6f5e3
Correctly restore term settings when interrupting the password query
Mikael Berthe <mikael@lilotux.net>
parents:
784
diff
changeset
|
201 backup_termios = NULL; |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
202 |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
203 for (p = (char*)password; *p; p++) |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
204 ; |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
205 for ( ; p > (char*)password ; p--) |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
206 if (*p == '\n' || *p == '\r') *p = 0; |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
207 |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
208 settings_set(SETTINGS_TYPE_OPTION, "password", password); |
482
2ea7591584ab
Use fgets() instead of getline()
Mikael Berthe <mikael@lilotux.net>
parents:
429
diff
changeset
|
209 g_free(password); |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
210 return; |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
211 } |
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
212 |
784
d7f3b58db522
Display hgcset (if defined) at startup
Mikael Berthe <mikael@lilotux.net>
parents:
781
diff
changeset
|
213 static void credits(void) |
24 | 214 { |
784
d7f3b58db522
Display hgcset (if defined) at startup
Mikael Berthe <mikael@lilotux.net>
parents:
781
diff
changeset
|
215 char *v = mcabber_version(); |
851
da03534e46c7
Add startup log messages to the status buffer
Mikael Berthe <mikael@lilotux.net>
parents:
848
diff
changeset
|
216 const char *v_fmt = "MCabber %s -- Email: mcabber [at] lilotux [dot] net\n"; |
da03534e46c7
Add startup log messages to the status buffer
Mikael Berthe <mikael@lilotux.net>
parents:
848
diff
changeset
|
217 printf(v_fmt, v); |
da03534e46c7
Add startup log messages to the status buffer
Mikael Berthe <mikael@lilotux.net>
parents:
848
diff
changeset
|
218 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8, v_fmt, v); |
784
d7f3b58db522
Display hgcset (if defined) at startup
Mikael Berthe <mikael@lilotux.net>
parents:
781
diff
changeset
|
219 g_free(v); |
24 | 220 } |
221 | |
222 int main(int argc, char **argv) | |
223 { | |
169 | 224 char *configFile = NULL; |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
225 const char *optstring; |
177 | 226 int optval, optval2; |
24 | 227 int key; |
228 unsigned int ping; | |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
322
diff
changeset
|
229 int ret; |
151 | 230 unsigned int refresh = 0; |
761
4532a9fe0e8c
Handle some keyboard escape sequences
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
231 keycode kcode; |
24 | 232 |
233 credits(); | |
234 | |
235 signal(SIGTERM, sig_handler); | |
312
f0b7ff2df7e8
Ctrl-C does not terminate mcabber
Mikael Berthe <mikael@lilotux.net>
parents:
307
diff
changeset
|
236 signal(SIGINT, sig_handler); |
163 | 237 signal(SIGCHLD, sig_handler); |
413 | 238 signal(SIGPIPE, SIG_IGN); |
24 | 239 |
77 | 240 /* Parse command line options */ |
24 | 241 while (1) { |
242 int c = getopt(argc, argv, "hf:"); | |
243 if (c == -1) { | |
244 break; | |
245 } else | |
246 switch (c) { | |
247 case 'h': | |
248 printf("Usage: %s [-f mcabberrc_file]\n\n", argv[0]); | |
249 printf("Thanks to AjMacias for cabber!\n\n"); | |
250 return 0; | |
251 case 'f': | |
169 | 252 configFile = g_strdup(optarg); |
24 | 253 break; |
254 } | |
255 } | |
256 | |
848
a9161d2dc414
Introduce special buffer stuff
Mikael Berthe <mikael@lilotux.net>
parents:
815
diff
changeset
|
257 /* Initialize commands system and roster */ |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
322
diff
changeset
|
258 cmd_init(); |
848
a9161d2dc414
Introduce special buffer stuff
Mikael Berthe <mikael@lilotux.net>
parents:
815
diff
changeset
|
259 roster_init(); |
779
057ffe7c43dc
Initialize locale charset earlier
Mikael Berthe <mikael@lilotux.net>
parents:
777
diff
changeset
|
260 /* Initialize charset */ |
057ffe7c43dc
Initialize locale charset earlier
Mikael Berthe <mikael@lilotux.net>
parents:
777
diff
changeset
|
261 scr_InitLocaleCharSet(); |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
322
diff
changeset
|
262 |
24 | 263 /* Parsing config file... */ |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
264 ret = cfg_read_file(configFile); |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
265 /* free() configFile if it has been allocated during options parsing */ |
774
46304b773a44
Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents:
772
diff
changeset
|
266 g_free(configFile); |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
322
diff
changeset
|
267 /* Leave if there was an error in the config. file */ |
872
a0ddc43b421e
Don't stop when there is no configuration file
Mikael Berthe <mikael@lilotux.net>
parents:
855
diff
changeset
|
268 if (ret == -2) |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
322
diff
changeset
|
269 exit(EXIT_FAILURE); |
24 | 270 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
271 optstring = settings_opt_get("tracelog_file"); |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
272 if (optstring) |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
273 ut_InitDebug(settings_opt_get_int("tracelog_level"), optstring); |
138 | 274 |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
275 /* If no password is stored, we ask for it before entering |
872
a0ddc43b421e
Don't stop when there is no configuration file
Mikael Berthe <mikael@lilotux.net>
parents:
855
diff
changeset
|
276 ncurses mode -- unless the username is unknown. */ |
a0ddc43b421e
Don't stop when there is no configuration file
Mikael Berthe <mikael@lilotux.net>
parents:
855
diff
changeset
|
277 if (settings_opt_get("username") && !settings_opt_get("password")) { |
855
cbeedd730b1b
Display username before asking for password
Mikael Berthe <mikael@lilotux.net>
parents:
851
diff
changeset
|
278 const char *p; |
cbeedd730b1b
Display username before asking for password
Mikael Berthe <mikael@lilotux.net>
parents:
851
diff
changeset
|
279 p = settings_opt_get("server"); |
cbeedd730b1b
Display username before asking for password
Mikael Berthe <mikael@lilotux.net>
parents:
851
diff
changeset
|
280 if (p) |
cbeedd730b1b
Display username before asking for password
Mikael Berthe <mikael@lilotux.net>
parents:
851
diff
changeset
|
281 printf("Server: %s\n", p); |
cbeedd730b1b
Display username before asking for password
Mikael Berthe <mikael@lilotux.net>
parents:
851
diff
changeset
|
282 p = settings_opt_get("username"); |
cbeedd730b1b
Display username before asking for password
Mikael Berthe <mikael@lilotux.net>
parents:
851
diff
changeset
|
283 if (p) |
cbeedd730b1b
Display username before asking for password
Mikael Berthe <mikael@lilotux.net>
parents:
851
diff
changeset
|
284 printf("Username: %s\n", p); |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
285 ask_password(); |
415
5692c3a13202
Display the server name/IP before asking for password
Mikael Berthe <mikael@lilotux.net>
parents:
413
diff
changeset
|
286 } |
24 | 287 |
288 /* Initialize N-Curses */ | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
289 scr_LogPrint(LPRINT_DEBUG, "Initializing N-Curses..."); |
24 | 290 scr_InitCurses(); |
151 | 291 scr_DrawMainWindow(TRUE); |
29 | 292 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
293 optval = (settings_opt_get_int("logging") > 0); |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
294 optval2 = (settings_opt_get_int("load_logs") > 0); |
177 | 295 if (optval || optval2) |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
296 hlog_enable(optval, settings_opt_get("logging_dir"), optval2); |
113 | 297 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
298 optstring = settings_opt_get("events_command"); |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
299 if (optstring) |
160 | 300 hk_ext_cmd_init(optstring); |
301 | |
112 | 302 ping = 40; |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
303 if (settings_opt_get("pinginterval")) |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
304 ping = (unsigned int) settings_opt_get_int("pinginterval"); |
112 | 305 jb_set_keepalive_delay(ping); |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
306 scr_LogPrint(LPRINT_DEBUG, "Ping interval established: %d secs", ping); |
24 | 307 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
308 if (settings_opt_get_int("hide_offline_buddies") > 0) |
120 | 309 buddylist_set_hide_offline_buddies(TRUE); |
87 | 310 |
872
a0ddc43b421e
Don't stop when there is no configuration file
Mikael Berthe <mikael@lilotux.net>
parents:
855
diff
changeset
|
311 if (ret < 0) { |
a0ddc43b421e
Don't stop when there is no configuration file
Mikael Berthe <mikael@lilotux.net>
parents:
855
diff
changeset
|
312 scr_LogPrint(LPRINT_NORMAL, "No configuration file has been found."); |
a0ddc43b421e
Don't stop when there is no configuration file
Mikael Berthe <mikael@lilotux.net>
parents:
855
diff
changeset
|
313 scr_ShowBuddyWindow(); |
a0ddc43b421e
Don't stop when there is no configuration file
Mikael Berthe <mikael@lilotux.net>
parents:
855
diff
changeset
|
314 } else { |
a0ddc43b421e
Don't stop when there is no configuration file
Mikael Berthe <mikael@lilotux.net>
parents:
855
diff
changeset
|
315 /* Connection */ |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
316 mcabber_connect(); |
872
a0ddc43b421e
Don't stop when there is no configuration file
Mikael Berthe <mikael@lilotux.net>
parents:
855
diff
changeset
|
317 } |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
318 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
319 scr_LogPrint(LPRINT_DEBUG, "Entering into main loop..."); |
112 | 320 |
333
db5bebe96c89
New config. file parsing
Mikael Berthe <mikael@lilotux.net>
parents:
322
diff
changeset
|
321 for (ret = 0 ; ret != 255 ; ) { |
761
4532a9fe0e8c
Handle some keyboard escape sequences
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
322 scr_Getch(&kcode); |
4532a9fe0e8c
Handle some keyboard escape sequences
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
323 key = kcode.value; |
151 | 324 |
307 | 325 /* The refresh is really an ugly hack, but we need to call doupdate() |
326 from time to time to catch the RESIZE events, because getch keep | |
327 returning ERR until a real key is pressed :-( | |
322
da138cdebf04
Implement auto-away mode
Mikael Berthe <mikael@lilotux.net>
parents:
314
diff
changeset
|
328 However, it allows us to handle an autoaway check here... |
307 | 329 */ |
156 | 330 if (key != ERR) { |
761
4532a9fe0e8c
Handle some keyboard escape sequences
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
331 ret = process_key(kcode); |
156 | 332 refresh = 0; |
333 } else if (refresh++ > 1) { | |
151 | 334 doupdate(); |
152 | 335 refresh = 0; |
322
da138cdebf04
Implement auto-away mode
Mikael Berthe <mikael@lilotux.net>
parents:
314
diff
changeset
|
336 scr_CheckAutoAway(FALSE); |
152 | 337 } |
151 | 338 |
152 | 339 if (key != KEY_RESIZE) |
340 jb_main(); | |
36 | 341 if (update_roster) |
81 | 342 scr_DrawRoster(); |
24 | 343 } |
344 | |
35 | 345 jb_disconnect(); |
24 | 346 scr_TerminateCurses(); |
347 | |
348 printf("\n\nHave a nice day!\nBye!\n"); | |
349 | |
350 return 0; | |
351 } | |
576 | 352 |
580 | 353 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |