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;
|
188
|
90 char *username, *password, *resource, *servername;
|
35
|
91 char *jid;
|
188
|
92 char *optstring;
|
177
|
93 int optval, optval2;
|
188
|
94 int ssl;
|
24
|
95 int key;
|
|
96 unsigned int port;
|
|
97 unsigned int ping;
|
|
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");
|
188
|
136 if (optval) ut_InitDebug(1, optstring);
|
138
|
137
|
24
|
138 servername = cfg_read("server");
|
188
|
139 username = cfg_read("username");
|
|
140 password = cfg_read("password");
|
|
141 resource = cfg_read("resource");
|
24
|
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
|
188
|
169 optval = (cfg_read_int("logging") > 0);
|
|
170 optval2 = (cfg_read_int("load_logs") > 0);
|
177
|
171 if (optval || optval2)
|
|
172 hlog_enable(optval, cfg_read("logging_dir"), optval2);
|
113
|
173
|
160
|
174 if ((optstring = cfg_read("events_command")) != NULL)
|
|
175 hk_ext_cmd_init(optstring);
|
|
176
|
188
|
177 ssl = (cfg_read_int("ssl") > 0);
|
|
178 port = (unsigned int) cfg_read_int("port");
|
24
|
179
|
35
|
180 /* Connect to server */
|
24
|
181 ut_WriteLog("Connecting to server: %s:%d\n", servername, port);
|
29
|
182 scr_LogPrint("Connecting to server: %s:%d", servername, port);
|
35
|
183
|
|
184 jid = compose_jid(username, servername, resource);
|
70
|
185 jc = jb_connect(jid, port, ssl, password);
|
169
|
186 g_free(jid);
|
29
|
187 if (!jc) {
|
24
|
188 ut_WriteLog("\terror!!!\n");
|
|
189 fprintf(stderr, "Error connecting to (%s)\n", servername);
|
|
190 scr_TerminateCurses();
|
|
191 return -2;
|
|
192 }
|
|
193
|
112
|
194 ping = 40;
|
24
|
195 if (cfg_read("pinginterval"))
|
27
|
196 ping = (unsigned int) atoi(cfg_read("pinginterval"));
|
112
|
197 jb_set_keepalive_delay(ping);
|
24
|
198 ut_WriteLog("Ping interval stablished: %d secs\n", ping);
|
|
199
|
188
|
200 if (cfg_read_int("hide_offline_buddies") > 0)
|
120
|
201 buddylist_set_hide_offline_buddies(TRUE);
|
87
|
202
|
96
|
203 /* Initialize commands system */
|
|
204 cmd_init();
|
|
205
|
112
|
206 ut_WriteLog("Entering into main loop...\n\n");
|
|
207 ut_WriteLog("Ready to send/receive messages...\n");
|
|
208
|
|
209 jb_reset_keepalive();
|
24
|
210 while (ret != 255) {
|
35
|
211 key = scr_Getch();
|
151
|
212
|
|
213 // The refresh is really an ugly hack, but we need to call doupdate()
|
|
214 // from time to time to catch the RESIZE events, because getch keep
|
|
215 // returning ERR until a real key is pressed :-(
|
156
|
216 if (key != ERR) {
|
29
|
217 ret = process_key(key);
|
156
|
218 refresh = 0;
|
|
219 } else if (refresh++ > 1) {
|
151
|
220 doupdate();
|
152
|
221 refresh = 0;
|
|
222 }
|
151
|
223
|
152
|
224 if (key != KEY_RESIZE)
|
|
225 jb_main();
|
36
|
226 if (update_roster)
|
81
|
227 scr_DrawRoster();
|
24
|
228 }
|
|
229
|
35
|
230 jb_disconnect();
|
81
|
231 //bud_TerminateBuddies();
|
24
|
232 scr_TerminateCurses();
|
|
233
|
|
234 printf("\n\nHave a nice day!\nBye!\n");
|
|
235
|
|
236 return 0;
|
|
237 }
|