wmclockmon: update change-log
[dockapps.git] / wmpop3 / wmpop3 / wmpop3.c
blobfb19061a081a8d2481fc7236abce08aced5bd2a9
1 /*
2 * wmpop3.c
3 * pop3 mail checker.
4 * by Scott Holden (scotth@thezone.net)
6 * Contains some code from WMInet Dockable app.
7 * - BlitNum and BlitString
9 */
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <time.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <sys/ioctl.h>
27 #include <X11/xpm.h>
28 #include <X11/Xlib.h>
29 #include <X11/extensions/shape.h>
31 #include <libdockapp/misc.h>
32 #include <libdockapp/wmgeneral.h>
33 #include "Pop3Client.h"
35 #include "wmpop3.xpm"
37 char wminet_mask_bits[64*64];
38 int wminet_mask_width = 64;
39 int wminet_mask_height = 64;
42 #define WMPOP3_VERSION "0.5.6a"
44 #define CHAR_WIDTH 5
45 #define CHAR_HEIGHT 7
46 #define SEC_IN_MIN 60
47 #define YES 1
48 #define NO 0
50 char *ProgName;
52 char mailclient[32] = "pine";
53 char password[32];
54 char username[32];
55 char popserver[128];
56 int serverport = 110;
57 int mailCheckDelay = 10; /* default */
58 int autoChecking = YES; /* default */
59 int newMessagesOnly = YES; /* default */
60 char config_file[256] = "not-defined";
62 void usage(void);
63 void printversion(void);
64 void wmCheckMail_routine(int, char **);
65 int readConfigFile( char *filename );
67 void BlitString(char *name, int x, int y);
68 void BlitNum(int num, int x, int y);
71 /********************************
72 * Main Program
73 ********************************/
74 int main(int argc, char *argv[]) {
76 int i;
78 ProgName = argv[0];
79 if (strlen(ProgName) >= 5)
80 ProgName += (strlen(ProgName) - 5);
82 for (i=1; i<argc; i++) {
83 char *arg = argv[i];
85 if (*arg=='-') {
86 switch (arg[1]) {
87 case 'd' :
88 if (strcmp(arg+1, "display")) {
89 usage();
90 exit(1);
92 break;
93 case 'g' :
94 if (strcmp(arg+1, "geometry")) {
95 usage();
96 exit(1);
98 break;
99 case 'v' :
100 printversion();
101 exit(0);
102 break;
103 case 'c' :
104 if (argc > (i+1))
106 strcpy(config_file, argv[i+1]);
107 i++;
109 break;
110 default:
111 usage();
112 exit(0);
113 break;
118 wmCheckMail_routine(argc, argv);
120 return 0;
124 * wmCheckMail_routine : This function is used to set up the X display
125 * for wmpop3 and check for mail every n number of minutes.
128 void wmCheckMail_routine(int argc, char **argv){
130 int mailWaiting = 0;
131 int totalMessages = 0;
132 long startTime = 0;
133 long nextCheckTime = 0;
134 int forcedCheck = NO;
135 int buttonStatus =-1;
136 int i;
137 XEvent Event;
138 Pop3 pc;
140 if( !strcmp( config_file, "not-defined") )
141 sprintf(config_file, "%s/.wmpop3rc", getenv("HOME"));
143 if( readConfigFile(config_file) == -1){
144 exit(0);
146 pc =pop3Create(); /* initialize Pop3 ADT */
148 /* Set up timer for checking mail */
149 startTime = time(0);
150 nextCheckTime = 0; /* Make 0, so it checks for mail on start */
153 createXBMfromXPM(wminet_mask_bits, wmpop3_xpm
154 , wminet_mask_width, wminet_mask_height);
156 openXwindow(argc, argv, wmpop3_xpm, wminet_mask_bits
157 , wminet_mask_width, wminet_mask_height);
159 AddMouseRegion(0, 18, 49, 45, 59 ); /* middle button */
160 AddMouseRegion(1, 5 , 49, 17, 59 ); /* left button */
161 AddMouseRegion(2, 46, 49, 59, 59 ); /* right button */
162 AddMouseRegion(3, 2, 2, 58, 33); /* main area */
163 AddMouseRegion(4, 5, 46, 58, 56);
165 /* Check if Autochecking is on or off */
166 if(autoChecking == NO ){
167 copyXPMArea(67, 7 ,4 ,4 ,52 ,7 );
170 RedrawWindow();
173 while (1){
174 if( (((time(0) > nextCheckTime) || (nextCheckTime == 0))
175 && ( autoChecking == YES))
176 || (forcedCheck == YES)){
177 if(pop3MakeConnection(pc,popserver,serverport) == -1){
178 mailWaiting = -1;
180 else if( pop3Login(pc, username,password) == -1 ){
181 mailWaiting = -1;
184 else if( (pop3CheckMail(pc)) == -1){
185 mailWaiting = -1;
187 else{
188 mailWaiting = pop3GetNumberOfUnreadMessages(pc);
189 totalMessages = pop3GetTotalNumberOfMessages(pc);
191 if( forcedCheck == YES )
192 forcedCheck = NO;
193 pop3Quit(pc);
194 nextCheckTime = time(0) + (mailCheckDelay * SEC_IN_MIN);
197 waitpid(0, NULL, WNOHANG);
199 if( mailWaiting == -1 ){
200 /* Error connecting to pop server */
201 BlitString("::error::", 5, (11*(4-1)) + 5);
203 else{
204 if( newMessagesOnly == YES ){
205 /* Show unread messages only */
206 BlitString("mesg : ", 5, (11*(4-1)) + 5);
207 BlitNum(mailWaiting, 45, (11*(4-1)) + 5);
208 }else{
209 /* Show unread Mesaages and Read messages */
210 BlitString(" / ", 5, (11*(4-1)) + 5);
211 BlitNum(mailWaiting, 15, (11*(4-1)) + 5);
212 BlitNum(totalMessages, 39, (11*(4-1)) + 5);
215 if( mailWaiting == 0)
216 copyXPMArea(72, 33, 45, 28, 4, 4 );
217 else
218 copyXPMArea(72, 2, 45, 28, 4, 4 );
221 RedrawWindow();
223 /* X Events */
224 while (XPending(display)){
225 XNextEvent(display, &Event);
226 switch (Event.type)
228 case Expose:
229 RedrawWindow();
230 break;
231 case DestroyNotify:
232 XCloseDisplay(display);
233 exit(0);
234 break;
235 case ButtonPress:
236 i = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
237 buttonStatus = i;
238 if (buttonStatus == i && buttonStatus >= 0){
239 switch (buttonStatus){
240 case 0 : /* Middle button pressed */
241 copyXPMArea(128,29 ,27 ,8 ,18 ,49 );
242 nextCheckTime = 0;
243 break;
244 case 1 : /* Left button pressed */
245 copyXPMArea(128,16 ,11 ,8 ,6 ,49 );
246 break;
247 case 2: /* right button pressed */
248 copyXPMArea(128,2 ,11 ,8 ,46 ,49 );
249 /* change view on # of messages */
250 if( newMessagesOnly == YES )
251 newMessagesOnly = NO;
252 else
253 newMessagesOnly = YES;
254 break;
255 case 3:
256 execCommand(mailclient);
257 /* Recheck mail after 30 seconds,
258 * This gives time for the mail
259 * browser to check the account */
260 nextCheckTime = time(0) + 30;
261 break;
265 break;
266 case ButtonRelease:
267 i = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
269 if (buttonStatus == i && buttonStatus >= 0){
270 switch (buttonStatus){
271 case 0 : /* Middle button */
272 copyXPMArea(128,39 ,27 ,8 ,18 ,49 );
273 forcedCheck = YES;
274 break;
275 case 1 : /* Left Button */
276 copyXPMArea(144,16 ,11 ,8 ,5 ,49 );
277 if( autoChecking == YES){
278 autoChecking = NO;
279 /* Activate Red Led */
280 copyXPMArea(67, 7 ,4 ,4 ,52 ,7 );
281 }else{
282 /* Activate Green Led */
283 copyXPMArea(67, 2 ,4 ,4 ,52 ,7 );
284 autoChecking = YES;
286 break;
287 case 2: /* Right Button */
288 copyXPMArea(144,3 ,11 ,8 ,46 ,49 );
289 break;
290 case 3:
291 break;
294 buttonStatus = -1;
295 RedrawWindow();
296 break;
299 usleep(100000L);
305 * usage : Prints proper command parameters of wmpop3.
307 void usage(void)
309 fprintf(stderr, "\nWMPop3 - Scott Holden <scotth@thezone.net>\n\n");
310 fprintf(stderr, "usage:\n");
311 fprintf(stderr, " -display <display name>\n");
312 fprintf(stderr, " -geometry +XPOS+YPOS initial window position\n");
313 fprintf(stderr, " -c <filename> use specified config file\n");
314 fprintf(stderr, " -h this help screen\n");
315 fprintf(stderr, " -v print the version number\n");
316 fprintf(stderr, "\n");
320 * printversion : This function is used to print the version info
321 * to standard output.
323 void printversion(void)
325 fprintf(stderr, "wmpop3 v%s\n", WMPOP3_VERSION);
328 // Blits a string at given co-ordinates
329 void BlitString(char *name, int x, int y)
331 int i;
332 int c;
333 int k;
335 k = x;
336 for (i=0; name[i]; i++)
338 c = toupper(name[i]);
339 if (c >= 'A' && c <= 'Z')
340 { /* its a letter */
341 c -= 'A';
342 copyXPMArea(c * 6, 74, 6, 8, k, y);
343 k += 6;
345 else if( c == ' ' ){
346 c = 14;
347 copyXPMArea(73, 64, 6, 8, k, y);
348 k += 6;
350 else if( c == '/' ){
351 c = 14;
352 copyXPMArea(68, 64, 6, 8, k, y);
353 k += 6;
355 else
356 { /* its a number or symbol */
357 c -= '0';
358 copyXPMArea(c * 6, 64, 6, 8, k, y);
359 k += 6;
366 /* Blits number to give coordinates.. two 0's, right justified */
369 void BlitNum(int num, int x, int y)
371 char buf[1024];
372 int newx=x;
374 if (num > 99)
376 newx -= CHAR_WIDTH;
379 if (num > 999)
381 newx -= CHAR_WIDTH;
384 sprintf(buf, "%02i", num);
386 BlitString(buf, newx, y);
390 int readConfigFile( char *filename ){
391 char buf[256];
392 char temp[32];
393 char *ptr = 0;
394 FILE *fp;
396 if( (fp = fopen( filename, "r")) == 0 ){
397 sprintf(config_file, "%s/.wmpop3rc", getenv("HOME"));
398 printf("-Config file does not exit : %s\n",config_file);
399 printf("+Trying to create new config file.\n");
400 if((fp = fopen(config_file,"w")) == 0){
401 printf("-Error creating new config file\n");
402 return -1;
404 fprintf(fp,"# Replace all < > with appropriate data\n#\n");
405 fprintf(fp,"popserver < pop3 server name >\n");
406 fprintf(fp,"port 110 # default port\n");
407 fprintf(fp,"username < pop3 login name >\n");
408 fprintf(fp,"password < pop3 password >\n");
409 fprintf(fp,"autochecking 0 # 1 enables, 0 disables\n");
410 fprintf(fp,"mailcheckdelay 10 # default mail check time in minutes\n");
411 fprintf(fp,"viewallmessages 0 # 0 Shows both read and unread messages\n");
412 fprintf(fp,"# and 1 shows only unread messages\n");
413 fprintf(fp,"mailclient pine # default mail client\n");
414 printf("+New config file created : ~/.wmpop3rc\n\n");
415 printf("+ ~/.wmpop3rc must be configured before running wmpop3.\n");
416 fclose(fp);
417 return -1;
420 while( fgets(buf, 256, fp) != 0){
422 ptr = strtok( buf," \n" );
424 if( ( ptr != 0) && (ptr[0] != '#') ){
425 if( !strcmp(ptr, "username") ){
426 ptr = strtok( 0, " \n");
427 if( ptr == 0){
428 printf("Invalid UserName.\n");
429 return -1;
431 strcpy(username, ptr);
433 else if( !strcmp( ptr, "password") ){
434 ptr = strtok( 0, " \n");
435 if( ptr == 0){
436 printf("Invalid password.\n");
437 return -1;
439 strcpy(password, ptr);
441 else if( !strcmp( ptr, "popserver") ){
442 ptr = strtok( 0, " \n");
443 if( ptr == 0){
444 printf("Invalid popserver address.\n");
445 return -1;
447 strcpy(popserver, ptr);
449 else if( !strcmp( ptr, "mailclient") ){
450 ptr = strtok( 0, " \n");
451 if( ptr == 0){
452 printf("Invalid mailclient.\n");
453 return -1;
455 strcpy(mailclient, ptr);
457 else if( !strcmp( ptr, "port") ){
458 ptr = strtok( 0, " \n");
459 if( ptr == 0){
460 printf("Invalid popserver port number.\n");
461 return -1;
463 if( sscanf(ptr,"%[0123456789]",temp) == 0)
464 serverport = 110;
465 else
466 serverport = atoi(temp);
468 else if( !strcmp( ptr, "viewallmessages") ){
469 ptr = strtok( 0, " \n");
470 if( ptr == 0){
471 printf("Invalid number. ( viewallmessages )\n");
472 return -1;
474 if( sscanf(ptr,"%[0123456789]",temp) != 0)
475 newMessagesOnly = atoi(temp);
477 else if( !strcmp( ptr, "mailcheckdelay") ){
478 ptr = strtok( 0, " \n");
479 if( ptr == 0){
480 printf("Invalid delay time.\n");
481 return -1;
483 if( sscanf(ptr,"%[0123456789]",temp) != 0)
484 mailCheckDelay = atoi(temp);
486 else if( !strcmp( ptr, "autochecking") ){
487 ptr = strtok( 0, " \n");
488 if( ptr == 0){
489 printf("Invalid value.\n");
490 return -1;
492 if( sscanf(ptr,"%[0123456789]",temp) != 0)
493 autoChecking = atoi(temp);
495 else
496 printf("Unknown indenifier : %s\n",ptr);
499 fclose(fp);
500 return 0;