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; |
88
|
93 char *optstring; |
24
|
94 int key; |
|
95 unsigned int port; |
|
96 unsigned int ping; |
70
|
97 int ssl; |
24
|
98 int ret = 0; |
151
|
99 unsigned int refresh = 0; |
24
|
100 |
|
101 credits(); |
|
102 |
|
103 /* SET THIS >0 TO ENABLE LOG */ |
138
|
104 ut_InitDebug(0, NULL); |
24
|
105 |
|
106 ut_WriteLog("Setting signals handlers...\n"); |
|
107 signal(SIGTERM, sig_handler); |
163
|
108 signal(SIGCHLD, sig_handler); |
24
|
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': |
169
|
122 configFile = g_strdup(optarg); |
24
|
123 break; |
|
124 } |
|
125 } |
|
126 |
169
|
127 if (configFile) |
|
128 ut_WriteLog("Setting config file: %s\n", configFile); |
24
|
129 |
|
130 /* Parsing config file... */ |
|
131 ut_WriteLog("Parsing config file...\n"); |
|
132 cfg_file(configFile); |
169
|
133 if (configFile) g_free(configFile); |
24
|
134 |
138
|
135 optstring = cfg_read("debug"); |
|
136 if (optstring) |
|
137 ut_InitDebug(1, optstring); |
|
138 |
24
|
139 servername = cfg_read("server"); |
|
140 username = cfg_read("username"); |
|
141 password = cfg_read("password"); |
|
142 resource = cfg_read("resource"); |
|
143 |
|
144 if (!servername) { |
|
145 printf("Server name has not been specified in the config file!\n"); |
|
146 return -1; |
|
147 } |
|
148 if (!username) { |
|
149 printf("User name has not been specified in the config file!\n"); |
|
150 return -1; |
|
151 } |
|
152 if (!password) { |
|
153 char *p; |
|
154 size_t passsize = 64; |
|
155 printf("Please enter password: "); |
|
156 my_getpass(&password, &passsize); |
|
157 printf("\n"); |
|
158 for (p = password; *p; p++); |
|
159 for ( ; p > password ; p--) |
|
160 if (*p == '\n' || *p == '\r') *p = 0; |
|
161 } |
|
162 |
|
163 /* Initialize N-Curses */ |
|
164 ut_WriteLog("Initializing N-Curses...\n"); |
|
165 scr_InitCurses(); |
|
166 |
29
|
167 ut_WriteLog("Drawing main window...\n"); |
151
|
168 scr_DrawMainWindow(TRUE); |
29
|
169 |
113
|
170 optstring = cfg_read("logging"); |
|
171 if (optstring && (atoi(optstring) > 0)) |
|
172 hlog_enable(TRUE, cfg_read("logging_dir")); |
|
173 |
160
|
174 if ((optstring = cfg_read("events_command")) != NULL) |
|
175 hk_ext_cmd_init(optstring); |
|
176 |
70
|
177 ssl = 0; |
88
|
178 optstring = cfg_read("ssl"); |
|
179 if (optstring && (atoi(optstring) > 0)) |
70
|
180 ssl = 1; |
24
|
181 portstring = cfg_read("port"); |
70
|
182 port = (portstring != NULL) ? (unsigned int) atoi(portstring) : 0; |
24
|
183 |
35
|
184 /* Connect to server */ |
24
|
185 ut_WriteLog("Connecting to server: %s:%d\n", servername, port); |
29
|
186 scr_LogPrint("Connecting to server: %s:%d", servername, port); |
35
|
187 |
|
188 jid = compose_jid(username, servername, resource); |
70
|
189 jc = jb_connect(jid, port, ssl, password); |
169
|
190 g_free(jid); |
29
|
191 if (!jc) { |
24
|
192 ut_WriteLog("\terror!!!\n"); |
|
193 fprintf(stderr, "Error connecting to (%s)\n", servername); |
|
194 scr_TerminateCurses(); |
|
195 return -2; |
|
196 } |
|
197 |
112
|
198 ping = 40; |
24
|
199 if (cfg_read("pinginterval")) |
27
|
200 ping = (unsigned int) atoi(cfg_read("pinginterval")); |
112
|
201 jb_set_keepalive_delay(ping); |
24
|
202 ut_WriteLog("Ping interval stablished: %d secs\n", ping); |
|
203 |
88
|
204 optstring = cfg_read("hide_offline_buddies"); |
|
205 if (optstring && (atoi(optstring) > 0)) |
120
|
206 buddylist_set_hide_offline_buddies(TRUE); |
87
|
207 |
96
|
208 /* Initialize commands system */ |
|
209 cmd_init(); |
|
210 |
112
|
211 ut_WriteLog("Entering into main loop...\n\n"); |
|
212 ut_WriteLog("Ready to send/receive messages...\n"); |
|
213 |
|
214 jb_reset_keepalive(); |
24
|
215 while (ret != 255) { |
35
|
216 key = scr_Getch(); |
151
|
217 |
|
218 // The refresh is really an ugly hack, but we need to call doupdate() |
|
219 // from time to time to catch the RESIZE events, because getch keep |
|
220 // returning ERR until a real key is pressed :-( |
156
|
221 if (key != ERR) { |
29
|
222 ret = process_key(key); |
156
|
223 refresh = 0; |
|
224 } else if (refresh++ > 1) { |
151
|
225 doupdate(); |
152
|
226 refresh = 0; |
|
227 } |
151
|
228 |
152
|
229 if (key != KEY_RESIZE) |
|
230 jb_main(); |
36
|
231 if (update_roster) |
81
|
232 scr_DrawRoster(); |
24
|
233 } |
|
234 |
35
|
235 jb_disconnect(); |
81
|
236 //bud_TerminateBuddies(); |
24
|
237 scr_TerminateCurses(); |
|
238 |
|
239 printf("\n\nHave a nice day!\nBye!\n"); |
|
240 |
|
241 return 0; |
|
242 } |