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