cmd2twitter

コマンドラインで打ったものをTwitterにpostする。 cURLが必要。 chatty を改造しています。

diff

diff -uw chatty-0.1/chatty.c cmd2twitter.c :

  • .c

    old new  
    4141 * - modify `script' to create `chatty'. 
    4242 */ 
    4343 
     44/* 2007-11-23 Motohiro Takayama <mootoh@gmail.com> 
     45 * - modify `chatty' to create `cmd2twitter'. 
     46 */ 
     47 
    4448/* 
    4549 * script 
    4650 */ 
     
    6367#ifdef __linux__ 
    6468#include <unistd.h> 
    6569#include <string.h> 
     70#else 
     71#ifdef __APPLE__ 
     72#include <unistd.h> 
     73#include <string.h> 
     74#endif 
    6675#endif 
    6776 
    6877#include <sys/time.h> 
    69 #include "dict.h" 
    7078 
    7179#define HAVE_inet_aton 
    7280#define HAVE_scsi_h 
     
    106114int     subchild; 
    107115char    *fname; 
    108116 
     117char user[BUFSIZ]; 
     118char pass[BUFSIZ]; 
     119char post[BUFSIZ]; 
     120 
    109121struct  termios tt; 
    110122struct  winsize win; 
    111123int     lb; 
     
    124136    char *tail; 
    125137} RevBuf; 
    126138 
    127 Dict*   read_dict               (const char *file_name); 
    128139RevBuf* revbuf_new              (void); 
    129140void    revbuf_add              (RevBuf *revbuf, int ch); 
    130141void    revbuf_clear            (RevBuf *revbuf); 
    131142char*   revbuf_top              (RevBuf *revbuf); 
    132143int     revbuf_full_p           (RevBuf *revbuf); 
    133 void    watch_input             (Dict *dict, RevBuf *revbuf, 
     144void    watch_input             (RevBuf *revbuf, 
    134145                                 const char *str, int len); 
    135146 
    136147int 
     
    143154        void finish(); 
    144155        char *getenv(); 
    145156        char *command = NULL; 
     157  char *usage = "usage: cmd2twitter <username> <password>\n"; 
    146158 
    147159        while ((ch = getopt(argc, argv, "h?")) != EOF) 
    148160                switch((char)ch) { 
    149161                case 'h': 
    150162                case '?': 
    151163                default: 
    152                         fprintf(stderr, _("usage: chatty <dict>\n")); 
     164        fprintf(stderr, _(usage)); 
    153165                        exit(1); 
    154166                } 
    155167        argc -= optind; 
    156168        argv += optind; 
    157169 
    158         if (argc != 1) { 
    159             fprintf(stderr, _("usage: chatty <dict>\n")); 
     170  if (argc != 2) { 
     171    fprintf(stderr, _(usage)); 
    160172            exit(1); 
    161173        } 
    162174 
     
    184196                else 
    185197                        doshell(command); 
    186198        } 
     199 
     200  strncpy(user, argv[0], BUFSIZ); 
     201  strncpy(pass, argv[1], BUFSIZ); 
     202 
    187203        doinput(argv[0]); 
    188204 
    189205        return 0; 
     
    203219    return str; 
    204220} 
    205221 
    206 Dict * 
    207 read_dict (const char *file_name) 
    208 { 
    209     char buf[BUFSIZ]; 
    210     Dict *dict = dict_new(); 
    211     FILE *fp = fopen(file_name, "r"); 
    212  
    213     if (fp == NULL) { 
    214         fprintf(stderr, "chatty: %s: %s\n", file_name, strerror(errno)); 
    215         exit(1); 
    216     } 
    217  
    218     while (fgets(buf, BUFSIZ, fp)) { 
    219         char *tabpos; 
    220         buf[strlen(buf)-1] = '\0'; 
    221         tabpos = strchr(buf, '\t'); 
    222         if (tabpos) { 
    223             char *key, value[BUFSIZ]; 
    224             *tabpos = '\0'; 
    225             key = buf; 
    226             if (strlen(key) > 0) { 
    227                 char *rkey = reverse(key); 
    228                 sprintf(value, "%s: %s", key, tabpos + 1); 
    229                 dict_add(dict, rkey, value); 
    230                 free(rkey); 
    231             } 
    232         } 
    233     } 
    234     fclose(fp); 
    235     return dict; 
    236 } 
    237  
    238222RevBuf * 
    239223revbuf_new (void) 
    240224{ 
     
    272256    revbuf->top = revbuf->tail; 
    273257} 
    274258 
     259void reverse_to_dst(char *dst, const char *src) { 
     260  int i; 
     261  int len = strlen(src)-1; 
     262  for (i=0; i<len; i++) { 
     263    dst[i] = src[len-i]; 
     264  } 
     265} 
     266 
    275267void 
    276 watch_input (Dict *dict, RevBuf *revbuf, const char *str, int len) 
     268watch_input (RevBuf *revbuf, const char *str, int len) 
    277269{ 
    278270    int i; 
    279271 
    280272    for (i = 0; i < len; i++) { 
    281273        int  ch = str[i]; 
    282         char *message; 
    283274        if (revbuf_full_p(revbuf)) 
    284275            revbuf_clear(revbuf); 
    285276        revbuf_add(revbuf, ch); 
    286         message = dict_search_longest(dict, revbuf_top(revbuf)); 
    287         if (message) 
    288             printf("\033]0;%s\a", message); 
    289         if (ch == '\n' || ch == '\r') 
     277 
     278    if (ch == '\n' || ch == '\r') { 
     279      char status[BUFSIZ]; 
     280      memset(status, 0, BUFSIZ); 
     281      memset(post, 0, BUFSIZ); 
     282      reverse_to_dst(status, revbuf_top(revbuf)); 
     283      snprintf(post, BUFSIZ, "curl -s " 
     284        "--basic --user \"%s:%s\" --data-ascii " 
     285        "\"status=%s\" " 
     286        "\"http://twitter.com/statuses/update.json\"", 
     287        user, pass, status); 
     288      system(post); 
     289 
    290290            revbuf_clear(revbuf); 
    291291    } 
    292292} 
     293} 
    293294 
    294295void 
    295296doinput(const char *file_name) 
     
    297298        register int cc; 
    298299        char ibuf[BUFSIZ]; 
    299300 
    300         Dict*   dict   = read_dict(file_name); 
    301301        RevBuf* revbuf = revbuf_new(); 
    302302 
    303303        setbuf(stdout, NULL); 
    304304#ifdef HAVE_openpty 
    305305        (void) close(slave); 
    306306#endif 
     307 
    307308        while ((cc = read(0, ibuf, BUFSIZ)) > 0) { 
    308                 watch_input(dict, revbuf, ibuf, cc); 
     309    watch_input(revbuf, ibuf, cc); 
    309310                (void) write(master, ibuf, cc); 
    310311        } 
    311312        done();