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