4 * by Scott Holden (scotth@thezone.net)
6 * Contains some code from WMInet Dockable app.
7 * - BlitNum and BlitString
20 #include <sys/socket.h>
22 #include <sys/param.h>
23 #include <sys/types.h>
25 #include <sys/ioctl.h>
29 #include <X11/extensions/shape.h>
31 #include <libdockapp/misc.h>
32 #include <libdockapp/wmgeneral.h>
33 #include "Pop3Client.h"
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"
52 char mailclient
[32] = "pine";
57 int mailCheckDelay
= 10; /* default */
58 int autoChecking
= YES
; /* default */
59 int newMessagesOnly
= YES
; /* default */
60 char config_file
[256] = "not-defined";
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 /********************************
73 ********************************/
74 int main(int argc
, char *argv
[]) {
79 if (strlen(ProgName
) >= 5)
80 ProgName
+= (strlen(ProgName
) - 5);
82 for (i
=1; i
<argc
; i
++) {
88 if (strcmp(arg
+1, "display")) {
94 if (strcmp(arg
+1, "geometry")) {
106 strcpy(config_file
, argv
[i
+1]);
118 wmCheckMail_routine(argc
, argv
);
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
){
131 int totalMessages
= 0;
133 long nextCheckTime
= 0;
134 int forcedCheck
= NO
;
135 int buttonStatus
=-1;
140 if( !strcmp( config_file
, "not-defined") )
141 sprintf(config_file
, "%s/.wmpop3rc", getenv("HOME"));
143 if( readConfigFile(config_file
) == -1){
146 pc
=pop3Create(); /* initialize Pop3 ADT */
148 /* Set up timer for checking mail */
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 );
174 if( (((time(0) > nextCheckTime
) || (nextCheckTime
== 0))
175 && ( autoChecking
== YES
))
176 || (forcedCheck
== YES
)){
177 if(pop3MakeConnection(pc
,popserver
,serverport
) == -1){
180 else if( pop3Login(pc
, username
,password
) == -1 ){
184 else if( (pop3CheckMail(pc
)) == -1){
188 mailWaiting
= pop3GetNumberOfUnreadMessages(pc
);
189 totalMessages
= pop3GetTotalNumberOfMessages(pc
);
191 if( forcedCheck
== YES
)
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);
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);
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 );
218 copyXPMArea(72, 2, 45, 28, 4, 4 );
224 while (XPending(display
)){
225 XNextEvent(display
, &Event
);
232 XCloseDisplay(display
);
236 i
= CheckMouseRegion(Event
.xbutton
.x
, Event
.xbutton
.y
);
238 if (buttonStatus
== i
&& buttonStatus
>= 0){
239 switch (buttonStatus
){
240 case 0 : /* Middle button pressed */
241 copyXPMArea(128,29 ,27 ,8 ,18 ,49 );
244 case 1 : /* Left button pressed */
245 copyXPMArea(128,16 ,11 ,8 ,6 ,49 );
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
;
253 newMessagesOnly
= YES
;
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;
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 );
275 case 1 : /* Left Button */
276 copyXPMArea(144,16 ,11 ,8 ,5 ,49 );
277 if( autoChecking
== YES
){
279 /* Activate Red Led */
280 copyXPMArea(67, 7 ,4 ,4 ,52 ,7 );
282 /* Activate Green Led */
283 copyXPMArea(67, 2 ,4 ,4 ,52 ,7 );
287 case 2: /* Right Button */
288 copyXPMArea(144,3 ,11 ,8 ,46 ,49 );
305 * usage : Prints proper command parameters of wmpop3.
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
)
336 for (i
=0; name
[i
]; i
++)
338 c
= toupper(name
[i
]);
339 if (c
>= 'A' && c
<= 'Z')
342 copyXPMArea(c
* 6, 74, 6, 8, k
, y
);
347 copyXPMArea(73, 64, 6, 8, k
, y
);
352 copyXPMArea(68, 64, 6, 8, k
, y
);
356 { /* its a number or symbol */
358 copyXPMArea(c
* 6, 64, 6, 8, k
, y
);
366 /* Blits number to give coordinates.. two 0's, right justified */
369 void BlitNum(int num
, int x
, int y
)
384 sprintf(buf
, "%02i", num
);
386 BlitString(buf
, newx
, y
);
390 int readConfigFile( char *filename
){
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");
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");
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");
428 printf("Invalid UserName.\n");
431 strcpy(username
, ptr
);
433 else if( !strcmp( ptr
, "password") ){
434 ptr
= strtok( 0, " \n");
436 printf("Invalid password.\n");
439 strcpy(password
, ptr
);
441 else if( !strcmp( ptr
, "popserver") ){
442 ptr
= strtok( 0, " \n");
444 printf("Invalid popserver address.\n");
447 strcpy(popserver
, ptr
);
449 else if( !strcmp( ptr
, "mailclient") ){
450 ptr
= strtok( 0, " \n");
452 printf("Invalid mailclient.\n");
455 strcpy(mailclient
, ptr
);
457 else if( !strcmp( ptr
, "port") ){
458 ptr
= strtok( 0, " \n");
460 printf("Invalid popserver port number.\n");
463 if( sscanf(ptr
,"%[0123456789]",temp
) == 0)
466 serverport
= atoi(temp
);
468 else if( !strcmp( ptr
, "viewallmessages") ){
469 ptr
= strtok( 0, " \n");
471 printf("Invalid number. ( viewallmessages )\n");
474 if( sscanf(ptr
,"%[0123456789]",temp
) != 0)
475 newMessagesOnly
= atoi(temp
);
477 else if( !strcmp( ptr
, "mailcheckdelay") ){
478 ptr
= strtok( 0, " \n");
480 printf("Invalid delay time.\n");
483 if( sscanf(ptr
,"%[0123456789]",temp
) != 0)
484 mailCheckDelay
= atoi(temp
);
486 else if( !strcmp( ptr
, "autochecking") ){
487 ptr
= strtok( 0, " \n");
489 printf("Invalid value.\n");
492 if( sscanf(ptr
,"%[0123456789]",temp
) != 0)
493 autoChecking
= atoi(temp
);
496 printf("Unknown indenifier : %s\n",ptr
);