Mercurial > hg
annotate mcabber/src/main.c @ 289:72fa52203651
Update documentation
* Update /alias doc
* Add documentation for /bind
* Improve /msay help for AsciiDoc and HTML formats. Don't know how to get a
better presentation with the manpage format :(
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Fri, 08 Jul 2005 22:10:42 +0100 |
parents | f562b9af2de7 |
children | 35cda94e570d 12f919be3da5 |
rev | line source |
---|---|
149 | 1 #define _GNU_SOURCE |
24 | 2 #include <stdio.h> |
3 #include <stdlib.h> | |
4 #include <unistd.h> | |
5 #include <string.h> | |
6 #include <signal.h> | |
7 #include <termios.h> | |
28 | 8 #include <getopt.h> |
163 | 9 #include <sys/types.h> |
10 #include <sys/wait.h> | |
169 | 11 #include <glib.h> |
24 | 12 |
81 | 13 #include "jabglue.h" |
24 | 14 #include "screen.h" |
15 #include "parsecfg.h" | |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
16 #include "settings.h" |
102 | 17 #include "roster.h" |
96 | 18 #include "commands.h" |
113 | 19 #include "histolog.h" |
163 | 20 #include "hooks.h" |
81 | 21 #include "utils.h" |
24 | 22 #include "harddefines.h" |
23 | |
24 | |
25 void sig_handler(int signum) | |
26 { | |
163 | 27 if (signum == SIGCHLD) { |
28 int status; | |
29 pid_t pid; | |
30 do { | |
31 pid = waitpid (WAIT_ANY, &status, WNOHANG); | |
32 } while (pid > 0); | |
165 | 33 //if (pid < 0) |
34 // ut_WriteLog("Error in waitpid: errno=%d\n", errno); | |
163 | 35 signal(SIGCHLD, sig_handler); |
36 } else if (signum == SIGTERM) { | |
81 | 37 // bud_TerminateBuddies(); |
24 | 38 scr_TerminateCurses(); |
35 | 39 jb_disconnect(); |
24 | 40 printf("Killed by SIGTERM\nBye!\n"); |
41 exit(EXIT_SUCCESS); | |
165 | 42 } else { |
43 ut_WriteLog("Caught signal: %d\n", signum); | |
24 | 44 } |
45 } | |
46 | |
47 ssize_t my_getpass (char **passstr, size_t *n) | |
48 { | |
49 struct termios orig, new; | |
50 int nread; | |
51 | |
52 /* Turn echoing off and fail if we can't. */ | |
28 | 53 if (tcgetattr(fileno(stdin), &orig) != 0) |
24 | 54 return -1; |
55 new = orig; | |
56 new.c_lflag &= ~ECHO; | |
28 | 57 if (tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0) |
24 | 58 return -1; |
59 | |
60 /* Read the password. */ | |
61 nread = getline(passstr, n, stdin); | |
62 | |
63 /* Restore terminal. */ | |
28 | 64 (void) tcsetattr(fileno(stdin), TCSAFLUSH, &orig); |
24 | 65 |
27 | 66 return (ssize_t)nread; |
24 | 67 } |
68 | |
35 | 69 char *compose_jid(const char *username, const char *servername, |
70 const char *resource) | |
71 { | |
170 | 72 char *jid = g_new(char, |
73 strlen(username)+strlen(servername)+strlen(resource)+3); | |
35 | 74 strcpy(jid, username); |
75 strcat(jid, "@"); | |
76 strcat(jid, servername); | |
77 strcat(jid, "/"); | |
78 strcat(jid, resource); | |
79 return jid; | |
80 } | |
81 | |
24 | 82 void credits(void) |
83 { | |
53 | 84 printf(MCABBER_VERSION "\n"); |
24 | 85 printf(EMAIL "\n"); |
86 } | |
87 | |
88 int main(int argc, char **argv) | |
89 { | |
169 | 90 char *configFile = NULL; |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
91 const char *username, *password, *resource, *servername; |
35 | 92 char *jid; |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
93 const char *optstring; |
177 | 94 int optval, optval2; |
188 | 95 int ssl; |
24 | 96 int key; |
97 unsigned int port; | |
98 unsigned int ping; | |
99 int ret = 0; | |
151 | 100 unsigned int refresh = 0; |
24 | 101 |
102 credits(); | |
103 | |
104 /* SET THIS >0 TO ENABLE LOG */ | |
138 | 105 ut_InitDebug(0, NULL); |
24 | 106 |
107 ut_WriteLog("Setting signals handlers...\n"); | |
108 signal(SIGTERM, sig_handler); | |
163 | 109 signal(SIGCHLD, sig_handler); |
24 | 110 |
77 | 111 /* Parse command line options */ |
24 | 112 while (1) { |
113 int c = getopt(argc, argv, "hf:"); | |
114 if (c == -1) { | |
115 break; | |
116 } else | |
117 switch (c) { | |
118 case 'h': | |
119 printf("Usage: %s [-f mcabberrc_file]\n\n", argv[0]); | |
120 printf("Thanks to AjMacias for cabber!\n\n"); | |
121 return 0; | |
122 case 'f': | |
169 | 123 configFile = g_strdup(optarg); |
24 | 124 break; |
125 } | |
126 } | |
127 | |
169 | 128 if (configFile) |
129 ut_WriteLog("Setting config file: %s\n", configFile); | |
24 | 130 |
131 /* Parsing config file... */ | |
132 ut_WriteLog("Parsing config file...\n"); | |
133 cfg_file(configFile); | |
169 | 134 if (configFile) g_free(configFile); |
24 | 135 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
136 optstring = settings_opt_get("debug"); |
217 | 137 if (optstring) ut_InitDebug(1, optstring); |
138 | 138 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
139 servername = settings_opt_get("server"); |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
140 username = settings_opt_get("username"); |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
141 password = settings_opt_get("password"); |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
142 resource = settings_opt_get("resource"); |
24 | 143 |
144 if (!servername) { | |
145 printf("Server name has not been specified in the config file!\n"); | |
146 return -1; | |
147 } | |
148 if (!username) { | |
149 printf("User name has not been specified in the config file!\n"); | |
150 return -1; | |
151 } | |
152 if (!password) { | |
153 char *p; | |
154 size_t passsize = 64; | |
155 printf("Please enter password: "); | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
156 my_getpass((char**)&password, &passsize); |
24 | 157 printf("\n"); |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
158 for (p = (char*)password; *p; p++); |
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
159 for ( ; p > (char*)password ; p--) |
24 | 160 if (*p == '\n' || *p == '\r') *p = 0; |
161 } | |
162 | |
163 /* Initialize N-Curses */ | |
164 ut_WriteLog("Initializing N-Curses...\n"); | |
165 scr_InitCurses(); | |
166 | |
29 | 167 ut_WriteLog("Drawing main window...\n"); |
151 | 168 scr_DrawMainWindow(TRUE); |
29 | 169 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
170 optval = (settings_opt_get_int("logging") > 0); |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
171 optval2 = (settings_opt_get_int("load_logs") > 0); |
177 | 172 if (optval || optval2) |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
173 hlog_enable(optval, settings_opt_get("logging_dir"), |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
174 optval2); |
113 | 175 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
176 optstring = settings_opt_get("events_command"); |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
177 if (optstring) |
160 | 178 hk_ext_cmd_init(optstring); |
179 | |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
180 ssl = (settings_opt_get_int("ssl") > 0); |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
181 port = (unsigned int) settings_opt_get_int("port"); |
24 | 182 |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
183 jb_set_priority(settings_opt_get_int("priority")); |
272 | 184 |
35 | 185 /* Connect to server */ |
24 | 186 ut_WriteLog("Connecting to server: %s:%d\n", servername, port); |
29 | 187 scr_LogPrint("Connecting to server: %s:%d", servername, port); |
35 | 188 |
189 jid = compose_jid(username, servername, resource); | |
70 | 190 jc = jb_connect(jid, port, ssl, password); |
169 | 191 g_free(jid); |
29 | 192 if (!jc) { |
24 | 193 ut_WriteLog("\terror!!!\n"); |
194 fprintf(stderr, "Error connecting to (%s)\n", servername); | |
195 scr_TerminateCurses(); | |
196 return -2; | |
197 } | |
198 | |
112 | 199 ping = 40; |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
200 if (settings_opt_get("pinginterval")) |
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
201 ping = (unsigned int) settings_opt_get_int("pinginterval"); |
112 | 202 jb_set_keepalive_delay(ping); |
24 | 203 ut_WriteLog("Ping interval stablished: %d secs\n", ping); |
204 | |
279
f5dd437c057b
Rewrite the settings system
Mikael Berthe <mikael@lilotux.net>
parents:
272
diff
changeset
|
205 if (settings_opt_get_int("hide_offline_buddies") > 0) |
120 | 206 buddylist_set_hide_offline_buddies(TRUE); |
87 | 207 |
96 | 208 /* Initialize commands system */ |
209 cmd_init(); | |
210 | |
112 | 211 ut_WriteLog("Entering into main loop...\n\n"); |
212 ut_WriteLog("Ready to send/receive messages...\n"); | |
213 | |
214 jb_reset_keepalive(); | |
24 | 215 while (ret != 255) { |
35 | 216 key = scr_Getch(); |
151 | 217 |
218 // The refresh is really an ugly hack, but we need to call doupdate() | |
219 // from time to time to catch the RESIZE events, because getch keep | |
220 // returning ERR until a real key is pressed :-( | |
156 | 221 if (key != ERR) { |
29 | 222 ret = process_key(key); |
156 | 223 refresh = 0; |
224 } else if (refresh++ > 1) { | |
151 | 225 doupdate(); |
152 | 226 refresh = 0; |
227 } | |
151 | 228 |
152 | 229 if (key != KEY_RESIZE) |
230 jb_main(); | |
36 | 231 if (update_roster) |
81 | 232 scr_DrawRoster(); |
24 | 233 } |
234 | |
35 | 235 jb_disconnect(); |
81 | 236 //bud_TerminateBuddies(); |
24 | 237 scr_TerminateCurses(); |
238 | |
239 printf("\n\nHave a nice day!\nBye!\n"); | |
240 | |
241 return 0; | |
242 } |