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" |
24
|
14 #include "lang.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 */ |
|
87 ut_InitDebug(1); |
|
88 |
|
89 lng_InitLanguage(); |
|
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 |
|
121 servername = cfg_read("server"); |
|
122 username = cfg_read("username"); |
|
123 password = cfg_read("password"); |
|
124 resource = cfg_read("resource"); |
|
125 |
|
126 if (!servername) { |
|
127 printf("Server name has not been specified in the config file!\n"); |
|
128 return -1; |
|
129 } |
|
130 if (!username) { |
|
131 printf("User name has not been specified in the config file!\n"); |
|
132 return -1; |
|
133 } |
|
134 if (!password) { |
|
135 char *p; |
|
136 size_t passsize = 64; |
|
137 printf("Please enter password: "); |
|
138 my_getpass(&password, &passsize); |
|
139 printf("\n"); |
|
140 for (p = password; *p; p++); |
|
141 for ( ; p > password ; p--) |
|
142 if (*p == '\n' || *p == '\r') *p = 0; |
|
143 } |
|
144 |
|
145 /* Initialize N-Curses */ |
|
146 ut_WriteLog("Initializing N-Curses...\n"); |
|
147 scr_InitCurses(); |
|
148 |
29
|
149 ut_WriteLog("Drawing main window...\n"); |
|
150 scr_DrawMainWindow(); |
|
151 |
70
|
152 ssl = 0; |
88
|
153 optstring = cfg_read("ssl"); |
|
154 if (optstring && (atoi(optstring) > 0)) |
70
|
155 ssl = 1; |
24
|
156 portstring = cfg_read("port"); |
70
|
157 port = (portstring != NULL) ? (unsigned int) atoi(portstring) : 0; |
24
|
158 |
35
|
159 /* Connect to server */ |
24
|
160 ut_WriteLog("Connecting to server: %s:%d\n", servername, port); |
29
|
161 scr_LogPrint("Connecting to server: %s:%d", servername, port); |
35
|
162 |
|
163 jid = compose_jid(username, servername, resource); |
70
|
164 jc = jb_connect(jid, port, ssl, password); |
35
|
165 free(jid); |
29
|
166 if (!jc) { |
24
|
167 ut_WriteLog("\terror!!!\n"); |
|
168 fprintf(stderr, "Error connecting to (%s)\n", servername); |
|
169 scr_TerminateCurses(); |
|
170 return -2; |
|
171 } |
|
172 |
112
|
173 ping = 40; |
24
|
174 if (cfg_read("pinginterval")) |
27
|
175 ping = (unsigned int) atoi(cfg_read("pinginterval")); |
112
|
176 jb_set_keepalive_delay(ping); |
24
|
177 ut_WriteLog("Ping interval stablished: %d secs\n", ping); |
|
178 |
88
|
179 optstring = cfg_read("hide_offline_buddies"); |
|
180 if (optstring && (atoi(optstring) > 0)) |
87
|
181 buddylist_hide_offline_buddies(TRUE); |
|
182 |
96
|
183 /* Initialize commands system */ |
|
184 cmd_init(); |
|
185 |
112
|
186 ut_WriteLog("Entering into main loop...\n\n"); |
|
187 ut_WriteLog("Ready to send/receive messages...\n"); |
|
188 |
|
189 jb_reset_keepalive(); |
81
|
190 keypad(scr_GetInputWindow(), TRUE); |
24
|
191 while (ret != 255) { |
35
|
192 key = scr_Getch(); |
|
193 if (key != ERR) |
29
|
194 ret = process_key(key); |
35
|
195 jb_main(); |
36
|
196 if (update_roster) |
81
|
197 scr_DrawRoster(); |
24
|
198 } |
|
199 |
35
|
200 jb_disconnect(); |
81
|
201 //bud_TerminateBuddies(); |
24
|
202 scr_TerminateCurses(); |
|
203 |
|
204 printf("\n\nHave a nice day!\nBye!\n"); |
|
205 |
|
206 return 0; |
|
207 } |