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> |
24
|
9 |
|
81
|
10 |
#include "jabglue.h" |
24
|
11 |
#include "screen.h" |
|
12 |
#include "parsecfg.h" |
102
|
13 |
#include "roster.h" |
96
|
14 |
#include "commands.h" |
113
|
15 |
#include "histolog.h" |
81
|
16 |
#include "utils.h" |
24
|
17 |
#include "harddefines.h" |
|
18 |
|
|
19 |
|
|
20 |
void sig_handler(int signum) |
|
21 |
{ |
112
|
22 |
if (signum == SIGTERM) { |
81
|
23 |
// bud_TerminateBuddies(); |
24
|
24 |
scr_TerminateCurses(); |
35
|
25 |
jb_disconnect(); |
24
|
26 |
printf("Killed by SIGTERM\nBye!\n"); |
|
27 |
exit(EXIT_SUCCESS); |
|
28 |
} |
112
|
29 |
ut_WriteLog("Caught signal: %d\n", signum); |
24
|
30 |
} |
|
31 |
|
|
32 |
ssize_t my_getpass (char **passstr, size_t *n) |
|
33 |
{ |
|
34 |
struct termios orig, new; |
|
35 |
int nread; |
|
36 |
|
|
37 |
/* Turn echoing off and fail if we can't. */ |
28
|
38 |
if (tcgetattr(fileno(stdin), &orig) != 0) |
24
|
39 |
return -1; |
|
40 |
new = orig; |
|
41 |
new.c_lflag &= ~ECHO; |
28
|
42 |
if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) |
24
|
43 |
return -1; |
|
44 |
|
|
45 |
/* Read the password. */ |
|
46 |
nread = getline(passstr, n, stdin); |
|
47 |
|
|
48 |
/* Restore terminal. */ |
28
|
49 |
(void) tcsetattr(fileno(stdin), TCSAFLUSH, &orig); |
24
|
50 |
|
27
|
51 |
return (ssize_t)nread; |
24
|
52 |
} |
|
53 |
|
35
|
54 |
char *compose_jid(const char *username, const char *servername, |
|
55 |
const char *resource) |
|
56 |
{ |
|
57 |
char *jid = malloc(strlen(username)+strlen(servername)+strlen(resource)+3); |
|
58 |
strcpy(jid, username); |
|
59 |
strcat(jid, "@"); |
|
60 |
strcat(jid, servername); |
|
61 |
strcat(jid, "/"); |
|
62 |
strcat(jid, resource); |
|
63 |
return jid; |
|
64 |
} |
|
65 |
|
24
|
66 |
void credits(void) |
|
67 |
{ |
53
|
68 |
printf(MCABBER_VERSION "\n"); |
24
|
69 |
printf(EMAIL "\n"); |
|
70 |
} |
|
71 |
|
|
72 |
int main(int argc, char **argv) |
|
73 |
{ |
|
74 |
char configFile[4096]; |
|
75 |
char *username, *password, *resource; |
88
|
76 |
char *servername, *portstring; |
35
|
77 |
char *jid; |
88
|
78 |
char *optstring; |
24
|
79 |
int key; |
|
80 |
unsigned int port; |
|
81 |
unsigned int ping; |
70
|
82 |
int ssl; |
24
|
83 |
int ret = 0; |
151
|
84 |
unsigned int refresh = 0; |
24
|
85 |
|
|
86 |
credits(); |
|
87 |
|
|
88 |
/* SET THIS >0 TO ENABLE LOG */ |
138
|
89 |
ut_InitDebug(0, NULL); |
24
|
90 |
|
|
91 |
ut_WriteLog("Setting signals handlers...\n"); |
|
92 |
signal(SIGTERM, sig_handler); |
|
93 |
signal(SIGALRM, sig_handler); |
|
94 |
|
|
95 |
sprintf(configFile, "%s/.mcabberrc", getenv("HOME")); |
|
96 |
|
77
|
97 |
/* Parse command line options */ |
24
|
98 |
while (1) { |
|
99 |
int c = getopt(argc, argv, "hf:"); |
|
100 |
if (c == -1) { |
|
101 |
break; |
|
102 |
} else |
|
103 |
switch (c) { |
|
104 |
case 'h': |
|
105 |
printf("Usage: %s [-f mcabberrc_file]\n\n", argv[0]); |
|
106 |
printf("Thanks to AjMacias for cabber!\n\n"); |
|
107 |
return 0; |
|
108 |
case 'f': |
|
109 |
strncpy(configFile, optarg, 1024); |
|
110 |
break; |
|
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
ut_WriteLog("Setting config file: %s\n", configFile); |
|
115 |
|
|
116 |
|
|
117 |
/* Parsing config file... */ |
|
118 |
ut_WriteLog("Parsing config file...\n"); |
|
119 |
cfg_file(configFile); |
|
120 |
|
138
|
121 |
optstring = cfg_read("debug"); |
|
122 |
if (optstring) |
|
123 |
ut_InitDebug(1, optstring); |
|
124 |
|
24
|
125 |
servername = cfg_read("server"); |
|
126 |
username = cfg_read("username"); |
|
127 |
password = cfg_read("password"); |
|
128 |
resource = cfg_read("resource"); |
|
129 |
|
|
130 |
if (!servername) { |
|
131 |
printf("Server name has not been specified in the config file!\n"); |
|
132 |
return -1; |
|
133 |
} |
|
134 |
if (!username) { |
|
135 |
printf("User name has not been specified in the config file!\n"); |
|
136 |
return -1; |
|
137 |
} |
|
138 |
if (!password) { |
|
139 |
char *p; |
|
140 |
size_t passsize = 64; |
|
141 |
printf("Please enter password: "); |
|
142 |
my_getpass(&password, &passsize); |
|
143 |
printf("\n"); |
|
144 |
for (p = password; *p; p++); |
|
145 |
for ( ; p > password ; p--) |
|
146 |
if (*p == '\n' || *p == '\r') *p = 0; |
|
147 |
} |
|
148 |
|
|
149 |
/* Initialize N-Curses */ |
|
150 |
ut_WriteLog("Initializing N-Curses...\n"); |
|
151 |
scr_InitCurses(); |
|
152 |
|
29
|
153 |
ut_WriteLog("Drawing main window...\n"); |
151
|
154 |
scr_DrawMainWindow(TRUE); |
29
|
155 |
|
113
|
156 |
optstring = cfg_read("logging"); |
|
157 |
if (optstring && (atoi(optstring) > 0)) |
|
158 |
hlog_enable(TRUE, cfg_read("logging_dir")); |
|
159 |
|
160
|
160 |
if ((optstring = cfg_read("events_command")) != NULL) |
|
161 |
hk_ext_cmd_init(optstring); |
|
162 |
|
70
|
163 |
ssl = 0; |
88
|
164 |
optstring = cfg_read("ssl"); |
|
165 |
if (optstring && (atoi(optstring) > 0)) |
70
|
166 |
ssl = 1; |
24
|
167 |
portstring = cfg_read("port"); |
70
|
168 |
port = (portstring != NULL) ? (unsigned int) atoi(portstring) : 0; |
24
|
169 |
|
35
|
170 |
/* Connect to server */ |
24
|
171 |
ut_WriteLog("Connecting to server: %s:%d\n", servername, port); |
29
|
172 |
scr_LogPrint("Connecting to server: %s:%d", servername, port); |
35
|
173 |
|
|
174 |
jid = compose_jid(username, servername, resource); |
70
|
175 |
jc = jb_connect(jid, port, ssl, password); |
35
|
176 |
free(jid); |
29
|
177 |
if (!jc) { |
24
|
178 |
ut_WriteLog("\terror!!!\n"); |
|
179 |
fprintf(stderr, "Error connecting to (%s)\n", servername); |
|
180 |
scr_TerminateCurses(); |
|
181 |
return -2; |
|
182 |
} |
|
183 |
|
112
|
184 |
ping = 40; |
24
|
185 |
if (cfg_read("pinginterval")) |
27
|
186 |
ping = (unsigned int) atoi(cfg_read("pinginterval")); |
112
|
187 |
jb_set_keepalive_delay(ping); |
24
|
188 |
ut_WriteLog("Ping interval stablished: %d secs\n", ping); |
|
189 |
|
88
|
190 |
optstring = cfg_read("hide_offline_buddies"); |
|
191 |
if (optstring && (atoi(optstring) > 0)) |
120
|
192 |
buddylist_set_hide_offline_buddies(TRUE); |
87
|
193 |
|
96
|
194 |
/* Initialize commands system */ |
|
195 |
cmd_init(); |
|
196 |
|
112
|
197 |
ut_WriteLog("Entering into main loop...\n\n"); |
|
198 |
ut_WriteLog("Ready to send/receive messages...\n"); |
|
199 |
|
|
200 |
jb_reset_keepalive(); |
24
|
201 |
while (ret != 255) { |
35
|
202 |
key = scr_Getch(); |
151
|
203 |
|
|
204 |
// The refresh is really an ugly hack, but we need to call doupdate() |
|
205 |
// from time to time to catch the RESIZE events, because getch keep |
|
206 |
// returning ERR until a real key is pressed :-( |
156
|
207 |
if (key != ERR) { |
29
|
208 |
ret = process_key(key); |
156
|
209 |
refresh = 0; |
|
210 |
} else if (refresh++ > 1) { |
151
|
211 |
doupdate(); |
152
|
212 |
refresh = 0; |
|
213 |
} |
151
|
214 |
|
152
|
215 |
if (key != KEY_RESIZE) |
|
216 |
jb_main(); |
36
|
217 |
if (update_roster) |
81
|
218 |
scr_DrawRoster(); |
24
|
219 |
} |
|
220 |
|
35
|
221 |
jb_disconnect(); |
81
|
222 |
//bud_TerminateBuddies(); |
24
|
223 |
scr_TerminateCurses(); |
|
224 |
|
|
225 |
printf("\n\nHave a nice day!\nBye!\n"); |
|
226 |
|
|
227 |
return 0; |
|
228 |
} |