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 { |
169
|
71 char *jid = g_new(strlen(username)+strlen(servername)+strlen(resource)+3); |
35
|
72 strcpy(jid, username); |
|
73 strcat(jid, "@"); |
|
74 strcat(jid, servername); |
|
75 strcat(jid, "/"); |
|
76 strcat(jid, resource); |
|
77 return jid; |
|
78 } |
|
79 |
24
|
80 void credits(void) |
|
81 { |
53
|
82 printf(MCABBER_VERSION "\n"); |
24
|
83 printf(EMAIL "\n"); |
|
84 } |
|
85 |
|
86 int main(int argc, char **argv) |
|
87 { |
169
|
88 char *configFile = NULL; |
24
|
89 char *username, *password, *resource; |
88
|
90 char *servername, *portstring; |
35
|
91 char *jid; |
88
|
92 char *optstring; |
24
|
93 int key; |
|
94 unsigned int port; |
|
95 unsigned int ping; |
70
|
96 int ssl; |
24
|
97 int ret = 0; |
151
|
98 unsigned int refresh = 0; |
24
|
99 |
|
100 credits(); |
|
101 |
|
102 /* SET THIS >0 TO ENABLE LOG */ |
138
|
103 ut_InitDebug(0, NULL); |
24
|
104 |
|
105 ut_WriteLog("Setting signals handlers...\n"); |
|
106 signal(SIGTERM, sig_handler); |
163
|
107 signal(SIGCHLD, sig_handler); |
24
|
108 |
77
|
109 /* Parse command line options */ |
24
|
110 while (1) { |
|
111 int c = getopt(argc, argv, "hf:"); |
|
112 if (c == -1) { |
|
113 break; |
|
114 } else |
|
115 switch (c) { |
|
116 case 'h': |
|
117 printf("Usage: %s [-f mcabberrc_file]\n\n", argv[0]); |
|
118 printf("Thanks to AjMacias for cabber!\n\n"); |
|
119 return 0; |
|
120 case 'f': |
169
|
121 configFile = g_strdup(optarg); |
24
|
122 break; |
|
123 } |
|
124 } |
|
125 |
169
|
126 if (configFile) |
|
127 ut_WriteLog("Setting config file: %s\n", configFile); |
24
|
128 |
|
129 /* Parsing config file... */ |
|
130 ut_WriteLog("Parsing config file...\n"); |
|
131 cfg_file(configFile); |
169
|
132 if (configFile) g_free(configFile); |
24
|
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); |
169
|
189 g_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 } |