ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CreditCardReminder/CreditCardReminder.c
(Generate patch)

Comparing CreditCardReminder/CreditCardReminder.c (file contents):
Revision 744 by douglas, 2006-04-08T00:22:17-07:00 vs.
Revision 752 by douglas, 2006-04-12T20:56:29-07:00

# Line 9 | Line 9
9   #include <sys/types.h>
10   #include <regex.h>
11   #include <setjmp.h>
12 + #include <stdbool.h>
13   #include <stdio.h>
14   #include <sys/stat.h>
15   #include <time.h>
# Line 17 | Line 18
18  
19   static jmp_buf environment;
20   static char *error = NULL;
21 + static bool debug = false;
22 + static char *data, *pass;
23  
24   static int check(int value, jmp_buf environment)
25   {
# Line 70 | Line 73 | inline static unsigned short mdate(const
73          return ((date->tm_year + 1900 - BASEYEAR) << 9) + ((date->tm_mon + 1) << 5) + date->tm_mday;
74   }
75  
76 < static void getnext(const struct tm *today, jmp_buf environment)
76 > static void get(const struct tm *today, jmp_buf environment)
77   {
78          struct tm date = *today;
79  
# Line 78 | Line 81 | static void getnext(const struct tm *tod
81  
82          timegm(&date);
83  
84 < #       include <c-client/linkage.c>
82 <
83 <        MAILSTREAM *stream = mcheck(mail_open(NIL, "{reptile.douglasthrift.net/novalidate-cert/tls}", OP_READONLY | OP_DEBUG), environment);
84 >        MAILSTREAM *stream = mcheck(mail_open(NIL, "{reptile.douglasthrift.net/novalidate-cert/tls}", (debug ? OP_DEBUG : 0) | OP_READONLY | OP_SHORTCACHE), environment);
85          STRINGLIST from = { .text = { .data = (unsigned char *)"wellsfargo.com", .size = 14 } }, body = { .text = { .data = (unsigned char *)" is due on ", .size = 11 } };
86          SEARCHPGM search = { .from = &from, .body = &body, .since = mdate(&date) };
87          SORTPGM sort = { .reverse = 1, .function = SORTARRIVAL };
# Line 107 | Line 108 | static void getnext(const struct tm *tod
108                          strptime(date, "%m/%d/%Y", &due);
109                          strftime(date, sizeof (date), "%F", &due);
110  
111 <                        char *path;
111 <
112 <                        check(asprintf(&path, "%s/.CreditCardReminder.data", getenv("HOME")), environment);
113 <
114 <                        FILE *file = fopen(path, "w");
111 >                        FILE *file = fopen(data, "w");
112  
113                          if (!file)
114                                  longjmp(environment, 1);
115  
119                        free(path);
116                          check(fprintf(file, "%s\n", date), environment);
117                          check(fclose(file), environment);
118  
# Line 128 | Line 124 | static void getnext(const struct tm *tod
124          mail_close(stream);
125   }
126  
127 + static void put(const char *message, const struct tm *next, jmp_buf environment)
128 + {
129 +        char date[11], content[strlen(message) + 18];
130 +
131 +        strftime(date, sizeof (date), "%F", next);
132 +        sprintf(content, "Due %s %s!", message, date);
133 +
134 +        SENDSTREAM *stream = mcheck(smtp_open_full(NIL, (char *[]){ "reptile.douglasthrift.net/novalidate-cert", NIL }, "submission", NIL, (debug ? SOP_DEBUG : 0) | SOP_SECURE), environment);
135 +        ADDRESS from = { .mailbox = "douglas", .host = "douglasthrift.net" }, to = { .mailbox = "douglasphone", .host = "douglasthrift.net" };
136 +        ENVELOPE envelope = { .subject = "Credit Card Reminder", .return_path = &from, .to = &to };
137 +        BODY body = { .type = TYPETEXT, .encoding = ENC7BIT, .contents = { .text = { .data = (unsigned char *)content, .size = sizeof (content) } } };
138 +
139 +        if (!smtp_mail(stream, "MAIL", &envelope, &body))
140 +                longjmp(environment, (int)stream->reply);
141 +
142 +        smtp_close(stream);
143 + }
144 +
145   int main(int argc, char *argv[])
146   {
147          int exception;
# Line 151 | Line 165 | int main(int argc, char *argv[])
165                  return 1;
166          }
167  
168 +        for (int index = 1; index != argc; ++index)
169 +                if (!strcmp(argv[index], "-debug"))
170 +                        debug = true;
171 +                else
172 +                        return printf("Usage: %s [-debug]\n", argv[0]), 1;
173 +
174          umask(0077);
175 +        check(asprintf(&data, "%s/.CreditCardReminder.data", getenv("HOME")), environment);
176 +        check(asprintf(&pass, "%s/.CreditCardReminder.pass", getenv("HOME")), environment);
177  
178          struct tm today, next = { .tm_mday = 1, .tm_year = 70 };
179  
# Line 165 | Line 187 | int main(int argc, char *argv[])
187                  today.tm_hour = 0;
188          }
189  
190 + #       include <c-client/linkage.c>
191 +
192          {
193                  jmp_buf environment_;
194  
195                  switch (exception = setjmp(environment_))
196                  {
197 +                case 2:
198 +                        get(&today, environment);
199                  case 0:
200                          break;
201                  case 1:
202                          if (errno == ENOENT)
203                          {
204 <                                getnext(&today, environment);
204 >                                get(&today, environment);
205  
206                                  break;
207                          }
# Line 183 | Line 209 | int main(int argc, char *argv[])
209                          longjmp(environment, exception);
210                  }
211  
212 <                char *path;
187 <
188 <                check(asprintf(&path, "%s/.CreditCardReminder.data", getenv("HOME")), environment);
189 <
190 <                FILE *file = fopen(path, "r");
212 >                FILE *file = fopen(data, "r");
213  
214                  if (!file)
215                          longjmp(environment_, 1);
216  
195                free(path);
196
217                  size_t size;
218                  char *line = fcheck(fgetln(file, &size), file, environment);
219  
220 +                if (!size)
221 +                        longjmp(environment_, 2);
222 +
223                  line[--size] = '\0';
224  
225                  if (!strptime(line, "%F", &next))
226 <                        longjmp(environment, 1);
226 >                        longjmp(environment_, 2);
227  
228                  check(fclose(file), environment);
229          }
230  
231 <        char date[11];
209 <
210 <        strftime(date, sizeof (date), "%F", &next);
211 <        fprintf(stderr, "%s\n", date);
212 <
213 <        return 0;
214 < }
215 <
216 < void mm_flags(MAILSTREAM *stream, unsigned long number)
217 < {
218 <        printf("flags:\n   number = %lu\n", number);
219 < }
220 <
221 < void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS *status)
222 < {
223 <        printf("mailbox %s:\n", mailbox);
224 <
225 <        if (status->flags & SA_MESSAGES)
226 <                printf("   messages = %lu\n", status->messages);
227 <
228 <        if (status->flags & SA_RECENT)
229 <                printf("   recent = %lu\n", status->recent);
230 <
231 <        if (status->flags & SA_UNSEEN)
232 <                printf("   unseen = %lu\n", status->unseen);
233 <
234 <        if (status->flags & SA_UIDNEXT)
235 <                printf("   uidnext = %lu\n", status->uidnext);
231 >        time_t apart = difftime(timegm(&next), timegm(&today));
232  
233 <        if (status->flags & SA_UIDVALIDITY)
234 <                printf("   uidvalidity = %lu\n", status->uidvalidity);
235 < }
236 <
241 < void mm_searched(MAILSTREAM *stream, unsigned long number)
242 < {
243 <        printf("searched:\n   number = %lu\n", number);
244 < }
233 >        switch (apart)
234 >        {
235 >        case 0:
236 >                put("today", &next, environment);
237  
238 < void mm_exists(MAILSTREAM *stream, unsigned long number)
239 < {
240 <        printf("exists:\n   number = %lu\n", number);
249 < }
238 >                break;
239 >        case 86400:
240 >                put("tomorrow", &next, environment);
241  
242 < void mm_expunged(MAILSTREAM *stream, unsigned long number)
243 < {
244 <        printf("expunged:\n   number = %lu\n", number);
245 < }
242 >                break;
243 >        default:
244 >                if (apart < 0)
245 >                        check(unlink(data), environment);
246 >        }
247  
248 < void mm_list(MAILSTREAM *stream, int delim, char *name, long attrib)
249 < {
258 <        printf("list");
259 < }
248 >        free(data);
249 >        free(pass);
250  
251 < void mm_lsub(MAILSTREAM *stream, int delim, char *name, long attrib)
262 < {
263 <        printf("lsub");
251 >        return 0;
252   }
253  
254 < void mm_notify(MAILSTREAM *stream, char *string, long errflg)
255 < {
256 <        char *flag;
257 <
258 <        asprintf(&flag, "%li", errflg);
259 <        printf("notify %s:\n   string = %s\n", errflg == NIL ? "NIL" : errflg == WARN ? "WARN" : errflg == ERROR ? "ERROR" : flag, string);
260 <        free(flag);
261 < }
254 > void mm_flags(MAILSTREAM *stream, unsigned long number) {}
255 > void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS *status) {}
256 > void mm_searched(MAILSTREAM *stream, unsigned long number) {}
257 > void mm_exists(MAILSTREAM *stream, unsigned long number) {}
258 > void mm_expunged(MAILSTREAM *stream, unsigned long number) {}
259 > void mm_list(MAILSTREAM *stream, int delim, char *name, long attrib) {}
260 > void mm_lsub(MAILSTREAM *stream, int delim, char *name, long attrib) {}
261 > void mm_notify(MAILSTREAM *stream, char *string, long errflg) {}
262  
263   void mm_log(char *string, long errflg)
264   {
277 /*      char *flag;
278
279        asprintf(&flag, "%li", errflg);
280        printf("log %s:\n   string = %s\n", errflg == NIL ? "NIL" : errflg == PARSE ? "PARSE" : errflg == WARN ? "WARN" : errflg == ERROR ? "ERROR" : flag, string);
281        free(flag);*/
282
265          if (errflg == ERROR)
266                  asprintf(&error, "%s", string);
267   }
268  
269   void mm_dlog(char *string)
270   {
271 <        printf("%s\n", string);
271 >        fprintf(stderr, "%s\n", string);
272   }
273  
274   void mm_login(NETMBX *mb, char *user, char *pwd, long trial)
275   {
276          strcpy(user, "douglas");
277  
278 <        char *path;
297 <
298 <        check(asprintf(&path, "%s/.CreditCardReminder.pass", getenv("HOME")), environment);
299 <
300 <        FILE *file = fopen(path, "r");
278 >        FILE *file = fopen(pass, "r");
279  
280          if (!file)
281                  longjmp(environment, 1);
282  
305        free(path);
306                
283          size_t size;
284          char *line = fcheck(fgetln(file, &size), file, environment);
285  
# Line 312 | Line 288 | void mm_login(NETMBX *mb, char *user, ch
288  
289   void mm_critical(MAILSTREAM *stream)
290   {
291 <        printf("critical\n");
291 >        fprintf(stderr, "critical\n");
292   }
293  
294   void mm_nocritical(MAILSTREAM *stream)
295   {
296 <        printf("nocritical\n");
296 >        fprintf(stderr, "nocritical\n");
297   }
298  
299   long mm_diskerror(MAILSTREAM *stream, long errcode, long serious)
300   {
301 <        printf("diskerror:\n   errcode = %li\n   serious = %li\n", errcode, serious);
301 >        fprintf(stderr, "diskerror:\n   errcode = %li\n   serious = %li\n", errcode, serious);
302  
303          return 1;
304   }
305  
306   void mm_fatal(char *string)
307   {
308 <        printf("fatal:\n   string = %s\n", string);
308 >        fprintf(stderr, "fatal:\n   string = %s\n", string);
309   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines