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