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