mail: put imap and pop3 code to where it belongs
authorPhil Sutter <phil@nwl.cc>
Sat, 24 Oct 2009 23:17:30 +0000 (01:17 +0200)
committerPhil Sutter <phil@nwl.cc>
Tue, 3 Nov 2009 00:50:28 +0000 (01:50 +0100)
src/conky.c
src/core.c
src/mail.c
src/mail.h

index db88a3c..13a6ea7 100644 (file)
@@ -708,52 +708,6 @@ void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct
        generate_text_internal(p, max_user_text, *root, cur);
 }
 
-static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
-               void *thread(void *), const char *text)
-{
-       if (obj->char_b && info.mail) {
-               // this means we use info
-               if (!info.mail->p_timed_thread) {
-                       info.mail->p_timed_thread =
-                               timed_thread_create(thread,
-                                               (void *) info.mail, info.mail->interval * 1000000);
-                       if (!info.mail->p_timed_thread) {
-                               NORM_ERR("Error creating %s timed thread", text);
-                       }
-                       timed_thread_register(info.mail->p_timed_thread,
-                                       &info.mail->p_timed_thread);
-                       if (timed_thread_run(info.mail->p_timed_thread)) {
-                               NORM_ERR("Error running %s timed thread", text);
-                       }
-               }
-               return info.mail;
-       } else if (obj->data.mail) {
-               // this means we use obj
-               if (!obj->data.mail->p_timed_thread) {
-                       obj->data.mail->p_timed_thread =
-                               timed_thread_create(thread,
-                                               (void *) obj->data.mail,
-                                               obj->data.mail->interval * 1000000);
-                       if (!obj->data.mail->p_timed_thread) {
-                               NORM_ERR("Error creating %s timed thread", text);
-                       }
-                       timed_thread_register(obj->data.mail->p_timed_thread,
-                                       &obj->data.mail->p_timed_thread);
-                       if (timed_thread_run(obj->data.mail->p_timed_thread)) {
-                               NORM_ERR("Error running %s timed thread", text);
-                       }
-               }
-               return obj->data.mail;
-       } else if (!obj->a) {
-               // something is wrong, warn once then stop
-               NORM_ERR("There's a problem with your mail settings.  "
-                               "Check that the global mail settings are properly defined"
-                               " (line %li).", obj->line);
-               obj->a++;
-       }
-       return NULL;
-}
-
 char *format_time(unsigned long timeval, const int width)
 {
        char buf[10];
@@ -1336,41 +1290,16 @@ void generate_text_internal(char *p, int p_max_size,
                                print_texeci(obj, p, p_max_size);
                        }
                        OBJ(imap_unseen) {
-                               struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
-
-                               if (mail && mail->p_timed_thread) {
-                                       timed_thread_lock(mail->p_timed_thread);
-                                       snprintf(p, p_max_size, "%lu", mail->unseen);
-                                       timed_thread_unlock(mail->p_timed_thread);
-                               }
+                               print_imap_unseen(obj, p, p_max_size);
                        }
                        OBJ(imap_messages) {
-                               struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
-
-                               if (mail && mail->p_timed_thread) {
-                                       timed_thread_lock(mail->p_timed_thread);
-                                       snprintf(p, p_max_size, "%lu", mail->messages);
-                                       timed_thread_unlock(mail->p_timed_thread);
-                               }
+                               print_imap_messages(obj, p, p_max_size);
                        }
                        OBJ(pop3_unseen) {
-                               struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
-
-                               if (mail && mail->p_timed_thread) {
-                                       timed_thread_lock(mail->p_timed_thread);
-                                       snprintf(p, p_max_size, "%lu", mail->unseen);
-                                       timed_thread_unlock(mail->p_timed_thread);
-                               }
+                               print_pop3_unseen(obj, p, p_max_size);
                        }
                        OBJ(pop3_used) {
-                               struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
-
-                               if (mail && mail->p_timed_thread) {
-                                       timed_thread_lock(mail->p_timed_thread);
-                                       snprintf(p, p_max_size, "%.1f",
-                                               mail->used / 1024.0 / 1024.0);
-                                       timed_thread_unlock(mail->p_timed_thread);
-                               }
+                               print_pop3_used(obj, p, p_max_size);
                        }
                        OBJ(fs_bar) {
                                print_fs_bar(obj, 0, p, p_max_size);
index 36d1e34..6d1a4e8 100644 (file)
@@ -1042,37 +1042,13 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
        END OBJ(apm_battery_time, 0)
 #endif /* __FreeBSD__ */
        END OBJ(imap_unseen, 0)
-               if (arg) {
-                       // proccss
-                       obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
-                       obj->char_b = 0;
-               } else {
-                       obj->char_b = 1;
-               }
+               parse_imap_mail_args(obj, arg);
        END OBJ(imap_messages, 0)
-               if (arg) {
-                       // proccss
-                       obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
-                       obj->char_b = 0;
-               } else {
-                       obj->char_b = 1;
-               }
+               parse_imap_mail_args(obj, arg);
        END OBJ(pop3_unseen, 0)
-               if (arg) {
-                       // proccss
-                       obj->data.mail = parse_mail_args(POP3_TYPE, arg);
-                       obj->char_b = 0;
-               } else {
-                       obj->char_b = 1;
-               }
+               parse_pop3_mail_args(obj, arg);
        END OBJ(pop3_used, 0)
-               if (arg) {
-                       // proccss
-                       obj->data.mail = parse_mail_args(POP3_TYPE, arg);
-                       obj->char_b = 0;
-               } else {
-                       obj->char_b = 1;
-               }
+               parse_pop3_mail_args(obj, arg);
 #ifdef IBM
        END OBJ_ARG(smapi, 0, "smapi needs an argument")
                obj->data.s = strndup(arg, text_buffer_size);
@@ -1627,24 +1603,10 @@ void free_text_objects(struct text_object *root, int internal)
                                free(data.local_mail.mbox);
                                break;
                        case OBJ_imap_unseen:
-                               if (!obj->char_b) {
-                                       free(data.mail);
-                               }
-                               break;
                        case OBJ_imap_messages:
-                               if (!obj->char_b) {
-                                       free(data.mail);
-                               }
-                               break;
                        case OBJ_pop3_unseen:
-                               if (!obj->char_b) {
-                                       free(data.mail);
-                               }
-                               break;
                        case OBJ_pop3_used:
-                               if (!obj->char_b) {
-                                       free(data.mail);
-                               }
+                               free_mail_obj(obj);
                                break;
                        case OBJ_if_empty:
                        case OBJ_if_match:
index fad7858..dbcdb1c 100644 (file)
@@ -33,6 +33,7 @@
 #include "common.h"
 #include "logging.h"
 #include "mail.h"
+#include "text_object.h"
 
 #include <errno.h>
 #include <stdio.h>
@@ -387,6 +388,35 @@ struct mail_s *parse_mail_args(char type, const char *arg)
        return mail;
 }
 
+void parse_imap_mail_args(struct text_object *obj, const char *arg)
+{
+       if (!arg) {
+               obj->char_b = 1;
+               return;
+       }
+       // proccss
+       obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
+       obj->char_b = 0;
+}
+
+void parse_pop3_mail_args(struct text_object *obj, const char *arg)
+{
+       if (!arg) {
+               obj->char_b = 1;
+               return;
+       }
+       // proccss
+       obj->data.mail = parse_mail_args(POP3_TYPE, arg);
+       obj->char_b = 0;
+}
+
+void free_mail_obj(struct text_object *obj)
+{
+       if (!obj->char_b) {
+               free(obj->data.mail);
+       }
+}
+
 int imap_command(int sockfd, const char *command, char *response, const char *verify)
 {
        struct timeval fetchtimeout;
@@ -448,7 +478,53 @@ void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned
        }
 }
 
