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