/
xmpp.py
986 lines (929 loc) · 35.8 KB
/
xmpp.py
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
#!/usr/bin/python
import twitter
import utils
import oauth
import cgi
import logging
import config
import re
from string import Template
from db import TwitterUser, GoogleUser, Db, Session, IdList, MODE_LIST, MODE_MENTION, MODE_DM, MODE_HOME
from pytz.gae import pytz
from mylocale import gettext, LOCALES
from google.appengine.ext import webapp
from google.appengine.api import xmpp, memcache
from google.appengine.runtime import DeadlineExceededError
from google.appengine.api.capabilities import CapabilitySet
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError
SHORT_COMMANDS = {
'@': 'reply',
'r': 'reply',
'd': 'dm',
'ho': 'home',
'lt': 'list',
'tl': 'timeline',
's': 'switch',
'fo': 'follow',
'unfo': 'unfollow',
'b': 'block',
'ub': 'unblock',
'm': 'msg',
'f': 'fav',
'uf': 'unfav',
'u': 'user',
'?': 'help',
'h': 'help'
}
_locale = config.DEFAULT_LANGUAGE
_ = lambda x: gettext(x, locale=_locale)
_check_username = lambda username: bool(re.match(r'^\w+$', username))
class Dummy(object):
def __getattr__(self, item):
raise NotImplementedError(item)
def __getitem__(self, item):
raise NotImplementedError(item)
class XMPP_handler(webapp.RequestHandler):
def post(self):
try:
self.process()
except CapabilityDisabledError:
try:
xmpp.send_presence(self.request.get('from'), presence_type=xmpp.PRESENCE_SHOW_AWAY)
except xmpp.Error:
pass
def process(self):
global _locale
try:
message = xmpp.Message(self.request.POST)
except xmpp.InvalidMessageError:
return
jid = message.sender.split('/')[0]
self._google_user = GoogleUser.get_by_jid(jid)
if self._google_user is None:
self._google_user = GoogleUser.add(jid)
_locale = self._google_user.locale
if self._google_user.enabled_user:
self._twitter_user = TwitterUser.get_by_twitter_name(self._google_user.enabled_user, self._google_user.jid)
self._api = Dummy()
if self._twitter_user is None:
self._google_user.enabled_user = ''
else:
self._api = twitter.Api(consumer_key=config.OAUTH_CONSUMER_KEY,
consumer_secret=config.OAUTH_CONSUMER_SECRET,
access_token_key=self._twitter_user.access_token_key,
access_token_secret=self._twitter_user.access_token_secret)
try:
self._user = self._api.verify_credentials()
if not self._user:
raise twitter.TwitterAuthenticationError
except twitter.TwitterAuthenticationError:
self._google_user.retry += 1
if self._google_user.retry >= config.MAX_RETRY:
GoogleUser.disable(self._google_user.jid)
xmpp.send_message(self._google_user.jid, _('NO_AUTHENTICATION'))
else:
Db.set_datastore(self._google_user)
return
else:
if self._google_user.retry > 0:
self._google_user.retry = 0
if self._twitter_user.twitter_name != self._user['screen_name']:
self._twitter_user.twitter_name = self._user['screen_name']
self._google_user.enabled_user = self._user['screen_name']
else:
self._twitter_user = Dummy()
self._api = Dummy()
self._user = Dummy()
utils.set_jid(self._google_user.jid)
result = self.parse_command(message.body)
if result is None:
return
if result:
while CapabilitySet('xmpp').is_enabled():
try:
message.reply(result)
except xmpp.Error:
pass
else:
break
IdList.flush(self._google_user.jid)
Db.set_datastore(self._google_user)
def parse_command(self, content):
content = content.rstrip()
if content.startswith(self._google_user.command_prefix):
args = content[len(self._google_user.command_prefix):].split(' ')
args[0] = args[0].lower()
if args[0] in SHORT_COMMANDS:
args[0] = SHORT_COMMANDS[args[0]]
func_name = 'func_' + args[0]
try:
func = getattr(self, func_name)
except (AttributeError, UnicodeEncodeError):
return _('INVALID_COMMAND')
try:
return func(args[1:])
except NotImplementedError:
return _('INVALID_COMMAND')
except twitter.TwitterInternalServerError:
return _('INTERNAL_SERVER_ERROR')
else:
if isinstance(self._api, Dummy):
return ''
if not len(content):
return ''
if len(content) > twitter.CHARACTER_LIMIT:
return _('WORDS_COUNT_EXCEED') % (len(content), twitter.CHARACTER_LIMIT)
try:
self._api.post_update(content)
except twitter.TwitterError, e:
if 'Status is a duplicate' in e.message:
return _('STATUS_DUPLICATE')
else:
return ''
return _('SUCCESSFULLY_UPDATE_STATUS')
def func_msg(self, args):
length = len(args)
if length == 1 and (args[0].isdigit() or (args[0] and args[0][0] == '#' and args[0][1:].isdigit())):
if args[0][0] == '#':
short_id = int(args[0][1:])
id = utils.restore_short_id(short_id, self._google_user.jid)
elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
short_id = int(args[0])
id = utils.restore_short_id(short_id, self._google_user.jid)
else:
short_id = None
id = long(args[0])
id_str = '#' + str(short_id) if short_id else str(id)
statuses = list()
for __ in xrange(config.MAX_REPLY_STATUS):
try:
status = self._api.get_status(id)
except twitter.TwitterError, e:
if 'No status found' in e.message:
if statuses:
break
else:
return _('STATUS_NOT_FOUND') % id_str
else:
return ''
if status is None:
break
statuses.append(status)
if 'in_reply_to_status_id' in status:
id = status['in_reply_to_status_id']
else:
break
return _('STATUSES_CONVERSATION') + '\n\n' + utils.parse_statuses(statuses, reverse=False)
raise NotImplementedError
def func_reply(self, args):
length = len(args)
if not length or (length == 1 and (args[0][0].lower() == 'p' and args[0][1:].isdigit())):
page = args[0][1:] if length else 1
statuses = self._api.get_mentions(page=int(page))
return _('MENTIONS') + _('PAGE') % str(page) + '\n\n' + utils.parse_statuses(statuses)
elif length > 1 and (args[0].isdigit() or (args[0] and args[0][0] == '#' and args[0][1:].isdigit())\
and ' '.join(args[1:])):
if args[0][0] == '#':
short_id = int(args[0][1:])
id = utils.restore_short_id(short_id, self._google_user.jid)
elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
short_id = int(args[0])
id = utils.restore_short_id(short_id, self._google_user.jid)
else:
short_id = None
id = int(args[0])
id_str = '#' + str(short_id) if short_id else str(id)
try:
status = self._api.get_status(id)
except twitter.TwitterError, e:
if 'No status found' in e.message:
return _('STATUS_NOT_FOUND') % id_str
else:
return ''
if status is None:
return ''
message = u'@%s %s' % (status['user']['screen_name'], ' '.join(args[1:]))
if len(message) > twitter.CHARACTER_LIMIT:
return _('WORDS_COUNT_EXCEED') % (len(message), twitter.CHARACTER_LIMIT)
try:
self._api.post_update(message, id)
except twitter.TwitterError, e:
if 'Status is a duplicate' in e.message:
return _('STATUS_DUPLICATE')
else:
return ''
return _('SUCCESSFULLY_REPLY_TO') % status['user']['screen_name']
raise NotImplementedError
def func_time(self, args):
length = len(args)
if not length:
return _('INTERVAL') % str(self._google_user.interval)
elif length == 1 and args[0].isdigit():
self._google_user.interval = int(args[0])
return _('INTERVAL') % str(self._google_user.interval)
raise NotImplementedError
def func_dm(self, args):
length = len(args)
if not length or (length == 1 and args[0][0].lower() == 'p' and args[0][1:].isdigit()):
page = 1 if not length else args[0][1:]
statuses = self._api.get_direct_messages(page=int(page))
return _('DIRECT_MESSAGES') + _('PAGE') % str(page) + '\n\n' + utils.parse_statuses(statuses)
elif length > 1 and ' '.join(args[1:]):
message = ' '.join(args[1:])
if len(message) > twitter.CHARACTER_LIMIT:
return _('WORDS_COUNT_EXCEED') % (len(message), twitter.CHARACTER_LIMIT)
try:
self._api.post_direct_message(args[0], message)
except twitter.TwitterError, e:
if 'Not found' in e.message:
return _('USER_NOT_FOUND') % args[0]
elif 'You cannot send messages to users who are not following you' in e.message:
return _('MESSAGE_TO_NOT_FOLLOWED')
else:
return ''
return _('SUCCESSFULLY_MESSAGE') % args[0]
raise NotImplementedError
def func_follow(self, args):
if len(args) == 1 and _check_username(args[0]):
try:
self._api.create_friendship(args[0])
except twitter.TwitterError, e:
if 'is already on your list' in e.message:
return _('ALREADY_FOLLOWED') % args[0]
elif 'You have been blocked' in e.message:
return _('BLOCKED') % args[0]
elif 'Not found' in e.message:
return _('USER_NOT_FOUND') % args[0]
else:
return ''
return _('SUCCESSFULLY_FOLLOW') % args[0]
raise NotImplementedError
def func_unfollow(self, args):
if len(args) == 1 and _check_username(args[0]):
try:
self._api.destroy_friendship(args[0])
except twitter.TwitterError, e:
if 'You are not friends with' in e.message:
return _('You are not friends with %s') % args[0]
elif 'Not found' in e.message:
return _('USER_NOT_FOUND') % args[0]
else:
return ''
return _('SUCCESSFULLY_UNFOLLOW') % args[0]
raise NotImplementedError
def func_if(self, args):
if len(args) == 1 and _check_username(args[0]):
try:
result = self._api.exists_friendship(args[0], self._google_user.enabled_user)
except twitter.TwitterError, e:
if 'Could not find both specified users' in e.message:
return _('USER_NOT_FOUND') % args[0]
else:
return ''
if result:
return _('FOLLOWING_YOU') % args[0]
else:
return _('NOT_FOLLOWING_YOU') % args[0]
raise NotImplementedError
def func_block(self, args):
if len(args) == 1 and _check_username(args[0]):
try:
self._api.create_block(args[0])
except twitter.TwitterError, e:
if 'Not found' in e.message:
return _('USER_NOT_FOUND') % args[0]
else:
return ''
return _('SUCCESSFULLY_BLOCK') % args[0]
raise NotImplementedError
def func_spam(self, args):
if len(args) == 1 and _check_username(args[0]):
try:
self._api.report_spam(args[0])
except twitter.TwitterError, e:
if 'Not found' in e.message:
return _('USER_NOT_FOUND') % args[0]
else:
return ''
return _('SUCCESSFULLY_SPAM') % args[0]
raise NotImplementedError
def func_unblock(self, args):
if len(args) == 1 and _check_username(args[0]):
try:
self._api.destroy_block(args[0])
except twitter.TwitterError, e:
if 'Not found' in e.message:
return _('USER_NOT_FOUND') % args[0]
else:
return ''
return _('SUCCESSFULLY_UNBLOCK') % args[0]
raise NotImplementedError
def func_del(self, args):
length = len(args)
if not length or (length == 1 and (args[0].isdigit() or (args[0] and args[0][0] == '#' and args[0][1:].isdigit()))):
if not length:
short_id = None
statuses = self._api.get_user_timeline(screen_name=self._google_user.enabled_user, count=1)
if statuses:
id = statuses[0]['id']
else:
return _('STATUS_NOT_FOUND') % ''
elif args[0][0] == '#':
short_id = int(args[0][1:])
id = utils.restore_short_id(short_id, self._google_user.jid)
elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
short_id = int(args[0])
id = utils.restore_short_id(short_id, self._google_user.jid)
else:
short_id = None
id = long(args[0])
id_str = '#' + str(short_id) if short_id else str(id)
try:
response = self._api.destroy_status(id)
except twitter.TwitterError, e:
if 'No status found' in e.message:
return _('STATUS_NOT_FOUND') % id_str
elif "You may not delete another user's status" in e.message:
return _('DELETE_ANOTHER_USER_STATUS')
else:
return ''
if response is None:
return ''
return _('STATUS_DELETED') % (id_str, response['text'])
raise NotImplementedError
def func_on(self, args):
if len(args):
for a in args:
if a == 'home':
self._google_user.display_timeline |= MODE_HOME
elif a == 'mention':
self._google_user.display_timeline |= MODE_MENTION
elif a == 'dm':
self._google_user.display_timeline |= MODE_DM
elif a == 'list':
self._google_user.display_timeline |= MODE_LIST
s = Session.get_by_key_name(self._google_user.jid)
if not s and self._google_user.display_timeline and self._google_user.enabled_user and self._google_user.msg_template.strip():
try:
flag = xmpp.get_presence(self._google_user.jid)
except (xmpp.Error, DeadlineExceededError):
flag = False
if flag:
Db.set_datastore(Session(key_name=self._google_user.jid, shard=self._google_user.shard))
modes = []
if self._google_user.display_timeline & MODE_LIST:
modes.append('list')
if self._google_user.display_timeline & MODE_HOME:
modes.append('home')
if self._google_user.display_timeline & MODE_MENTION:
modes.append('mention')
if self._google_user.display_timeline & MODE_DM:
modes.append('dm')
return _('ON_MODE') % ', '.join(modes)
def func_off(self, args):
if len(args):
for a in args:
if a == 'home':
self._google_user.display_timeline &= ~MODE_HOME
elif a == 'mention':
self._google_user.display_timeline &= ~MODE_MENTION
elif a == 'dm':
self._google_user.display_timeline &= ~MODE_DM
elif a == 'list':
self._google_user.display_timeline &= ~MODE_LIST
return self.func_on([])
def func_live(self, args):
length = len(args)
if not length:
if self._google_user.list_user and self._google_user.list_id and self._google_user.list_name:
return _('LIVE_MODE') % (self._google_user.list_user + '/' + self._google_user.list_name)
return _('USE_LIVE')
elif 1 <= length <= 2:
if length == 1:
path = args[0].split('/')
if len(path) == 1:
user = self._google_user.enabled_user
list_id = path[0]
elif len(path) == 2:
user = path[0]
list_id = path[1]
else:
raise NotImplementedError
elif _check_username(args[0]):
user = args[0]
list_id = args[1]
else:
raise NotImplementedError
try:
response = self._api.get_list(user, list_id)
except twitter.TwitterError, e:
if 'Not found' in e.message:
return _('LIST_NOT_FOUND') % (list_id if user == self._google_user.enabled_user else user + '/' + list_id)
else:
return ''
if response is None:
return None
self._google_user.list_user = response['user']['screen_name']
self._google_user.list_id = response['id']
self._google_user.list_name = response['slug']
return _('LIVE_MODE') % (self._google_user.list_user + '/' + self._google_user.list_name)
raise NotImplementedError
def func_fav(self, args):
length = len(args)
if not length or (length == 1 and (args[0][0].lower() == 'p' and args[0][1:].isdigit())):
page = args[0][1:] if length else 1
statuses = self._api.get_favorites(page=int(page))
return _('FAVOURITES') + _('PAGE') % str(page) + '\n\n' + utils.parse_statuses(statuses)
elif length == 1 and (args[0].isdigit() or (args[0] and args[0][0] == '#' and args[0][1:].isdigit())):
if args[0][0] == '#':
short_id = int(args[0][1:])
id = utils.restore_short_id(short_id, self._google_user.jid)
elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
short_id = int(args[0])
id = utils.restore_short_id(short_id, self._google_user.jid)
else:
short_id = None
id = long(args[0])
id_str = '#' + str(short_id) if short_id else str(id)
try:
self._api.create_favorite(id)
except twitter.TwitterError, e:
if 'No status found' in e.message:
return _('STATUS_NOT_FOUND') % id_str
else:
return ''
return _('FAVOURITED') % id_str
raise NotImplementedError
def func_unfav(self, args):
if len(args) == 1 and (args[0].isdigit() or (args[0] and args[0][0] == '#' and args[0][1:].isdigit())):
if args[0][0] == '#':
short_id = int(args[0][1:])
id = utils.restore_short_id(short_id, self._google_user.jid)
elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
short_id = int(args[0])
id = utils.restore_short_id(short_id, self._google_user.jid)
else:
short_id = None
id = long(args[0])
id_str = '#' + str(short_id) if short_id else str(id)
try:
self._api.destroy_favorite(id)
except twitter.TwitterError, e:
if 'No status found' in e.message:
return _('STATUS_NOT_FOUND') % id_str
else:
return ''
return _('UNFAVOURITED') % id_str
raise NotImplementedError
def func_home(self, args):
length = len(args)
if not length:
page = 1
elif length == 1 and args[0][0].lower() == 'p' and args[0][1:].isdigit():
page = args[0][1:]
else:
raise NotImplementedError
try:
statuses = self._api.get_home_timeline(page=int(page))
except twitter.TwitterError:
return ''
if statuses is None:
return None
return _('TIMELINE') + _('PAGE') % str(page) + '\n\n' + utils.parse_statuses(statuses)
def func_timeline(self, args):
length = len(args)
if not length or (length == 1 and (args[0][0].lower() == 'p' and args[0][1:].isdigit())):
screen_name = self._user['screen_name']
page = args[0][1:] if length else 1
elif length == 1 or (length == 2 and (args[1][0].lower() == 'p' and args[1][1:].isdigit())):
screen_name = args[0]
page = args[1][1:] if length == 2 else 1
else:
raise NotImplementedError
try:
statuses = self._api.get_user_timeline(screen_name=screen_name, page=int(page))
except twitter.TwitterError, e:
if 'Not found' in e.message:
return _('USER_NOT_FOUND') % screen_name
elif 'Not authorized' in e.message:
return _('PROTECTED_USER')
else:
return ''
if statuses is None:
return ''
return _('USER_TIMELINE') % screen_name + _('PAGE') % str(page) + '\n\n' + utils.parse_statuses(statuses)
def func_list(self, args):
length = len(args)
if length == 1 or (length == 2 and (args[1][0].lower() == 'p' and args[1][1:].isdigit())):
path = args[0].split('/')
if len(path) == 1:
user = self._google_user.enabled_user
list_id = path[0]
elif len(path) == 2:
user = path[0]
list_id = path[1]
else:
raise NotImplementedError
page = args[1][1:] if length == 2 else 1
elif length == 2 or (length == 3 and (args[2][0].lower() == 'p' and args[2][1:].isdigit())):
user = args[0]
list_id = args[1]
page = args[2][1:] if length == 3 else 1
else:
raise NotImplementedError
try:
statuses = self._api.get_list_statuses(user=user, id=list_id, page=int(page))
except twitter.TwitterError, e:
if 'Not found' in e.message:
return _('LIST_NOT_FOUND') % args[0]
else:
return ''
if statuses is None:
return ''
return _('LIST') % (user + '/' + str(list_id)) + _('PAGE') % str(page) + '\n\n' + utils.parse_statuses(statuses)
def func_ra(self, args):
if len(args) > 1 and (
args[0].isdigit() or (args[0] and args[0][0] == '#' and args[0][1:].isdigit()) and ' '.join(args[1:])):
if args[0][0] == '#':
short_id = int(args[0][1:])
id = utils.restore_short_id(short_id, self._google_user.jid)
elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
short_id = int(args[0])
id = utils.restore_short_id(short_id, self._google_user.jid)
else:
short_id = None
id = int(args[0])
id_str = '#' + str(short_id) if short_id else str(id)
try:
status = self._api.get_status(id)
except twitter.TwitterError, e:
if 'No status found' in e.message:
return _('STATUS_NOT_FOUND') % id_str
else:
return ''
if status is None:
return ''
mention_users = [status['user']['screen_name']]
if 'user_mentions' in status['entities']:
for u in status['entities']['user_mentions']:
name = u['screen_name']
if name != self._user['screen_name'] and name not in mention_users:
mention_users.append(name)
message = u'%s %s' % (' '.join(['@' + x for x in mention_users]), ' '.join(args[1:]))
if len(message) > twitter.CHARACTER_LIMIT:
return _('WORDS_COUNT_EXCEED') % (len(message), twitter.CHARACTER_LIMIT)
try:
self._api.post_update(message, id)
except twitter.TwitterError, e:
if 'Status is a duplicate' in e.message:
return _('STATUS_DUPLICATE')
else:
return ''
return _('SUCCESSFULLY_REPLY_TO') % ', '.join(mention_users)
raise NotImplementedError
def func_re(self, args):
if len(args) == 1 and (args[0] and args[0].isdigit() or (args[0][0] == '#' and args[0][1:].isdigit())):
if args[0][0] == '#':
short_id = int(args[0][1:])
id = utils.restore_short_id(short_id, self._google_user.jid)
elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
short_id = int(args[0])
id = utils.restore_short_id(short_id, self._google_user.jid)
else:
short_id = None
id = int(args[0])
id_str = '#' + str(short_id) if short_id else str(id)
try:
self._api.create_retweet(id)
except twitter.TwitterError, e:
if 'No status found' in e.message:
return _('STATUS_NOT_FOUND') % id_str
else:
return ''
return _('SUCCESSFULLY_RETWEET') % id_str
raise NotImplementedError
def func_rt(self, args):
length = len(args)
if length >= 1 and (args[0].isdigit() or (args[0] and args[0][0] == '#' and args[0][1:].isdigit())):
if args[0][0] == '#':
short_id = int(args[0][1:])
id = utils.restore_short_id(short_id, self._google_user.jid)
elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
short_id = int(args[0])
id = utils.restore_short_id(short_id, self._google_user.jid)
else:
short_id = None
id = int(args[0])
id_str = '#' + str(short_id) if short_id else str(id)
try:
status = self._api.get_status(id)
except twitter.TwitterError, e:
if 'No status found' in e.message:
return _('STATUS_NOT_FOUND') % id_str
else:
logging.error(e)
return ''
if status is None:
return ''
if length > 1:
user_msg = ' '.join(args[1:]) + ' '
else:
user_msg = ''
message = u'%sRT @%s:%s' % (user_msg, status['user']['screen_name'], status['text'])
if len(message) > twitter.CHARACTER_LIMIT:
message = message[:138] + '..'
try:
json = self._api.post_update(message, id)
except twitter.TwitterError, e:
if 'Status is a duplicate' in e.message:
return _('STATUS_DUPLICATE')
else:
return ''
else:
return _('SUCCESSFULLY_RT') % (id_str, json['text'])
raise NotImplementedError
def func_timezone(self, args):
length = len(args)
if not length:
return _('CURRENT_TIMEZONE') % self._google_user.timezone
elif length == 1:
try:
pytz.timezone(args[0])
except pytz.UnknownTimeZoneError:
return _('INVALID_TIMEZONE')
self._google_user.timezone = args[0]
return _('SET_TIMEZONE_SUCCESSFULLY')
raise NotImplementedError
def func_locale(self, args):
length = len(args)
if not length:
return _('CURRENT_LOCALE') % LOCALES[self._google_user.locale]
elif length == 1:
if args[0] not in LOCALES:
return _('INVALID_LOCALE')
global _locale
_locale = args[0]
self._google_user.locale = args[0]
return _('SET_LOCALE_SUCCESSFULLY')
raise NotImplementedError
def func_retweet(self, args):
length = len(args)
if not length or length == 1:
if length == 1:
value = args[0].lower()
if value in ('true', '1', 'yes'):
value = True
elif value in ('false', '0', 'no'):
value = False
else:
raise NotImplementedError
self._google_user.official_retweet = value
return _('RETWEET_MODE') if self._google_user.official_retweet else _('RT_MODE')
raise NotImplementedError
def func_bold(self, args):
length = len(args)
if not length or length == 1:
if length == 1:
value = args[0].lower()
if value in ('true', '1', 'yes'):
value = True
elif value in ('false', '0', 'no'):
value = False
else:
raise NotImplementedError
self._google_user.bold_username = value
return _('BOLD_MODE_ON') if self._google_user.bold_username else _('BOLD_MODE_OFF')
raise NotImplementedError
def func_reverse(self, args):
length = len(args)
if not length or length:
if length == 1:
value = args[0].lower()
if value in ('true', '1', 'yes'):
value = True
elif value in ('false', '0', 'no'):
value = False
else:
raise NotImplementedError
self._google_user.reverse = value
return _('REVERSE_MODE_ON') if self._google_user.reverse else _('REVERSE_MODE_OFF')
raise NotImplementedError
def func_msgtpl(self, args):
if len(args):
tpl = ' '.join(args)
while tpl[-2::] == r'\n':
tpl = tpl[:len(tpl) - 2] + '\n'
self._google_user.msg_template = tpl
s = Session.get_by_key_name(self._google_user.jid)
if not s and tpl.strip() and self._google_user.display_timeline and self._google_user.enabled_user:
try:
flag = xmpp.get_presence(self._google_user.jid)
except (xmpp.Error, DeadlineExceededError):
flag = False
if flag:
Db.set_datastore(Session(key_name=self._google_user.jid, shard=self._google_user.shard))
return _('MSG_TEMPLATE') % self._google_user.msg_template
def func_datefmt(self, args):
if len(args):
self._google_user.date_format = ' '.join(args)
return _('DATE_TEMPLATE') % self._google_user.date_format
def func_prefix(self, args):
length = len(args)
if length <= 1:
if length:
self._google_user.command_prefix = args[0]
return _('COMMAND_PREFIX') % self._google_user.command_prefix
raise NotImplementedError
def func_prefix_space(self, args):
if not args:
return self.func_prefix([' '])
else:
raise NotImplementedError
def func_switch(self, args):
length = len(args)
if length > 1:
raise NotImplementedError
else:
twitter_users = TwitterUser.get_by_jid(self._google_user.jid)
twitter_users_name = [u.twitter_name for u in twitter_users if u.twitter_name is not None]
if not length:
return _('NOW_USING') % self._google_user.enabled_user + '\n'\
+ _('ALL_TWITTER_USERS_NAME') % '\n'.join(twitter_users_name)
elif _check_username(args[0]):
twitter_users_name_ci = [x.lower() for x in twitter_users_name]
twitter_name_ci = args[0].lower()
if twitter_name_ci in twitter_users_name_ci:
i = twitter_users_name_ci.index(twitter_name_ci)
if not self._google_user.enabled_user and self._google_user.display_timeline and self._google_user.msg_template.strip():
try:
flag = xmpp.get_presence(self._google_user.jid)
except (xmpp.Error, DeadlineExceededError):
flag = False
if flag:
Db.set_datastore(Session(key_name=self._google_user.jid, shard=self._google_user.shard))
self._google_user.enabled_user = twitter_users_name[i]
return _('ENABLED_TWITTER_USER_CHANGED') % self._google_user.enabled_user
return _('NOT_ASSOCIATED_TWITTER_USER')
def func_user(self, args):
length = len(args)
if length > 1:
raise NotImplementedError
else:
if not length:
user = self._user['screen_name']
elif _check_username(args[0]):
user = args[0]
else:
raise NotImplementedError
try:
result = self._api.get_user(user)
except twitter.TwitterError, e:
if 'Not found' in e.message:
return _('USER_NOT_FOUND') % args[0]
else:
return ''
if result is None:
return ''
result['profile_image_url'] = result['profile_image_url'].replace('_normal.', '.')
for x in result:
if result[x] is None:
result[x] = ''
if 'status' in result:
result['status'] = result['status']['text']
else:
result['status'] = ''
if result['following']:
result['following'] = _('YOU_FOLLOWING') % result['screen_name']
elif result['follow_request_sent']:
result['following'] = _('YOU_HAVE_REQUESTED') % result['screen_name']
else:
result['following'] = _('YOU_DONT_FOLLOW') % result['screen_name']
return Template(_('USER_PROFILE')).safe_substitute(result)
def func_remove(self, args):
length = len(args)
msg = ''
if length <= 1:
if not length:
user = self._google_user.enabled_user
else:
user = args[0]
if user != '':
twitter_user = TwitterUser.get_by_twitter_name(user, self._google_user.jid)
if twitter_user is not None:
twitter_user.delete()
memcache.delete(self._google_user.jid + ':' + user, namespace='twitter_name')
if self._google_user.enabled_user == user:
self._google_user.enabled_user = ''
msg += _('TWITTER_USER_DELETED') % user
msg += _('SPECIFY_ANOTHER_USER')
return msg
raise NotImplementedError
def func_oauth(self, args):
length = len(args)
if not length or length == 2:
consumer = oauth.Consumer(config.OAUTH_CONSUMER_KEY, config.OAUTH_CONSUMER_SECRET)
client = oauth.Client(consumer)
resp = client.request(twitter.REQUEST_TOKEN_URL, "GET")
if not resp:
return _('NETWORK_ERROR')
self._request_token = dict(cgi.parse_qsl(resp))
oauth_token = self._request_token['oauth_token']
redirect_url = "%s?oauth_token=%s" % (twitter.AUTHORIZATION_URL, oauth_token)
if not length:
TwitterUser.add(self._google_user.jid, oauth_token)
return _('OAUTH_URL') % redirect_url
import oauth_proxy
pin = oauth_proxy.login_oauth(redirect_url, args[0], args[1])
if pin == '':
return _('NETWORK_ERROR')
elif pin is None:
return _('WRONG_USER_OR_PASSWORD')
return self.func_bind([pin], oauth_token)
else:
raise NotImplementedError
def func_bind(self, args, oauth_token=None):
if len(args) == 1 and args[0].isalnum():
jid = self._google_user.jid
if oauth_token is None:
twitter_user = TwitterUser.get_by_twitter_name(None, jid)
if twitter_user is None:
return _('INVALID_PIN_CODE') % ''
oauth_token = twitter_user.access_token_key
token = oauth.Token(oauth_token)
token.set_verifier(args[0].encode('UTF8'))
consumer = oauth.Consumer(config.OAUTH_CONSUMER_KEY, config.OAUTH_CONSUMER_SECRET)
client = oauth.Client(consumer, token)
resp = client.request(twitter.ACCESS_TOKEN_URL, "POST")
if not resp:
return _('NETWORK_ERROR')
access_token = dict(cgi.parse_qsl(resp))
if 'oauth_token' not in access_token:
return _('INVALID_PIN_CODE') % args[0]
if oauth_token is None:
twitter_user.delete()
TwitterUser.add(jid, access_token['oauth_token'], access_token['oauth_token_secret'], access_token['screen_name'])
if self._google_user.enabled_user == '':
self._google_user.enabled_user = access_token['screen_name']
if IdList.get_by_jid(jid, self._google_user.shard) is None:
IdList.add(jid, self._google_user.shard)
return _('SUCCESSFULLY_BIND') % access_token['screen_name']
else:
raise NotImplementedError
def func_help(self, args):
length = len(args)
if not length:
return _('ALL_COMMANDS')
elif length == 1:
command = args[0].lower()
if command in SHORT_COMMANDS:
command = SHORT_COMMANDS[command]
if command == 'help':
return _('ALL_COMMANDS')
if command == 'locale':
return _('HELP_LOCALE') % '\n'.join(k + ':' + LOCALES[k] for k in LOCALES)
func_name = 'func_' + command
if func_name in dir(self):
return _('HELP_' + command.upper())
else:
return ''
class XMPP_Available_handler(webapp.RequestHandler):
def post(self):
jid = self.request.get('from').split('/')[0]
s = Session.get_by_key_name(jid)
if not s:
u = GoogleUser.get_by_jid(jid)
if u and u.enabled_user and u.display_timeline and u.msg_template.strip():
try:
Db.set_datastore(Session(key_name=jid, shard=u.shard))
except CapabilityDisabledError:
try:
xmpp.send_presence(jid, presence_type=xmpp.PRESENCE_SHOW_AWAY)
except xmpp.Error:
pass
class XMPP_Unavailable_handler(webapp.RequestHandler):
def post(self):
jid = self.request.get('from').split('/')[0]
s = Session.get_by_key_name(jid)
if s:
u = GoogleUser.get_by_jid(jid)
if u:
try:
flag = xmpp.get_presence(jid)
except xmpp.Error:
flag = False
if not flag:
s.delete()
else:
s.delete()
class XMPP_Probe_handler(webapp.RequestHandler):
def post(self):
return
def main():
application = webapp.WSGIApplication([('/_ah/xmpp/message/chat/', XMPP_handler),
('/_ah/xmpp/presence/available/', XMPP_Available_handler),
('/_ah/xmpp/presence/unavailable/', XMPP_Unavailable_handler),
('/_ah/xmpp/presence/probe/', XMPP_Probe_handler)])
run_wsgi_app(application)
if __name__ == "__main__":
main()