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