-void *imap_thread(void *arg)
+static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
+               void *thread(void *), const char *text)
+{
+       if (obj->char_b && info.mail) {
+               // this means we use info
+               if (!info.mail->p_timed_thread) {
+                       info.mail->p_timed_thread =
+                               timed_thread_create(thread,
+                                               (void *) info.mail, info.mail->interval * 1000000);
+                       if (!info.mail->p_timed_thread) {
+                               NORM_ERR("Error creating %s timed thread", text);
+                       }
+                       timed_thread_register(info.mail->p_timed_thread,
+                                       &info.mail->p_timed_thread);
+                       if (timed_thread_run(info.mail->p_timed_thread)) {
+                               NORM_ERR("Error running %s timed thread", text);
+                       }
+               }
+               return info.mail;
+       } else if (obj->data.mail) {
+               // this means we use obj
+               if (!obj->data.mail->p_timed_thread) {
+                       obj->data.mail->p_timed_thread =
+                               timed_thread_create(thread,
+                                               (void *) obj->data.mail,
+                                               obj->data.mail->interval * 1000000);
+                       if (!obj->data.mail->p_timed_thread) {
+                               NORM_ERR("Error creating %s timed thread", text);
+                       }
+                       timed_thread_register(obj->data.mail->p_timed_thread,
+                                       &obj->data.mail->p_timed_thread);
+                       if (timed_thread_run(obj->data.mail->p_timed_thread)) {
+                               NORM_ERR("Error running %s timed thread", text);
+                       }
+               }
+               return obj->data.mail;
+       } else if (!obj->a) {
+               // something is wrong, warn once then stop
+               NORM_ERR("There's a problem with your mail settings.  "
+                               "Check that the global mail settings are properly defined"
+                               " (line %li).", obj->line);
+               obj->a++;
+       }
+       return NULL;
+}
+
+static void *imap_thread(void *arg)
 {
        int sockfd, numbytes;
        char recvbuf[MAXDATASIZE];
@@ -743,6 +819,28 @@ void *imap_thread(void *arg)
        return 0;
 }
 
