1 //  MaCoPiX = Mascot Construnctive Pilot for X
2 //                                (ActX / Gtk+ Evolution)
3 //
4 //
5 //     mail.c
6 //     Biff functions for MaCoPiX (POP3 /APOP and local spool)
7 //
8 //                            Copyright 2002-2008  K.Chimari
9 //                                     http://rosegray.sakura.ne.jp/
10 //
11 //
12 //  This program is free software; you can redistribute it and/or modify
13 //  it under the terms of the GNU General Public License as published by
14 //  the Free Software Foundation; either version 2 of the License, or
15 //  (at your option) any later version.
16 //
17 //  This program is distributed in the hope that it will be useful,
18 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 //  GNU General Public License for more details.
21 //
22 //  You should have received a copy of the GNU General Public License
23 //  along with this program; if not, write to the Free Software
24 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
25 //
26 
27 
28 #include "main.h"
29 
30 #ifdef USE_BIFF
31 
32 #ifdef USE_WIN32
33 #include <windows.h>
34 #endif
35 
36 #include <dirent.h>
37 
38 #ifdef HAVE_SYS_WAIT_H
39 #include <sys/wait.h>
40 #endif
41 
42 #include <signal.h>
43 #include "codeconv.h"
44 
45 #define BUFFSIZE 200
46 #define BUFFSIZE5 1000
47 #define HEADER_MAX 256
48 #define MIME_BUFFSIZE 1024
49 
50 
51 void biff_init();
52 
53 gint SetMailChecker();
54 gint ResetMailChecker();
55 gboolean MailChecker();
56 
57 void make_biff_pix();
58 void display_biff_balloon();
59 void remap_biff_pix();
60 
61 void mail_check();
62 void mail_check_mbox();
63 void mail_check_qmail();
64 void mail_check_pop3();
65 #ifdef USE_WIN32
66 unsigned __stdcall get_pop3();
67 #else
68 int get_pop3();
69 #endif
70 gchar * fs_get_mbox();
71 gchar * fs_get_procmail();
72 gchar * fs_get_qmail();
73 void fs_get_pop3();
74 void pop3_error();
75 #ifndef USE_WIN32
76 void pop3_signal();
77 void pop3_data_read();
78 #endif
79 char* strbuf();
80 
81 
82 void make_fs_max();
83 void create_biff_dialog();
84 static void close_biff();
85 static void mailer_start();
86 
87 gchar* set_mhdir();
88 
89 void mail_arg_init();
90 
91 void strip_last_ret();
92 
93 extern void pop_debug_print (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
94 
95 //
96 extern void ext_play();
97 extern void sound_play();
98 
99 extern GtkWidget *biff_pix;
100 extern typMascot *Mascot;
101 extern gboolean flag_balloon;
102 
103 
104 extern gint dw_configure_biff_pix();
105 extern gint dw_expose_biff_pix();
106 extern void biff_drag_begin();
107 extern void biff_drag_end();
108 extern void biff_window_motion();
109 
110 extern void conv_unmime_header();
111 extern void conv_unmime_header_overwrite();
112 
113 extern void create_pop_pass_dialog();
114 
115 #ifdef __GTK_STOCK_H__
116 extern GtkWidget* gtkut_button_new_from_stock();
117 #endif
118 
119 #ifdef USE_SSL
120 extern gint ssl_manager_verify_cert();
121 #endif
122 
123 extern void my_signal_connect();
124 extern gboolean my_main_iteration();
125 
126 // global argument
127 #ifndef USE_WIN32
128 int pop3_fd[2];
129 #endif
130 
131 gchar *pop_froms;
132 time_t former_newest;
133 
134 
biff_init(typMascot * mascot)135 void biff_init(typMascot *mascot)
136 {
137   pop_debug_print("\nBiff init\n");
138 
139   mascot->mail.size=0;
140   mascot->mail.last_check=0;
141   mascot->mail.count=0;
142   mascot->mail.fetched_count = 0;    // POP3 fetched fs this access
143   mascot->mail.displayed_count = 0;  // POP3 displayed fs
144   mascot->mail.spam_count = 0;       // SPAM count
145   mascot->mail.status = NO_MAIL;
146   mascot->mail.pop3_fs_status = POP3_OK_NORMAL;
147   if(mascot->mail.last_f) g_free(mascot->mail.last_f);
148   mascot->mail.last_f= NULL;
149   if(mascot->mail.last_s) g_free(mascot->mail.last_s);
150   mascot->mail.last_s=NULL;
151   if(pop_froms) g_free(pop_froms);
152   pop_froms=strbuf(NULL);
153 #ifdef __GTK_TOOLTIP_H__
154   //gtk_widget_set_tooltip_text(mascot->mail.e_draw,
155   //gtk_widget_set_tooltip_text(mascot->mail.e_draw,
156   gtk_widget_set_tooltip_text(biff_pix,
157 			      _("Initializing Biff..."));
158 #else
159   gtk_tooltips_set_tip(GTK_TOOLTIPS(mascot->mail.tooltips),
160 		       //mascot->mail.e_draw,
161 		       biff_pix,
162 		       _("Initializing Biff..."),
163 		       NULL);
164 #endif
165 
166   if(mascot->mail.tooltips_fl){
167 #ifdef __GTK_TOOLTIP_H__
168     //gtk_widget_set_tooltip_text(mascot->mail.e_draw,NULL);
169     gtk_widget_set_tooltip_text(biff_pix,NULL);
170 #else
171     gtk_tooltips_enable(mascot->mail.tooltips);
172 #endif
173   }
174   else{
175 #ifdef __GTK_TOOLTIP_H__
176     //gtk_widget_set_tooltip_text(mascot->mail.e_draw,NULL);
177     gtk_widget_set_tooltip_text(biff_pix,NULL);
178 #else
179     gtk_tooltips_disable(mascot->mail.tooltips);
180 #endif
181   }
182   mascot->mail.pop_readed=FALSE;
183 
184   if((mascot->mail.type==MAIL_POP3)||(mascot->mail.type==MAIL_APOP)){
185     if(mascot->mail.pop_pass==NULL){
186       if((mascot->mail.pop_server!=NULL)&&(mascot->mail.pop_id!=NULL)){
187 	create_pop_pass_dialog();
188 	// Get Pop Pass from GUI
189       }
190     }
191   }
192 
193   if(mascot->mail.ssl_cert_skip)
194     mascot->mail.ssl_cert_res=SSL_CERT_ACCEPT;
195   else mascot->mail.ssl_cert_res=SSL_CERT_NONE;
196   if(mascot->mail.ssl_sub) g_free(mascot->mail.ssl_sub);
197   mascot->mail.ssl_sub=NULL;
198   if(mascot->mail.ssl_iss) g_free(mascot->mail.ssl_iss);
199   mascot->mail.ssl_iss=NULL;
200   mascot->mail.ssl_verify=0;
201 }
202 
203 
SetMailChecker(gpointer gdata)204 gint SetMailChecker(gpointer gdata){
205   typMascot *mascot;
206 
207   mascot=(typMascot *)gdata;
208 
209   if(mascot->mail.proc_id>0){
210     g_source_remove((guint)mascot->mail.proc_id);
211     fflush(stdin);
212     fflush(stdout);
213   }
214   gtk_widget_unmap(biff_pix);
215 
216   if(mascot->mail.flag){
217     biff_init(mascot);
218     MailChecker(mascot);
219     mascot->mail.proc_id
220       =(gint)g_timeout_add((guint32)(mascot->mail.interval*1000),
221 			   (GSourceFunc)MailChecker,
222 			   (gpointer)mascot);
223 
224     pop_debug_print("proc_id=%d\n",mascot->mail.proc_id);
225   }
226 }
227 
ResetMailChecker(gpointer gdata)228 gint ResetMailChecker(gpointer gdata){
229   typMascot *mascot;
230 
231   mascot=(typMascot *)gdata;
232 
233   if(mascot->mail.pop_child_fl){
234     return (0);
235   }
236 
237   if(mascot->mail.proc_id>0){
238     g_source_remove((guint)mascot->mail.proc_id);
239     fflush(stdin);
240     fflush(stdout);
241   }
242   gtk_widget_unmap(biff_pix);
243 
244   if(mascot->mail.flag){
245     MailChecker(mascot);
246     mascot->mail.proc_id
247       =(gint)g_timeout_add((guint32)(mascot->mail.interval*1000),
248 			   (GSourceFunc)MailChecker,
249 			   (gpointer)mascot);
250 
251     pop_debug_print("proc_id=%d\n",mascot->mail.proc_id);
252   }
253 
254   return (0);
255 }
256 
257 
MailChecker(gpointer gdata)258 gboolean MailChecker(gpointer gdata){
259   typMascot *mascot;
260 
261   mascot=(typMascot *)gdata;
262 
263   if(mascot->flag_menu) return(TRUE);
264 
265   gdk_flush();
266 
267   ext_play(mascot,mascot->mail.polling);
268   mail_check(mascot);
269 
270   gdk_flush();
271 
272   if((mascot->mail.type!=MAIL_POP3)&&(mascot->mail.type!=MAIL_APOP))
273     display_biff_balloon(mascot);
274 
275   gdk_flush();
276   return(TRUE);
277 }
278 
279 
make_biff_pix(typMascot * mascot)280 void make_biff_pix(typMascot *mascot){
281 
282   biff_pix = gtk_window_new(GTK_WINDOW_POPUP);
283 #ifdef USE_GTK2
284   gtk_window_set_accept_focus(GTK_WINDOW(biff_pix),FALSE);
285 #endif
286   gtk_widget_set_app_paintable(biff_pix, TRUE);
287   gtk_widget_set_events(GTK_WIDGET (biff_pix),
288 			GDK_FOCUS_CHANGE_MASK |
289 			GDK_BUTTON_MOTION_MASK |
290 			GDK_BUTTON_RELEASE_MASK |
291 			GDK_BUTTON_PRESS_MASK |
292 			GDK_EXPOSURE_MASK);
293   gtk_widget_realize(biff_pix);
294 #ifdef USE_GTK2
295   gtk_window_set_resizable(GTK_WINDOW(biff_pix),TRUE);
296 #endif
297   gdk_window_set_decorations(biff_pix->window, 0);
298 #ifndef USE_WIN32
299   /* gdk_window_set_override_redirect is not implemented (for warning) */
300   gdk_window_set_override_redirect(biff_pix->window,TRUE);
301 #endif
302 
303   my_signal_connect(biff_pix, "configure_event",
304 		    dw_configure_biff_pix, (gpointer)Mascot);
305   my_signal_connect(biff_pix, "expose_event",
306 		    dw_expose_biff_pix, (gpointer)Mascot);
307   my_signal_connect(biff_pix, "button_press_event",
308 		    biff_drag_begin, (gpointer)Mascot);
309   my_signal_connect(biff_pix, "button_release_event",
310 		    biff_drag_end, (gpointer)Mascot);
311   my_signal_connect(biff_pix, "motion_notify_event",
312 		    biff_window_motion, (gpointer)Mascot);
313 
314 #ifdef USE_GTK2
315   gtk_window_resize (GTK_WINDOW(biff_pix), 1, 1);
316 #endif
317 }
318 
319 
display_biff_balloon(typMascot * mascot)320 void display_biff_balloon(typMascot *mascot)
321 {
322   gchar *text_tip=NULL;
323 
324   if(mascot->mail.tooltips_fl){
325 
326     if(mascot->mail.count!=0){
327       if((mascot->mail.type==MAIL_POP3)||(mascot->mail.type==MAIL_APOP)){
328 	if(mascot->mail.spam_count!=0){
329 	  if((mascot->mail.last_f!=NULL)&&(mascot->mail.last_s!=NULL))
330 	    text_tip=g_strdup_printf(_("You have %d mails (%d SPAMs).\n [Latest Mail]\n  From: %s\n  Subject: %s"),
331 				     mascot->mail.count,mascot->mail.spam_count,
332 				     mascot->mail.last_f,mascot->mail.last_s);
333 	}
334 	else{
335 	  if((mascot->mail.last_f!=NULL)&&(mascot->mail.last_s!=NULL))
336 	    text_tip=g_strdup_printf(_("You have %d mails.\n [Latest Mail]\n  From: %s\n  Subject: %s"),
337 				     mascot->mail.count,
338 				     mascot->mail.last_f,mascot->mail.last_s);
339 	}
340 	if(text_tip){
341 #ifdef __GTK_TOOLTIP_H__
342 	  gtk_widget_set_tooltip_text(biff_pix,
343 				      //gtk_widget_set_tooltip_text(mascot->mail.e_draw,
344 				      text_tip);
345 #else
346 	  gtk_tooltips_set_tip(GTK_TOOLTIPS(mascot->mail.tooltips),
347 			       biff_pix,
348 			       //mascot->mail.e_draw,
349 			       text_tip,NULL);
350 #endif
351 	  g_free(text_tip);
352 	}
353       }
354       else{
355 	text_tip=g_strdup_printf(_("You have %d mails."),mascot->mail.count);
356 #ifdef __GTK_TOOLTIP_H__
357 	//	gtk_widget_set_tooltip_text(mascot->mail.e_draw,
358 	gtk_widget_set_tooltip_text(biff_pix,
359 				    text_tip);
360 #else
361 	gtk_tooltips_set_tip(GTK_TOOLTIPS(mascot->mail.tooltips),
362 			     //mascot->mail.e_draw,
363 			     biff_pix,
364 			     text_tip,NULL);
365 #endif
366 	g_free(text_tip);
367       }
368     }
369     else{
370 #ifdef __GTK_TOOLTIP_H__
371       //gtk_widget_set_tooltip_text(mascot->mail.e_draw,
372       gtk_widget_set_tooltip_text(biff_pix,
373 				  _("You have no mails."));
374 #else
375       gtk_tooltips_set_tip(GTK_TOOLTIPS(mascot->mail.tooltips),
376 			   //mascot->mail.e_draw,
377 			   biff_pix,
378 			   _("You have no mails."),
379 			   NULL);
380 #endif
381     }
382   }
383 
384   if(mascot->mail.status == POP3_ERROR){
385     gtk_widget_unmap(biff_pix);
386 
387     if(flag_balloon==FALSE){
388       mascot->balseq=0;
389       mascot->bal_mode=BALLOON_POPERROR;
390       DoBalloon(mascot);
391       flag_balloon=TRUE;
392       mascot->mail.status = NO_MAIL;
393     }
394   }
395   else if(mascot->mail.status == POP3_SSL_CERT){
396     gtk_widget_unmap(biff_pix);
397 
398 #ifdef USE_SSL
399     mascot->mail.ssl_cert_res=ssl_manager_verify_cert(mascot);
400     if(mascot->mail.ssl_cert_res==SSL_CERT_ACCEPT)
401       ResetMailChecker((gpointer)mascot);
402 #endif
403   }
404   else if(mascot->mail.status != NO_MAIL){
405     MoveBiffPix(mascot,mascot->x,mascot->y);
406     gtk_widget_map(biff_pix);
407   }
408   else{
409     gtk_widget_unmap(biff_pix);
410   }
411   if(mascot->mail.status==NEW_MAIL){
412     sound_play(mascot,mascot->mail.sound);
413 
414     if((mascot->mail.word)&&(flag_balloon==FALSE)) {
415       mascot->balseq=0;
416       mascot->bal_mode=BALLOON_MAIL;
417       DoBalloon(mascot);
418       flag_balloon=TRUE;
419       if((mascot->mail.type==MAIL_POP3)||(mascot->mail.type==MAIL_APOP)){
420 	mascot->mail.status=KEEP_NEW_MAIL;
421       }
422     }
423   }
424 }
425 
426 
remap_biff_pix(typMascot * mascot)427 void remap_biff_pix(typMascot *mascot){
428   if(!mascot->mail.flag) return;
429   if(mascot->mail.status == (POP3_ERROR|POP3_SSL_CERT)){
430     gtk_widget_unmap(biff_pix);
431   }
432   else if(mascot->mail.status != NO_MAIL){
433     MoveBiffPix(mascot,mascot->x,mascot->y);
434     gtk_widget_map(biff_pix);
435   }
436   else{
437     gtk_widget_unmap(biff_pix);
438   }
439 }
440 
mail_check(typMascot * mascot)441 void mail_check(typMascot *mascot){
442 #ifndef USE_WIN32
443   static struct sigaction act;
444 #endif
445 
446   switch(mascot->mail.type){
447   case MAIL_LOCAL:
448   case MAIL_PROCMAIL:
449     mail_check_mbox(mascot);
450     break;
451   case MAIL_POP3:
452   case MAIL_APOP:
453 #ifndef USE_WIN32
454     act.sa_handler=pop3_signal;
455     sigemptyset(&act.sa_mask);
456     act.sa_flags=0;
457     sigaction(SIGUSR1, &act,0);
458 #endif
459     mail_check_pop3(mascot);
460     break;
461   case MAIL_QMAIL:
462     mail_check_qmail(mascot);
463     break;
464   }
465 
466 }
467 
468 
mail_check_mbox(typMascot * mascot)469 void mail_check_mbox(typMascot *mascot){
470     struct stat t;
471     DIR *dp;
472     struct dirent *entry;
473     time_t newest_time=0;
474     int filenum=0;
475     int mc=0;
476     FILE *fp;
477     char buf[BUFFSIZE];
478     int wo_spam;
479 
480     if (!stat(mascot->mail.file, &t)){
481 
482       if(t.st_size == 0){ // �ᥤ��ե����륵����������
483 	mascot->mail.status = NO_MAIL;
484 	mascot->mail.count = 0;
485       }
486       else{
487 	wo_spam = mascot->mail.count - mascot->mail.spam_count;
488 
489 	if (t.st_size < mascot->mail.size){
490 	  // mailbox smaller in size; some mails have been deleted
491 	  mascot->mail.status = OLD_MAIL;
492 	}
493 	else if (t.st_atime > t.st_mtime){
494 	  // mailbox read after most recent write
495 	  mascot->mail.status = OLD_MAIL;
496 	}
497 	else if (t.st_size > mascot->mail.size){
498 	  // mailbox modified after most recent read, and larger in size
499 	  // this implies the arrival of some new mails
500 	  mascot->mail.status = NEW_MAIL;
501 	  // get mail count
502 	  if((fp=fopen(mascot->mail.file,"r"))!= NULL){
503 	    do{
504 	      if(fgets(buf,BUFFSIZE-1,fp)==NULL) break;
505 	      if(strncmp(buf,"From ",5)==0){
506 		mc++;
507 	      }
508 	    } while (1);
509 	    fclose(fp);
510 	    mascot->mail.count = mc;
511 	  }
512 	}
513 	else if ((t.st_size == mascot->mail.size)&&
514 		 ((mascot->mail.status == NEW_MAIL)||
515 		  (mascot->mail.status ==KEEP_NEW_MAIL))){
516 	  mascot->mail.status=KEEP_NEW_MAIL;
517 	}
518 	else{
519 	  mascot->mail.status = NO_MAIL;          // no such mailbox
520 	  mascot->mail.count = 0;
521 	}
522 
523 	if( mc == mascot->mail.spam_count){
524 	  mascot->mail.status = NO_MAIL; // SPAM�����ΤȤ�
525 	}
526 	else if((mc - mascot->mail.spam_count) == wo_spam){
527 	  mascot->mail.status = KEEP_NEW_MAIL; // �������Τ�SPAM�������ä��Ȥ�
528 	}
529       }
530     }
531     else{ // �ᥤ��ե����뤬¸�ߤ��ʤ��Ȥ�
532       mascot->mail.status = NO_MAIL;
533       mascot->mail.count = 0;
534     }
535     mascot->mail.size = t.st_size;
536 }
537 
538 
mail_check_qmail(typMascot * mascot)539 void mail_check_qmail(typMascot *mascot){
540     struct stat t;
541     DIR *dp;
542     struct dirent *entry;
543     char tmp[256];
544     time_t newest_time=0;
545     int filenum=0;
546     int wo_spam;
547 
548     if ((dp=opendir(mascot->mail.file))==NULL){
549       mascot->mail.status = NO_MAIL;
550       mascot->mail.count = 0;
551       return;
552     }
553 
554     while((entry=readdir(dp))!=NULL){
555       if(entry->d_name[0]!='.'){
556 	sprintf(tmp,"%s/%s",mascot->mail.file,entry->d_name);
557 	if (!stat(tmp, &t)){
558 	  filenum++;
559 	  if (t.st_mtime>newest_time){
560 	    newest_time=t.st_mtime;
561 	  }
562 	}
563       }
564     }
565 
566     closedir(dp);
567 
568     if(filenum==0){
569       mascot->mail.status = NO_MAIL;
570       mascot->mail.count = 0;
571     }
572     else{
573       wo_spam = mascot->mail.count - mascot->mail.spam_count;
574 
575       if(newest_time==former_newest){
576 	mascot->mail.status = KEEP_NEW_MAIL;
577       }
578       else if(newest_time>former_newest){
579 	mascot->mail.status = NEW_MAIL;
580       }
581       else{
582 	mascot->mail.status = OLD_MAIL;
583       }
584 
585       if( filenum == mascot->mail.spam_count){
586 	mascot->mail.status = NO_MAIL; // SPAM�����ΤȤ�
587       }
588       else if((filenum - mascot->mail.spam_count) == wo_spam){
589 	mascot->mail.status = KEEP_NEW_MAIL; // �������Τ�SPAM�������ä��Ȥ�
590       }
591     }
592     former_newest=newest_time;
593     mascot->mail.count = filenum;
594 }
595 
596 
mail_check_pop3(typMascot * mascot)597 void mail_check_pop3(typMascot *mascot){
598 #ifdef USE_WIN32
599   DWORD dwErrorNumber;
600   SECURITY_ATTRIBUTES SecAttrib;    // �������ƥ�°��
601   //DWORD dwThreadID;
602   unsigned int dwThreadID;
603   HANDLE hPipe;
604   HANDLE hThread;
605   BOOL fConnected;
606 
607   if(mascot->mail.pop_child_fl){
608     return ;
609   }
610   mascot->mail.pop_child_fl=TRUE;
611 
612   /*hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)get_pop3,
613     (LPVOID)mascot, 0, &dwThreadID);*/
614   pop_debug_print("Begin POP Thread.\n");
615   hThread = (HANDLE)_beginthreadex(NULL,0,
616 				   get_pop3,
617 				   (LPVOID)mascot, 0, &dwThreadID);
618 
619   if (hThread == NULL) {
620     dwErrorNumber = GetLastError();
621     fprintf(stderr,"_beginthreadex() error(%ld).\n", dwErrorNumber);
622     //ExitProcess(dwErrorNumber);
623     pop3_error(mascot);
624     mascot->mail.pop_child_fl=FALSE;
625   }
626   else{
627     CloseHandle(hThread);
628   }
629 
630 
631 #else
632   int status = 0;
633   static int pid;
634   char *buff = NULL;
635   char buf[BUFFSIZE];
636 
637   if(mascot->mail.pop_child_fl){
638     return ;
639   }
640   mascot->mail.pop_child_fl=TRUE;
641 
642   if(pipe(pop3_fd)==-1) {
643     fprintf(stderr,"pipe open error\n");
644   }
645   waitpid(pid,0,WNOHANG);
646 
647   if( (pid = fork()) <0){
648     fprintf(stderr,"fork error\n");
649   }
650   else if(pid ==0) {
651     pop_debug_print("child\n");
652 
653     status = get_pop3(mascot);
654 
655     pop_debug_print("get pop3 end\n");
656     pop_debug_print("pop3 fs status = %d\n", mascot->mail.pop3_fs_status);
657 
658     if(buff) g_free(buff);
659     buff = strbuf(NULL);  // stand by buff
660     sprintf(buf,"%d\n",mascot->mail.status); // Write mail status
661     buff = strbuf(buf);
662     if(status == POP3_SSL_CERT){
663       if(mascot->mail.ssl_sub!=NULL){
664 	sprintf(buf,"%s\n",mascot->mail.ssl_sub);  // SSL subject
665       }
666       else{
667 	sprintf(buf,"\n");
668       }
669       buff = strbuf(buf);
670 
671       if(mascot->mail.ssl_iss!=NULL){
672 	sprintf(buf,"%s\n",mascot->mail.ssl_iss);  // SSL issuer
673       }
674       else{
675 	sprintf(buf,"\n");
676       }
677       buff = strbuf(buf);
678 
679       sprintf(buf,"%ld\n",mascot->mail.ssl_verify);  // SSL verify_result
680       buff = strbuf(buf);
681     }
682     if((status != POP3_ERROR)&&(status != POP3_SSL_CERT)){          // POP status is GOOD?
683       sprintf(buf,"%d\n",mascot->mail.count);  // write total mail count
684       buff = strbuf(buf);
685       sprintf(buf,"%d\n",mascot->mail.fetched_count);  // write fetched mail count
686       buff = strbuf(buf);
687       sprintf(buf,"%d\n",mascot->mail.pop3_fs_status);  // write POP status
688       buff = strbuf(buf);
689       sprintf(buf,"%d\n",mascot->mail.spam_count);  // write SPAM count
690       buff = strbuf(buf);
691       if(mascot->mail.last_f!=NULL){
692 	sprintf(buf,"%s\n",mascot->mail.last_f);  // Last_from
693       }
694       else{
695 	sprintf(buf,"\n");
696       }
697       buff = strbuf(buf);
698       if(mascot->mail.last_s!=NULL){
699 	sprintf(buf,"%s\n",mascot->mail.last_s);  // Last_subject
700       }
701       else{
702 	sprintf(buf,"\n");
703       }
704       buff = strbuf(buf);
705 
706       buff = strbuf(pop_froms);                // write fs data
707     }
708     if(close(STDOUT_FILENO)==-1) fprintf(stderr,"pipe close error\n");
709     if(dup2(pop3_fd[1],STDOUT_FILENO)==-1)
710        fprintf(stderr,"pipe duplicate error\n");
711     if(close(pop3_fd[0])==-1) fprintf(stderr,"pipe close error\n");
712 
713     pop_debug_print("pipe open/close end\n");
714     pop_debug_print("buff size = %d\n", strlen(buff));
715 
716     printf ("%s",buff);
717 
718     pop_debug_print("child end\n");
719 
720     kill(getppid(), SIGUSR1);  //calling pop3_data_read
721     fflush(stdout);
722     fflush(stdin);
723      _exit(-1);
724   }
725   else{
726 
727     pop_debug_print("Parent: --------------------------------\n");
728     pop_debug_print("Parent: forked a child with pid = %d\n", (int)pid);
729     pop_debug_print("Parent: mail status = %d\n", mascot->mail.status);
730 
731   }
732 #endif //USE_WIN32
733 }
734 
735 #ifdef USE_WIN32
get_pop3(LPVOID lpvPipe)736 unsigned __stdcall get_pop3(LPVOID lpvPipe)
737 {
738   typMascot *mascot;
739   signed int ret;
740   int num;
741   long size;
742   gchar *apop_key;
743   int wo_spam;
744   static int pid;
745   int pre_mail_count;
746 
747   mascot = (typMascot *)lpvPipe;
748 
749   pre_mail_count = mascot->mail.count;  // store mail count
750 
751   pop_debug_print("Conncet %s : %d\n", mascot->mail.pop_server, mascot->mail.pop_port);
752 
753   apop_key=g_malloc0(sizeof(gchar)*BUFFSIZE);
754   memset(apop_key, '\0', BUFFSIZE);
755   ret = popConnect(mascot->mail.pop_server, mascot->mail.pop_port, apop_key,
756 		   mascot->mail.ssl_mode, mascot->mail.ssl_nonblock,
757 		   mascot->mail.ssl_cert_res,
758 		   &mascot->mail.ssl_sub, &mascot->mail.ssl_iss,
759 		   &mascot->mail.ssl_verify);
760 
761   if( ret != 0 ){
762     if(apop_key) g_free(apop_key);
763     if(ret==-(30+4)){
764       pop_debug_print("popConnect() [ret=%d] :  Certification is required\n", ret);
765       mascot->mail.status = POP3_SSL_CERT;
766     }
767     else{
768       fprintf(stderr, "ERR: popConnect() [ret=%d]\n", ret);
769       mascot->mail.status = POP3_ERROR;
770       pop3_error(mascot);
771     }
772     mascot->mail.pop_child_fl=FALSE;
773     mascot->mail.pop_readed=TRUE;
774     _endthreadex(0);
775     //ExitThread(0);
776     //return(POP3_ERROR);
777   }
778 
779   pop_debug_print("POP LOGIN %s %s %s\n",mascot->mail.pop_id, mascot->mail.pop_pass,
780 	 apop_key);
781   ret = popLogin(mascot->mail.pop_id, mascot->mail.pop_pass, apop_key, mascot->mail.ssl_mode);
782   if( ret != 0 ){
783     if(apop_key) g_free(apop_key);
784     fprintf(stderr, "ERR: popLogin() [ret=%d]\n", ret);
785     mascot->mail.status = POP3_ERROR;
786     popQuit(mascot->mail.ssl_mode);
787     popClose();
788     pop3_error(mascot);
789     mascot->mail.pop_child_fl=FALSE;
790     mascot->mail.pop_readed=TRUE;
791     _endthreadex(0);
792     //ExitThread(0);
793     //return(POP3_ERROR);
794   }
795 
796   pop_debug_print("POP STAT\n");
797 
798   ret = popStat(&num, &size, mascot->mail.ssl_mode);
799   if( ret != 0 ){
800     if(apop_key) g_free(apop_key);
801     fprintf(stderr, "ERR: popStat() [ret=%d]\n", ret);
802     mascot->mail.status = POP3_ERROR;
803     popQuit(mascot->mail.ssl_mode);
804     popClose();
805     pop3_error(mascot);
806     mascot->mail.pop_child_fl=FALSE;
807     mascot->mail.pop_readed=TRUE;
808     _endthreadex(0);
809     //ExitThread(0);
810     //return(POP3_ERROR);
811   }
812 
813   pop_debug_print("mail: %d, size: %ld\n", num, size);
814 
815   if(num == 0){  // �᡼��̵��
816     mascot->mail.status = NO_MAIL;
817     mascot->mail.fetched_count = 0;
818     mascot->mail.spam_count = 0;
819     mascot->mail.pop3_fs_status = POP3_OK_NORMAL;
820     if(mascot->mail.last_f) g_free(mascot->mail.last_f);
821     mascot->mail.last_f = NULL;
822     if(mascot->mail.last_s) g_free(mascot->mail.last_s);
823     mascot->mail.last_s =  NULL;
824     if(pop_froms) g_free(pop_froms);
825     pop_froms = strbuf(NULL);
826   }
827   else{
828     wo_spam = mascot->mail.count - mascot->mail.spam_count;
829 
830     if(num == mascot->mail.count){  // �᡼�������ʤ�
831       mascot->mail.status = KEEP_NEW_MAIL;
832       mascot->mail.fetched_count = 0;
833       mascot->mail.pop3_fs_status = POP3_OK_NORMAL;
834     }
835     else if(num > mascot->mail.count){  // �᡼��������
836       mascot->mail.status = NEW_MAIL;
837       fs_get_pop3(num,mascot); // ������SPAM�ο�, pop_froms�����������
838       // for WIN32 fs_get_pop3 should not initialize pop_froms
839     }
840     else{  // �᡼��������äƤ����餹�٤ƿ����᡼��ȸ��ʤ�
841       mascot->mail.status = NEW_MAIL;
842       if(mascot->mail.last_f) g_free(mascot->mail.last_f);
843       mascot->mail.last_f = NULL;
844       if(mascot->mail.last_f) g_free(mascot->mail.last_s);
845       mascot->mail.last_s =  NULL;
846       if(pop_froms) g_free(pop_froms);
847       pop_froms = strbuf(NULL);
848       mascot->mail.count = 0;
849       mascot->mail.spam_count = 0;
850       fs_get_pop3(num,mascot);
851     }
852 
853     if( num == mascot->mail.spam_count){
854       mascot->mail.status = NO_MAIL; // SPAM�����ΤȤ�
855       if(mascot->mail.last_f) g_free(mascot->mail.last_f);
856       mascot->mail.last_f = NULL;
857       if(mascot->mail.last_s) g_free(mascot->mail.last_s);
858       mascot->mail.last_s =  NULL;
859       if(pop_froms) g_free(pop_froms);
860       pop_froms = strbuf(NULL);
861     }
862     else if((num - mascot->mail.spam_count) == wo_spam){
863       mascot->mail.status = KEEP_NEW_MAIL; // �������Τ�SPAM�������ä��Ȥ�
864     }
865   }
866 
867 
868   pop_debug_print("data read end\n");
869 
870   popQuit(mascot->mail.ssl_mode);
871   popClose();
872 
873   pop_debug_print("pop quit\n");
874 
875   mascot->mail.count = num;
876 
877   /*************** from pop3 data read *********/
878 
879   // Should be in parent process in UNIX ver
880 
881   pop_debug_print("POP Thread: pop3 data read\n");
882 
883   if( mascot->mail.status == POP3_ERROR ){  // POP3 status is ERR
884     pop3_error(mascot);
885   }
886 #ifdef USE_SSL
887   else if( mascot->mail.status == POP3_SSL_CERT){  // SSL Certification
888     //mascot->mail.ssl_cert_res=ssl_manager_verify_cert(mascot);
889   }
890 #endif
891   else{
892     if(mascot->mail.last_f!=NULL){
893 #ifdef USE_GTK2
894       if(g_utf8_validate(mascot->mail.last_f,-1,NULL)){
895 #endif
896 	strip_last_ret(mascot->mail.last_f);
897 #ifdef USE_GTK2
898       }
899       else{
900 	mascot->mail.last_f = g_strdup(_("(Decode Error)"));
901       }
902 #endif
903     }
904 
905     if(mascot->mail.last_s!=NULL){
906 #ifdef USE_GTK2
907       if(g_utf8_validate(mascot->mail.last_s,-1,NULL)){
908 #endif
909 	strip_last_ret(mascot->mail.last_s);
910 #ifdef USE_GTK2
911       }
912       else{
913 	mascot->mail.last_s = g_strdup(_("(Decode Error)"));
914       }
915 #endif
916     }
917   }
918 
919   if(mascot->mail.count == 0){
920     mascot->mail.displayed_count = 0;
921   }
922   else {
923     if(mascot->mail.pop3_fs_status == POP3_OK_NORMAL){
924       if(pre_mail_count > mascot->mail.count){  //�᡼�븺�ä�
925 	mascot->mail.displayed_count = mascot->mail.fetched_count;
926       }
927       else { // added or no changed
928 	mascot->mail.displayed_count += mascot->mail.fetched_count;
929       }
930 
931     }
932     else if(mascot->mail.pop3_fs_status == POP3_OK_FS_OVER){
933       gchar buf[BUFFSIZE], *tmp_froms;
934       //������̵���� fs clear ���Ƥ褤
935       mascot->mail.displayed_count = mascot->mail.fetched_count;
936 
937       sprintf(buf,_(" \n     ***** %d mails are skipped *****\n \n"),
938 	      mascot->mail.count - mascot->mail.displayed_count);
939 
940       tmp_froms=strdup(pop_froms);
941       if(pop_froms) g_free(pop_froms);
942       pop_froms=strbuf(NULL);
943       pop_froms=strbuf(buf);
944       pop_froms=strbuf(tmp_froms);
945       if(tmp_froms) g_free(tmp_froms);
946     }
947   }
948 
949 
950   mascot->mail.pop_child_fl=FALSE;
951   mascot->mail.pop_readed=TRUE;
952 
953   pop_debug_print("POP Thread: status = %d\n",mascot->mail.status);
954   pop_debug_print("POP Thread: pop3 fs status = %d\n",mascot->mail.pop3_fs_status);
955   pop_debug_print("POP Thread: count = %d\n",mascot->mail.count);
956   pop_debug_print("POP Thread: fetched count = %d\n",mascot->mail.fetched_count);
957   pop_debug_print("POP Thread: disped count = %d\n",mascot->mail.displayed_count);
958   pop_debug_print("POP Thread: spam count = %d\n",mascot->mail.spam_count);
959   if(mascot->mail.last_f!=NULL)
960     pop_debug_print("Last From:       %s\n",mascot->mail.last_f);
961   if(mascot->mail.last_s!=NULL)
962     pop_debug_print("Last Subject:    %s\n",mascot->mail.last_s);
963   if(pop_froms!=NULL)
964     pop_debug_print("data2 = %s\n",pop_froms);
965 
966   pop_debug_print("POP Thread: Done\n");
967 
968 
969   /*************** end of  pop3 data read *********/
970 
971 
972   if(apop_key) g_free(apop_key);
973   _endthreadex(0);
974   //ExitThread(0);
975   //return(mascot->mail.status);
976 }
977 #else
get_pop3(typMascot * mascot)978 int get_pop3(typMascot *mascot)
979 {
980   signed int ret;
981   int num;
982   long size;
983   gchar *apop_key;
984   int wo_spam;
985 
986   pop_debug_print("Conncet %s : %d\n", mascot->mail.pop_server, mascot->mail.pop_port);
987 
988   apop_key=g_malloc0(sizeof(gchar)*BUFFSIZE);
989   memset(apop_key, '\0', BUFFSIZE);
990   ret = popConnect(mascot->mail.pop_server, mascot->mail.pop_port, apop_key,
991 		   mascot->mail.ssl_mode, mascot->mail.ssl_nonblock,
992 		   mascot->mail.ssl_cert_res,
993 		   &mascot->mail.ssl_sub, &mascot->mail.ssl_iss,
994 		   &mascot->mail.ssl_verify);
995   if( ret != 0 ){
996     if(apop_key) g_free(apop_key);
997     if(ret==-(30+4)){
998       pop_debug_print("popConnect() [ret=%d] :  Certification is required\n", ret);
999       mascot->mail.status = POP3_SSL_CERT;
1000       return(POP3_SSL_CERT);
1001     }
1002     else{
1003       fprintf(stderr, "ERR: popConnect() [ret=%d]\n", ret);
1004       mascot->mail.status = POP3_ERROR;
1005       return(POP3_ERROR);
1006     }
1007   }
1008 
1009   pop_debug_print("POP LOGIN %s %s %s\n",mascot->mail.pop_id, mascot->mail.pop_pass,
1010 	 apop_key);
1011 
1012   ret = popLogin(mascot->mail.pop_id, mascot->mail.pop_pass, apop_key, mascot->mail.ssl_mode);
1013   if( ret != 0 ){
1014     fprintf(stderr, "ERR: popLogin() [ret=%d]\n", ret);
1015     mascot->mail.status = POP3_ERROR;
1016     popQuit(mascot->mail.ssl_mode);
1017     popClose();
1018     if(apop_key) g_free(apop_key);
1019     return(POP3_ERROR);
1020   }
1021 
1022   pop_debug_print("POP STAT\n");
1023 
1024   ret = popStat(&num, &size, mascot->mail.ssl_mode);
1025   if( ret != 0 ){
1026     fprintf(stderr, "ERR: popStat() [ret=%d]\n", ret);
1027     mascot->mail.status = POP3_ERROR;
1028     popQuit(mascot->mail.ssl_mode);
1029     popClose();
1030     if(apop_key) g_free(apop_key);
1031     return(POP3_ERROR);
1032   }
1033 
1034   pop_debug_print("mail: %d, size: %ld\n", num, size);
1035 
1036   if(num == 0){  // �᡼��̵��
1037     mascot->mail.status = NO_MAIL;
1038     mascot->mail.fetched_count = 0;
1039     mascot->mail.spam_count = 0;
1040     mascot->mail.pop3_fs_status = POP3_OK_NORMAL;
1041     if(mascot->mail.last_f) g_free(mascot->mail.last_f);
1042     mascot->mail.last_f = NULL;
1043     if(mascot->mail.last_s) g_free(mascot->mail.last_s);
1044     mascot->mail.last_s =  NULL;
1045     if(pop_froms) g_free(pop_froms);
1046     pop_froms = strbuf(NULL);
1047   }
1048   else{
1049     wo_spam = mascot->mail.count - mascot->mail.spam_count;
1050 
1051     if(num == mascot->mail.count){  // �᡼�������ʤ�
1052       mascot->mail.status = KEEP_NEW_MAIL;
1053       mascot->mail.fetched_count = 0;
1054       mascot->mail.pop3_fs_status = POP3_OK_NORMAL;
1055       if(pop_froms) g_free(pop_froms);  // ���� fs ���ꥢ
1056       pop_froms = strbuf(NULL);
1057     }
1058     else if(num > mascot->mail.count){  // �᡼��������
1059       mascot->mail.status = NEW_MAIL;
1060       fs_get_pop3(num,mascot); // ������SPAM�ο������������
1061     }
1062     else{  // �᡼��������äƤ����餹�٤ƿ����᡼��ȸ��ʤ�
1063       mascot->mail.status = NEW_MAIL;
1064       if(mascot->mail.last_f) g_free(mascot->mail.last_f);
1065       mascot->mail.last_f = NULL;
1066       if(mascot->mail.last_s) g_free(mascot->mail.last_s);
1067       mascot->mail.last_s =  NULL;
1068       if(pop_froms) g_free(pop_froms);
1069       pop_froms = strbuf(NULL);
1070       mascot->mail.count = 0;
1071       mascot->mail.spam_count = 0;
1072       fs_get_pop3(num,mascot);
1073     }
1074 
1075     if( num == mascot->mail.spam_count){
1076       mascot->mail.status = NO_MAIL; // SPAM�����ΤȤ�
1077     }
1078     else if((num - mascot->mail.spam_count) == wo_spam){
1079       mascot->mail.status = KEEP_NEW_MAIL; // �������Τ�SPAM�������ä��Ȥ�
1080     }
1081   }
1082 
1083   pop_debug_print("data read end\n");
1084 
1085   popQuit(mascot->mail.ssl_mode);
1086   popClose();
1087   if(apop_key) g_free(apop_key);
1088 
1089   pop_debug_print("pop quit\n");
1090 
1091   mascot->mail.count = num;
1092   return(mascot->mail.status);
1093 }
1094 #endif //USE_WIN32
1095 
pop3_error(typMascot * mascot)1096 void pop3_error(typMascot *mascot){
1097   if( mascot->mail.status < 0){  // POP3 status is ERR
1098     fprintf(stderr,"POP3 error\n");
1099     mascot->mail.count = 0;
1100     mascot->mail.spam_count = 0;
1101     mascot->mail.fetched_count = 0;
1102     mascot->mail.displayed_count = 0;
1103     mascot->mail.status = POP3_ERROR;
1104     if(mascot->mail.last_f) g_free(mascot->mail.last_f);
1105     mascot->mail.last_f = NULL;
1106     if(mascot->mail.last_s) g_free(mascot->mail.last_s);
1107     mascot->mail.last_s =  NULL;
1108     if(pop_froms) g_free(pop_froms);
1109     pop_froms = strbuf(NULL);
1110   }
1111 }
1112 
1113 #ifndef USE_WIN32
pop3_signal(int sig)1114 void pop3_signal(int sig){
1115 
1116   pop_debug_print("get child end\n");
1117 
1118   pop3_data_read(Mascot);
1119 }
1120 
pop3_data_read(typMascot * mascot)1121 void pop3_data_read(typMascot *mascot)
1122 {
1123   FILE *fp;
1124   char buf[BUFFSIZE];
1125   int pre_mail_count;
1126 
1127   if(mascot->mail.pop_child_fl){
1128     pre_mail_count = mascot->mail.count;  // store mail count
1129 
1130     pop_debug_print("Parent: pop3 data read\n");
1131 
1132     close(pop3_fd[1]);
1133     if( (fp = fdopen( pop3_fd[0], "r" )) == NULL ){
1134       fprintf(stderr,"pipe open error\n");
1135     }
1136     else{
1137       fgets( buf,BUFFSIZE-1,fp );  // read mail status
1138       mascot->mail.status = atoi(buf);
1139       if( mascot->mail.status == POP3_ERROR){  // POP3 status is ERR
1140 	pop3_error(mascot);
1141       }
1142 #ifdef USE_SSL
1143       else if( mascot->mail.status == POP3_SSL_CERT){  // SSL Certification
1144 	if(mascot->mail.ssl_sub) g_free(mascot->mail.ssl_sub);
1145 	fgets( buf,BUFFSIZE-1,fp );
1146 	if(strlen(buf)>=2){
1147 	  mascot->mail.ssl_sub = g_strdup(buf);
1148 	  strip_last_ret(mascot->mail.ssl_sub);
1149 	}
1150 	else{
1151 	  mascot->mail.ssl_sub = NULL;
1152 	}
1153 
1154 	if(mascot->mail.ssl_iss) g_free(mascot->mail.ssl_iss);
1155 	fgets( buf,BUFFSIZE-1,fp );
1156 	if(strlen(buf)>=2){
1157 	  mascot->mail.ssl_iss = g_strdup(buf);
1158 	  strip_last_ret(mascot->mail.ssl_iss);
1159 	}
1160 	else{
1161 	  mascot->mail.ssl_iss = NULL;
1162 	}
1163 
1164 	fgets( buf,BUFFSIZE-1,fp );  // SSL verify_result
1165 	mascot->mail.ssl_verify = atoi(buf);
1166       }
1167 #endif
1168       else{
1169 	fgets( buf,BUFFSIZE-1,fp );  // read total mail count
1170 	mascot->mail.count = atoi(buf);
1171 	fgets( buf,BUFFSIZE-1,fp );  // read fetched mail count
1172 	mascot->mail.fetched_count = atoi(buf);
1173 	fgets( buf,BUFFSIZE-1,fp );  // read pop status
1174 	mascot->mail.pop3_fs_status = atoi(buf);
1175 	fgets( buf,BUFFSIZE-1,fp );  // read SPAM count
1176 	mascot->mail.spam_count = atoi(buf);
1177 
1178 	fgets( buf,BUFFSIZE-1,fp );  // Last From
1179 	if(mascot->mail.last_f) g_free(mascot->mail.last_f);
1180 	if(strlen(buf)>=2){
1181 #ifdef USE_GTK2
1182 	  if(g_utf8_validate(buf,-1,NULL)){
1183 #endif
1184 	    mascot->mail.last_f = g_strdup(buf);
1185 	    strip_last_ret(mascot->mail.last_f);
1186 #ifdef USE_GTK2
1187 	  }
1188 	  else{
1189 	    mascot->mail.last_f = g_strdup(_("(Decode Error)"));
1190 	  }
1191 #endif
1192 	}
1193 	else{
1194 	  mascot->mail.last_f = NULL;
1195 	}
1196 	fgets( buf,BUFFSIZE-1,fp );  // Last Sub
1197 	if(mascot->mail.last_s) g_free(mascot->mail.last_s);
1198 	if(strlen(buf)>=2){
1199 #ifdef USE_GTK2
1200 	  if(g_utf8_validate(buf,-1,NULL)){
1201 #endif
1202 	    mascot->mail.last_s = g_strdup(buf);
1203 	    strip_last_ret(mascot->mail.last_s);
1204 #ifdef USE_GTK2
1205 	  }
1206 	  else{
1207 	    mascot->mail.last_s = g_strdup(_("(Decode Error)"));
1208 	  }
1209 #endif
1210 	}
1211 	else{
1212 	  mascot->mail.last_s = NULL;
1213 	}
1214 
1215 	if(mascot->mail.count == 0){
1216 	  if(pop_froms) g_free(pop_froms);  // fs data clear
1217  	  pop_froms = strbuf(NULL);
1218 	  mascot->mail.displayed_count = 0;
1219 	}
1220 	else {
1221 	  if(mascot->mail.pop3_fs_status == POP3_OK_NORMAL){
1222 	    if(pre_mail_count > mascot->mail.count){  //�᡼�븺�ä�
1223 	      // �᡼�븺�ä������������ľ���ʤΤ� fs clear
1224 	      mascot->mail.displayed_count = mascot->mail.fetched_count;
1225 	      if(pop_froms) g_free(pop_froms);
1226 	      pop_froms = strbuf(NULL);
1227 	    }
1228 	    else {
1229 	      mascot->mail.displayed_count += mascot->mail.fetched_count;
1230 	    }
1231 
1232 	    while( fgets( buf,BUFFSIZE-1,fp ) != NULL ){
1233 	      pop_froms = strbuf( buf );
1234 	    }
1235 	  }
1236 	  else if(mascot->mail.pop3_fs_status == POP3_OK_FS_OVER){
1237 	    //������̵���� fs clear ���Ƥ褤
1238 	    mascot->mail.displayed_count = mascot->mail.fetched_count;
1239 
1240 	    if(pop_froms) g_free(pop_froms);    // fs data clear
1241 	    pop_froms = strbuf(NULL);
1242 	    sprintf(buf,_(" \n     ***** %d mails are skipped *****\n \n"),
1243 		    mascot->mail.count - mascot->mail.displayed_count);
1244 	    pop_froms = strbuf( buf );
1245 	    while( fgets( buf,BUFFSIZE-1,fp ) != NULL ){
1246 	      pop_froms = strbuf( buf );
1247 	    }
1248 	  }
1249 	}
1250       }
1251       fclose( fp );
1252     }
1253     close(pop3_fd[0]);
1254 
1255     //gdk_flush();
1256     //display_biff_balloon(mascot);
1257     //gdk_flush();
1258     mascot->mail.pop_child_fl=FALSE;
1259     mascot->mail.pop_readed=TRUE;
1260 
1261 
1262     pop_debug_print("Parent: status = %d\n",mascot->mail.status);
1263     pop_debug_print("Parent: pop3 fs status = %d\n",mascot->mail.pop3_fs_status);
1264     pop_debug_print("Parent: count = %d\n",mascot->mail.count);
1265     pop_debug_print("Parent: fetched count = %d\n",mascot->mail.fetched_count);
1266     pop_debug_print("Parent: disped count = %d\n",mascot->mail.displayed_count);
1267     pop_debug_print("Parent: spam count = %d\n",mascot->mail.spam_count);
1268   if(mascot->mail.last_f!=NULL)
1269     pop_debug_print("Last From:       %s\n",mascot->mail.last_f);
1270   if(mascot->mail.last_s!=NULL)
1271     pop_debug_print("Last Subject:    %s\n",mascot->mail.last_s);
1272   if(pop_froms!=NULL)
1273     pop_debug_print("data2 = %s\n",pop_froms);
1274   }
1275 }
1276 #endif
1277 
1278 // �᡼��ܥå������� From: Subject: �����
fs_get_mbox(typMascot * mascot)1279 gchar * fs_get_mbox(typMascot *mascot){
1280   FILE *fp;
1281   char buf[BUFFSIZE],bufs[BUFFSIZE5];
1282   gchar *froms=NULL;
1283   char *p;
1284   gboolean ed_fl=FALSE, spam_flag;
1285   fpos_t pos;
1286   int i;
1287   char *f, *s, *sdel;
1288 
1289   froms=strbuf(NULL);
1290   mascot->mail.spam_count=0;
1291 
1292   if((fp=fopen(mascot->mail.file,"r"))!= NULL){
1293     do{
1294       if(fgets(buf,BUFFSIZE-1,fp)==NULL) break;
1295       if(strncmp(buf,"From ",5)==0){  // ���ĤΥ᡼��λϤޤ�
1296 	f=s=NULL;
1297 	spam_flag = FALSE;
1298 	for(;;){
1299 	  if((p=fgets(buf,BUFFSIZE-1,fp))==NULL
1300 	     ||buf[0]=='\n'||buf[0]=='\0')
1301 	    break;
1302 	  if(g_strncasecmp(buf,"from:",5)==0)
1303 	    {
1304 	      strcpy(bufs,buf);
1305 	      ///***for(i=0;i<10;i++){
1306 	      while(1){
1307 		fgetpos(fp,&pos);
1308 		if((p=fgets(buf,BUFFSIZE-1,fp))==NULL
1309 		   ||buf[0]=='\n'||buf[0]=='\0'){
1310 		  ed_fl=TRUE;
1311 /* tnaka start */
1312 		  if( strlen(bufs)) {
1313 		    f = g_strdup(bufs);
1314 		    fsetpos(fp,&pos);
1315 		  }
1316 /* tnaka end */
1317 		  break;
1318 		}
1319 		if(bufs[strlen(bufs)-1]!='\n'){
1320 		  strcat(bufs,buf); /* ��Ԥ�200byte�ʾ� */
1321 		}
1322 
1323 		else if((buf[0]==' ') || (buf[0]=='\t')){
1324 		  bufs[strlen(bufs)-1]='\0';
1325 		  sdel = &buf[1];     /* \t ���� */
1326 		  strcat(bufs,sdel); /* ʣ���� */
1327 		}
1328 		else{
1329 		  f=g_strdup(bufs);
1330 		  fsetpos(fp,&pos);
1331 		  break;
1332 		}
1333 	      }
1334 	      if(ed_fl) break;
1335 	    }
1336 	  else if(g_strncasecmp(buf,"subject:",8)==0)
1337 	    {
1338 	      strcpy(bufs,buf);
1339 	      ///***for(i=0;i<10;i++){
1340 	      while(1){
1341 		fgetpos(fp,&pos);
1342 		if((p=fgets(buf,BUFFSIZE-1,fp))==NULL
1343 		   ||buf[0]=='\n'||buf[0]=='\0'){
1344 		  ed_fl=TRUE;
1345 /* tnaka start */
1346 		  if( strlen(bufs)) {
1347 		    s = g_strdup(bufs);
1348 		    fsetpos(fp,&pos);
1349 		  }
1350 /* tnaka end */
1351 		  break;
1352 		}
1353 		if(bufs[strlen(bufs)-1]!='\n'){
1354 		  strcat(bufs,buf); /* ��Ԥ�200byte�ʾ� */
1355 		}
1356 		else if((buf[0]==' ') || (buf[0]=='\t')){
1357 		  bufs[strlen(bufs)-1]='\0';
1358 		  sdel = &buf[1];     /* \t ���� */
1359 		  strcat(bufs,sdel); /* ʣ���� */
1360 		}
1361 		else{
1362 		  s=g_strdup(bufs);
1363 		  fsetpos(fp,&pos);
1364 		  break;
1365 		}
1366 	      }
1367 	      if(ed_fl) break;
1368 	    }
1369 	  else if(strncmp(buf, mascot->mail.spam_mark,
1370 			  strlen(mascot->mail.spam_mark))==0){
1371 	    spam_flag = TRUE;
1372 	    mascot->mail.spam_count++;
1373 	  }
1374 	}
1375 
1376 	//
1377 	// From: Subject:
1378 	// �ν��ɽ��������
1379 	//
1380 	if ( (!spam_flag) || (!mascot->mail.spam_check) ){
1381 	  if (f) {
1382 	    conv_unmime_header_overwrite(f);
1383 	    froms=strbuf(f);
1384 	    g_free(f);
1385 	  }
1386 	  else{
1387 	    froms=strbuf("From: (no From: in original)\n");
1388 	  }
1389 	  if (s) {
1390 	    froms=strbuf(" ");
1391 	    conv_unmime_header_overwrite(s);
1392 	    froms=strbuf(s);
1393 	    g_free(s);
1394 	  }
1395 	  else{
1396 	    froms=strbuf(" Subject: (no Subject: in original)\n");
1397 	  }
1398 	}
1399 ed_fl = FALSE;  /* tnaka */
1400       }
1401       while (p != NULL && buf[0] != '\n' && buf[0] != '\0')
1402 	p = fgets(buf, BUFFSIZE - 1, fp);
1403     } while (1);
1404     fclose(fp);
1405   }
1406   return(froms);
1407 }
1408 
1409 
1410 // MH + Procmail ����
fs_get_procmail(typMascot * mascot)1411 gchar *  fs_get_procmail(typMascot  *mascot){
1412     FILE *fp,*fp_folder;
1413     gchar buf[BUFFSIZE],tmp[10],bufs[BUFFSIZE5];
1414     gchar *folder_file,folder_tmp[BUFFSIZE];
1415     gchar *froms=NULL, *p;
1416     gboolean ed_fl=FALSE;
1417     fpos_t pos;
1418     int i;
1419     gchar *f, *s, *sdel;
1420 
1421 
1422     froms=strbuf(NULL);
1423 
1424     if((fp=fopen(mascot->mail.file,"r"))==NULL){
1425 	return(NULL);
1426     }
1427 
1428     while(!feof(fp)){
1429 	f=s=NULL;
1430 	if(fgets(buf,BUFFSIZE-1,fp)==NULL) break;
1431 	if(strncmp(buf,"  Folder:",9)==0){
1432 	    sscanf(buf,"%s%s",tmp,folder_tmp);
1433 	    folder_file=g_strconcat(set_mhdir(),G_DIR_SEPARATOR,
1434 				    folder_tmp,NULL);
1435 
1436 	    if((fp_folder=fopen(folder_file,"r"))!=NULL){
1437 		while(!feof(fp_folder)){
1438 		    if((p=fgets(buf,BUFFSIZE-1,fp_folder))==NULL
1439 		       ||buf[0]=='\n'||buf[0]=='\0')
1440 			break;
1441 		    if(g_strncasecmp(buf,"from:",5)==0)
1442 		    {
1443 			strcpy(bufs,buf);
1444 			///***for(i=0;i<10;i++){
1445 			while(1){
1446 			    fgetpos(fp_folder,&pos);
1447 			    if((p=fgets(buf,BUFFSIZE-1,fp_folder))==NULL
1448 			       ||buf[0]=='\n'||buf[0]=='\0'){
1449 				ed_fl=TRUE;
1450 				break;
1451 			    }
1452 			    if(bufs[strlen(bufs)-1]!='\n'){
1453 				strcat(bufs,buf); /* ��Ԥ�200byte�ʾ� */
1454 			    }
1455 			    else if((buf[0]==' ') || (buf[0]=='\t')){
1456 				bufs[strlen(bufs)-1]='\0';
1457 				sdel = &buf[1];     /* \t ���� */
1458 				strcat(bufs,sdel); /* ʣ���� */
1459 			    }
1460 			    else{
1461 			        f=g_strdup(bufs);
1462 				fsetpos(fp_folder,&pos);
1463 				break;
1464 			    }
1465 			}
1466 			if(ed_fl) break;
1467 		    }
1468 		    else if(g_strncasecmp(buf,"subject:",8)==0)
1469 		    {
1470 			strcpy(bufs,buf);
1471 			///***for(i=0;i<10;i++){
1472 			while(1){
1473 			    fgetpos(fp_folder,&pos);
1474 			    if((p=fgets(buf,BUFFSIZE-1,fp_folder))==NULL
1475 			       ||buf[0]=='\n'||buf[0]=='\0'){
1476 				ed_fl=TRUE;
1477 				break;
1478 			    }
1479 			    if(bufs[strlen(bufs)-1]!='\n'){
1480 				strcat(bufs,buf); /* ��Ԥ�200byte�ʾ� */
1481 			    }
1482 			    else if((buf[0]==' ') || (buf[0]=='\t')){
1483 				bufs[strlen(bufs)-1]='\0';
1484 				sdel = &buf[1];     /* \t ���� */
1485 				strcat(bufs,sdel); /* ʣ���� */
1486 			    }
1487 			    else{
1488 			        s=g_strdup(bufs);
1489 				fsetpos(fp_folder,&pos);
1490 				break;
1491 			    }
1492 			}
1493 			if(ed_fl) break;
1494 		    }
1495 		}
1496 		fclose(fp_folder);
1497 		//
1498 		// From: Subject:
1499 		// �ν��ɽ��������
1500 		//
1501 		if (f) {
1502 		  conv_unmime_header_overwrite(f);
1503 		  froms=strbuf(f);
1504 		  g_free(f);
1505 		}
1506 		else{
1507 		    froms=strbuf("From: (no From: in original)\n");
1508 		}
1509 		if (s) {
1510 		  froms=strbuf(" ");
1511 		  conv_unmime_header_overwrite(s);
1512 		  froms=strbuf(s);
1513 		  g_free(s);
1514 		}
1515 		else{
1516 		    froms=strbuf(" Subject: (no Subject: in original)\n");
1517 		}
1518 	    }
1519 	}
1520 
1521     }
1522     fclose(fp);
1523 
1524     if(folder_file) g_free(folder_file);
1525 
1526     return(froms);
1527 }
1528 
1529 
1530 // Qmail ����
fs_get_qmail(typMascot * mascot)1531 gchar * fs_get_qmail(typMascot *mascot){
1532     FILE *fp_folder;
1533     gchar buf[BUFFSIZE],tmp[10],bufs[BUFFSIZE5];
1534     gchar folder_file[BUFFSIZE],folder_tmp[BUFFSIZE];
1535     gchar *froms=NULL, *p;
1536     gboolean ed_fl=FALSE, spam_flag;
1537     fpos_t pos;
1538     int i;
1539     DIR *dp;
1540     struct dirent *entry;
1541 
1542     gchar *f, *s, *sdel;
1543 
1544     mascot->mail.spam_count=0;
1545     froms=strbuf(NULL);
1546 
1547     if ((dp=opendir(mascot->mail.file))==NULL){
1548 	return(NULL);
1549     }
1550 
1551 
1552     while((entry=readdir(dp))!=NULL){
1553 	f=s=NULL;
1554 	if(entry->d_name[0]!='.'){
1555 	    sprintf(folder_file,"%s/%s",mascot->mail.file,entry->d_name);
1556 
1557 	    if((fp_folder=fopen(folder_file,"r"))!=NULL){
1558 	        spam_flag = FALSE;
1559                 while(!feof(fp_folder)){
1560 		  if((p=fgets(buf,BUFFSIZE-1,fp_folder))==NULL
1561 		     ||buf[0]=='\n'||buf[0]=='\0')
1562 		    break;
1563 
1564 		  if(strncmp(buf, mascot->mail.spam_mark,
1565 			     strlen(mascot->mail.spam_mark))==0){
1566 		    spam_flag = TRUE;
1567 		    mascot->mail.spam_count++;
1568 		  }
1569 		}
1570 		if( (spam_flag) && (mascot->mail.spam_check) ){
1571 		  fclose(fp_folder);
1572 		  continue;
1573 		}
1574 
1575 		rewind(fp_folder);
1576 		while(!feof(fp_folder)){
1577 		    if((p=fgets(buf,BUFFSIZE-1,fp_folder))==NULL
1578 		       ||buf[0]=='\n'||buf[0]=='\0')
1579 			break;
1580 		    if(g_strncasecmp(buf,"from:",5)==0)
1581                     {
1582 			strcpy(bufs,buf);
1583 			///***for(i=0;i<10;i++){
1584 			while(1){
1585 			    fgetpos(fp_folder,&pos);
1586 			    if((p=fgets(buf,BUFFSIZE-1,fp_folder))==NULL
1587 			       ||buf[0]=='\n'||buf[0]=='\0'){
1588 				ed_fl=TRUE;
1589 				break;
1590 			    }
1591 			    if(bufs[strlen(bufs)-1]!='\n'){
1592 			      strcat(bufs,buf); // ��Ԥ�200byte�ʾ�
1593 			    }
1594 			    else if((buf[0]==' ') || (buf[0]=='\t')){
1595 				bufs[strlen(bufs)-1]='\0';
1596 				sdel = &buf[1];     // \t ����
1597 				strcat(bufs,sdel); // ʣ����
1598 			    }
1599 			    else{
1600 			        f=g_strdup(bufs);
1601 				fsetpos(fp_folder,&pos);
1602 				break;
1603 			    }
1604 			}
1605 			if(ed_fl) break;
1606 		    }
1607 		    else if(g_strncasecmp(buf,"subject:",8)==0)
1608                     {
1609 			strcpy(bufs,buf);
1610 			///***for(i=0;i<10;i++){
1611 			while(1){
1612 			    fgetpos(fp_folder,&pos);
1613 			    if((p=fgets(buf,BUFFSIZE-1,fp_folder))==NULL
1614 			       ||buf[0]=='\n'||buf[0]=='\0'){
1615 				ed_fl=TRUE;
1616 				break;
1617 			    }
1618 			    if(bufs[strlen(bufs)-1]!='\n'){
1619 				strcat(bufs,buf); /* ��Ԥ�200byte�ʾ� */
1620 			    }
1621 			    else if((buf[0]==' ') || (buf[0]=='\t')){
1622 				bufs[strlen(bufs)-1]='\0';
1623 				sdel = &buf[1];     /* \t ���� */
1624 				strcat(bufs,sdel); /* ʣ���� */
1625 			    }
1626 			    else{
1627 				s=g_strdup(bufs);
1628 				fsetpos(fp_folder,&pos);
1629 				break;
1630 			    }
1631 			}
1632 			if(ed_fl) break;
1633 		    }
1634 		}
1635 		fclose(fp_folder);
1636 		//
1637 		// From: Subject:
1638 		// �ν��ɽ��������
1639 		//
1640 		if (f) {
1641 		  conv_unmime_header_overwrite(f);
1642 		  froms=strbuf(f);
1643 		  g_free(f);
1644 		}
1645 		else{
1646 		    froms=strbuf("From: (no From: in original)\n");
1647 		}
1648 		if (s) {
1649 		    froms=strbuf(" ");
1650 		    conv_unmime_header_overwrite(s);
1651 		    froms=strbuf(s);
1652 		    g_free(s);
1653 		}
1654 		else{
1655 		    froms=strbuf(" Subject: (no Subject: in original)\n");
1656 		}
1657 
1658 	    }
1659 	}
1660 
1661     }
1662     closedir(dp);
1663 
1664     return(froms);
1665 }
1666 
1667 
1668 // pop3 server ���� From: Subject: �����
fs_get_pop3(int num,typMascot * mascot)1669 void fs_get_pop3(int num, typMascot *mascot){
1670 
1671   int funcret;
1672   char buffer_header[HEADER_MAX][POP_MAX_LINE], bufs[BUFFSIZE5];
1673   char buffer[POP_MAX_LINE];
1674   int mail_cnt, header, header_line, i;
1675   int mail_cnt_start;
1676   char *f, *s, *sdel, tmp_fs[256];
1677   int disp=0;
1678   gboolean spam_flag;
1679 #ifndef USE_WIN32
1680   char *tmp_froms;
1681 
1682   tmp_froms=strbuf(NULL);
1683 #endif
1684 
1685   pop_debug_print("fs read num = %d mail_count = %d\n",
1686 	  num, mascot->mail.count);
1687 
1688   if((num - mascot->mail.count) > mascot->mail.pop_max_fs){  //overflow
1689     mail_cnt_start = num - mascot->mail.pop_max_fs +1;
1690     mascot->mail.fetched_count = mascot->mail.pop_max_fs;
1691     mascot->mail.pop3_fs_status = POP3_OK_FS_OVER;
1692   }
1693   else {    // under
1694     mail_cnt_start = mascot->mail.count +1;
1695     mascot->mail.fetched_count = num - mascot->mail.count;
1696     mascot->mail.pop3_fs_status = POP3_OK_NORMAL;
1697   }
1698 
1699   pop_debug_print("fs read num = %d mail_cnt_start = %d\n",
1700 	  num, mail_cnt_start);
1701   pop_debug_print("fs mail_fetched = %d\n", mascot->mail.fetched_count);
1702 
1703   for(mail_cnt = mail_cnt_start; mail_cnt <= num; mail_cnt++){
1704 
1705     sprintf(buffer, "TOP %d %d\r\n", mail_cnt, 0);  // get header only
1706 
1707     funcret = popWriteLine(buffer, mascot->mail.ssl_mode);
1708     if( funcret != 0 ){
1709       fprintf(stderr,"write err = %d\n",funcret);
1710 #ifndef USE_WIN32
1711       mascot->mail.status=POP3_ERROR;
1712 #endif
1713       return;
1714     }
1715     for(header=0; header < HEADER_MAX; header++){
1716       funcret = popReadLine(buffer_header[header], POP_MAX_LINE, mascot->mail.ssl_mode);
1717       //pop_debug_print("%d %d   %s\n",mail_cnt,header,buffer_header[header]);
1718       if( funcret != 0 ){
1719 	fprintf(stderr,"read err\n");
1720 #ifndef USE_WIN32
1721       mascot->mail.status=POP3_ERROR;
1722 #endif
1723 	return;
1724       }
1725       if( strcmp(buffer_header[header], ".\r\n") == 0 )      break;
1726       buffer_header[header][strlen(buffer_header[header])-2]=' ';  // \r delete
1727     }
1728     header_line = header-1;
1729 
1730     spam_flag = FALSE;
1731     if(mascot->mail.spam_check){
1732       for(header=0; header < header_line; header++){
1733 	if(strncmp(buffer_header[header], mascot->mail.spam_mark,
1734 		   strlen(mascot->mail.spam_mark))==0){
1735 	  spam_flag = TRUE;
1736 	  mascot->mail.spam_count++;
1737 
1738 	  pop_debug_print("SPAM detected = %d\n", mascot->mail.spam_count);
1739 	}
1740       }
1741     }
1742     if( (spam_flag) && (mascot->mail.spam_check) )  continue;
1743 
1744     f=s=NULL;
1745     for(header=0; header < header_line; header++){
1746       if(g_strncasecmp(buffer_header[header],"from:",5)==0)
1747       {
1748 	strcpy(bufs, buffer_header[header]);
1749 	//bufs=g_strconcat(buffer_header[header],NULL);
1750 	///***for(i=1;i<10;i++){
1751 	i=1;
1752 	while(1){
1753 	  if((i+header) > header_line)  break;
1754 	  if((buffer_header[i+header][0]==' ') || (buffer_header[i+header][0]=='\t')){
1755 	    bufs[strlen(bufs)-2]='\0';
1756 	    sdel = &buffer_header[i+header][1];     /* \t ���� */
1757 	    strcat(bufs,sdel); /* ʣ���� */
1758 	    //bufs=g_strconcat(bufs,sdel,NULL); /* ʣ���� */
1759 	  }
1760 	  else{
1761 	    f=g_strdup(bufs);
1762 	    break;
1763 	  }
1764 	  i++;
1765 	}
1766       }
1767       else if(g_strncasecmp(buffer_header[header],"subject:",8)==0)
1768       {
1769 	strcpy(bufs, buffer_header[header]);
1770 	//bufs=g_strconcat(buffer_header[header],NULL);
1771 	///***for(i=1;i<10;i++){
1772 	i=1;
1773 	while(1){
1774 	  if((i+header) > header_line)  break;
1775 	  if((buffer_header[i+header][0]==' ') || (buffer_header[i+header][0]=='\t')){
1776 	    bufs[strlen(bufs)-2]='\0';
1777 	    sdel = &buffer_header[i+header][1];     /* \t ���� */
1778 	    strcat(bufs,sdel); /* ʣ���� */
1779 	    //bufs=g_strconcat(bufs,sdel,NULL); /* ʣ���� */
1780 	  }
1781 	  else{
1782 	    s=g_strdup(bufs);
1783 	    break;
1784 	  }
1785 	  i++;
1786 	}
1787       }
1788     }
1789 
1790     if (f) {
1791       conv_unmime_header_overwrite(f);
1792 #ifdef USE_WIN32
1793       pop_froms=strbuf(f);
1794 #else
1795       tmp_froms=strbuf(f);
1796 #endif
1797       if(mascot->mail.last_f) g_free(mascot->mail.last_f);
1798       mascot->mail.last_f=g_strdup(f+strlen("From: "));
1799       strip_last_ret(mascot->mail.last_f);
1800       g_free(f);
1801     }
1802     else{
1803 #ifdef USE_WIN32
1804       pop_froms=strbuf("From: (no From: in original)\n");
1805 #else
1806       tmp_froms=strbuf("From: (no From: in original)\n");
1807 #endif
1808     }
1809     if (s) {
1810 #ifdef USE_WIN32
1811       pop_froms=strbuf(" ");
1812 #else
1813       tmp_froms=strbuf(" ");
1814 #endif
1815       conv_unmime_header_overwrite(s);
1816 #ifdef USE_WIN32
1817       pop_froms=strbuf(s);
1818 #else
1819       tmp_froms=strbuf(s);
1820 #endif
1821       if(mascot->mail.last_s) g_free(mascot->mail.last_s);
1822       mascot->mail.last_s=g_strdup(s+strlen("Subject: "));
1823       strip_last_ret(mascot->mail.last_s);
1824       g_free(s);
1825     }
1826     else{
1827 #ifdef USE_WIN32
1828       pop_froms=strbuf(" Subject: (no Subject: in original)\n");
1829 #else
1830       tmp_froms=strbuf(" Subject: (no Subject: in original)\n");
1831 #endif
1832     }
1833   }
1834 #ifndef USE_WIN32
1835   if(pop_froms) g_free(pop_froms);
1836   pop_froms=strbuf(NULL);
1837   pop_froms=strbuf(tmp_froms);
1838   if(tmp_froms) g_free(tmp_froms);
1839 #endif
1840   pop_debug_print("fs_get_pop3 end\n");
1841 }
1842 
1843 //
strbuf(char * p)1844 char* strbuf(char *p)
1845 {
1846   static char    *start = NULL;
1847   static char    *next;
1848   static int      len, len2, l;
1849 
1850   if (p == NULL) {
1851     len = len2 = 4096;
1852     start = next = (char *) g_malloc(len + 1);
1853     start[0] = '\0';
1854   } else if (start != NULL) {
1855     l = strlen(p);
1856     while (len - l < 0) {
1857       char           *p;
1858       len2 += 256;
1859       len += 256;
1860       p = (char *) g_realloc(start, len2 + 1);
1861       start = p;
1862       next = p + len2 - len;
1863                 }
1864     strcpy(next,p);
1865     len -= l;
1866     next += l;
1867   }
1868   return start;
1869 }
1870 
1871 
1872 
make_fs_max(GtkWidget * widget,typMascot * mascot)1873 void make_fs_max(GtkWidget *widget, typMascot *mascot){
1874   GtkWidget *label;
1875   gchar tmp_fs_max[256];
1876   gchar tmp_fs[8];
1877 
1878   if(mascot->mail.displayed_count != mascot->mail.count){
1879     if(mascot->mail.spam_count!=0){
1880       sprintf(tmp_fs_max,
1881 	      _("Displaying the newest %d mails out of %ds. [%d SPAMs are excluded.]"),
1882 	      mascot->mail.displayed_count,
1883 	      mascot->mail.count, mascot->mail.spam_count);
1884     }
1885     else{
1886       sprintf(tmp_fs_max,
1887 	      _("Displaying the newest %d mails out of %ds."),
1888 	      mascot->mail.displayed_count,
1889 	      mascot->mail.count);
1890     }
1891   }
1892   else{
1893     if(mascot->mail.spam_count!=0){
1894       sprintf(tmp_fs_max,_("You have %d new mails.  [%d SPAMs are excluded.]"),
1895 	      mascot->mail.count, mascot->mail.spam_count);
1896     }
1897     else{
1898       sprintf(tmp_fs_max,_("You have %d new mails."),
1899 	      mascot->mail.count);
1900     }
1901   }
1902   label=gtk_label_new(tmp_fs_max);
1903   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0);
1904   gtk_table_attach (GTK_TABLE (widget), label, 0, 5, 1, 2,
1905 		    GTK_FILL, GTK_SHRINK , 0, 0);
1906 }
1907 
1908 
1909 // �ᥤ���忮�ꥹ�Ȥ�����
create_biff_dialog(typMascot * mascot)1910 void create_biff_dialog(typMascot *mascot)
1911 {
1912   GtkWidget *biff_main;
1913   GtkWidget *biff_tbl;
1914   GtkWidget *biff_text;
1915   GtkWidget *button;
1916   GtkWidget *biff_scroll;
1917   gchar *tmp_froms=NULL;
1918   gchar *tmp;
1919   gchar *fp_1, *fp_2;
1920   gchar *p;
1921   gchar *buf_unmime;
1922   gchar *err_msg;
1923 #ifdef USE_GTK2
1924   GtkTextBuffer *text_buffer;
1925   GtkTextIter start_iter, end_iter;
1926   GtkTextMark *end_mark;
1927 #endif
1928 
1929 
1930   // Win���ۤϽŤ��Τ����Expose���٥�������٤ƽ������Ƥ���
1931   while (my_main_iteration(FALSE));
1932 
1933   mascot->flag_menu=TRUE;
1934 
1935 
1936 #ifdef USE_GTK2
1937   biff_main = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1938   text_buffer = gtk_text_buffer_new(NULL);
1939 #else
1940   biff_main = gtk_window_new(GTK_WINDOW_DIALOG);
1941 #endif
1942 
1943   gtk_widget_set_usize (biff_main, mascot->mail.win_width,
1944   			mascot->mail.win_height);
1945   if(mascot->mail.type==MAIL_POP3|MAIL_APOP){
1946     tmp=g_strconcat(_("MaCoPiX : Arrived mail list"),"  [",
1947 		    mascot->mail.pop_server,"]",NULL);
1948   }
1949   else{
1950     tmp=g_strdup(_("MaCoPiX : Arrived mail list"));
1951   }
1952   gtk_window_set_title(GTK_WINDOW(biff_main), tmp);
1953   g_free(tmp);
1954   gtk_widget_realize(biff_main);
1955   my_signal_connect(biff_main,"destroy",close_biff, GTK_WIDGET(biff_main));
1956   gtk_container_set_border_width (GTK_CONTAINER (biff_main), 5);
1957 
1958   // 6x3�Υơ��֥�
1959   biff_tbl = gtk_table_new (6, 3, FALSE);
1960   gtk_container_add (GTK_CONTAINER (biff_main), biff_tbl);
1961 
1962 #ifdef USE_GTK2
1963   biff_scroll = gtk_scrolled_window_new(NULL, NULL);
1964   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(biff_scroll),
1965                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1966 
1967   biff_text = gtk_text_view_new_with_buffer (text_buffer);
1968   gtk_text_view_set_editable (GTK_TEXT_VIEW (biff_text), FALSE);
1969   gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (biff_text), FALSE);
1970 
1971   gtk_container_add(GTK_CONTAINER(biff_scroll), biff_text);
1972 
1973   gtk_table_attach_defaults (GTK_TABLE (biff_tbl), biff_scroll, 0, 5, 0, 1);
1974 #else
1975   biff_text = gtk_text_new (NULL, NULL);
1976   gtk_text_set_editable (GTK_TEXT (biff_text), FALSE);
1977   gtk_table_attach_defaults (GTK_TABLE(biff_tbl), biff_text, 0, 5, 0, 1);
1978 
1979   biff_scroll = gtk_vscrollbar_new (GTK_TEXT (biff_text)->vadj);
1980   gtk_table_attach (GTK_TABLE (biff_tbl), biff_scroll, 5, 6, 0, 1,
1981 		    GTK_FILL,
1982 		    GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
1983 #endif
1984 
1985 
1986   switch(mascot->mail.type){
1987   case MAIL_LOCAL:
1988     tmp_froms=fs_get_mbox(mascot);
1989     break;
1990   case MAIL_POP3:
1991   case MAIL_APOP:
1992     tmp_froms=g_strdup(pop_froms);
1993     make_fs_max(biff_tbl, mascot);
1994     break;
1995   case MAIL_QMAIL:
1996     tmp_froms=fs_get_qmail(mascot);
1997     break;
1998   case MAIL_PROCMAIL:
1999     tmp_froms=fs_get_procmail(mascot);
2000     break;
2001   }
2002 
2003 
2004 #ifdef USE_GTK2
2005   gtk_text_buffer_create_tag (text_buffer, "underline",
2006 			      "underline", PANGO_UNDERLINE_SINGLE, NULL);
2007   gtk_text_buffer_create_tag (text_buffer, "big_gap_after_line",
2008 			      "pixels_below_lines", 5, NULL);
2009   gtk_text_buffer_create_tag (text_buffer, "heading",
2010 			      "weight", PANGO_WEIGHT_BOLD,
2011 			      //"size", 15 * PANGO_SCALE,
2012 			      NULL);
2013   gtk_text_buffer_get_start_iter(text_buffer, &start_iter);
2014 
2015   if((strlen(tmp_froms)<5)||(mascot->mail.status == NO_MAIL)){
2016     err_msg=g_strdup(_("   === Failed to get From/Subject list of arrived mails. ==="));
2017     g_locale_to_utf8(err_msg,-1,NULL,NULL,NULL);
2018     gtk_text_buffer_insert (text_buffer, &start_iter, err_msg,-1);
2019     g_free(err_msg);
2020   }
2021   else{
2022     p=(gchar *)strtok(tmp_froms,"\n");
2023     do{
2024       buf_unmime=g_strdup(p);
2025 
2026       if(g_strncasecmp(buf_unmime," subject:",9)==0){
2027 	gtk_text_buffer_insert_with_tags_by_name (text_buffer, &start_iter,
2028 						  "Subject: ", -1,
2029 						  "heading",
2030 						  "big_gap_after_line",
2031 						  NULL);
2032 	if(g_utf8_validate(buf_unmime+9,-1,NULL)){
2033 	  gtk_text_buffer_insert_with_tags_by_name (text_buffer, &start_iter,
2034 	  					    buf_unmime+9, -1,
2035 	  					    "underline",
2036 	  					    "big_gap_after_line",
2037 	  					    NULL);
2038 	}
2039 	else
2040 	  gtk_text_buffer_insert (text_buffer, &start_iter,
2041 				  "(invalid/no subject in original)", -1);
2042       }
2043       else if(g_strncasecmp(buf_unmime,"from:",5)==0){
2044 	gtk_text_buffer_insert_with_tags_by_name (text_buffer, &start_iter,
2045 						  "From: ", -1,
2046 						  "heading",
2047 						  NULL);
2048 	if(g_utf8_validate(buf_unmime+5,-1,NULL))
2049 	  gtk_text_buffer_insert (text_buffer, &start_iter, buf_unmime+5, -1);
2050 	else
2051 	  gtk_text_buffer_insert (text_buffer, &start_iter,
2052 				  "(invalid/no from in original)", -1);
2053 
2054       }
2055       else{
2056 	if(g_utf8_validate(buf_unmime,-1,NULL))
2057 	  gtk_text_buffer_insert (text_buffer, &start_iter, buf_unmime, -1);
2058       }
2059       gtk_text_buffer_insert (text_buffer, &start_iter, "\n", -1);
2060       if(buf_unmime) g_free(buf_unmime);
2061     }while((p=(gchar *)strtok(NULL,"\n"))!=NULL);
2062   }
2063 
2064   gtk_text_buffer_get_end_iter(text_buffer, &end_iter);
2065   gtk_text_buffer_place_cursor(text_buffer, &end_iter);
2066   end_mark= gtk_text_buffer_create_mark(text_buffer, "end", &end_iter, FALSE);
2067   gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(biff_text),
2068 			       end_mark, 0.0, FALSE, 0.0, 0.0);
2069   gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(biff_text),
2070 			       &end_iter,0.0, FALSE,0.0, 0.0);
2071 
2072 #else
2073   if((strlen(tmp_froms)<5)||(mascot->mail.status == NO_MAIL)){
2074     gtk_text_insert (GTK_TEXT (biff_text), NULL, NULL, NULL,
2075 		     _("   === Failed to get From/Subject list of arrived mails. ==="),-1);
2076   }
2077   else{
2078     p=(gchar *)strtok(tmp_froms,"\n");
2079     do{
2080       gtk_text_insert (GTK_TEXT (biff_text), NULL, NULL, NULL,
2081 		       p, -1);
2082       gtk_text_insert (GTK_TEXT (biff_text), NULL, NULL, NULL,
2083 		       "\n", -1);
2084     }while((p=(gchar *)strtok(NULL,"\n"))!=NULL);
2085   }
2086 
2087   gtk_adjustment_set_value(GTK_TEXT (biff_text)->vadj,
2088     			   (gfloat)(GTK_TEXT(biff_text)->text_len));
2089 #endif
2090 
2091 #ifdef __GTK_STOCK_H__
2092   button=gtkut_button_new_from_stock(_("Start Mailer"),GTK_STOCK_EXECUTE);
2093 #else
2094   button=gtk_button_new_with_label(_("Start Mailer"));
2095 #endif
2096   gtk_table_attach(GTK_TABLE(biff_tbl), button, 0, 1, 2, 3,
2097 		   GTK_FILL,GTK_SHRINK,0,0);
2098   my_signal_connect(button,"clicked",mailer_start, GTK_WIDGET(biff_main));
2099 
2100 
2101 #ifdef __GTK_STOCK_H__
2102   button=gtkut_button_new_from_stock(_("Close"),GTK_STOCK_CLOSE);
2103 #else
2104   button=gtk_button_new_with_label(_("Close"));
2105 #endif
2106   gtk_table_attach(GTK_TABLE(biff_tbl), button, 4, 5, 2, 3,
2107 		   GTK_FILL,GTK_SHRINK,0,0);
2108   my_signal_connect(button,"clicked",close_biff, GTK_WIDGET(biff_main));
2109 
2110   gtk_widget_show_all(biff_main);
2111 
2112   gdk_flush();
2113 }
2114 
2115 
close_biff(GtkWidget * w,GtkWidget * dialog)2116 static void close_biff(GtkWidget *w, GtkWidget *dialog)
2117 {
2118   //gdk_pointer_ungrab(GDK_CURRENT_TIME);
2119 
2120   //Mascot->mail.win_width=dialog->allocation.width;
2121   //Mascot->mail.win_height=dialog->allocation.height;
2122 
2123   while (my_main_iteration(FALSE));
2124   gtk_widget_destroy(GTK_WIDGET(dialog));
2125   while (my_main_iteration(FALSE));
2126 
2127   Mascot->flag_menu=FALSE;
2128   gdk_flush();
2129 }
2130 
2131 
2132 // Biff win����Υᥤ�鵯ư
mailer_start(GtkWidget * w,GtkWidget * dialog)2133 static void mailer_start(GtkWidget *w, GtkWidget *dialog)
2134 {
2135   while (my_main_iteration(FALSE));
2136   gtk_widget_destroy(GTK_WIDGET(dialog));
2137   while (my_main_iteration(FALSE));
2138 
2139   Mascot->flag_menu=FALSE;
2140 
2141   ext_play(Mascot,Mascot->mail.mailer);
2142   gdk_flush();
2143 }
2144 
2145 
set_mhdir()2146 gchar* set_mhdir(){
2147   FILE *fp;
2148   gchar *c=NULL,buf[256],*mhd=NULL, *tmp;
2149 
2150   c=g_strconcat(g_get_home_dir(),PROCMAILRC,NULL);
2151 
2152   if((fp=fopen(c,"r"))!=NULL){
2153     while(!feof(fp)){
2154       if(fgets(buf,256-1,fp)==NULL) break;
2155       if(strncmp(buf,"MAILDIR=",8)==0){
2156 	tmp=buf+8;
2157 	if(strncmp(tmp,"$HOME",5)==0){
2158 	  mhd=g_strconcat(g_get_home_dir(),tmp+5,NULL);
2159 	}
2160 	else{
2161 	  mhd=tmp;
2162 	}
2163 	break;
2164       }
2165     }
2166   }
2167 
2168   if(mhd==NULL){
2169     mhd=g_strconcat(g_get_home_dir(),MH_MAIL_DIR,NULL);
2170   }
2171   if(mhd[strlen(mhd)-1]=='\n') mhd[strlen(mhd)-1]='\0';
2172 
2173   if(c) g_free(c);
2174 
2175   return mhd;
2176 }
2177 
mail_arg_init()2178 void mail_arg_init(){
2179   pop_froms=NULL;
2180   former_newest=0;
2181 }
2182 
strip_last_ret(gchar * p)2183 void strip_last_ret(gchar *p){
2184   if(p[strlen(p)-1]=='\n') p[strlen(p)-1]='\0';
2185 }
2186 
2187 
2188 #endif // USE_BIFF
2189