forked from hayamiz/twittering-mode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
twittering-mode.el
7274 lines (6743 loc) · 260 KB
/
twittering-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; -*- indent-tabs-mode: t; tab-width: 8 -*-
;;;
;;; twittering-mode.el --- Major mode for Twitter
;; Copyright (C) 2007, 2009, 2010 Yuto Hayamizu.
;; 2008 Tsuyoshi CHO
;; Author: Y. Hayamizu <[email protected]>
;; Tsuyoshi CHO <[email protected]>
;; Alberto Garcia <[email protected]>
;; Created: Sep 4, 2007
;; Version: HEAD
;; Identity: $Id$
;; Keywords: twitter web
;; URL: http://twmode.sf.net/
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; twittering-mode.el is a major mode for Twitter.
;; You can check friends timeline, and update your status on Emacs.
;;; Feature Request:
;; URL : http://twitter.com/d00dle/statuses/577876082
;; * Status Input from Popup buffer and C-cC-c to POST.
;; URL : http://code.nanigac.com/source/view/419
;; * update status for region
;;; Code:
(eval-when-compile (require 'cl))
(require 'xml)
(require 'parse-time)
(when (> 22 emacs-major-version)
(setq load-path
(append (mapcar (lambda (dir)
(expand-file-name
dir
(if load-file-name
(or (file-name-directory load-file-name)
".")
".")))
'("url-emacs21" "emacs21"))
load-path))
(and (require 'un-define nil t)
;; the explicitly require 'unicode to update a workaround with
;; navi2ch. see a comment of `twittering-ucs-to-char' for more
;; details.
(require 'unicode nil t)))
(require 'url)
(defconst twittering-mode-version "HEAD")
(defconst twittering-mode-identity "$Id$")
(defvar twittering-api-host "api.twitter.com")
(defvar twittering-api-search-host "search.twitter.com")
(defvar twittering-web-host "twitter.com")
(defvar twittering-oauth-request-token-url-without-scheme
"://api.twitter.com/oauth/request_token")
(defvar twittering-oauth-authorization-url-base-without-scheme
"://api.twitter.com/oauth/authorize?oauth_token=")
(defvar twittering-oauth-access-token-url-without-scheme
"://api.twitter.com/oauth/access_token")
(defun twittering-mode-version ()
"Display a message for twittering-mode version."
(interactive)
(let ((version-string
(format "twittering-mode-v%s" twittering-mode-version)))
(if (interactive-p)
(message "%s" version-string)
version-string)))
(defvar twittering-auth-method 'oauth
"*Authentication method for `twittering-mode'.
The symbol `basic' means Basic Authentication. The symbol `oauth' means
OAuth Authentication. The symbol `xauth' means xAuth Authentication.
OAuth Authentication requires `twittering-oauth-consumer-key' and
`twittering-oauth-consumer-secret'. Additionally, it requires an external
command `curl' or another command included in `tls-program', which may be
`openssl' or `gnutls-cli', for SSL.")
(defvar twittering-account-authorization nil
"State of account authorization for `twittering-username' and
`twittering-password'. The value is one of the following symbols:
nil -- The account have not been authorized yet.
queried -- The authorization has been queried, but not finished yet.
authorized -- The account has been authorized.")
(defvar twittering-oauth-use-ssl t
"*Whether to use SSL on authentication via OAuth. Twitter requires SSL
on authorization via OAuth.")
(defvar twittering-oauth-invoke-browser nil
"*Whether to invoke a browser on authorization of access key automatically.")
(defvar twittering-oauth-consumer-key nil)
(defvar twittering-oauth-consumer-secret nil)
(defvar twittering-oauth-access-token-alist nil)
(defconst twittering-max-number-of-tweets-on-retrieval 200
"The maximum number of `twittering-number-of-tweets-on-retrieval'.")
(defvar twittering-number-of-tweets-on-retrieval 20
"*The number of tweets which will be retrieved in one request.
The upper limit is `twittering-max-number-of-tweets-on-retrieval'.")
(defvar twittering-tinyurl-service 'tinyurl
"The service to use. One of 'tinyurl' or 'toly'.")
(defvar twittering-tinyurl-services-map
'((tinyurl . "http://tinyurl.com/api-create.php?url=")
(toly . "http://to.ly/api.php?longurl="))
"Alist of tinyfy services.")
(defvar twittering-mode-map (make-sparse-keymap))
(defvar twittering-mode-menu-on-uri-map (make-sparse-keymap "Twittering Mode"))
(defvar twittering-mode-on-uri-map (make-sparse-keymap))
(defvar twittering-tweet-history nil)
(defvar twittering-user-history nil)
(defvar twittering-timeline-history nil)
(defvar twittering-hashtag-history nil)
(defvar twittering-search-history nil)
(defvar twittering-current-hashtag nil
"A hash tag string currently set. You can set it by calling
`twittering-set-current-hashtag'.")
(defvar twittering-timer nil
"Timer object for timeline refreshing will be stored here.
DO NOT SET VALUE MANUALLY.")
(defvar twittering-timer-interval 90
"The interval of auto reloading. You should use 60 or more
seconds for this variable because the number of API call is
limited by the hour.")
(defvar twittering-timer-for-redisplaying nil
"Timer object for timeline redisplay statuses will be stored here.
DO NOT SET VALUE MANUALLY.")
(defvar twittering-timer-interval-for-redisplaying 5.0
"The interval of auto redisplaying statuses.
Each time Emacs remains idle for the interval, twittering-mode updates parts
requiring to be redrawn.")
(defvar twittering-username nil
"*An username of your Twitter account.")
(defvar twittering-password nil
"*A password of your Twitter account. Leave it blank is the
recommended way because writing a password in .emacs file is so
dangerous.")
(defvar twittering-initial-timeline-spec-string ":home"
"*The initial timeline spec string. If the value of the variable is a
list of timeline spec strings, the timelines are rendered on their own
buffers.")
(defvar twittering-timeline-spec nil
"The timeline spec for the current buffer.")
(defvar twittering-timeline-spec-string ""
"The timeline spec string for the current buffer.")
(defvar twittering-timeline-spec-alias nil
"*Alist for aliases of timeline spec.
Each element is (NAME . SPEC-STRING), where NAME is a string and
SPEC-STRING is a string or a function that returns a timeline spec string.
The alias can be referred as \"$NAME\" or \"$NAME(ARG)\" in timeline spec
string. If SPEC-STRING is a string, ARG is simply ignored.
If SPEC-STRING is a function, it is called with a string argument.
For the style \"$NAME\", the function is called with nil.
For the style \"$NAME(ARG)\", the function is called with a string ARG.
For example, if you specify
`((\"FRIENDS\" . \"my-account/friends-list\")
(\"related-to\" .
,(lambda (username)
(if username
(format \":search/to:%s OR from:%s OR @%s/\"
username username username)
\":home\")))),
then you can use \"$FRIENDS\" and \"$related-to(USER)\" as
\"my-account/friends-list\" and \":search/to:USER OR from:USER OR @USER/\",
respectively.")
(defvar twittering-current-timeline-spec-string nil
"The current timeline spec string. This variable should not be referred
directly. Use `twittering-current-timeline-spec-string' or
`twittering-current-timeline-spec'.")
(defvar twittering-list-index-retrieved nil)
(defvar twittering-process-info-alist nil
"Alist of active process and timeline spec retrieved by the process.")
(defvar twittering-server-info-alist nil
"Alist of server information.")
(defvar twittering-new-tweets-count 0
"Number of new tweets when `twittering-new-tweets-hook' is run.")
(defvar twittering-new-tweets-spec nil
"Timeline spec, which new tweets belong to, when
`twittering-new-tweets-hook' is run.")
(defvar twittering-new-tweets-statuses nil
"New tweet status messages, when
`twittering-new-tweets-hook' is run.")
(defvar twittering-new-tweets-hook nil
"*Hook run when new tweets are received.
You can read `twittering-new-tweets-count' or `twittering-new-tweets-spec'
to get the number of new tweets received when this hook is run.")
(defvar twittering-active-mode nil
"Non-nil if new statuses should be retrieved periodically.
Do not modify this variable directly. Use `twittering-activate-buffer',
`twittering-deactivate-buffer', `twittering-toggle-activate-buffer' or
`twittering-set-active-flag-for-buffer'.")
(defvar twittering-scroll-mode nil)
(defvar twittering-jojo-mode nil)
(defvar twittering-reverse-mode nil
"*Non-nil means tweets are aligned in reverse order of `http://twitter.com/'.")
(defvar twittering-display-remaining nil
"*If non-nil, display remaining of rate limit on the mode-line.")
(defvar twittering-display-connection-method t
"*If non-nil, display the current connection method on the mode-line.")
(defvar twittering-status-format "%i %s, %@:\n%FILL[ ]{%T // from %f%L%r%R}\n "
"Format string for rendering statuses.
Ex. \"%i %s, %@:\\n%FILL{ %T // from %f%L%r%R}\n \"
Items:
%s - screen_name
%S - name
%i - profile_image
%d - description
%l - location
%L - \" [location]\"
%r - \" sent to user\" (use on direct_messages{,_sent})
%r - \" in reply to user\" (use on other standard timeline)
%R - \" (retweeted by user)\"
%u - url
%j - user.id
%p - protected?
%c - created_at (raw UTC string)
%C{time-format-str} - created_at (formatted with time-format-str)
%@ - X seconds ago
%T - raw text
%t - text filled as one paragraph
%' - truncated
%FACE[face-name]{...} - strings decorated with the specified face.
%FILL[prefix]{...} - strings filled as a paragraph. The prefix is optional.
You can use any other specifiers in braces.
%FOLD[prefix]{...} - strings folded within the frame width.
The prefix is optional. This keeps newlines.
You can use any other specifiers in braces.
%f - source
%# - id
")
(defvar twittering-retweet-format "RT: %t (via @%s)"
"Format string for retweet.
Items:
%s - screen_name
%t - text
%% - %
")
(defvar twittering-fill-column nil
"*The fill-column used for \"%FILL{...}\" in `twittering-status-format'.
If nil, the fill-column is automatically calculated.")
(defvar twittering-show-replied-tweets t
"*The number of replied tweets which will be showed in one tweet.
If the value is not a number and is non-nil, show all replied tweets
which is already fetched.
If the value is nil, doesn't show replied tweets.")
(defvar twittering-default-show-replied-tweets nil
"*The number of default replied tweets which will be showed in one tweet.
This value will be used only when showing new tweets.
See `twittering-show-replied-tweets' for more details.")
(defvar twittering-use-show-minibuffer-length t
"*Show current length of minibuffer if this variable is non-nil.
We suggest that you should set to nil to disable the showing function
when it conflict with your input method (such as AquaSKK, etc.)")
(defvar twittering-notify-successful-http-get t)
(defvar twittering-use-ssl t
"Use SSL connection if this variable is non-nil.
SSL connections use 'curl' command as a backend.")
(defvar twittering-allow-insecure-server-cert nil
"*If non-nil, twittering-mode allows insecure server certificates.")
(defvar twittering-curl-program nil
"Cache a result of `twittering-find-curl-program'.
DO NOT SET VALUE MANUALLY.")
(defvar twittering-curl-program-https-capability nil
"Cache a result of `twittering-start-http-session-curl-https-p'.
DO NOT SET VALUE MANUALLY.")
(defvar twittering-wget-program nil
"Cache a result of `twittering-find-wget-program'.
DO NOT SET VALUE MANUALLY.")
(defvar twittering-tls-program nil
"*List of strings containing commands to start TLS stream to a host.
Each entry in the list is tried until a connection is successful.
%h is replaced with server hostname, %p with port to connect to.
Also see `tls-program'.
If nil, this is initialized with a list of valied entries extracted from
`tls-program'.")
(defvar twittering-connection-type-order
'(curl wget urllib-http native urllib-https))
"*A list of connection methods in the preferred order."
(defvar twittering-connection-type-table
'((native (check . t)
(https . twittering-start-http-session-native-tls-p)
(send-http-request . twittering-send-http-request-native)
(pre-process-buffer . twittering-pre-process-buffer-native))
(curl (check . twittering-start-http-session-curl-p)
(https . twittering-start-http-session-curl-https-p)
(send-http-request . twittering-send-http-request-curl)
(pre-process-buffer . twittering-pre-process-buffer-curl))
(wget (check . twittering-start-http-session-wget-p)
(https . t)
(send-http-request . twittering-send-http-request-wget)
(pre-process-buffer . twittering-pre-process-buffer-wget))
(urllib-http
(display-name . "urllib")
(check . twittering-start-http-session-urllib-p)
(https . nil)
(send-http-request . twittering-send-http-request-urllib)
(pre-process-buffer . twittering-pre-process-buffer-urllib))
(urllib-https
(display-name . "urllib")
(check . twittering-start-http-session-urllib-p)
(https . twittering-start-http-session-urllib-https-p)
(send-http-request . twittering-send-http-request-urllib)
(pre-process-buffer . twittering-pre-process-buffer-urllib)))
"A list of alist of connection methods.")
(defvar twittering-format-status-function-source ""
"The status format string that has generated the current
`twittering-format-status-function'.")
(defvar twittering-format-status-function nil
"The formating function generated from `twittering-format-status-function-source'.")
(defvar twittering-timeline-data-table (make-hash-table :test 'equal))
(defvar twittering-username-face 'twittering-username-face)
(defvar twittering-uri-face 'twittering-uri-face)
(defvar twittering-use-native-retweet nil
"Post retweets using native retweets if this variable is non-nil.")
(defvar twittering-update-status-function
'twittering-update-status-from-pop-up-buffer
"The function used to posting a tweet. It takes two arguments:
the first argument INIT-STR is initial text to be edited and the
second argument REPLY-TO-ID is a user ID of a tweet to which you
are going to reply.
Twittering-mode provides two functions for updating status:
* `twittering-update-status-from-minibuffer': edit tweets in minibuffer
* `twittering-update-status-from-pop-up-buffer': edit tweets in pop-up buffer")
(defvar twittering-request-confirmation-on-posting nil
"*If *non-nil*, confirmation will be requested on posting a tweet edited in
pop-up buffer.")
(defvar twittering-use-master-password nil
"*Wheter to store private information encrypted with a master password.")
(defvar twittering-private-info-file
(expand-file-name "~/.twittering-mode.gpg")
"*File for storing encrypted private information when
`twittering-use-master-password' is non-nil.")
(defvar twittering-variables-stored-with-encryption
'(twittering-oauth-access-token-alist))
(defvar twittering-api-prefix "1/")
(defvar twittering-search-api-method "search")
(defvar twittering-web-path-prefix "")
(defvar twittering-service-method 'twitter
"*Service method for `twittering-mode'.
The symbol `twitter' means Twitter Service. The symbol `statusnet' means
StatusNet Service.")
(defvar twittering-service-method-table
'((twitter (status-url . twittering-get-status-url-twitter)
(search-url . twittering-get-search-url-twitter))
(statusnet (status-url . twittering-get-status-url-statusnet)
(search-url . twittering-get-search-url-statusnet)))
"A list of alist of service methods.")
;;;;
;;;; Macro and small utility function
;;;;
(defun assocref (item alist)
(cdr (assoc item alist)))
(defmacro list-push (value listvar)
`(setq ,listvar (cons ,value ,listvar)))
(defmacro case-string (str &rest clauses)
`(cond
,@(mapcar
(lambda (clause)
(let ((keylist (car clause))
(body (cdr clause)))
`(,(if (listp keylist)
`(or ,@(mapcar (lambda (key) `(string-equal ,str ,key))
keylist))
't)
,@body)))
clauses)))
;;;;
;;;; Utility for portability
;;;;
(defun twittering-remove-duplicates (list)
"Return a copy of LIST with all duplicate elements removed.
This is non-destructive version of `delete-dups' which is not
defined in Emacs21."
(if (fboundp 'delete-dups)
(delete-dups (copy-sequence list))
(let ((rest list)
(result nil))
(while rest
(unless (member (car rest) result)
(setq result (cons (car rest) result)))
(setq rest (cdr rest)))
(nreverse result))))
(defun twittering-completing-read (prompt collection &optional predicate require-match initial-input hist def inherit-input-method)
"Read a string in the minibuffer, with completion.
This is a modified version of `completing-read' and accepts candidates
as a list of a string on Emacs21."
;; completing-read() of Emacs21 does not accepts candidates as
;; a list. Candidates must be given as an alist.
(let* ((collection (twittering-remove-duplicates collection))
(collection
(if (and (> 22 emacs-major-version)
(listp collection)
(stringp (car collection)))
(mapcar (lambda (x) (cons x nil)) collection)
collection)))
(completing-read prompt collection predicate require-match
initial-input hist def inherit-input-method)))
;;;;
;;;; Debug mode
;;;;
(defvar twittering-debug-mode nil)
(defvar twittering-debug-buffer "*debug*")
(defun twittering-get-or-generate-buffer (buffer)
(if (bufferp buffer)
(if (buffer-live-p buffer)
buffer
(generate-new-buffer (buffer-name buffer)))
(if (stringp buffer)
(or (get-buffer buffer)
(generate-new-buffer buffer)))))
(defun twittering-debug-buffer ()
(twittering-get-or-generate-buffer twittering-debug-buffer))
(defmacro debug-print (obj)
(let ((obsym (gensym)))
`(let ((,obsym ,obj))
(if twittering-debug-mode
(with-current-buffer (twittering-debug-buffer)
(insert "[debug] " (prin1-to-string ,obsym))
(newline)
,obsym)
,obsym))))
(defun debug-printf (fmt &rest args)
(when twittering-debug-mode
(with-current-buffer (twittering-debug-buffer)
(insert "[debug] " (apply 'format fmt args))
(newline))))
(defun twittering-debug-mode ()
(interactive)
(setq twittering-debug-mode
(not twittering-debug-mode))
(message (if twittering-debug-mode "debug mode:on" "debug mode:off")))
;;;;
;;;; Proxy setting / functions
;;;;
(defvar twittering-proxy-use nil)
(defvar twittering-proxy-server nil
"*Proxy server for `twittering-mode'.
If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.
To use individual proxies for HTTP and HTTPS, both `twittering-proxy-server'
and `twittering-proxy-port' must be nil.")
(defvar twittering-proxy-port nil
"*Port number for `twittering-mode'.
If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.
To use individual proxies for HTTP and HTTPS, both `twittering-proxy-server'
and `twittering-proxy-port' must be nil.")
(defvar twittering-proxy-keep-alive nil)
(defvar twittering-proxy-user nil
"*Username for `twittering-proxy-server'.
NOTE: If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.")
(defvar twittering-proxy-password nil
"*Password for `twittering-proxy-server'.
NOTE: If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.")
(defvar twittering-http-proxy-server nil
"*HTTP proxy server for `twittering-mode'.
If nil, it is initialized on entering `twittering-mode'.
The port number is specified by `twittering-http-proxy-port'.
For HTTPS connection, the proxy specified by `twittering-https-proxy-server'
and `twittering-https-proxy-port' is used.
NOTE: If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.")
(defvar twittering-http-proxy-port nil
"*Port number of a HTTP proxy server for `twittering-mode'.
If nil, it is initialized on entering `twittering-mode'.
The server is specified by `twittering-http-proxy-server'.
For HTTPS connection, the proxy specified by `twittering-https-proxy-server'
and `twittering-https-proxy-port' is used.
NOTE: If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.")
(defvar twittering-http-proxy-keep-alive nil
"*If non-nil, the Keep-alive is enabled. This is experimental.")
(defvar twittering-http-proxy-user nil
"*Username for `twittering-http-proxy-server'.
NOTE: If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.")
(defvar twittering-http-proxy-password nil
"*Password for `twittering-http-proxy-server'.
NOTE: If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.")
(defvar twittering-https-proxy-server nil
"*HTTPS proxy server for `twittering-mode'.
If nil, it is initialized on entering `twittering-mode'.
The port number is specified by `twittering-https-proxy-port'.
For HTTP connection, the proxy specified by `twittering-http-proxy-server'
and `twittering-http-proxy-port' is used.
NOTE: If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.")
(defvar twittering-https-proxy-port nil
"*Port number of a HTTPS proxy server for `twittering-mode'.
If nil, it is initialized on entering `twittering-mode'.
The server is specified by `twittering-https-proxy-server'.
For HTTP connection, the proxy specified by `twittering-http-proxy-server'
and `twittering-http-proxy-port' is used.
NOTE: If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.")
(defvar twittering-https-proxy-keep-alive nil
"*If non-nil, the Keep-alive is enabled. This is experimental.")
(defvar twittering-https-proxy-user nil
"*Username for `twittering-https-proxy-server'.
NOTE: If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.")
(defvar twittering-https-proxy-password nil
"*Password for `twittering-https-proxy-server'.
NOTE: If both `twittering-proxy-server' and `twittering-proxy-port' are
non-nil, the variables `twittering-proxy-*' have priority over other
variables `twittering-http-proxy-*' or `twittering-https-proxy-*'
regardless of HTTP or HTTPS.")
(defun twittering-normalize-proxy-vars ()
"Normalize the type of `twittering-http-proxy-port' and
`twittering-https-proxy-port'."
(mapc (lambda (sym)
(let ((value (symbol-value sym)))
(cond
((null value)
nil)
((integerp value)
nil)
((stringp value)
(set sym (string-to-number value)))
(t
(set sym nil)))))
'(twittering-proxy-port
twittering-http-proxy-port
twittering-https-proxy-port)))
(defun twittering-proxy-info (scheme &optional item)
"Return an alist for proxy configuration registered for SCHEME.
SCHEME must be a string \"http\", \"https\" or a symbol 'http or 'https.
The server name is a string and the port number is an integer."
(twittering-normalize-proxy-vars)
(let ((scheme (if (symbolp scheme)
(symbol-name scheme)
scheme))
(info-list
`((("http" "https")
. ((server . ,twittering-proxy-server)
(port . ,twittering-proxy-port)
(keep-alive . ,twittering-proxy-keep-alive)
(user . ,twittering-proxy-user)
(password . ,twittering-proxy-password)))
(("http")
. ((server . ,twittering-http-proxy-server)
(port . ,twittering-http-proxy-port)
(keep-alive . ,twittering-http-proxy-keep-alive)
(user . ,twittering-http-proxy-user)
(password . ,twittering-http-proxy-password)))
(("https")
. ((server . ,twittering-https-proxy-server)
(port . ,twittering-https-proxy-port)
(keep-alive . ,twittering-https-proxy-keep-alive)
(user . ,twittering-https-proxy-user)
(password . ,twittering-https-proxy-password))))))
(let ((info
(car (remove nil
(mapcar
(lambda (entry)
(when (member scheme (car entry))
(let ((info (cdr entry)))
(when (and (cdr (assq 'server info))
(cdr (assq 'port info)))
info))))
info-list)))))
(if item
(cdr (assq item info))
info))))
(defun twittering-url-proxy-services ()
"Return the current proxy configuration for `twittering-mode' in the format
of `url-proxy-services'."
(remove nil (mapcar
(lambda (scheme)
(let ((server (twittering-proxy-info scheme 'server))
(port (twittering-proxy-info scheme 'port)))
(when (and server port)
`(,scheme . ,(format "%s:%s" server port)))))
'("http" "https"))))
(defun twittering-find-proxy (scheme)
"Find proxy server and its port from the environmental variables and return
a cons pair of them.
SCHEME must be \"http\" or \"https\"."
(cond
((require 'url-methods nil t)
(url-scheme-register-proxy scheme)
(let* ((proxy-service (assoc scheme url-proxy-services))
(proxy (if proxy-service (cdr proxy-service) nil)))
(if (and proxy
(string-match "^\\([^:]+\\):\\([0-9]+\\)$" proxy))
(let ((host (match-string 1 proxy))
(port (string-to-number (match-string 2 proxy))))
(cons host port))
nil)))
(t
(let* ((env-var (concat scheme "_proxy"))
(env-proxy (or (getenv (upcase env-var))
(getenv (downcase env-var))))
(default-port (if (string= "https" scheme) "443" "80")))
(if (and env-proxy
(string-match
"^\\(https?://\\)?\\([^:/]+\\)\\(:\\([0-9]+\\)\\)?/?$"
env-proxy))
(let* ((host (match-string 2 env-proxy))
(port-str (or (match-string 4 env-proxy) default-port))
(port (string-to-number port-str)))
(cons host port))
nil)))))
(defun twittering-setup-proxy ()
(when (require 'url-methods nil t)
;; If `url-scheme-registry' is not initialized,
;; `url-proxy-services' will be reset by calling
;; `url-insert-file-contents' or `url-retrieve-synchronously', etc.
;; To avoid it, initialize `url-scheme-registry' by calling
;; `url-scheme-get-property' before calling such functions.
(url-scheme-get-property "http" 'name)
(url-scheme-get-property "https" 'name))
(unless (and twittering-http-proxy-server
twittering-http-proxy-port)
(let ((info (twittering-find-proxy "http")))
(setq twittering-http-proxy-server (car-safe info))
(setq twittering-http-proxy-port (cdr-safe info))))
(unless (and twittering-https-proxy-server
twittering-https-proxy-port)
(let ((info (twittering-find-proxy "https")))
(setq twittering-https-proxy-server (car-safe info))
(setq twittering-https-proxy-port (cdr-safe info))))
(if (and twittering-proxy-use
(null (twittering-proxy-info "http"))
(null (twittering-proxy-info "https")))
(progn
(message "Disabling proxy due to lack of configuration.")
(setq twittering-proxy-use nil))
t))
(defun twittering-toggle-proxy ()
(interactive)
(setq twittering-proxy-use
(not twittering-proxy-use))
(if (twittering-setup-proxy)
(message (if twittering-proxy-use "Use Proxy:on" "Use Proxy:off")))
(twittering-update-mode-line))
;;;;
;;;; Functions for URL library
;;;;
(defvar twittering-url-show-status nil
"*Whether to show a running total of bytes transferred.")
(defun twittering-url-wrapper (func &rest args)
(let ((url-proxy-services
(when twittering-proxy-use
(twittering-url-proxy-services)))
(url-show-status twittering-url-show-status))
(if (eq func 'url-retrieve)
(let ((buffer (apply func args)))
(when (buffer-live-p buffer)
(with-current-buffer buffer
(set (make-local-variable 'url-show-status)
twittering-url-show-status)))
buffer)
(apply func args))))
(defun twittering-url-insert-file-contents (url)
(twittering-url-wrapper 'url-insert-file-contents url))
(defun twittering-url-retrieve-synchronously (url)
(twittering-url-wrapper 'url-retrieve-synchronously url))
;;;;
;;;; CA certificate
;;;;
(defvar twittering-cert-file nil)
(defun twittering-delete-ca-cert-file ()
(when (and twittering-cert-file
(file-exists-p twittering-cert-file))
(delete-file twittering-cert-file)
(setq twittering-cert-file nil)))
;;; FIXME: file name is hard-coded. More robust way is desired.
;;; https://www.geotrust.com/resources/root_certificates/certificates/Equifax_Secure_Certificate_Authority.cer
(defun twittering-ensure-ca-cert ()
"Create a CA certificate file if it does not exist, and return
its file name. The certificate is retrieved from
`https://www.geotrust.com/resources/root_certificates/certificates/Equifax_Secure_Certificate_Authority.cer'."
(if twittering-cert-file
twittering-cert-file
(let ((file-name (make-temp-file "twmode-cacert")))
(with-temp-file file-name
(insert "-----BEGIN CERTIFICATE-----
MIIDIDCCAomgAwIBAgIENd70zzANBgkqhkiG9w0BAQUFADBOMQswCQYDVQQGEwJV
UzEQMA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2Vy
dGlmaWNhdGUgQXV0aG9yaXR5MB4XDTk4MDgyMjE2NDE1MVoXDTE4MDgyMjE2NDE1
MVowTjELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0VxdWlmYXgxLTArBgNVBAsTJEVx
dWlmYXggU2VjdXJlIENlcnRpZmljYXRlIEF1dGhvcml0eTCBnzANBgkqhkiG9w0B
AQEFAAOBjQAwgYkCgYEAwV2xWGcIYu6gmi0fCG2RFGiYCh7+2gRvE4RiIcPRfM6f
BeC4AfBONOziipUEZKzxa1NfBbPLZ4C/QgKO/t0BCezhABRP/PvwDN1Dulsr4R+A
cJkVV5MW8Q+XarfCaCMczE1ZMKxRHjuvK9buY0V7xdlfUNLjUA86iOe/FP3gx7kC
AwEAAaOCAQkwggEFMHAGA1UdHwRpMGcwZaBjoGGkXzBdMQswCQYDVQQGEwJVUzEQ
MA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2VydGlm
aWNhdGUgQXV0aG9yaXR5MQ0wCwYDVQQDEwRDUkwxMBoGA1UdEAQTMBGBDzIwMTgw
ODIyMTY0MTUxWjALBgNVHQ8EBAMCAQYwHwYDVR0jBBgwFoAUSOZo+SvSspXXR9gj
IBBPM5iQn9QwHQYDVR0OBBYEFEjmaPkr0rKV10fYIyAQTzOYkJ/UMAwGA1UdEwQF
MAMBAf8wGgYJKoZIhvZ9B0EABA0wCxsFVjMuMGMDAgbAMA0GCSqGSIb3DQEBBQUA
A4GBAFjOKer89961zgK5F7WF0bnj4JXMJTENAKaSbn+2kmOeUJXRmm/kEd5jhW6Y
7qj/WsjTVbJmcVfewCHrPSqnI0kBBIZCe/zuf6IWUrVnZ9NA2zsmWLIodz2uFHdh
1voqZiegDfqnc1zqcPGUIWVEX/r87yloqaKHee9570+sB3c4
-----END CERTIFICATE-----
"))
(add-hook 'kill-emacs-hook 'twittering-delete-ca-cert-file)
(setq twittering-cert-file file-name))))
;;;;
;;;; User agent
;;;;
(defvar twittering-user-agent-function 'twittering-user-agent-default-function)
(defun twittering-user-agent-default-function ()
"Twittering mode default User-Agent function."
(format "Emacs/%d.%d Twittering-mode/%s"
emacs-major-version emacs-minor-version
twittering-mode-version))
(defun twittering-user-agent ()
"Return User-Agent header string."
(funcall twittering-user-agent-function))
;;;;
;;;; Basic HTTP functions (general)
;;;;
(defun twittering-percent-encode (str &optional coding-system)
"Encode STR according to Percent-Encoding defined in RFC 3986."
(twittering-oauth-url-encode str coding-system))
(defun twittering-lookup-connection-type (use-ssl &optional order table)
"Return available entry extracted fron connection type table.
TABLE is connection type table, which is an alist of type symbol and its
item alist, such as
'((native (check . t)
(https . twittering-start-http-session-native-tls-p)
(start . twittering-start-http-session-native))
(curl (check . twittering-start-http-session-curl-p)
(https . twittering-start-http-session-curl-https-p)
(start . twittering-start-http-session-curl))) .
ORDER means the priority order of type symbols.
If USE-SSL is nil, the item `https' is ignored.
When the type `curl' has priority and is available for the above table,
the function returns
'((check . twittering-start-http-session-curl-p)
(https . twittering-start-http-session-curl-https-p)
(start . twittering-start-http-session-curl)) ."
(let ((rest (or order twittering-connection-type-order))
(table (or table twittering-connection-type-table))
(result nil))
(while (and rest (null result))
(let* ((candidate (car rest))
(entry (cons `(symbol . ,candidate)
(cdr (assq candidate table))))
(entry (if (assq 'display-name entry)
entry
(cons `(display-name . ,(symbol-name candidate))
entry)))
(validate (lambda (item)
(let ((v (cdr (assq item entry))))
(or (null v) (eq t v) (functionp v)))))
(confirm (lambda (item)
(let ((v (cdr (assq item entry))))
(cond
((null v) nil)
((eq t v) t)
((functionp v) (funcall v)))))))
(if (and (funcall validate 'check)
(or (not use-ssl) (funcall validate 'https)))
(cond
((and (funcall confirm 'check)
(or (not use-ssl) (funcall confirm 'https)))
(setq rest nil)
(setq result entry))
(t
(setq rest (cdr rest))))
(message "The configuration for conncetion type `%s' is invalid."
candidate)
(setq rest nil))))
result))
(defun twittering-get-connection-method-name (use-ssl)
"Return a name of the preferred connection method.
If USE-SSL is non-nil, return a connection method for HTTPS.
If USE-SSL is nil, return a connection method for HTTP."
(cdr (assq 'display-name (twittering-lookup-connection-type use-ssl))))
(defun twittering-lookup-http-start-function (&optional order table)
"Decide a connection method from currently available methods."
(let ((entry
(twittering-lookup-connection-type twittering-use-ssl order table)))
(cdr (assq 'send-http-request entry))))
(defun twittering-ensure-connection-method (&optional order table)
"Ensure a connection method with a compromise.
Return nil if no connection methods are available with a compromise."
(let* ((use-ssl (or twittering-use-ssl twittering-oauth-use-ssl))
(entry (twittering-lookup-connection-type use-ssl order table)))
(cond
(entry
t)
((and (null entry) use-ssl
(yes-or-no-p "HTTPS(SSL) is unavailable. Use HTTP instead? "))
;; Fall back on connection without SSL.
(setq twittering-use-ssl nil)
(setq twittering-oauth-use-ssl nil)
(twittering-update-mode-line)
(twittering-ensure-connection-method order table)
t)
(t
nil))))
(defun twittering-make-http-request (method header-list host port path query-parameters post-body use-ssl)
"Returns an alist specifying a HTTP request.
METHOD specifies HTTP method. It must be \"GET\" or \"POST\".
HEADER-LIST is a list of (field-name . field-value) specifying HTTP header
fields. The fields \"Host\", \"User-Agent\" and \"Content-Length\" are
automatically filled if necessary.
HOST specifies the host.
PORT specifies the port. This must be an integer.
PATH specifies the absolute path in URI (without query string).
QUERY-PARAMTERS is a string or an alist.
If QUERY-PARAMTERS is a string, it is treated as an encoded query string.
If QUERY-PARAMTERS is an alist, it represents a list of cons pairs of
string, (query-key . query-value).
POST-BODY specifies the post body sent when METHOD equals to \"POST\".
If POST-BODY is nil, no body is posted.
If USE-SSL is non-nil, the request is performed with SSL.
The result alist includes the following keys, where a key is a symbol.
method: HTTP method such as \"GET\" or \"POST\".
scheme: the scheme name. \"http\" or \"https\".
host: the host to which the request is sent.
port: the port to which the request is sent (integer).
path: the absolute path string. Note that it does not include query string.
query-string: the query string.
encoded-query-alist: the alist consisting of pairs of encoded query-name and
encoded query-value.
uri: the URI. It includes the query string.
uri-without-query: the URI without the query string.
header-list: an alist specifying pairs of a parameter and its value in HTTP
header field.
post-body: the entity that will be posted."
(let* ((scheme (if use-ssl "https" "http"))
(default-port (if use-ssl 443 80))
(port (if port port default-port))
(query-string
(cond
((stringp query-parameters)
query-parameters)
((consp query-parameters)
(mapconcat (lambda (pair)
(cond
((stringp pair)
(twittering-percent-encode pair))
((consp pair)
(format
"%s=%s"
(twittering-percent-encode (car pair))
(twittering-percent-encode (cdr pair))))
(t
nil)))
query-parameters
"&"))
(t