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