Mercurial > ~mikael > mcabber > hg
annotate mcabber/src/main.c @ 306:b2d11b11675f
Fix interactive password prompt
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Tue, 12 Jul 2005 17:10:41 +0100 |
parents | 12f919be3da5 |
children | 566818afee1c |
rev | line source |
---|---|
149 | 1 #define _GNU_SOURCE |
24 | 2 #include <stdio.h> |
3 #include <stdlib.h> | |
4 #include <unistd.h> | |
5 #include <string.h> | |
6 #include <signal.h> | |
7 #include <termios.h> | |
28 | 8 #include <getopt.h> |
163 | 9 #include <sys/types.h> |
10 #include <sys/wait.h> | |
169 | 11 #include <glib.h> |
24 | 12 |
81 | 13 #include "jabglue.h" |
24 | 14 #include "screen.h" |
15 #include "parsecfg.h" | |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
16 #include "settings.h" |
102 | 17 #include "roster.h" |
96 | 18 #include "commands.h" |
113 | 19 #include "histolog.h" |
163 | 20 #include "hooks.h" |
81 | 21 #include "utils.h" |
24 | 22 #include "harddefines.h" |
23 | |
24 | |
25 void sig_handler(int signum) | |
26 { | |
163 | 27 if (signum == SIGCHLD) { |
28 int status; | |
29 pid_t pid; | |
30 do { | |
31 pid = waitpid (WAIT_ANY, &status, WNOHANG); | |
32 } while (pid > 0); | |
165 | 33 //if (pid < 0) |
34 // ut_WriteLog("Error in waitpid: errno=%d\n", errno); | |
163 | 35 signal(SIGCHLD, sig_handler); |
36 } else if (signum == SIGTERM) { | |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
37 jb_disconnect(); |
24 | 38 scr_TerminateCurses(); |
39 printf("Killed by SIGTERM\nBye!\n"); | |
40 exit(EXIT_SUCCESS); | |
165 | 41 } else { |
42 ut_WriteLog("Caught signal: %d\n", signum); | |
24 | 43 } |
44 } | |
45 | |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
46 void ask_password(void) |
24 | 47 { |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
48 char *password, *p; |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
49 size_t passsize = 128; |
24 | 50 struct termios orig, new; |
51 int nread; | |
52 | |
53 /* Turn echoing off and fail if we can't. */ | |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
54 if (tcgetattr(fileno(stdin), &orig) != 0) return; |
24 | 55 new = orig; |
56 new.c_lflag &= ~ECHO; | |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
57 if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) return; |
24 | 58 |
59 /* Read the password. */ | |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
60 password = NULL; |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
61 printf("Please enter password: "); |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
62 nread = getline(&password, &passsize, stdin); |
24 | 63 |
64 /* Restore terminal. */ | |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
65 tcsetattr(fileno(stdin), TCSAFLUSH, &orig); |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
66 printf("\n"); |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
67 |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
68 if (nread == -1 || !password) return; |
24 | 69 |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
70 for (p = (char*)password; *p; p++) |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
71 ; |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
72 for ( ; p > (char*)password ; p--) |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
73 if (*p == '\n' || *p == '\r') *p = 0; |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
74 |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
75 settings_set(SETTINGS_TYPE_OPTION, "password", password); |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
76 free(password); |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
77 return; |
24 | 78 } |
79 | |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
80 void mcabber_connect(void) |
35 | 81 { |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
82 const char *username, *password, *resource, *servername; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
83 char *jid; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
84 int ssl; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
85 unsigned int port; |
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 servername = settings_opt_get("server"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
88 username = settings_opt_get("username"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
89 password = settings_opt_get("password"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
90 resource = settings_opt_get("resource"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
91 |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
92 if (!servername) { |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
93 scr_LogPrint("Server name has not been specified!\n"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
94 return; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
95 } |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
96 if (!username) { |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
97 scr_LogPrint("User name has not been specified!\n"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
98 return; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
99 } |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
100 if (!password) { |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
101 scr_LogPrint("Password has not been specified!\n"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
102 return; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
103 } |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
104 if (!resource) |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
105 resource = "mcabber"; |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
106 |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
107 ssl = (settings_opt_get_int("ssl") > 0); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
108 port = (unsigned int) settings_opt_get_int("port"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
109 |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
110 jb_set_priority(settings_opt_get_int("priority")); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
111 |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
112 /* Connect to server */ |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
113 ut_WriteLog("Connecting to server: %s:%d\n", servername, port); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
114 scr_LogPrint("Connecting to server: %s:%d", servername, port); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
115 |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
116 jid = compose_jid(username, servername, resource); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
117 jc = jb_connect(jid, port, ssl, password); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
118 g_free(jid); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
119 |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
120 if (!jc) { |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
121 ut_WriteLog("\tConnection error!!!\n"); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
122 scr_LogPrint("Error connecting to (%s)\n", servername); |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
123 } |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
124 |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
125 jb_reset_keepalive(); |
35 | 126 } |
127 | |
24 | 128 void credits(void) |
129 { | |
53 | 130 printf(MCABBER_VERSION "\n"); |
24 | 131 printf(EMAIL "\n"); |
132 } | |
133 | |
134 int main(int argc, char **argv) | |
135 { | |
169 | 136 char *configFile = NULL; |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
137 const char *optstring; |
177 | 138 int optval, optval2; |
24 | 139 int key; |
140 unsigned int ping; | |
141 int ret = 0; | |
151 | 142 unsigned int refresh = 0; |
24 | 143 |
144 credits(); | |
145 | |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
146 /* Set this >0 to enable log */ |
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
147 /* Note: debug can be enabled via the config file */ |
138 | 148 ut_InitDebug(0, NULL); |
24 | 149 |
150 ut_WriteLog("Setting signals handlers...\n"); | |
151 signal(SIGTERM, sig_handler); | |
163 | 152 signal(SIGCHLD, sig_handler); |
24 | 153 |
77 | 154 /* Parse command line options */ |
24 | 155 while (1) { |
156 int c = getopt(argc, argv, "hf:"); | |
157 if (c == -1) { | |
158 break; | |
159 } else | |
160 switch (c) { | |
161 case 'h': | |
162 printf("Usage: %s [-f mcabberrc_file]\n\n", argv[0]); | |
163 printf("Thanks to AjMacias for cabber!\n\n"); | |
164 return 0; | |
165 case 'f': | |
169 | 166 configFile = g_strdup(optarg); |
24 | 167 break; |
168 } | |
169 } | |
170 | |
169 | 171 if (configFile) |
172 ut_WriteLog("Setting config file: %s\n", configFile); | |
24 | 173 |
174 /* Parsing config file... */ | |
175 ut_WriteLog("Parsing config file...\n"); | |
176 cfg_file(configFile); | |
169 | 177 if (configFile) g_free(configFile); |
24 | 178 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
179 optstring = settings_opt_get("debug"); |
217 | 180 if (optstring) ut_InitDebug(1, optstring); |
138 | 181 |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
182 /* If no password is stored, we ask for it before entering |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
183 ncurses mode */ |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
184 if (!settings_opt_get("password")) |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
185 ask_password(); |
24 | 186 |
187 /* Initialize N-Curses */ | |
188 ut_WriteLog("Initializing N-Curses...\n"); | |
189 scr_InitCurses(); | |
190 | |
29 | 191 ut_WriteLog("Drawing main window...\n"); |
151 | 192 scr_DrawMainWindow(TRUE); |
29 | 193 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
194 optval = (settings_opt_get_int("logging") > 0); |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
195 optval2 = (settings_opt_get_int("load_logs") > 0); |
177 | 196 if (optval || optval2) |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
197 hlog_enable(optval, settings_opt_get("logging_dir"), optval2); |
113 | 198 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
199 optstring = settings_opt_get("events_command"); |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
200 if (optstring) |
160 | 201 hk_ext_cmd_init(optstring); |
202 | |
112 | 203 ping = 40; |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
204 if (settings_opt_get("pinginterval")) |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
205 ping = (unsigned int) settings_opt_get_int("pinginterval"); |
112 | 206 jb_set_keepalive_delay(ping); |
24 | 207 ut_WriteLog("Ping interval stablished: %d secs\n", ping); |
208 | |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
209 if (settings_opt_get_int("hide_offline_buddies") > 0) |
120 | 210 buddylist_set_hide_offline_buddies(TRUE); |
87 | 211 |
306
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
212 /* Connection */ |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
213 if (settings_opt_get("password")) |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
214 mcabber_connect(); |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
215 else |
b2d11b11675f
Fix interactive password prompt
Mikael Berthe <mikael@lilotux.net>
parents:
300
diff
changeset
|
216 scr_LogPrint("Can't connect: no password supplied"); |
298
35cda94e570d
Add /connect and /disconnect commands
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
217 |
96 | 218 /* Initialize commands system */ |
219 cmd_init(); | |
220 | |
112 | 221 ut_WriteLog("Entering into main loop...\n\n"); |
222 ut_WriteLog("Ready to send/receive messages...\n"); | |
223 | |
24 | 224 while (ret != 255) { |
35 | 225 key = scr_Getch(); |
151 | 226 |
227 // The refresh is really an ugly hack, but we need to call doupdate() | |
228 // from time to time to catch the RESIZE events, because getch keep | |
229 // returning ERR until a real key is pressed :-( | |
156 | 230 if (key != ERR) { |
29 | 231 ret = process_key(key); |
156 | 232 refresh = 0; |
233 } else if (refresh++ > 1) { | |
151 | 234 doupdate(); |
152 | 235 refresh = 0; |
236 } | |
151 | 237 |
152 | 238 if (key != KEY_RESIZE) |
239 jb_main(); | |
36 | 240 if (update_roster) |
81 | 241 scr_DrawRoster(); |
24 | 242 } |
243 | |
35 | 244 jb_disconnect(); |
24 | 245 scr_TerminateCurses(); |
246 | |
247 printf("\n\nHave a nice day!\nBye!\n"); | |
248 | |
249 return 0; | |
250 } |