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