24
|
1 |
#include <stdio.h> |
|
2 |
#include <stdlib.h> |
|
3 |
#include <unistd.h> |
|
4 |
#include <string.h> |
|
5 |
#include <signal.h> |
|
6 |
#include <termios.h> |
28
|
7 |
#include <getopt.h> |
24
|
8 |
|
|
9 |
#include "utils.h" |
|
10 |
#include "screen.h" |
|
11 |
#include "buddies.h" |
|
12 |
#include "parsecfg.h" |
|
13 |
#include "lang.h" |
|
14 |
#include "server.h" |
|
15 |
#include "harddefines.h" |
|
16 |
#include "socket.h" |
29
|
17 |
#include "jabglue.h" |
24
|
18 |
|
29
|
19 |
//int sock; |
24
|
20 |
|
|
21 |
void sig_handler(int signum) |
|
22 |
{ |
|
23 |
switch (signum) { |
|
24 |
case SIGALRM: |
29
|
25 |
jb_keepalive(); |
24
|
26 |
break; |
|
27 |
|
|
28 |
case SIGTERM: |
|
29 |
bud_TerminateBuddies(); |
|
30 |
scr_TerminateCurses(); |
29
|
31 |
// TODO srv_setpresence(sock, "unavailable"); |
|
32 |
// TODO close(sock); |
24
|
33 |
printf("Killed by SIGTERM\nBye!\n"); |
|
34 |
exit(EXIT_SUCCESS); |
|
35 |
break; |
|
36 |
|
|
37 |
} |
|
38 |
signal(SIGALRM, sig_handler); |
|
39 |
} |
|
40 |
|
|
41 |
ssize_t my_getpass (char **passstr, size_t *n) |
|
42 |
{ |
|
43 |
struct termios orig, new; |
|
44 |
int nread; |
|
45 |
|
|
46 |
/* Turn echoing off and fail if we can't. */ |
28
|
47 |
if (tcgetattr(fileno(stdin), &orig) != 0) |
24
|
48 |
return -1; |
|
49 |
new = orig; |
|
50 |
new.c_lflag &= ~ECHO; |
28
|
51 |
if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) |
24
|
52 |
return -1; |
|
53 |
|
|
54 |
/* Read the password. */ |
|
55 |
nread = getline(passstr, n, stdin); |
|
56 |
|
|
57 |
/* Restore terminal. */ |
28
|
58 |
(void) tcsetattr(fileno(stdin), TCSAFLUSH, &orig); |
24
|
59 |
|
27
|
60 |
return (ssize_t)nread; |
24
|
61 |
} |
|
62 |
|
|
63 |
void credits(void) |
|
64 |
{ |
|
65 |
printf(VERSION "\n"); |
|
66 |
printf(EMAIL "\n"); |
|
67 |
} |
|
68 |
|
|
69 |
int main(int argc, char **argv) |
|
70 |
{ |
|
71 |
char configFile[4096]; |
|
72 |
char *username, *password, *resource; |
|
73 |
char *servername; |
29
|
74 |
//char *idsession; |
24
|
75 |
char *portstring; |
|
76 |
int key; |
|
77 |
unsigned int port; |
|
78 |
unsigned int ping; |
|
79 |
int ret = 0; |
|
80 |
|
|
81 |
|
|
82 |
credits(); |
|
83 |
|
|
84 |
/* SET THIS >0 TO ENABLE LOG */ |
|
85 |
ut_InitDebug(1); |
|
86 |
|
|
87 |
lng_InitLanguage(); |
|
88 |
|
|
89 |
ut_WriteLog("Setting signals handlers...\n"); |
|
90 |
signal(SIGTERM, sig_handler); |
|
91 |
signal(SIGALRM, sig_handler); |
|
92 |
|
|
93 |
|
|
94 |
sprintf(configFile, "%s/.mcabberrc", getenv("HOME")); |
|
95 |
|
|
96 |
/* Proceso opciones de usuario */ |
|
97 |
while (1) { |
|
98 |
int c = getopt(argc, argv, "hf:"); |
|
99 |
if (c == -1) { |
|
100 |
break; |
|
101 |
} else |
|
102 |
switch (c) { |
|
103 |
case 'h': |
|
104 |
printf("Usage: %s [-f mcabberrc_file]\n\n", argv[0]); |
|
105 |
printf("Thanks to AjMacias for cabber!\n\n"); |
|
106 |
return 0; |
|
107 |
case 'f': |
|
108 |
strncpy(configFile, optarg, 1024); |
|
109 |
break; |
|
110 |
} |
|
111 |
} |
|
112 |
|
|
113 |
ut_WriteLog("Setting config file: %s\n", configFile); |
|
114 |
|
|
115 |
|
|
116 |
/* Parsing config file... */ |
|
117 |
ut_WriteLog("Parsing config file...\n"); |
|
118 |
cfg_file(configFile); |
|
119 |
|
|
120 |
servername = cfg_read("server"); |
|
121 |
username = cfg_read("username"); |
|
122 |
password = cfg_read("password"); |
|
123 |
resource = cfg_read("resource"); |
|
124 |
|
|
125 |
if (!servername) { |
|
126 |
printf("Server name has not been specified in the config file!\n"); |
|
127 |
return -1; |
|
128 |
} |
|
129 |
if (!username) { |
|
130 |
printf("User name has not been specified in the config file!\n"); |
|
131 |
return -1; |
|
132 |
} |
|
133 |
if (!password) { |
|
134 |
char *p; |
|
135 |
size_t passsize = 64; |
|
136 |
printf("Please enter password: "); |
|
137 |
my_getpass(&password, &passsize); |
|
138 |
printf("\n"); |
|
139 |
for (p = password; *p; p++); |
|
140 |
for ( ; p > password ; p--) |
|
141 |
if (*p == '\n' || *p == '\r') *p = 0; |
|
142 |
} |
|
143 |
|
|
144 |
/* Initialize N-Curses */ |
|
145 |
ut_WriteLog("Initializing N-Curses...\n"); |
|
146 |
scr_InitCurses(); |
|
147 |
|
29
|
148 |
ut_WriteLog("Drawing main window...\n"); |
|
149 |
scr_DrawMainWindow(); |
|
150 |
|
24
|
151 |
/* Connect to server */ |
|
152 |
portstring = cfg_read("port"); |
27
|
153 |
port = (portstring != NULL) ? (unsigned int) atoi(portstring) : -1U; |
24
|
154 |
|
|
155 |
ut_WriteLog("Connecting to server: %s:%d\n", servername, port); |
29
|
156 |
scr_LogPrint("Connecting to server: %s:%d", servername, port); |
|
157 |
jc = jb_connect(servername, port, 0, username, password, resource); |
|
158 |
if (!jc) { |
24
|
159 |
ut_WriteLog("\terror!!!\n"); |
|
160 |
fprintf(stderr, "Error connecting to (%s)\n", servername); |
|
161 |
scr_TerminateCurses(); |
|
162 |
return -2; |
|
163 |
} |
|
164 |
|
29
|
165 |
/* |
|
166 |
bud_InitBuddies(sock); // TODO |
24
|
167 |
|
|
168 |
ut_WriteLog("Sending presence...\n"); |
|
169 |
srv_setpresence(sock, "Online!"); |
29
|
170 |
*/ |
24
|
171 |
|
|
172 |
ping = 15; |
|
173 |
if (cfg_read("pinginterval")) |
27
|
174 |
ping = (unsigned int) atoi(cfg_read("pinginterval")); |
24
|
175 |
|
|
176 |
ut_WriteLog("Ping interval stablished: %d secs\n", ping); |
|
177 |
|
|
178 |
ut_WriteLog("Entering into main loop...\n\n"); |
|
179 |
ut_WriteLog("Ready to send/receive messages...\n"); |
|
180 |
|
29
|
181 |
sleep(1); |
|
182 |
jb_main(); |
|
183 |
sleep(1); |
|
184 |
jb_main(); |
|
185 |
sleep(2); |
|
186 |
jb_disconnect(); |
|
187 |
sleep(1); |
|
188 |
jb_main(); |
|
189 |
scr_TerminateCurses(); exit(0); // XXX |
24
|
190 |
while (ret != 255) { |
|
191 |
int x; |
|
192 |
alarm(ping); |
29
|
193 |
//x = check_io(sock, 0); |
|
194 |
x = check_io(0, 0); // FIXME |
|
195 |
/* |
24
|
196 |
if ((x & 1) == 1) { |
|
197 |
srv_msg *incoming = readserver(sock); |
|
198 |
|
|
199 |
switch (incoming->m) { |
|
200 |
case SM_PRESENCE: |
|
201 |
bud_SetBuddyStatus(incoming->from, incoming->connected); |
|
202 |
break; |
|
203 |
|
|
204 |
case SM_MESSAGE: |
|
205 |
scr_WriteIncomingMessage(incoming->from, incoming->body); |
|
206 |
free(incoming->body); |
|
207 |
free(incoming->from); |
|
208 |
break; |
|
209 |
|
|
210 |
case SM_UNHANDLED: |
|
211 |
break; |
|
212 |
} |
|
213 |
free(incoming); |
|
214 |
} |
|
215 |
if ((x & 2) == 2) { |
29
|
216 |
*/ |
|
217 |
if (x) { |
24
|
218 |
keypad(scr_GetInputWindow(), TRUE); |
|
219 |
key = scr_Getch(); |
29
|
220 |
ret = process_key(key); |
24
|
221 |
/* |
|
222 |
switch (key) { |
|
223 |
case KEY_IC: |
|
224 |
bud_AddBuddy(sock); |
|
225 |
break; |
|
226 |
case KEY_DC: |
|
227 |
bud_DeleteBuddy(sock); |
|
228 |
break; |
|
229 |
|
|
230 |
case KEY_RESIZE: |
|
231 |
endwin(); |
|
232 |
printf("\nRedimensionado no implementado\n"); |
|
233 |
printf("Reinicie Cabber.\n\n\n"); |
|
234 |
exit(EXIT_FAILURE); |
|
235 |
break; |
|
236 |
} |
|
237 |
*/ |
|
238 |
} |
|
239 |
if (update_roaster) { |
|
240 |
// scr_LogPrint("Update roaster"); |
|
241 |
bud_DrawRoster(scr_GetRosterWindow()); |
|
242 |
} |
|
243 |
} |
|
244 |
|
|
245 |
bud_TerminateBuddies(); |
|
246 |
scr_TerminateCurses(); |
|
247 |
|
29
|
248 |
//srv_setpresence(sock, "unavailable"); |
|
249 |
//close(sock); |
24
|
250 |
|
|
251 |
printf("\n\nHave a nice day!\nBye!\n"); |
|
252 |
|
|
253 |
return 0; |
|
254 |
} |