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