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