+void print_imap_unseen(struct text_object *obj, char *p, int p_max_size)
+{
+       struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
+
+       if (mail && mail->p_timed_thread) {
+               timed_thread_lock(mail->p_timed_thread);
+               snprintf(p, p_max_size, "%lu", mail->unseen);
+               timed_thread_unlock(mail->p_timed_thread);
+       }
+}
+
+void print_imap_messages(struct text_object *obj, char *p, int p_max_size)
+{
+       struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
+
+       if (mail && mail->p_timed_thread) {
+               timed_thread_lock(mail->p_timed_thread);
+               snprintf(p, p_max_size, "%lu", mail->messages);
+               timed_thread_unlock(mail->p_timed_thread);
+       }
+}
+
 int pop3_command(int sockfd, const char *command, char *response, const char *verify)
 {
        struct timeval fetchtimeout;
@@ -771,7 +869,7 @@ int pop3_command(int sockfd, const char *command, char *response, const char *ve
        return 0;
 }
 
-void *pop3_thread(void *arg)
+static void *pop3_thread(void *arg)
 {
        int sockfd, numbytes;
        char recvbuf[MAXDATASIZE];
@@ -921,3 +1019,25 @@ void *pop3_thread(void *arg)
        return 0;
 }
 
+void print_pop3_unseen(struct text_object *obj, char *p, int p_max_size)
+{
+       struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
+
+       if (mail && mail->p_timed_thread) {
+               timed_thread_lock(mail->p_timed_thread);
+               snprintf(p, p_max_size, "%lu", mail->unseen);
+               timed_thread_unlock(mail->p_timed_thread);
+       }
+}
+
+void print_pop3_used(struct text_object *obj, char *p, int p_max_size)
+{
+       struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
+
+       if (mail && mail->p_timed_thread) {
+               timed_thread_lock(mail->p_timed_thread);
+               snprintf(p, p_max_size, "%.1f",
+                               mail->used / 1024.0 / 1024.0);
+               timed_thread_unlock(mail->p_timed_thread);
+       }
+}
index 21664d8..e6cb90e 100644 (file)
@@ -6,6 +6,9 @@
 #include "timed_thread.h"
 #include <time.h>
 
+/* forward declare to make gcc happy */
+struct text_object;
+
 extern char *current_mail_spool;
 
 struct mail_s {                        // for imap and pop3
@@ -50,8 +53,15 @@ void update_mail_count(struct local_mail_s *);
 #define POP3_TYPE 1
 #define IMAP_TYPE 2
 
-struct mail_s *parse_mail_args(char type, const char *arg);
-void *imap_thread(void *arg);
-void *pop3_thread(void *arg);
+/* FIXME: this is here for the config leftovers only */
+struct mail_s *parse_mail_args(char, const char *);
+
+void parse_imap_mail_args(struct text_object *, const char *);
+void parse_pop3_mail_args(struct text_object *, const char *);
+void free_mail_obj(struct text_object *);
+void print_imap_unseen(struct text_object *, char *, int);
+void print_imap_messages(struct text_object *, char *, int);
+void print_pop3_unseen(struct text_object *, char *, int);
+void print_pop3_used(struct text_object *, char *, int);
 
 #endif /* _MAIL_H */