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