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