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